Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(state-transition): fix deposit index upon genesis processing #2116

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mod/state-transition/pkg/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 21 additions & 13 deletions mod/state-transition/pkg/core/state_processor_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions mod/state-transition/pkg/core/state_processor_genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deposits should be zero-indexed, not 1-indexed as they currently are

}
19 changes: 13 additions & 6 deletions mod/state-transition/pkg/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
Loading