small cleanup on functions (#13666)

This commit is contained in:
james-prysm 2024-02-26 16:23:23 -06:00 committed by GitHub
parent 0132c1b17d
commit 0453d18395
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 21 additions and 16 deletions

View File

@ -63,7 +63,7 @@ func TestSaveHead_Different(t *testing.T) {
wsb := util.SaveBlock(t, context.Background(), service.cfg.BeaconDB, newHeadSignedBlock) wsb := util.SaveBlock(t, context.Background(), service.cfg.BeaconDB, newHeadSignedBlock)
newRoot, err := newHeadBlock.HashTreeRoot() newRoot, err := newHeadBlock.HashTreeRoot()
require.NoError(t, err) 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, err)
require.NoError(t, service.cfg.ForkChoiceStore.InsertNode(ctx, state, blkRoot)) 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) wsb := util.SaveBlock(t, context.Background(), service.cfg.BeaconDB, newHeadSignedBlock)
newRoot, err := newHeadBlock.HashTreeRoot() newRoot, err := newHeadBlock.HashTreeRoot()
require.NoError(t, err) 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, err)
require.NoError(t, service.cfg.ForkChoiceStore.InsertNode(ctx, state, blkRoot)) require.NoError(t, service.cfg.ForkChoiceStore.InsertNode(ctx, state, blkRoot))

View File

@ -96,6 +96,7 @@ go_test(
"//testing/benchmark:go_default_library", "//testing/benchmark:go_default_library",
"//testing/require:go_default_library", "//testing/require:go_default_library",
"//testing/util:go_default_library", "//testing/util:go_default_library",
"//time/slots:go_default_library",
"@com_github_google_gofuzz//:go_default_library", "@com_github_google_gofuzz//:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library", "@org_golang_google_protobuf//proto:go_default_library",

View File

@ -21,6 +21,7 @@ import (
"github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/assert"
"github.com/prysmaticlabs/prysm/v5/testing/require" "github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/prysmaticlabs/prysm/v5/testing/util" "github.com/prysmaticlabs/prysm/v5/testing/util"
"github.com/prysmaticlabs/prysm/v5/time/slots"
) )
func TestExecuteAltairStateTransitionNoVerify_FullProcess(t *testing.T) { func TestExecuteAltairStateTransitionNoVerify_FullProcess(t *testing.T) {
@ -48,7 +49,7 @@ func TestExecuteAltairStateTransitionNoVerify_FullProcess(t *testing.T) {
epoch := time.CurrentEpoch(beaconState) epoch := time.CurrentEpoch(beaconState)
randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys) randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys)
require.NoError(t, err) 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) nextSlotState, err := transition.ProcessSlots(context.Background(), beaconState.Copy(), beaconState.Slot()+1)
require.NoError(t, err) require.NoError(t, err)
@ -135,7 +136,7 @@ func TestExecuteAltairStateTransitionNoVerifySignature_CouldNotVerifyStateRoot(t
epoch := time.CurrentEpoch(beaconState) epoch := time.CurrentEpoch(beaconState)
randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys) randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys)
require.NoError(t, err) 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) nextSlotState, err := transition.ProcessSlots(context.Background(), beaconState.Copy(), beaconState.Slot()+1)
require.NoError(t, err) require.NoError(t, err)

View File

@ -23,6 +23,7 @@ import (
"github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/assert"
"github.com/prysmaticlabs/prysm/v5/testing/require" "github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/prysmaticlabs/prysm/v5/testing/util" "github.com/prysmaticlabs/prysm/v5/testing/util"
"github.com/prysmaticlabs/prysm/v5/time/slots"
) )
func TestExecuteBellatrixStateTransitionNoVerify_FullProcess(t *testing.T) { func TestExecuteBellatrixStateTransitionNoVerify_FullProcess(t *testing.T) {
@ -50,7 +51,7 @@ func TestExecuteBellatrixStateTransitionNoVerify_FullProcess(t *testing.T) {
epoch := time.CurrentEpoch(beaconState) epoch := time.CurrentEpoch(beaconState)
randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys) randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys)
require.NoError(t, err) 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) nextSlotState, err := transition.ProcessSlots(context.Background(), beaconState.Copy(), beaconState.Slot()+1)
require.NoError(t, err) require.NoError(t, err)
@ -124,7 +125,7 @@ func TestExecuteBellatrixStateTransitionNoVerifySignature_CouldNotVerifyStateRoo
DepositRoot: bytesutil.PadTo([]byte{2}, 32), DepositRoot: bytesutil.PadTo([]byte{2}, 32),
BlockHash: make([]byte, 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 := beaconState.Eth1Data()
e.DepositCount = 100 e.DepositCount = 100
require.NoError(t, beaconState.SetEth1Data(e)) require.NoError(t, beaconState.SetEth1Data(e))
@ -137,7 +138,7 @@ func TestExecuteBellatrixStateTransitionNoVerifySignature_CouldNotVerifyStateRoo
epoch := time.CurrentEpoch(beaconState) epoch := time.CurrentEpoch(beaconState)
randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys) randaoReveal, err := util.RandaoReveal(beaconState, epoch, privKeys)
require.NoError(t, err) 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) nextSlotState, err := transition.ProcessSlots(context.Background(), beaconState.Copy(), beaconState.Slot()+1)
require.NoError(t, err) require.NoError(t, err)

View File

@ -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)) httputil.WriteError(w, handleWrapError(err, "could not get optimistic mode info", http.StatusInternalServerError))
return return
} }
root, err := helpers.BlockRootAtSlot(st, st.Slot()-1) root, err := helpers.BlockRootAtSlot(st, slots.PrevSlot(st.Slot()))
if err != nil { if err != nil {
httputil.WriteError(w, handleWrapError(err, "could not get block root", http.StatusInternalServerError)) httputil.WriteError(w, handleWrapError(err, "could not get block root", http.StatusInternalServerError))
return return

View File

@ -14,6 +14,7 @@ import (
consensusblocks "github.com/prysmaticlabs/prysm/v5/consensus-types/blocks" consensusblocks "github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
"github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces"
"github.com/prysmaticlabs/prysm/v5/network/httputil" "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 // 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. // 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). // 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. // 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 { if err != nil {
return nil, &httputil.DefaultJsonError{ return nil, &httputil.DefaultJsonError{
Message: "Could not get state: " + err.Error(), Message: "Could not get state: " + err.Error(),

View File

@ -12,6 +12,7 @@ import (
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
synccontribution "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1/attestation/aggregation/sync_contribution" 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/runtime/version"
"github.com/prysmaticlabs/prysm/v5/time/slots"
"go.opencensus.io/trace" "go.opencensus.io/trace"
) )
@ -20,7 +21,7 @@ func (vs *Server) setSyncAggregate(ctx context.Context, blk interfaces.SignedBea
return 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 { if err != nil {
log.WithError(err).Error("Could not get sync aggregate") log.WithError(err).Error("Could not get sync aggregate")
emptySig := [96]byte{0xC0} emptySig := [96]byte{0xC0}

View File

@ -838,7 +838,7 @@ func TestProposer_ComputeStateRoot_OK(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
proposerIdx, err := helpers.BeaconProposerIndex(ctx, beaconState) proposerIdx, err := helpers.BeaconProposerIndex(ctx, beaconState)
require.NoError(t, err) 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 req.Block.Body.RandaoReveal = randaoReveal
currentEpoch := coretime.CurrentEpoch(beaconState) currentEpoch := coretime.CurrentEpoch(beaconState)
req.Signature, err = signing.ComputeDomainAndSign(beaconState, currentEpoch, req.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) req.Signature, err = signing.ComputeDomainAndSign(beaconState, currentEpoch, req.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])

View File

@ -221,7 +221,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
msg.ValidatorIndex = primitives.ValidatorIndex(chosenVal) msg.ValidatorIndex = primitives.ValidatorIndex(chosenVal)
msg.Slot = slots.PrevSlot(hState.Slot()) 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'} vr := [32]byte{'A'}
clock := startup.NewClock(gt, vr) clock := startup.NewClock(gt, vr)
digest, err := forks.CreateForkDigest(gt, vr[:]) digest, err := forks.CreateForkDigest(gt, vr[:])
@ -268,7 +268,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
msg.ValidatorIndex = primitives.ValidatorIndex(chosenVal) msg.ValidatorIndex = primitives.ValidatorIndex(chosenVal)
msg.Slot = slots.PrevSlot(hState.Slot()) 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'} vr := [32]byte{'A'}
digest, err := forks.CreateForkDigest(gt, vr[:]) digest, err := forks.CreateForkDigest(gt, vr[:])
assert.NoError(t, err) assert.NoError(t, err)
@ -322,7 +322,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
} }
// Set Topic and Subnet // 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'} vr := [32]byte{'A'}
digest, err := forks.CreateForkDigest(gt, vr[:]) digest, err := forks.CreateForkDigest(gt, vr[:])
assert.NoError(t, err) assert.NoError(t, err)
@ -380,7 +380,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
msg.Slot = slots.PrevSlot(hState.Slot()) msg.Slot = slots.PrevSlot(hState.Slot())
// Set Topic and Subnet // 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'} vr := [32]byte{'A'}
digest, err := forks.CreateForkDigest(gt, vr[:]) digest, err := forks.CreateForkDigest(gt, vr[:])
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -26,7 +26,7 @@ func TestBlockSignature(t *testing.T) {
proposerIdx, err := helpers.BeaconProposerIndex(context.Background(), beaconState) proposerIdx, err := helpers.BeaconProposerIndex(context.Background(), beaconState)
assert.NoError(t, err) 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) epoch := slots.ToEpoch(block.Block.Slot)
blockSig, err := signing.ComputeDomainAndSign(beaconState, epoch, block.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) blockSig, err := signing.ComputeDomainAndSign(beaconState, epoch, block.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])
require.NoError(t, err) require.NoError(t, err)