From e1421573dffdd9442b29fccf5dd8b50cd0d38967 Mon Sep 17 00:00:00 2001 From: flywukong <2229306838@qq.com> Date: Mon, 28 Mar 2022 21:10:24 +0800 Subject: [PATCH 1/2] add sharedStorage to the prefetcher of miner --- core/blockchain.go | 5 +++++ core/state/state_object.go | 4 ++-- miner/worker.go | 6 ++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 47ac859441..eeaf1b7e0a 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -842,6 +842,11 @@ func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) { return state.New(root, bc.stateCache, bc.snaps) } +// StateAtWithSharedPool returns a new mutable state based on a particular point in time with sharedStorage +func (bc *BlockChain) StateAtWithSharedPool(root common.Hash) (*state.StateDB, error) { + return state.NewWithSharedPool(root, bc.stateCache, bc.snaps) +} + // StateCache returns the caching database underpinning the blockchain instance. func (bc *BlockChain) StateCache() state.Database { return bc.stateCache diff --git a/core/state/state_object.go b/core/state/state_object.go index a89feebf28..b40a8a2f85 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -81,8 +81,8 @@ type StateObject struct { trie Trie // storage trie, which becomes non-nil on first access code Code // contract bytecode, which gets set when code is loaded - sharedOriginStorage *sync.Map // Storage cache of original entries to dedup rewrites, reset for every transaction - originStorage Storage + sharedOriginStorage *sync.Map // Point to the entry of the stateObject in sharedPool + originStorage Storage // Storage cache of original entries to dedup rewrites, reset for every transaction pendingStorage Storage // Storage entries that need to be flushed to disk, at the end of an entire block dirtyStorage Storage // Storage entries that have been modified in the current transaction execution diff --git a/miner/worker.go b/miner/worker.go index 2224dc0071..b013aeff7a 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -661,7 +661,7 @@ func (w *worker) resultLoop() { func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error { // Retrieve the parent state to execute on top and start a prefetcher for // the miner to speed block sealing up a bit - state, err := w.chain.StateAt(parent.Root()) + state, err := w.chain.StateAtWithSharedPool(parent.Root()) if err != nil { return err } @@ -786,7 +786,9 @@ func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coin txCurr := &tx //prefetch txs from all pending txs txsPrefetch := txs.Copy() - w.prefetcher.PrefetchMining(txsPrefetch, w.current.header, w.current.gasPool.Gas(), w.current.state.Copy(), *w.chain.GetVMConfig(), interruptCh, txCurr) + newStatedb := w.current.state.Copy() + newStatedb.EnableWriteOnSharedStorage() + w.prefetcher.PrefetchMining(txsPrefetch, w.current.header, w.current.gasPool.Gas(), newStatedb, *w.chain.GetVMConfig(), interruptCh, txCurr) LOOP: for { From 9789d31b438df3941e45b67cf4e43e89888ec286 Mon Sep 17 00:00:00 2001 From: flywukong <2229306838@qq.com> Date: Tue, 29 Mar 2022 10:42:34 +0800 Subject: [PATCH 2/2] fix miner prefetcher copy --- core/state_prefetcher.go | 1 + miner/worker.go | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/core/state_prefetcher.go b/core/state_prefetcher.go index e45a6306df..4909cc3081 100644 --- a/core/state_prefetcher.go +++ b/core/state_prefetcher.go @@ -101,6 +101,7 @@ func (p *statePrefetcher) PrefetchMining(txs *types.TransactionsByPriceAndNonce, go func(startCh <-chan *types.Transaction, stopCh <-chan struct{}) { idx := 0 newStatedb := statedb.Copy() + newStatedb.EnableWriteOnSharedStorage() gaspool := new(GasPool).AddGas(gasLimit) blockContext := NewEVMBlockContext(header, p.bc, nil) evm := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg) diff --git a/miner/worker.go b/miner/worker.go index b013aeff7a..34b3c2fc89 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -786,9 +786,7 @@ func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coin txCurr := &tx //prefetch txs from all pending txs txsPrefetch := txs.Copy() - newStatedb := w.current.state.Copy() - newStatedb.EnableWriteOnSharedStorage() - w.prefetcher.PrefetchMining(txsPrefetch, w.current.header, w.current.gasPool.Gas(), newStatedb, *w.chain.GetVMConfig(), interruptCh, txCurr) + w.prefetcher.PrefetchMining(txsPrefetch, w.current.header, w.current.gasPool.Gas(), w.current.state.Copy(), *w.chain.GetVMConfig(), interruptCh, txCurr) LOOP: for {