From 5e657a5a856773a2120dc2149db995dcc7505b3e Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 26 Jan 2022 10:07:51 +0100 Subject: [PATCH] fix: build more than one block in stateless test (#66) * reproduce the bug * fix the nil AccessWitness when Resetting * fix nonce management in blocks * fix: make sure the snapshot is reused during the chain generation --- core/chain_makers.go | 5 ++++- core/state/statedb.go | 4 ++++ core/state_processor_test.go | 8 ++++---- core/vm/evm.go | 3 +++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/chain_makers.go b/core/chain_makers.go index 6d691dbc3685..528627261119 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethdb" @@ -357,8 +358,9 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine } return nil, nil } + var snaps *snapshot.Tree for i := 0; i < n; i++ { - statedb, err := state.New(parent.Root(), state.NewDatabaseWithConfig(db, &trie.Config{UseVerkle: true}), nil) + statedb, err := state.New(parent.Root(), state.NewDatabaseWithConfig(db, &trie.Config{UseVerkle: true}), snaps) if err != nil { panic(err) } @@ -366,6 +368,7 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine blocks[i] = block receipts[i] = receipt parent = block + snaps = statedb.Snaps() } return blocks, receipts } diff --git a/core/state/statedb.go b/core/state/statedb.go index 0828ae95060a..3a63e7faceea 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -168,6 +168,10 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) return sdb, nil } +func (s *StateDB) Snaps() *snapshot.Tree { + return s.snaps +} + func (s *StateDB) Witness() *types.AccessWitness { return s.witness } diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 93a41f2ecba9..82ec894c74f7 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -377,12 +377,12 @@ func TestProcessStateless(t *testing.T) { genesis := gspec.MustCommit(db) blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil) defer blockchain.Stop() - chain, _ := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 1, func(_ int, gen *BlockGen) { - tx, _ := types.SignTx(types.NewTransaction(0, common.Address{1, 2, 3}, big.NewInt(999), params.TxGas, big.NewInt(875000000), nil), signer, testKey) + chain, _ := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) { + tx, _ := types.SignTx(types.NewTransaction(uint64(i)*3, common.Address{1, 2, 3}, big.NewInt(999), params.TxGas, big.NewInt(875000000), nil), signer, testKey) gen.AddTx(tx) - tx, _ = types.SignTx(types.NewTransaction(1, common.Address{}, big.NewInt(999), params.TxGas, big.NewInt(875000000), nil), signer, testKey) + tx, _ = types.SignTx(types.NewTransaction(uint64(i)*3+1, common.Address{}, big.NewInt(999), params.TxGas, big.NewInt(875000000), nil), signer, testKey) gen.AddTx(tx) - tx, _ = types.SignTx(types.NewTransaction(2, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil), signer, testKey) + tx, _ = types.SignTx(types.NewTransaction(uint64(i)*3+2, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil), signer, testKey) gen.AddTx(tx) }) diff --git a/core/vm/evm.go b/core/vm/evm.go index 178ff4b36a18..a976eca45871 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -148,6 +148,9 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig // Reset resets the EVM with a new transaction context.Reset // This is not threadsafe and should only be done very cautiously. func (evm *EVM) Reset(txCtx TxContext, statedb StateDB) { + if txCtx.Accesses == nil && evm.chainConfig.IsCancun(evm.Context.BlockNumber) { + txCtx.Accesses = types.NewAccessWitness() + } evm.TxContext = txCtx evm.StateDB = statedb }