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

perf(state-transition): reduce amount of withdrawals processed #2110

Merged
merged 31 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b4a1077
skip zero amount withdrawals to reduce number of withdrawals processed
abi87 Oct 28, 2024
f4a7d79
wip: adding UTs to state transition package
abi87 Oct 29, 2024
0f46172
wip: completed simple UT for state transition package
abi87 Oct 29, 2024
22c7717
wip: minimal execution engine stub
abi87 Oct 29, 2024
05cba80
extended asserts
abi87 Oct 29, 2024
443ac1b
added test case
abi87 Oct 29, 2024
10efd5e
nits
abi87 Oct 29, 2024
099716d
tests for helpers in state transition using mock
nidhi-singh02 Oct 30, 2024
151a533
Revert "tests for helpers in state transition using mock"
nidhi-singh02 Oct 30, 2024
9818c7e
tests with only mock for execution engine
nidhi-singh02 Oct 30, 2024
160cc88
removed test for VerifyAndNotifyNewPayload
nidhi-singh02 Oct 30, 2024
4a9fe1c
nit
abi87 Oct 31, 2024
e048be4
improved unit tests asserts
abi87 Oct 31, 2024
8bf34db
appease linter
abi87 Oct 31, 2024
d90a95a
fix(state-transition): fix deposit index upon genesis processing (#2116)
abi87 Oct 31, 2024
e17d29c
fixed bad merge
abi87 Oct 31, 2024
8dfe59d
Merge branch 'main' into fix-maximum-number-withdrawals
abi87 Nov 1, 2024
daa9262
Merge branch 'state-transition-add-UTs' into fix-maximum-number-withd…
abi87 Nov 1, 2024
92d494f
added UTs to show no withdrawals are made when unnecessary
abi87 Nov 1, 2024
6286b20
Merge branch 'main' into state-transition-add-UTs
abi87 Nov 1, 2024
a1a3def
Merge branch 'state-transition-add-UTs' into fix-maximum-number-withd…
abi87 Nov 1, 2024
97fba5c
fix withdrawal index update
abi87 Nov 1, 2024
af8c5e0
fix(build): erigon repo
gummybera Nov 1, 2024
023ebfd
fix(build): bump erigon to recent version
gummybera Nov 1, 2024
d66b298
nits from code review
abi87 Nov 1, 2024
420f621
Merge branch 'state-transition-add-UTs' into fix-maximum-number-withd…
abi87 Nov 1, 2024
09eee8e
Merge branch 'fix-erigon' into fix-maximum-number-withdrawals
abi87 Nov 2, 2024
d8a267e
hacked fix for withdrawals SSZ serialization
abi87 Nov 4, 2024
6dde55a
Merge branch 'main' into fix-maximum-number-withdrawals
abi87 Nov 6, 2024
e2d5cc2
fixed faulty merge
abi87 Nov 6, 2024
3cfb23e
improved comment
abi87 Nov 6, 2024
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
10 changes: 10 additions & 0 deletions mod/consensus-types/pkg/types/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ func (p *ExecutionPayload) DefineSSZ(codec *ssz.Codec) {
constants.MaxBytesPerTx,
)
ssz.DefineSliceOfStaticObjectsContent(codec, &p.Withdrawals, 16)

// Post Shangai an EL explicitly check that Withdrawals are not nil
// (instead empty slices are fine). Currently BeaconKit duly builds
// a block with Withdrawals set to empty slice if there are no
// withdrawals) but as soon as the block is returned by CometBFT
// for verification, the SSZ decoding sets the empty slice to nil.
// This code change solves the issue.
if p.Withdrawals == nil {
p.Withdrawals = make([]*engineprimitives.Withdrawal, 0)
}
}

// MarshalSSZ serializes the ExecutionPayload object into a slice of bytes.
Expand Down
36 changes: 19 additions & 17 deletions mod/state-transition/pkg/core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,6 @@ func (s *StateDB[

// Iterate through indices to find the next validators to withdraw.
for range bound {
var (
withdrawal WithdrawalT
amount math.Gwei
)
validator, err = s.ValidatorByIndex(validatorIndex)
if err != nil {
return nil, err
Expand All @@ -247,24 +243,30 @@ func (s *StateDB[

// Set the amount of the withdrawal depending on the balance of the
// validator.
var withdrawal WithdrawalT
if validator.IsFullyWithdrawable(balance, epoch) {
amount = balance
withdrawals = append(withdrawals, withdrawal.New(
math.U64(withdrawalIndex),
validatorIndex,
withdrawalAddress,
balance,
))
abi87 marked this conversation as resolved.
Show resolved Hide resolved

// Increment the withdrawal index to process the next withdrawal.
withdrawalIndex++
} else if validator.IsPartiallyWithdrawable(
balance, math.Gwei(s.cs.MaxEffectiveBalance()),
) {
amount = balance - math.Gwei(s.cs.MaxEffectiveBalance())
withdrawals = append(withdrawals, withdrawal.New(
math.U64(withdrawalIndex),
validatorIndex,
withdrawalAddress,
balance-math.Gwei(s.cs.MaxEffectiveBalance()),
))

// Increment the withdrawal index to process the next withdrawal.
withdrawalIndex++
}
withdrawal = withdrawal.New(
math.U64(withdrawalIndex),
validatorIndex,
withdrawalAddress,
amount,
)

withdrawals = append(withdrawals, withdrawal)

// Increment the withdrawal index to process the next withdrawal.
withdrawalIndex++

// Cap the number of withdrawals to the maximum allowed per payload.
//#nosec:G701 // won't overflow in practice.
Expand Down
6 changes: 4 additions & 2 deletions mod/state-transition/pkg/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package core
import (
"fmt"

"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
"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 @@ -131,11 +132,12 @@ func (sp *StateProcessor[
}

// TODO: Modify balance here and then effective balance once per epoch.
newBalance := min(
updatedBalance := types.ComputeEffectiveBalance(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixing faulty merge conflict from previous merge

val.GetEffectiveBalance()+dep.GetAmount(),
math.Gwei(sp.cs.EffectiveBalanceIncrement()),
math.Gwei(sp.cs.MaxEffectiveBalance()),
)
val.SetEffectiveBalance(newBalance)
val.SetEffectiveBalance(updatedBalance)
if err = st.UpdateValidatorAtIndex(idx, val); err != nil {
return err
}
Expand Down
21 changes: 4 additions & 17 deletions mod/state-transition/pkg/core/state_processor_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,10 @@ func TestTransitionUpdateValidators(t *testing.T) {
StateRoot: common.Root{},
Body: &types.BeaconBlockBody{
ExecutionPayload: &types.ExecutionPayload{
Timestamp: 10,
ExtraData: []byte("testing"),
Transactions: [][]byte{},
Withdrawals: []*engineprimitives.Withdrawal{
{ // fill empty withdrawals
Index: 0,
Validator: 0,
Address: emptyAddress,
Amount: 0,
},
{
Index: 1,
Validator: 1,
Address: emptyAddress,
Amount: 0,
},
},
Timestamp: 10,
ExtraData: []byte("testing"),
Transactions: [][]byte{},
Withdrawals: []*engineprimitives.Withdrawal{}, // no withdrawals
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 is the point of the test here, which is different from the base branch here: the list of withdrawals is empty instead of containing two withdrawals with zero amount.

BaseFeePerGas: math.NewU256(0),
},
Eth1Data: &types.Eth1Data{},
Expand Down
Loading