Skip to content

Commit 0585e32

Browse files
committed
Merge branch 'feat/serve-blob-sidecars' into release/serving-bundle-fix
2 parents ebfe6f7 + 4f0baeb commit 0585e32

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

pkg/beacon/download.go

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -311,30 +311,9 @@ func (d *Default) fetchBundle(ctx context.Context, root phase0.Root, upstream *N
311311
}
312312

313313
if d.shouldDownloadStates() {
314-
// If the state already exists, don't bother downloading it again.
315-
existingState, err := d.states.GetByStateRoot(stateRoot)
316-
if err == nil && existingState != nil {
317-
d.log.Infof("Successfully fetched bundle from %s", upstream.Config.Name)
318-
319-
return block, nil
320-
}
321-
322-
beaconState, err := upstream.Beacon.FetchRawBeaconState(ctx, eth.SlotAsString(slot), "application/octet-stream")
323-
if err != nil {
324-
return nil, fmt.Errorf("failed to fetch beacon state: %w", err)
325-
}
326-
327-
if beaconState == nil {
328-
return nil, errors.New("beacon state is nil")
329-
}
330-
331-
expiresAt := time.Now().Add(FinalityHaltedServingPeriod)
332-
if slot == phase0.Slot(0) {
333-
expiresAt = time.Now().Add(999999 * time.Hour)
334-
}
335-
336-
if err := d.states.Add(stateRoot, &beaconState, expiresAt, slot); err != nil {
337-
return nil, fmt.Errorf("failed to store beacon state: %w", err)
314+
// Download and store beacon state
315+
if err := d.downloadAndStoreBeaconState(ctx, stateRoot, slot, upstream); err != nil {
316+
return nil, fmt.Errorf("failed to download and store beacon state: %w", err)
338317
}
339318
}
340319

@@ -345,31 +324,56 @@ func (d *Default) fetchBundle(ctx context.Context, root phase0.Root, upstream *N
345324
if err := d.downloadAndStoreDepositSnapshot(ctx, epoch, upstream); err != nil {
346325
return nil, fmt.Errorf("failed to download and store deposit snapshot: %w", err)
347326
}
327+
}
348328

349-
spec, err := upstream.Beacon.Spec()
350-
if err != nil {
351-
return nil, fmt.Errorf("failed to fetch spec from upstream node: %w", err)
352-
}
353-
354-
denebFork, err := spec.ForkEpochs.GetByName("DENEB")
355-
if err != nil {
356-
return nil, fmt.Errorf("failed to fetch deneb fork: %w", err)
357-
}
329+
sp, err := upstream.Beacon.Spec()
330+
if err != nil {
331+
return nil, fmt.Errorf("failed to fetch spec from upstream node: %w", err)
332+
}
358333

359-
if denebFork.Active(slot, spec.SlotsPerEpoch) {
334+
denebFork, err := sp.ForkEpochs.GetByName("DENEB")
335+
if err == nil && denebFork != nil {
336+
if denebFork.Active(slot, sp.SlotsPerEpoch) {
360337
// Download and store blob sidecars
361338
if err := d.downloadAndStoreBlobSidecars(ctx, slot, upstream); err != nil {
362339
return nil, fmt.Errorf("failed to download and store blob sidecars: %w", err)
363340
}
364341
}
365-
366342
}
367343

368344
d.log.Infof("Successfully fetched bundle from %s", upstream.Config.Name)
369345

370346
return block, nil
371347
}
372348

349+
func (d *Default) downloadAndStoreBeaconState(ctx context.Context, stateRoot phase0.Root, slot phase0.Slot, node *Node) error {
350+
// If the state already exists, don't bother downloading it again.
351+
existingState, err := d.states.GetByStateRoot(stateRoot)
352+
if err == nil && existingState != nil {
353+
return nil
354+
}
355+
356+
beaconState, err := node.Beacon.FetchRawBeaconState(ctx, eth.SlotAsString(slot), "application/octet-stream")
357+
if err != nil {
358+
return fmt.Errorf("failed to fetch beacon state: %w", err)
359+
}
360+
361+
if beaconState == nil {
362+
return errors.New("beacon state is nil")
363+
}
364+
365+
expiresAt := time.Now().Add(FinalityHaltedServingPeriod)
366+
if slot == phase0.Slot(0) {
367+
expiresAt = time.Now().Add(999999 * time.Hour)
368+
}
369+
370+
if err := d.states.Add(stateRoot, &beaconState, expiresAt, slot); err != nil {
371+
return fmt.Errorf("failed to store beacon state: %w", err)
372+
}
373+
374+
return nil
375+
}
376+
373377
func (d *Default) downloadAndStoreDepositSnapshot(ctx context.Context, epoch phase0.Epoch, node *Node) error {
374378
// Check if we already have the deposit snapshot.
375379
if _, err := d.depositSnapshots.GetByEpoch(epoch); err == nil {

0 commit comments

Comments
 (0)