Skip to content

Commit

Permalink
fix for unit tests: do not call NewAccessWitness in NewEVMTxContext (#49
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
gballet committed Jan 6, 2022
1 parent 510ba33 commit 1fd6684
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
5 changes: 2 additions & 3 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,8 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
r.Div(blockReward, big32)
reward.Add(reward, r)
}
coinbase := utils.GetTreeKeyBalance(header.Coinbase.Bytes())

if state.Witness() != nil {
if config.IsCancun(header.Number) {
coinbase := utils.GetTreeKeyBalance(header.Coinbase.Bytes())
state.Witness().TouchAddress(coinbase, state.GetBalance(header.Coinbase).Bytes())
}
state.AddBalance(header.Coinbase, reward)
Expand Down
1 change: 0 additions & 1 deletion core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func NewEVMTxContext(msg Message) vm.TxContext {
return vm.TxContext{
Origin: msg.From(),
GasPrice: new(big.Int).Set(msg.GasPrice()),
Accesses: types.NewAccessWitness(),
}
}

Expand Down
18 changes: 11 additions & 7 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
journal: newJournal(),
accessList: newAccessList(),
hasher: crypto.NewKeccakState(),
witness: types.NewAccessWitness(),
}
if sdb.snaps == nil && tr.IsVerkle() {
sdb.snaps, err = snapshot.New(db.TrieDB().DiskDB(), db.TrieDB(), 1, root, false, true, false, true)
if err != nil {
return nil, err
if tr.IsVerkle() {
sdb.witness = types.NewAccessWitness()
if sdb.snaps == nil {
sdb.snaps, err = snapshot.New(db.TrieDB().DiskDB(), db.TrieDB(), 1, root, false, true, false, true)
if err != nil {
return nil, err
}
}
}
if sdb.snaps != nil {
Expand Down Expand Up @@ -182,7 +184,7 @@ func (s *StateDB) StartPrefetcher(namespace string) {
s.prefetcher.close()
s.prefetcher = nil
}
if s.snap != nil {
if s.snap != nil && !s.trie.IsVerkle() {
s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace)
}
}
Expand Down Expand Up @@ -713,7 +715,9 @@ func (s *StateDB) Copy() *StateDB {
preimages: make(map[common.Hash][]byte, len(s.preimages)),
journal: newJournal(),
hasher: crypto.NewKeccakState(),
witness: s.witness.Copy(),
}
if s.witness != nil {
state.witness = s.witness.Copy()
}
// Copy the dirty states, logs, and preimages
for addr := range s.journal.dirties {
Expand Down
3 changes: 3 additions & 0 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
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) {
// Create a new context to be used in the EVM environment.
txContext := NewEVMTxContext(msg)
if config.IsCancun(blockNumber) {
txContext.Accesses = types.NewAccessWitness()
}
evm.Reset(txContext, statedb)

// Apply the transaction to the current state (included in the env).
Expand Down
2 changes: 1 addition & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type EVM struct {
// NewEVM returns a new EVM. The returned EVM is not thread safe and should
// only ever be used *once*.
func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig *params.ChainConfig, config Config) *EVM {
if txCtx.Accesses == nil {
if txCtx.Accesses == nil && chainConfig.IsCancun(blockCtx.BlockNumber) {
txCtx.Accesses = types.NewAccessWitness()
}
evm := &EVM{
Expand Down
5 changes: 1 addition & 4 deletions miner/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
if _, err := chain.InsertChain([]*types.Block{block}); err != nil {
t.Fatalf("failed to insert new mined block %d: %v", block.NumberU64(), err)
}
// TODO(gballet) the timeout had to be increased from 3s to 7s with verkle
// trees, presumably because calculating an address is orders of magnitude
// slower with perdersen_hash not using the multi-exponentiation.
case <-time.After(5 * time.Second): // Worker needs 1s to include new changes.
case <-time.After(3 * time.Second): // Worker needs 1s to include new changes.
t.Fatalf("timeout")
}
}
Expand Down

0 comments on commit 1fd6684

Please sign in to comment.