From fda458925117adf37f0a320cbcc513a162b54a6f Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Mon, 18 Mar 2024 20:57:21 +0800 Subject: [PATCH] Rewrite Pruning Implementation To Handle EIP 7045 (#13762) * make it very big * use new pruning implementation * handle pre deneb * revert cache change * less verbose * gaz * Update beacon-chain/operations/attestations/prune_expired.go Co-authored-by: Potuz * gofmt * be safer --------- Co-authored-by: Potuz --- .../operations/attestations/BUILD.bazel | 1 + .../operations/attestations/prune_expired.go | 14 ++++++++++++- .../attestations/prune_expired_test.go | 20 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/beacon-chain/operations/attestations/BUILD.bazel b/beacon-chain/operations/attestations/BUILD.bazel index c68a0d11a..5b4cc57b3 100644 --- a/beacon-chain/operations/attestations/BUILD.bazel +++ b/beacon-chain/operations/attestations/BUILD.bazel @@ -49,6 +49,7 @@ go_test( "//beacon-chain/operations/attestations/kv:go_default_library", "//config/fieldparams:go_default_library", "//config/params:go_default_library", + "//consensus-types/primitives:go_default_library", "//crypto/bls:go_default_library", "//proto/prysm/v1alpha1:go_default_library", "//proto/prysm/v1alpha1/attestation/aggregation/attestations:go_default_library", diff --git a/beacon-chain/operations/attestations/prune_expired.go b/beacon-chain/operations/attestations/prune_expired.go index 8413c3ff8..e956fee3a 100644 --- a/beacon-chain/operations/attestations/prune_expired.go +++ b/beacon-chain/operations/attestations/prune_expired.go @@ -6,6 +6,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/config/params" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" prysmTime "github.com/prysmaticlabs/prysm/v5/time" + "github.com/prysmaticlabs/prysm/v5/time/slots" ) // pruneAttsPool prunes attestations pool on every slot interval. @@ -66,7 +67,18 @@ func (s *Service) pruneExpiredAtts() { // Return true if the input slot has been expired. // Expired is defined as one epoch behind than current time. -func (s *Service) expired(slot primitives.Slot) bool { +func (s *Service) expired(providedSlot primitives.Slot) bool { + providedEpoch := slots.ToEpoch(providedSlot) + currSlot := slots.CurrentSlot(s.genesisTime) + currEpoch := slots.ToEpoch(currSlot) + if currEpoch < params.BeaconConfig().DenebForkEpoch { + return s.expiredPreDeneb(providedSlot) + } + return providedEpoch+1 < currEpoch +} + +// Handles expiration of attestations before deneb. +func (s *Service) expiredPreDeneb(slot primitives.Slot) bool { expirationSlot := slot + params.BeaconConfig().SlotsPerEpoch expirationTime := s.genesisTime + uint64(expirationSlot.Mul(params.BeaconConfig().SecondsPerSlot)) currentTime := uint64(prysmTime.Now().Unix()) diff --git a/beacon-chain/operations/attestations/prune_expired_test.go b/beacon-chain/operations/attestations/prune_expired_test.go index d27213444..816e4689b 100644 --- a/beacon-chain/operations/attestations/prune_expired_test.go +++ b/beacon-chain/operations/attestations/prune_expired_test.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/async" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" "github.com/prysmaticlabs/prysm/v5/config/params" + "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/require" @@ -127,3 +128,22 @@ func TestPruneExpired_Expired(t *testing.T) { assert.Equal(t, true, s.expired(0), "Should be expired") assert.Equal(t, false, s.expired(1), "Should not be expired") } + +func TestPruneExpired_ExpiredDeneb(t *testing.T) { + params.SetupTestConfigCleanup(t) + cfg := params.BeaconConfig() + cfg.DenebForkEpoch = 3 + params.OverrideBeaconConfig(cfg) + + s, err := NewService(context.Background(), &Config{Pool: NewPool()}) + require.NoError(t, err) + + // Rewind back 4 epochs + 10 slots worth of time. + s.genesisTime = uint64(prysmTime.Now().Unix()) - (4*uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot)) + 10) + secondEpochStart := primitives.Slot(2 * uint64(params.BeaconConfig().SlotsPerEpoch)) + thirdEpochStart := primitives.Slot(3 * uint64(params.BeaconConfig().SlotsPerEpoch)) + + assert.Equal(t, true, s.expired(secondEpochStart), "Should be expired") + assert.Equal(t, false, s.expired(thirdEpochStart), "Should not be expired") + +}