From 98299ee4044f5b98b3ce928e9ee360d81e05cd06 Mon Sep 17 00:00:00 2001 From: aBear Date: Thu, 31 Oct 2024 12:29:46 +0100 Subject: [PATCH] fixed deposit index initialization upon genesis --- mod/state-transition/pkg/core/helpers_test.go | 39 ++++++++++++++++ .../pkg/core/state_processor.go | 4 ++ .../pkg/core/state_processor_genesis.go | 34 ++++++++------ .../pkg/core/state_processor_genesis_test.go | 44 +++---------------- .../pkg/core/state_processor_staking.go | 19 +++++--- 5 files changed, 82 insertions(+), 58 deletions(-) diff --git a/mod/state-transition/pkg/core/helpers_test.go b/mod/state-transition/pkg/core/helpers_test.go index ceba3b6432..25b71c2035 100644 --- a/mod/state-transition/pkg/core/helpers_test.go +++ b/mod/state-transition/pkg/core/helpers_test.go @@ -30,7 +30,9 @@ import ( "cosmossdk.io/store/metrics" storetypes "cosmossdk.io/store/types" "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" + engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives" "github.com/berachain/beacon-kit/mod/node-core/pkg/components" + statedb "github.com/berachain/beacon-kit/mod/state-transition/pkg/core/state" "github.com/berachain/beacon-kit/mod/storage/pkg/beacondb" "github.com/berachain/beacon-kit/mod/storage/pkg/db" "github.com/berachain/beacon-kit/mod/storage/pkg/encoding" @@ -38,6 +40,43 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +type ( + TestBeaconStateMarshallableT = types.BeaconState[ + *types.BeaconBlockHeader, + *types.Eth1Data, + *types.ExecutionPayloadHeader, + *types.Fork, + *types.Validator, + types.BeaconBlockHeader, + types.Eth1Data, + types.ExecutionPayloadHeader, + types.Fork, + types.Validator, + ] + + TestKVStoreT = beacondb.KVStore[ + *types.BeaconBlockHeader, + *types.Eth1Data, + *types.ExecutionPayloadHeader, + *types.Fork, + *types.Validator, + types.Validators, + ] + + TestBeaconStateT = statedb.StateDB[ + *types.BeaconBlockHeader, + *TestBeaconStateMarshallableT, + *types.Eth1Data, + *types.ExecutionPayloadHeader, + *types.Fork, + *TestKVStoreT, + *types.Validator, + types.Validators, + *engineprimitives.Withdrawal, + types.WithdrawalCredentials, + ] +) + type testKVStoreService struct { ctx sdk.Context } diff --git a/mod/state-transition/pkg/core/state_processor.go b/mod/state-transition/pkg/core/state_processor.go index 439cf3895f..df473e6ebc 100644 --- a/mod/state-transition/pkg/core/state_processor.go +++ b/mod/state-transition/pkg/core/state_processor.go @@ -85,6 +85,10 @@ type StateProcessor[ executionEngine ExecutionEngine[ ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalsT, ] + + // processingGenesis allows initializing correctly + // eth1 deposit index upon genesis + processingGenesis bool } // NewStateProcessor creates a new state processor. diff --git a/mod/state-transition/pkg/core/state_processor_genesis.go b/mod/state-transition/pkg/core/state_processor_genesis.go index a142950598..9e7502b0df 100644 --- a/mod/state-transition/pkg/core/state_processor_genesis.go +++ b/mod/state-transition/pkg/core/state_processor_genesis.go @@ -47,6 +47,11 @@ func (sp *StateProcessor[ executionPayloadHeader ExecutionPayloadHeaderT, genesisVersion common.Version, ) (transition.ValidatorUpdates, error) { + sp.processingGenesis = true + defer func() { + sp.processingGenesis = false + }() + var ( blkHeader BeaconBlockHeaderT blkBody BeaconBlockBodyT @@ -67,25 +72,28 @@ func (sp *StateProcessor[ return nil, err } - if err := st.SetEth1DepositIndex(0); err != nil { - return nil, err - } + // Eth1DepositIndex will be set in processDeposit - if err := st.SetEth1Data(eth1Data.New( - common.Root{}, - 0, - executionPayloadHeader.GetBlockHash(), - )); err != nil { + if err := st.SetEth1Data( + eth1Data.New( + common.Root{}, + 0, + executionPayloadHeader.GetBlockHash(), + )); err != nil { return nil, err } // TODO: we need to handle common.Version vs // uint32 better. - bodyRoot := blkBody.Empty( - version.ToUint32(genesisVersion)).HashTreeRoot() - if err := st.SetLatestBlockHeader(blkHeader.New( - 0, 0, common.Root{}, common.Root{}, bodyRoot, - )); err != nil { + bodyRoot := blkBody.Empty(version.ToUint32(genesisVersion)).HashTreeRoot() + if err := st.SetLatestBlockHeader( + blkHeader.New( + 0, // slot + 0, // proposer index + common.Root{}, // parent block root + common.Root{}, // state root + bodyRoot, + )); err != nil { return nil, err } diff --git a/mod/state-transition/pkg/core/state_processor_genesis_test.go b/mod/state-transition/pkg/core/state_processor_genesis_test.go index 20996901ea..5b97149ede 100644 --- a/mod/state-transition/pkg/core/state_processor_genesis_test.go +++ b/mod/state-transition/pkg/core/state_processor_genesis_test.go @@ -34,49 +34,10 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/version" "github.com/berachain/beacon-kit/mod/state-transition/pkg/core" "github.com/berachain/beacon-kit/mod/state-transition/pkg/core/mocks" - statedb "github.com/berachain/beacon-kit/mod/state-transition/pkg/core/state" - "github.com/berachain/beacon-kit/mod/storage/pkg/beacondb" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) -type ( - TestBeaconStateMarshallableT = types.BeaconState[ - *types.BeaconBlockHeader, - *types.Eth1Data, - *types.ExecutionPayloadHeader, - *types.Fork, - *types.Validator, - types.BeaconBlockHeader, - types.Eth1Data, - types.ExecutionPayloadHeader, - types.Fork, - types.Validator, - ] - - TestKVStoreT = beacondb.KVStore[ - *types.BeaconBlockHeader, - *types.Eth1Data, - *types.ExecutionPayloadHeader, - *types.Fork, - *types.Validator, - types.Validators, - ] - - TestBeaconStateT = statedb.StateDB[ - *types.BeaconBlockHeader, - *TestBeaconStateMarshallableT, - *types.Eth1Data, - *types.ExecutionPayloadHeader, - *types.Fork, - *TestKVStoreT, - *types.Validator, - types.Validators, - *engineprimitives.Withdrawal, - types.WithdrawalCredentials, - ] -) - func TestInitialize(t *testing.T) { // Create state processor to test cs := spec.TestnetChainSpec() @@ -183,4 +144,9 @@ func TestInitialize(t *testing.T) { require.Equal(t, dep.Pubkey, val.Pubkey) require.Equal(t, dep.Amount, val.EffectiveBalance) } + + // check that validator index is duly set + latestValIdx, err := beaconState.GetEth1DepositIndex() + require.NoError(t, err) + require.Equal(t, uint64(len(deposits)-1), latestValIdx) } diff --git a/mod/state-transition/pkg/core/state_processor_staking.go b/mod/state-transition/pkg/core/state_processor_staking.go index fc336908da..a798b1fcfc 100644 --- a/mod/state-transition/pkg/core/state_processor_staking.go +++ b/mod/state-transition/pkg/core/state_processor_staking.go @@ -21,6 +21,8 @@ package core import ( + "fmt" + "github.com/berachain/beacon-kit/mod/errors" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" @@ -83,14 +85,19 @@ func (sp *StateProcessor[ st BeaconStateT, dep DepositT, ) error { - depositIndex, err := st.GetEth1DepositIndex() - if err != nil { - return err + var nextDepositIndex uint64 + switch depositIndex, err := st.GetEth1DepositIndex(); { + case err == nil: + nextDepositIndex = depositIndex + 1 + case sp.processingGenesis && err != nil: + // Eth1DepositIndex may have not been set yet + nextDepositIndex = 0 + default: + // Failed retrieving Eth1DepositIndex outside genesis is an error + return fmt.Errorf("failed retrieving eth1 deposit index: %w", err) } - if err = st.SetEth1DepositIndex( - depositIndex + 1, - ); err != nil { + if err := st.SetEth1DepositIndex(nextDepositIndex); err != nil { return err }