From b3caaa9acc8ad0495acb4cd995bd89ea4c305dd6 Mon Sep 17 00:00:00 2001 From: kasey <489222+kasey@users.noreply.github.com> Date: Mon, 4 Mar 2024 20:57:06 -0600 Subject: [PATCH] exit blob fetching for cp block if outside retention (#13686) * exit blob fetching for cp block if outside retention * regression test --------- Co-authored-by: Kasey Kirkham --- beacon-chain/sync/initial-sync/BUILD.bazel | 1 + beacon-chain/sync/initial-sync/service.go | 3 +++ .../sync/initial-sync/service_test.go | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/beacon-chain/sync/initial-sync/BUILD.bazel b/beacon-chain/sync/initial-sync/BUILD.bazel index 61024a83a..df9be0ad5 100644 --- a/beacon-chain/sync/initial-sync/BUILD.bazel +++ b/beacon-chain/sync/initial-sync/BUILD.bazel @@ -77,6 +77,7 @@ go_test( "//beacon-chain/das:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/filesystem:go_default_library", + "//beacon-chain/db/kv:go_default_library", "//beacon-chain/db/testing:go_default_library", "//beacon-chain/p2p:go_default_library", "//beacon-chain/p2p/peers:go_default_library", diff --git a/beacon-chain/sync/initial-sync/service.go b/beacon-chain/sync/initial-sync/service.go index bb3f6b967..876d9dc80 100644 --- a/beacon-chain/sync/initial-sync/service.go +++ b/beacon-chain/sync/initial-sync/service.go @@ -316,6 +316,9 @@ func (s *Service) fetchOriginBlobs(pids []peer.ID) error { log.WithField("root", fmt.Sprintf("%#x", r)).Error("Block for checkpoint sync origin root not found in db") return err } + if !params.WithinDAPeriod(slots.ToEpoch(blk.Block().Slot()), slots.ToEpoch(s.clock.CurrentSlot())) { + return nil + } rob, err := blocks.NewROBlockWithRoot(blk, r) if err != nil { return err diff --git a/beacon-chain/sync/initial-sync/service_test.go b/beacon-chain/sync/initial-sync/service_test.go index 84efcfd03..cccb99bc0 100644 --- a/beacon-chain/sync/initial-sync/service_test.go +++ b/beacon-chain/sync/initial-sync/service_test.go @@ -6,10 +6,12 @@ import ( "testing" "time" + "github.com/libp2p/go-libp2p/core/peer" "github.com/paulbellamy/ratecounter" "github.com/prysmaticlabs/prysm/v5/async/abool" mock "github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/v5/beacon-chain/db/filesystem" + "github.com/prysmaticlabs/prysm/v5/beacon-chain/db/kv" dbtest "github.com/prysmaticlabs/prysm/v5/beacon-chain/db/testing" p2pt "github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/testing" "github.com/prysmaticlabs/prysm/v5/beacon-chain/startup" @@ -487,3 +489,25 @@ func TestMissingBlobRequest(t *testing.T) { }) } } + +func TestOriginOutsideRetention(t *testing.T) { + ctx := context.Background() + bdb := dbtest.SetupDB(t) + genesis := time.Unix(0, 0) + secsPerEpoch := params.BeaconConfig().SecondsPerSlot * uint64(params.BeaconConfig().SlotsPerEpoch) + retentionSeconds := time.Second * time.Duration(uint64(params.BeaconConfig().MinEpochsForBlobsSidecarsRequest+1)*secsPerEpoch) + outsideRetention := genesis.Add(retentionSeconds) + now := func() time.Time { + return outsideRetention + } + clock := startup.NewClock(genesis, [32]byte{}, startup.WithNower(now)) + s := &Service{ctx: ctx, cfg: &Config{DB: bdb}, clock: clock} + blk, _ := util.GenerateTestDenebBlockWithSidecar(t, [32]byte{}, 0, 1) + require.NoError(t, bdb.SaveBlock(ctx, blk)) + concreteDB, ok := bdb.(*kv.Store) + require.Equal(t, true, ok) + require.NoError(t, concreteDB.SaveOriginCheckpointBlockRoot(ctx, blk.Root())) + // This would break due to missing service dependencies, but will return nil fast due to being outside retention. + require.Equal(t, false, params.WithinDAPeriod(slots.ToEpoch(blk.Block().Slot()), slots.ToEpoch(clock.CurrentSlot()))) + require.NoError(t, s.fetchOriginBlobs([]peer.ID{})) +}