From 193aee0063582c48eea798f18f82b50cc509897b Mon Sep 17 00:00:00 2001 From: Jerry Date: Thu, 4 May 2023 11:36:55 -0700 Subject: [PATCH] Make block context sharable in parallel state processor --- core/evm.go | 6 ++++++ core/parallel_state_processor.go | 13 ++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/core/evm.go b/core/evm.go index bb3afc0006..0adc0ac27e 100644 --- a/core/evm.go +++ b/core/evm.go @@ -18,6 +18,7 @@ package core import ( "math/big" + "sync" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" @@ -83,7 +84,12 @@ func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash // Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...] var cache []common.Hash + cacheMutex := &sync.Mutex{} + return func(n uint64) common.Hash { + cacheMutex.Lock() + defer cacheMutex.Unlock() + // If there's no hash cache yet, make one if len(cache) == 0 { cache = append(cache, ref.ParentHash) diff --git a/core/parallel_state_processor.go b/core/parallel_state_processor.go index a5a71753c5..9eb56c766f 100644 --- a/core/parallel_state_processor.go +++ b/core/parallel_state_processor.go @@ -88,6 +88,7 @@ type ExecutionTask struct { // next k elements in dependencies -> transaction indexes on which transaction i is dependent on dependencies []int coinbase common.Address + blockContext vm.BlockContext } func (task *ExecutionTask) Execute(mvh *blockstm.MVHashMap, incarnation int) (err error) { @@ -96,9 +97,7 @@ func (task *ExecutionTask) Execute(mvh *blockstm.MVHashMap, incarnation int) (er task.statedb.SetMVHashmap(mvh) task.statedb.SetIncarnation(incarnation) - blockContext := NewEVMBlockContext(task.header, task.blockChain, nil) - - evm := vm.NewEVM(blockContext, vm.TxContext{}, task.statedb, task.config, task.evmConfig) + evm := vm.NewEVM(task.blockContext, vm.TxContext{}, task.statedb, task.config, task.evmConfig) // Create a new context to be used in the EVM environment. txContext := NewEVMTxContext(task.msg) @@ -125,8 +124,8 @@ func (task *ExecutionTask) Execute(mvh *blockstm.MVHashMap, incarnation int) (er reads := task.statedb.MVReadMap() - if _, ok := reads[blockstm.NewSubpathKey(blockContext.Coinbase, state.BalancePath)]; ok { - log.Info("Coinbase is in MVReadMap", "address", blockContext.Coinbase) + if _, ok := reads[blockstm.NewSubpathKey(task.blockContext.Coinbase, state.BalancePath)]; ok { + log.Info("Coinbase is in MVReadMap", "address", task.blockContext.Coinbase) task.shouldRerunWithoutFeeDelay = true } @@ -293,6 +292,8 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat metadata = true } + blockContext := NewEVMBlockContext(header, p.bc, nil) + // Iterate over and process the individual transactions for i, tx := range block.Transactions() { msg, err := tx.AsMessage(types.MakeSigner(p.config, header.Number), header.BaseFee) @@ -328,6 +329,7 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat allLogs: &allLogs, dependencies: deps[i], coinbase: coinbase, + blockContext: blockContext, } tasks = append(tasks, task) @@ -352,6 +354,7 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat allLogs: &allLogs, dependencies: nil, coinbase: coinbase, + blockContext: blockContext, } tasks = append(tasks, task)