@@ -125,8 +125,9 @@ func (a *Archiver) Stop(ctx context.Context) error {
125125// perform any validation of the blobs, it assumes a trusted beacon node. See:
126126// https://github.com/base/blob-archiver/issues/4.
127127//
128- // The function prefers the /eth/v1/beacon/blobs endpoint but falls back to /eth/v1/beacon/blob_sidecars
129- // if the blobs endpoint fails.
128+ // The function prefers the /eth/v1/beacon/blob_sidecars endpoint but falls back to /eth/v1/beacon/blobs
129+ // if the blob sidecars endpoint fails. This avoids recomputing KZG commitments and proofs when they're
130+ // available from the beacon node.
130131func (a * Archiver ) persistBlobsForBlockToS3 (ctx context.Context , blockIdentifier string , overwrite bool ) (* v1.BeaconBlockHeader , bool , error ) {
131132 currentHeader , err := a .beaconClient .BeaconBlockHeader (ctx , & api.BeaconBlockHeaderOpts {
132133 Block : blockIdentifier ,
@@ -148,38 +149,44 @@ func (a *Archiver) persistBlobsForBlockToS3(ctx context.Context, blockIdentifier
148149 return currentHeader .Data , true , nil
149150 }
150151
151- // Try the new blobs endpoint first
152+ // Try the blob sidecars endpoint first to get commitments and proofs directly
152153 var blobSidecarData []* deneb.BlobSidecar
153- blobs , err := a .beaconClient .Blobs (ctx , & api.BlobsOpts {
154+ blobSidecarsResp , err := a .beaconClient .BlobSidecars (ctx , & api.BlobSidecarsOpts {
154155 Block : currentHeader .Data .Root .String (),
155156 })
156157
157- if err == nil && blobs != nil && blobs .Data != nil && len (blobs .Data ) > 0 {
158- // Successfully fetched blobs, compute commitments and proofs from blob data
159- a .log .Debug ("fetched blobs from blobs endpoint, computing KZG commitments and proofs" , "count" , len (blobs .Data ))
160-
161- var err error
162- blobSidecarData , err = blobsToSidecars (blobs .Data , currentHeader .Data )
163- if err != nil {
164- a .log .Error ("failed to compute KZG commitments and proofs for blobs" , "err" , err )
165- return nil , false , err
166- }
158+ if err == nil && blobSidecarsResp != nil && blobSidecarsResp .Data != nil {
159+ // Successfully fetched blob sidecars with KZG commitments and proofs
160+ a .log .Debug ("fetched blob sidecars from blob_sidecars endpoint" , "count" , len (blobSidecarsResp .Data ))
161+ blobSidecarData = blobSidecarsResp .Data
167162 } else {
168- // Fall back to blob sidecars endpoint
163+ // Fall back to blobs endpoint and compute commitments and proofs
169164 if err != nil {
170- a .log .Debug ("blobs endpoint failed, falling back to blob sidecars " , "err" , err )
165+ a .log .Debug ("blob sidecars endpoint failed, falling back to blobs " , "err" , err )
171166 }
172167
173- blobSidecarsResp , fallbackErr := a .beaconClient .BlobSidecars (ctx , & api.BlobSidecarsOpts {
168+ blobs , fallbackErr := a .beaconClient .Blobs (ctx , & api.BlobsOpts {
174169 Block : currentHeader .Data .Root .String (),
175170 })
176171
177172 if fallbackErr != nil {
178- a .log .Error ("failed to fetch blob sidecars " , "err" , fallbackErr )
173+ a .log .Error ("failed to fetch blobs " , "err" , fallbackErr )
179174 return nil , false , fallbackErr
180175 }
181176
182- blobSidecarData = blobSidecarsResp .Data
177+ if blobs == nil || blobs .Data == nil {
178+ a .log .Error ("blobs endpoint returned nil data" )
179+ return nil , false , errors .New ("blobs endpoint returned nil data" )
180+ }
181+
182+ a .log .Debug ("fetched blobs from blobs endpoint, computing KZG commitments and proofs" , "count" , len (blobs .Data ))
183+
184+ var computeErr error
185+ blobSidecarData , computeErr = blobsToSidecars (blobs .Data , currentHeader .Data )
186+ if computeErr != nil {
187+ a .log .Error ("failed to compute KZG commitments and proofs for blobs" , "err" , computeErr )
188+ return nil , false , computeErr
189+ }
183190 }
184191
185192 a .log .Debug ("fetched blob sidecars" , "count" , len (blobSidecarData ))
0 commit comments