@@ -2451,3 +2451,60 @@ func (bc *BlockChain) SetTrieFlushInterval(interval time.Duration) {
24512451func (bc * BlockChain ) GetTrieFlushInterval () time.Duration {
24522452 return time .Duration (bc .flushInterval .Load ())
24532453}
2454+
2455+ // ProcessState processes the state changes according to the Ethereum rules by running
2456+ // the transaction messages using the statedb and applying any rewards to both
2457+ // the processor (coinbase) and any included uncles. It doesn't persist any data.
2458+ func (bc * BlockChain ) ProcessState (block * types.Block ) (* state.StateDB , types.Receipts , []* types.Log , uint64 , error ) {
2459+ parent := bc .GetBlockByHash (block .ParentHash ())
2460+ if parent == nil {
2461+ err := fmt .Errorf ("failed to retrieve parent by hash to process block, %s: %v, %s: %s" , "parent number" , block .NumberU64 ()- 1 ,
2462+ "parent hash" , block .ParentHash ().String ())
2463+ log .Error (err .Error ())
2464+ parent = bc .GetBlockByNumber (block .NumberU64 () - 1 )
2465+ if parent == nil {
2466+ err = fmt .Errorf ("failed to retrieve canonical parent by number to process block, %s: %v, %s: %s" , "parent number" , block .NumberU64 ()- 1 ,
2467+ "parent hash" , block .ParentHash ().String ())
2468+ log .Error (err .Error ())
2469+ return nil , nil , nil , 0 , err
2470+ }
2471+ }
2472+ statedb , err := bc .StateAt (parent .Root ())
2473+ if err != nil {
2474+ err = fmt .Errorf ("failed to retrieve state at %s: %w" , parent .Root (), err )
2475+ log .Error (err .Error ())
2476+ return nil , nil , nil , 0 , err
2477+ }
2478+
2479+ receipts , logs , usedGas , err := bc .processor .Process (block , statedb , bc .vmConfig )
2480+ if err != nil {
2481+ err = fmt .Errorf ("failed to process block: %w" , err )
2482+ log .Error (err .Error ())
2483+ return nil , nil , nil , 0 , err
2484+ }
2485+ return statedb , receipts , logs , usedGas , nil
2486+ }
2487+
2488+ // VerifyBlock checks block state
2489+ func (bc * BlockChain ) VerifyBlock (block * types.Block ) (* state.StateDB , types.Receipts , error ) {
2490+ err := bc .validator .ValidateBody (block )
2491+ if err != nil {
2492+ err = fmt .Errorf ("failed to validate body: %w" , err )
2493+ log .Error (err .Error ())
2494+ return nil , nil , err
2495+ }
2496+
2497+ statedb , receipts , _ , usedGas , err := bc .ProcessState (block )
2498+ if err != nil {
2499+ err = fmt .Errorf ("failed to process block state: %w" , err )
2500+ log .Error (err .Error ())
2501+ return nil , nil , err
2502+ }
2503+
2504+ if err := bc .validator .ValidateState (block , statedb , receipts , usedGas ); err != nil {
2505+ err = fmt .Errorf ("failed to verify state: %w" , err )
2506+ log .Error (err .Error ())
2507+ return nil , nil , err
2508+ }
2509+ return statedb , receipts , nil
2510+ }
0 commit comments