Skip to content

Commit 5e657a5

Browse files
authored
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
1 parent 15b353d commit 5e657a5

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

core/chain_makers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ethereum/go-ethereum/consensus"
2525
"github.com/ethereum/go-ethereum/consensus/misc"
2626
"github.com/ethereum/go-ethereum/core/state"
27+
"github.com/ethereum/go-ethereum/core/state/snapshot"
2728
"github.com/ethereum/go-ethereum/core/types"
2829
"github.com/ethereum/go-ethereum/core/vm"
2930
"github.com/ethereum/go-ethereum/ethdb"
@@ -357,15 +358,17 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
357358
}
358359
return nil, nil
359360
}
361+
var snaps *snapshot.Tree
360362
for i := 0; i < n; i++ {
361-
statedb, err := state.New(parent.Root(), state.NewDatabaseWithConfig(db, &trie.Config{UseVerkle: true}), nil)
363+
statedb, err := state.New(parent.Root(), state.NewDatabaseWithConfig(db, &trie.Config{UseVerkle: true}), snaps)
362364
if err != nil {
363365
panic(err)
364366
}
365367
block, receipt := genblock(i, parent, statedb)
366368
blocks[i] = block
367369
receipts[i] = receipt
368370
parent = block
371+
snaps = statedb.Snaps()
369372
}
370373
return blocks, receipts
371374
}

core/state/statedb.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
168168
return sdb, nil
169169
}
170170

171+
func (s *StateDB) Snaps() *snapshot.Tree {
172+
return s.snaps
173+
}
174+
171175
func (s *StateDB) Witness() *types.AccessWitness {
172176
return s.witness
173177
}

core/state_processor_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,12 @@ func TestProcessStateless(t *testing.T) {
377377
genesis := gspec.MustCommit(db)
378378
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
379379
defer blockchain.Stop()
380-
chain, _ := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 1, func(_ int, gen *BlockGen) {
381-
tx, _ := types.SignTx(types.NewTransaction(0, common.Address{1, 2, 3}, big.NewInt(999), params.TxGas, big.NewInt(875000000), nil), signer, testKey)
380+
chain, _ := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
381+
tx, _ := types.SignTx(types.NewTransaction(uint64(i)*3, common.Address{1, 2, 3}, big.NewInt(999), params.TxGas, big.NewInt(875000000), nil), signer, testKey)
382382
gen.AddTx(tx)
383-
tx, _ = types.SignTx(types.NewTransaction(1, common.Address{}, big.NewInt(999), params.TxGas, big.NewInt(875000000), nil), signer, testKey)
383+
tx, _ = types.SignTx(types.NewTransaction(uint64(i)*3+1, common.Address{}, big.NewInt(999), params.TxGas, big.NewInt(875000000), nil), signer, testKey)
384384
gen.AddTx(tx)
385-
tx, _ = types.SignTx(types.NewTransaction(2, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil), signer, testKey)
385+
tx, _ = types.SignTx(types.NewTransaction(uint64(i)*3+2, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil), signer, testKey)
386386
gen.AddTx(tx)
387387

388388
})

core/vm/evm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig
148148
// Reset resets the EVM with a new transaction context.Reset
149149
// This is not threadsafe and should only be done very cautiously.
150150
func (evm *EVM) Reset(txCtx TxContext, statedb StateDB) {
151+
if txCtx.Accesses == nil && evm.chainConfig.IsCancun(evm.Context.BlockNumber) {
152+
txCtx.Accesses = types.NewAccessWitness()
153+
}
151154
evm.TxContext = txCtx
152155
evm.StateDB = statedb
153156
}

0 commit comments

Comments
 (0)