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

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
28 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
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
5 changes: 5 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ packages:
recursive: False
with-expecter: true
all: True
github.com/berachain/beacon-kit/mod/state-transition/pkg/core:
config:
recursive: False
with-expecter: true
include-regex: ExecutionEngine
138 changes: 138 additions & 0 deletions mod/state-transition/pkg/core/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package core_test

import (
"context"
"fmt"

corestore "cosmossdk.io/core/store"
"cosmossdk.io/log"
"cosmossdk.io/store"
"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"
dbm "github.com/cosmos/cosmos-db"
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
}

func (kvs *testKVStoreService) OpenKVStore(context.Context) corestore.KVStore {
//nolint:contextcheck // fine with tests
return components.NewKVStore(
sdk.UnwrapSDKContext(kvs.ctx).KVStore(testStoreKey),
)
}

var (
testStoreKey = storetypes.NewKVStoreKey("state-transition-tests")
testCodec = &encoding.SSZInterfaceCodec[*types.ExecutionPayloadHeader]{}
)

func initTestStore() (
*beacondb.KVStore[
*types.BeaconBlockHeader,
*types.Eth1Data,
*types.ExecutionPayloadHeader,
*types.Fork,
*types.Validator,
types.Validators,
], error) {
db, err := db.OpenDB("", dbm.MemDBBackend)
if err != nil {
return nil, fmt.Errorf("failed opening mem db: %w", err)
}
var (
nopLog = log.NewNopLogger()
nopMetrics = metrics.NewNoOpMetrics()
)

cms := store.NewCommitMultiStore(
db,
nopLog,
nopMetrics,
)

ctx := sdk.NewContext(cms, true, nopLog)
cms.MountStoreWithDB(testStoreKey, storetypes.StoreTypeIAVL, nil)
if err = cms.LoadLatestVersion(); err != nil {
return nil, fmt.Errorf("failed to load latest version: %w", err)
}
testStoreService := &testKVStoreService{ctx: ctx}

return beacondb.New[
*types.BeaconBlockHeader,
*types.Eth1Data,
*types.ExecutionPayloadHeader,
*types.Fork,
*types.Validator,
types.Validators,
](
testStoreService,
testCodec,
), nil
}
86 changes: 86 additions & 0 deletions mod/state-transition/pkg/core/mocks/execution_engine.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 13 additions & 14 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,21 +243,24 @@ 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
} 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()),
))
}
withdrawal = withdrawal.New(
math.U64(withdrawalIndex),
validatorIndex,
withdrawalAddress,
amount,
)

withdrawals = append(withdrawals, withdrawal)

// Increment the withdrawal index to process the next withdrawal.
withdrawalIndex++
Expand Down
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
Loading
Loading