diff --git a/beacon-chain/blockchain/head_test.go b/beacon-chain/blockchain/head_test.go index 6c1822a6a5ac..012cb54c127d 100644 --- a/beacon-chain/blockchain/head_test.go +++ b/beacon-chain/blockchain/head_test.go @@ -63,7 +63,7 @@ func TestSaveHead_Different(t *testing.T) { wsb := util.SaveBlock(t, context.Background(), service.cfg.BeaconDB, newHeadSignedBlock) newRoot, err := newHeadBlock.HashTreeRoot() require.NoError(t, err) - state, blkRoot, err = prepareForkchoiceState(ctx, wsb.Block().Slot()-1, wsb.Block().ParentRoot(), service.cfg.ForkChoiceStore.CachedHeadRoot(), [32]byte{}, ojc, ofc) + state, blkRoot, err = prepareForkchoiceState(ctx, slots.PrevSlot(wsb.Block().Slot()), wsb.Block().ParentRoot(), service.cfg.ForkChoiceStore.CachedHeadRoot(), [32]byte{}, ojc, ofc) require.NoError(t, err) require.NoError(t, service.cfg.ForkChoiceStore.InsertNode(ctx, state, blkRoot)) @@ -238,7 +238,7 @@ func TestRetrieveHead_ReadOnly(t *testing.T) { wsb := util.SaveBlock(t, context.Background(), service.cfg.BeaconDB, newHeadSignedBlock) newRoot, err := newHeadBlock.HashTreeRoot() require.NoError(t, err) - state, blkRoot, err := prepareForkchoiceState(ctx, wsb.Block().Slot()-1, wsb.Block().ParentRoot(), service.cfg.ForkChoiceStore.CachedHeadRoot(), [32]byte{}, ojc, ofc) + state, blkRoot, err := prepareForkchoiceState(ctx, slots.PrevSlot(wsb.Block().Slot()), wsb.Block().ParentRoot(), service.cfg.ForkChoiceStore.CachedHeadRoot(), [32]byte{}, ojc, ofc) require.NoError(t, err) require.NoError(t, service.cfg.ForkChoiceStore.InsertNode(ctx, state, blkRoot)) diff --git a/beacon-chain/core/transition/BUILD.bazel b/beacon-chain/core/transition/BUILD.bazel index 17b41c191b50..5aa1cbcc1ef0 100644 --- a/beacon-chain/core/transition/BUILD.bazel +++ b/beacon-chain/core/transition/BUILD.bazel @@ -96,6 +96,7 @@ go_test( "//testing/benchmark:go_default_library", "//testing/require:go_default_library", "//testing/util:go_default_library", + "//time/slots:go_default_library", "@com_github_google_gofuzz//:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", "@org_golang_google_protobuf//proto:go_default_library", diff --git a/beacon-chain/core/transition/altair_transition_no_verify_sig_test.go b/beacon-chain/core/transition/altair_transition_no_verify_sig_test.go index 381b773f3778..b49a6c62f37d 100644 --- a/beacon-chain/core/transition/altair_transition_no_verify_sig_test.go +++ b/beacon-chain/core/transition/altair_transition_no_verify_sig_test.go @@ -21,6 +21,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/require" "github.com/prysmaticlabs/prysm/v5/testing/util" + "github.com/prysmaticlabs/prysm/v5/time/slots" ) func TestExecuteAltairStateTransitionNoVerify_FullProcess(t *testing.T) { @@ -48,7 +49,7 @@ func TestExecuteAltairStateTransitionNoVerify_FullProcess(t *testing.T) { epoch := time.CurrentEpoch(beaconState) randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys) require.NoError(t, err) - require.NoError(t, beaconState.SetSlot(beaconState.Slot()-1)) + require.NoError(t, beaconState.SetSlot(slots.PrevSlot(beaconState.Slot()))) nextSlotState, err := transition.ProcessSlots(context.Background(), beaconState.Copy(), beaconState.Slot()+1) require.NoError(t, err) @@ -135,7 +136,7 @@ func TestExecuteAltairStateTransitionNoVerifySignature_CouldNotVerifyStateRoot(t epoch := time.CurrentEpoch(beaconState) randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys) require.NoError(t, err) - require.NoError(t, beaconState.SetSlot(beaconState.Slot()-1)) + require.NoError(t, beaconState.SetSlot(slots.PrevSlot(beaconState.Slot()))) nextSlotState, err := transition.ProcessSlots(context.Background(), beaconState.Copy(), beaconState.Slot()+1) require.NoError(t, err) diff --git a/beacon-chain/core/transition/bellatrix_transition_no_verify_sig_test.go b/beacon-chain/core/transition/bellatrix_transition_no_verify_sig_test.go index e2542fe5ded2..68cde2f906f5 100644 --- a/beacon-chain/core/transition/bellatrix_transition_no_verify_sig_test.go +++ b/beacon-chain/core/transition/bellatrix_transition_no_verify_sig_test.go @@ -23,6 +23,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/require" "github.com/prysmaticlabs/prysm/v5/testing/util" + "github.com/prysmaticlabs/prysm/v5/time/slots" ) func TestExecuteBellatrixStateTransitionNoVerify_FullProcess(t *testing.T) { @@ -50,7 +51,7 @@ func TestExecuteBellatrixStateTransitionNoVerify_FullProcess(t *testing.T) { epoch := time.CurrentEpoch(beaconState) randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys) require.NoError(t, err) - require.NoError(t, beaconState.SetSlot(beaconState.Slot()-1)) + require.NoError(t, beaconState.SetSlot(slots.PrevSlot(beaconState.Slot()))) nextSlotState, err := transition.ProcessSlots(context.Background(), beaconState.Copy(), beaconState.Slot()+1) require.NoError(t, err) @@ -124,7 +125,7 @@ func TestExecuteBellatrixStateTransitionNoVerifySignature_CouldNotVerifyStateRoo DepositRoot: bytesutil.PadTo([]byte{2}, 32), BlockHash: make([]byte, 32), } - require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch-1)) + require.NoError(t, beaconState.SetSlot(slots.PrevSlot(params.BeaconConfig().SlotsPerEpoch))) e := beaconState.Eth1Data() e.DepositCount = 100 require.NoError(t, beaconState.SetEth1Data(e)) @@ -137,7 +138,7 @@ func TestExecuteBellatrixStateTransitionNoVerifySignature_CouldNotVerifyStateRoo epoch := time.CurrentEpoch(beaconState) randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys) require.NoError(t, err) - require.NoError(t, beaconState.SetSlot(beaconState.Slot()-1)) + require.NoError(t, beaconState.SetSlot(slots.PrevSlot(beaconState.Slot()))) nextSlotState, err := transition.ProcessSlots(context.Background(), beaconState.Copy(), beaconState.Slot()+1) require.NoError(t, err) diff --git a/beacon-chain/rpc/eth/builder/handlers.go b/beacon-chain/rpc/eth/builder/handlers.go index 986a669a5202..0d9fc4b136fc 100644 --- a/beacon-chain/rpc/eth/builder/handlers.go +++ b/beacon-chain/rpc/eth/builder/handlers.go @@ -80,7 +80,7 @@ func (s *Server) ExpectedWithdrawals(w http.ResponseWriter, r *http.Request) { httputil.WriteError(w, handleWrapError(err, "could not get optimistic mode info", http.StatusInternalServerError)) return } - root, err := helpers.BlockRootAtSlot(st, st.Slot()-1) + root, err := helpers.BlockRootAtSlot(st, slots.PrevSlot(st.Slot())) if err != nil { httputil.WriteError(w, handleWrapError(err, "could not get block root", http.StatusInternalServerError)) return diff --git a/beacon-chain/rpc/eth/rewards/service.go b/beacon-chain/rpc/eth/rewards/service.go index 2487334300eb..85f586be6fb3 100644 --- a/beacon-chain/rpc/eth/rewards/service.go +++ b/beacon-chain/rpc/eth/rewards/service.go @@ -14,6 +14,7 @@ import ( consensusblocks "github.com/prysmaticlabs/prysm/v5/consensus-types/blocks" "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v5/network/httputil" + "github.com/prysmaticlabs/prysm/v5/time/slots" ) // BlockRewardsFetcher is a interface that provides access to reward related responses @@ -123,7 +124,7 @@ func (rs *BlockRewardService) GetStateForRewards(ctx context.Context, blk interf // We want to run several block processing functions that update the proposer's balance. // This will allow us to calculate proposer rewards for each operation (atts, slashings etc). // To do this, we replay the state up to the block's slot, but before processing the block. - st, err := rs.Replayer.ReplayerForSlot(blk.Slot()-1).ReplayToSlot(ctx, blk.Slot()) + st, err := rs.Replayer.ReplayerForSlot(slots.PrevSlot(blk.Slot())).ReplayToSlot(ctx, blk.Slot()) if err != nil { return nil, &httputil.DefaultJsonError{ Message: "Could not get state: " + err.Error(), diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_altair.go b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_altair.go index 81b15468c5a1..abb1564724ea 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_altair.go +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_altair.go @@ -12,6 +12,7 @@ import ( ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" synccontribution "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1/attestation/aggregation/sync_contribution" "github.com/prysmaticlabs/prysm/v5/runtime/version" + "github.com/prysmaticlabs/prysm/v5/time/slots" "go.opencensus.io/trace" ) @@ -20,7 +21,7 @@ func (vs *Server) setSyncAggregate(ctx context.Context, blk interfaces.SignedBea return } - syncAggregate, err := vs.getSyncAggregate(ctx, blk.Block().Slot()-1, blk.Block().ParentRoot()) + syncAggregate, err := vs.getSyncAggregate(ctx, slots.PrevSlot(blk.Block().Slot()), blk.Block().ParentRoot()) if err != nil { log.WithError(err).Error("Could not get sync aggregate") emptySig := [96]byte{0xC0} diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_test.go b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_test.go index ce1542bace58..f9aa94e33900 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_test.go +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_test.go @@ -838,7 +838,7 @@ func TestProposer_ComputeStateRoot_OK(t *testing.T) { require.NoError(t, err) proposerIdx, err := helpers.BeaconProposerIndex(ctx, beaconState) require.NoError(t, err) - require.NoError(t, beaconState.SetSlot(beaconState.Slot()-1)) + require.NoError(t, beaconState.SetSlot(slots.PrevSlot(beaconState.Slot()))) req.Block.Body.RandaoReveal = randaoReveal currentEpoch := coretime.CurrentEpoch(beaconState) req.Signature, err = signing.ComputeDomainAndSign(beaconState, currentEpoch, req.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) diff --git a/beacon-chain/sync/validate_sync_committee_message_test.go b/beacon-chain/sync/validate_sync_committee_message_test.go index 66425961275d..befbf3b83de1 100644 --- a/beacon-chain/sync/validate_sync_committee_message_test.go +++ b/beacon-chain/sync/validate_sync_committee_message_test.go @@ -221,7 +221,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) { msg.ValidatorIndex = primitives.ValidatorIndex(chosenVal) msg.Slot = slots.PrevSlot(hState.Slot()) - gt := time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(hState.Slot()-1)) + gt := time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(slots.PrevSlot(hState.Slot()))) vr := [32]byte{'A'} clock := startup.NewClock(gt, vr) digest, err := forks.CreateForkDigest(gt, vr[:]) @@ -268,7 +268,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) { msg.ValidatorIndex = primitives.ValidatorIndex(chosenVal) msg.Slot = slots.PrevSlot(hState.Slot()) - gt := time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(hState.Slot()-1)) + gt := time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(slots.PrevSlot(hState.Slot()))) vr := [32]byte{'A'} digest, err := forks.CreateForkDigest(gt, vr[:]) assert.NoError(t, err) @@ -322,7 +322,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) { } // Set Topic and Subnet - gt := time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(hState.Slot()-1)) + gt := time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(slots.PrevSlot(hState.Slot()))) vr := [32]byte{'A'} digest, err := forks.CreateForkDigest(gt, vr[:]) assert.NoError(t, err) @@ -380,7 +380,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) { msg.Slot = slots.PrevSlot(hState.Slot()) // Set Topic and Subnet - gt := time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(hState.Slot()-1)) + gt := time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(slots.PrevSlot(hState.Slot()))) vr := [32]byte{'A'} digest, err := forks.CreateForkDigest(gt, vr[:]) assert.NoError(t, err) diff --git a/testing/util/helpers_test.go b/testing/util/helpers_test.go index 5b601cc96fe6..d2e10272eff6 100644 --- a/testing/util/helpers_test.go +++ b/testing/util/helpers_test.go @@ -26,7 +26,7 @@ func TestBlockSignature(t *testing.T) { proposerIdx, err := helpers.BeaconProposerIndex(context.Background(), beaconState) assert.NoError(t, err) - assert.NoError(t, beaconState.SetSlot(beaconState.Slot()-1)) + assert.NoError(t, beaconState.SetSlot(slots.PrevSlot(beaconState.Slot()))) epoch := slots.ToEpoch(block.Block.Slot) blockSig, err := signing.ComputeDomainAndSign(beaconState, epoch, block.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) require.NoError(t, err)