Skip to content

Commit 1fd6684

Browse files
committed
fix for unit tests: do not call NewAccessWitness in NewEVMTxContext (#49)
* potential fix: do not call NewAccessWitness in NewEVMTxContext * more fixes: check for the existence of Accesses * fix absence of witness in copy * fix another witness issue * workaround: ensure the prefetcher is off in verkle mode * fix the remaining issues in tests * review feedback * fix witness allocation in stateless test
1 parent 510ba33 commit 1fd6684

File tree

6 files changed

+18
-16
lines changed

6 files changed

+18
-16
lines changed

consensus/ethash/consensus.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,8 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
671671
r.Div(blockReward, big32)
672672
reward.Add(reward, r)
673673
}
674-
coinbase := utils.GetTreeKeyBalance(header.Coinbase.Bytes())
675-
676-
if state.Witness() != nil {
674+
if config.IsCancun(header.Number) {
675+
coinbase := utils.GetTreeKeyBalance(header.Coinbase.Bytes())
677676
state.Witness().TouchAddress(coinbase, state.GetBalance(header.Coinbase).Bytes())
678677
}
679678
state.AddBalance(header.Coinbase, reward)

core/evm.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ func NewEVMTxContext(msg Message) vm.TxContext {
6969
return vm.TxContext{
7070
Origin: msg.From(),
7171
GasPrice: new(big.Int).Set(msg.GasPrice()),
72-
Accesses: types.NewAccessWitness(),
7372
}
7473
}
7574

core/state/statedb.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
148148
journal: newJournal(),
149149
accessList: newAccessList(),
150150
hasher: crypto.NewKeccakState(),
151-
witness: types.NewAccessWitness(),
152151
}
153-
if sdb.snaps == nil && tr.IsVerkle() {
154-
sdb.snaps, err = snapshot.New(db.TrieDB().DiskDB(), db.TrieDB(), 1, root, false, true, false, true)
155-
if err != nil {
156-
return nil, err
152+
if tr.IsVerkle() {
153+
sdb.witness = types.NewAccessWitness()
154+
if sdb.snaps == nil {
155+
sdb.snaps, err = snapshot.New(db.TrieDB().DiskDB(), db.TrieDB(), 1, root, false, true, false, true)
156+
if err != nil {
157+
return nil, err
158+
}
157159
}
158160
}
159161
if sdb.snaps != nil {
@@ -182,7 +184,7 @@ func (s *StateDB) StartPrefetcher(namespace string) {
182184
s.prefetcher.close()
183185
s.prefetcher = nil
184186
}
185-
if s.snap != nil {
187+
if s.snap != nil && !s.trie.IsVerkle() {
186188
s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace)
187189
}
188190
}
@@ -713,7 +715,9 @@ func (s *StateDB) Copy() *StateDB {
713715
preimages: make(map[common.Hash][]byte, len(s.preimages)),
714716
journal: newJournal(),
715717
hasher: crypto.NewKeccakState(),
716-
witness: s.witness.Copy(),
718+
}
719+
if s.witness != nil {
720+
state.witness = s.witness.Copy()
717721
}
718722
// Copy the dirty states, logs, and preimages
719723
for addr := range s.journal.dirties {

core/state_processor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
9595
func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) {
9696
// Create a new context to be used in the EVM environment.
9797
txContext := NewEVMTxContext(msg)
98+
if config.IsCancun(blockNumber) {
99+
txContext.Accesses = types.NewAccessWitness()
100+
}
98101
evm.Reset(txContext, statedb)
99102

100103
// Apply the transaction to the current state (included in the env).

core/vm/evm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type EVM struct {
130130
// NewEVM returns a new EVM. The returned EVM is not thread safe and should
131131
// only ever be used *once*.
132132
func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig *params.ChainConfig, config Config) *EVM {
133-
if txCtx.Accesses == nil {
133+
if txCtx.Accesses == nil && chainConfig.IsCancun(blockCtx.BlockNumber) {
134134
txCtx.Accesses = types.NewAccessWitness()
135135
}
136136
evm := &EVM{

miner/worker_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
267267
if _, err := chain.InsertChain([]*types.Block{block}); err != nil {
268268
t.Fatalf("failed to insert new mined block %d: %v", block.NumberU64(), err)
269269
}
270-
// TODO(gballet) the timeout had to be increased from 3s to 7s with verkle
271-
// trees, presumably because calculating an address is orders of magnitude
272-
// slower with perdersen_hash not using the multi-exponentiation.
273-
case <-time.After(5 * time.Second): // Worker needs 1s to include new changes.
270+
case <-time.After(3 * time.Second): // Worker needs 1s to include new changes.
274271
t.Fatalf("timeout")
275272
}
276273
}

0 commit comments

Comments
 (0)