Skip to content

Commit

Permalink
feat: PEVM fallback to serial process
Browse files Browse the repository at this point in the history
fallback to serial processor when pevm execution result is not correct
  • Loading branch information
sunny2022da committed Nov 19, 2024
1 parent 9ca5adc commit 38a5423
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
log.Info("Parallel V2 enabled", "parallelNum", ParallelNum())
} else {
bc.processor = NewStateProcessor(chainConfig, bc, engine)
bc.serialProcessor = bc.processor
}
// Start future block processor.
bc.wg.Add(1)
Expand Down Expand Up @@ -1832,6 +1833,11 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
defer func() {
DebugInnerExecutionDuration = 0
}()

if bc.serialProcessor == nil {
bc.serialProcessor = bc.processor
}

for ; block != nil && err == nil || errors.Is(err, ErrKnownBlock); block, err = it.next() {
DebugInnerExecutionDuration = 0
// If the chain is terminating, stop processing blocks
Expand Down Expand Up @@ -1897,6 +1903,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
err error
)

blockProcessedInParallel := false
// skip block process if we already have the state, receipts and logs from mining work
if !(receiptExist && logExist && stateExist) {
// Retrieve the parent block and it's state to execute on top
Expand Down Expand Up @@ -1940,8 +1947,19 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
pstart = time.Now()
if bc.vmConfig.TxDAG == nil && bc.vmConfig.EnableParallelUnorderedMerge {
receipts, logs, usedGas, err = bc.serialProcessor.Process(block, statedb, bc.vmConfig)
blockProcessedInParallel = false
} else {
receipts, logs, usedGas, err = bc.processor.Process(block, statedb, bc.vmConfig)
blockProcessedInParallel = true
if err != nil {
// parallel processing fail , fallback to serial with new statDB.
log.Warn("ParallelEVM fallback!!!", "error", err.Error())
statedb, err = bc.reGenerateStateForFallBack(parent.Root, block.Root(), statedb)
statedb.StartPrefetcher("chain")
activeState = statedb
receipts, logs, usedGas, err = bc.serialProcessor.Process(block, statedb, bc.vmConfig)
blockProcessedInParallel = false
}
}
if err != nil {
bc.reportBlock(block, receipts, err)
Expand All @@ -1953,9 +1971,30 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)

vstart := time.Now()
if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil {
bc.reportBlock(block, receipts, err)
followupInterrupt.Store(true)
return it.index, err
if blockProcessedInParallel {
// invalid parallel execution, try serial
log.Warn("ParallelEVM fallback after ValidateState!!!", "error", err.Error())
parent := it.previous()
if parent == nil {
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
}

statedb, err = bc.reGenerateStateForFallBack(parent.Root, block.Root(), statedb)
statedb.StartPrefetcher("chain")
activeState = statedb

receipts, logs, usedGas, err = bc.serialProcessor.Process(block, statedb, bc.vmConfig)
blockProcessedInParallel = false
if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil {
bc.reportBlock(block, receipts, err)
followupInterrupt.Store(true)
return it.index, err
}
} else {
bc.reportBlock(block, receipts, err)
followupInterrupt.Store(true)
return it.index, err
}
}

vtime := time.Since(vstart)
Expand Down Expand Up @@ -2784,6 +2823,17 @@ func (bc *BlockChain) SetupTxDAGGeneration(output string, readFile bool) {

}

func (bc *BlockChain) reGenerateStateForFallBack(parentRoot common.Hash, blockRoot common.Hash, oldDB *state.StateDB) (*state.StateDB, error) {
oldDB.StopPrefetcher()
statedb, err := state.New(parentRoot, bc.stateCache, bc.snaps)
if err != nil {
return nil, err
}

statedb.SetExpectedStateRoot(blockRoot)
return statedb, nil
}

type TxDAGOutputItem struct {
blockNumber uint64
txDAG types.TxDAG
Expand Down

0 comments on commit 38a5423

Please sign in to comment.