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-transitions): Make setting of eth1 deposit index backward compatible #2202

Merged
merged 9 commits into from
Dec 3, 2024
10 changes: 0 additions & 10 deletions mod/state-transition/pkg/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,3 @@ func buildNextBlock(
Body: nextBlkBody,
}
}

var (
dummyExecutionPayload = &types.ExecutionPayload{
Timestamp: 0,
ExtraData: []byte("testing"),
Transactions: [][]byte{},
Withdrawals: []*engineprimitives.Withdrawal{},
BaseFeePerGas: math.NewU256(0),
}
)
5 changes: 5 additions & 0 deletions mod/state-transition/pkg/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ func (sp *StateProcessor[
if err = st.SetEth1DepositIndex(fixedDepositIdx); err != nil {
return nil, err
}

sp.logger.Info(
calbera marked this conversation as resolved.
Show resolved Hide resolved
"Fixed Eth 1 deposit index",
"previous", idx, "fixed", fixedDepositIdx,
)
}

// Process the Epoch Boundary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func TestInitialize(t *testing.T) {
checkValidatorNonBartio(t, cs, st, dep)
}

// check that validator index is duly set
// check that deposit index is duly set. On betnet
// deposit index is set to the last accepted deposit.
latestValIdx, err := st.GetEth1DepositIndex()
require.NoError(t, err)
require.Equal(t, uint64(len(genDeposits)-1), latestValIdx)
Expand Down Expand Up @@ -246,10 +247,11 @@ func TestInitializeBartio(t *testing.T) {
checkValidatorBartio(t, cs, st, dep)
}

// check that validator index is duly set
// check that deposit index is duly set. On Bartio
// deposit index is off by 1.
latestValIdx, err := st.GetEth1DepositIndex()
require.NoError(t, err)
require.Equal(t, uint64(len(genDeposits)-1), latestValIdx)
require.Equal(t, uint64(len(genDeposits)), latestValIdx)
}

func checkValidatorBartio(
Expand Down
52 changes: 49 additions & 3 deletions mod/state-transition/pkg/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,38 @@ func (sp *StateProcessor[
st BeaconStateT,
dep DepositT,
) error {
if err := st.SetEth1DepositIndex(dep.GetIndex().Unwrap()); err != nil {
slot, err := st.GetSlot()
if err != nil {
return err
}

depositIndex := dep.GetIndex().Unwrap()
switch {
case sp.cs.DepositEth1ChainID() == spec.BartioChainID:
// Bartio has a bug which makes DepositEth1ChainID point to
// next deposit index, not latest processed deposit index.
// We keep it for backward compatibility.
depositIndex++
case sp.cs.DepositEth1ChainID() == spec.BoonetEth1ChainID &&
slot < math.U64(spec.BoonetFork2Height):
// Boonet pre fork2 has a bug which makes DepositEth1ChainID point to
// next deposit index, not latest processed deposit index.
// We keep it for backward compatibility.
depositIndex++
Comment on lines +86 to +91
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This can be removed as soon as fork activates

default:
// Nothing to do. We correctly set the deposit index to the last
// processed deposit index.
}

if err = st.SetEth1DepositIndex(depositIndex); err != nil {
return err
}

sp.logger.Info(
"Processed deposit to set Eth 1 deposit index",
"deposit_index", dep.GetIndex().Unwrap(),
)

return sp.applyDeposit(st, dep)
}

Expand All @@ -94,7 +122,16 @@ func (sp *StateProcessor[
}

// if validator exist, just update its balance
return st.IncreaseBalance(idx, dep.GetAmount())
if err = st.IncreaseBalance(idx, dep.GetAmount()); err != nil {
return err
}

sp.logger.Info(
"Processed deposit to increase balance",
"deposit_amount", float64(dep.GetAmount().Unwrap())/math.GweiPerWei,
"validator_index", idx,
)
return nil
}

// createValidator creates a validator if the deposit is valid.
Expand Down Expand Up @@ -286,5 +323,14 @@ func (sp *StateProcessor[
if err != nil {
return err
}
return st.IncreaseBalance(idx, depositAmount)
if err = st.IncreaseBalance(idx, depositAmount); err != nil {
return err
}
sp.logger.Info(
"Processed deposit to create new validator",
"deposit_amount", float64(depositAmount.Unwrap())/math.GweiPerWei,
"validator_index", idx,
"withdrawal_epoch", val.GetWithdrawableEpoch(),
)
return nil
}
Loading
Loading