From 9c67d521acedca4f3870bd47fcc19eb102f42501 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 25 Jan 2022 16:19:58 +0100 Subject: [PATCH 1/4] reproduce the bug --- core/state_processor_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 93a41f2ecba9..c0c62d075f53 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -377,7 +377,7 @@ 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) { + chain, _ := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 2, 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) gen.AddTx(tx) tx, _ = types.SignTx(types.NewTransaction(1, common.Address{}, big.NewInt(999), params.TxGas, big.NewInt(875000000), nil), signer, testKey) From aeae18a2f9c255eae59e3729fabea872f9fb73f9 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 25 Jan 2022 16:44:31 +0100 Subject: [PATCH 2/4] fix the nil AccessWitness when Resetting --- core/vm/evm.go | 3 +++ 1 file changed, 3 insertions(+) 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 } From 754205a5a3ece6460cb7b3c8d32aabbd8649178e Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 25 Jan 2022 17:41:38 +0100 Subject: [PATCH 3/4] fix nonce management in blocks --- core/state_processor_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index c0c62d075f53..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, 2, 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) }) From 8506a632b68e2120fa09fa96b804d00f0ee5a1ea Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 25 Jan 2022 18:55:09 +0100 Subject: [PATCH 4/4] fix: make sure the snapshot is reused during the chain generation --- core/chain_makers.go | 5 ++++- core/state/statedb.go | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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 }