1717package eth
1818
1919import (
20+ "context"
2021 "errors"
2122 "fmt"
2223 "math/big"
@@ -57,7 +58,7 @@ var noopReleaser = tracers.StateReleaseFunc(func() {})
5758// - preferDisk: this arg can be used by the caller to signal that even though the 'base' is
5859// provided, it would be preferable to start from a fresh state, if we have it
5960// on disk.
60- func (eth * Ethereum ) StateAtBlock (block * types.Block , reexec uint64 , base * state.StateDB , readOnly bool , preferDisk bool ) (statedb * state.StateDB , release tracers.StateReleaseFunc , err error ) {
61+ func (eth * Ethereum ) StateAtBlock (ctx context. Context , block * types.Block , reexec uint64 , base * state.StateDB , readOnly bool , preferDisk bool ) (statedb * state.StateDB , release tracers.StateReleaseFunc , err error ) {
6162 var (
6263 current * types.Block
6364 database state.Database
@@ -112,6 +113,9 @@ func (eth *Ethereum) StateAtBlock(block *types.Block, reexec uint64, base *state
112113 }
113114 // Database does not have the state for the given block, try to regenerate
114115 for i := uint64 (0 ); i < reexec ; i ++ {
116+ if err := ctx .Err (); err != nil {
117+ return nil , nil , err
118+ }
115119 if current .NumberU64 () == 0 {
116120 return nil , nil , errors .New ("genesis state is missing" )
117121 }
@@ -143,6 +147,9 @@ func (eth *Ethereum) StateAtBlock(block *types.Block, reexec uint64, base *state
143147 parent common.Hash
144148 )
145149 for current .NumberU64 () < origin {
150+ if err := ctx .Err (); err != nil {
151+ return nil , nil , err
152+ }
146153 // Print progress logs if long enough time elapsed
147154 if time .Since (logged ) > 8 * time .Second && report {
148155 log .Info ("Regenerating historical state" , "block" , current .NumberU64 ()+ 1 , "target" , origin , "remaining" , origin - current .NumberU64 ()- 1 , "elapsed" , time .Since (start ))
@@ -183,7 +190,7 @@ func (eth *Ethereum) StateAtBlock(block *types.Block, reexec uint64, base *state
183190}
184191
185192// stateAtTransaction returns the execution environment of a certain transaction.
186- func (eth * Ethereum ) stateAtTransaction (block * types.Block , txIndex int , reexec uint64 ) (core.Message , vm.BlockContext , * state.StateDB , tracers.StateReleaseFunc , error ) {
193+ func (eth * Ethereum ) stateAtTransaction (ctx context. Context , block * types.Block , txIndex int , reexec uint64 ) (core.Message , vm.BlockContext , * state.StateDB , tracers.StateReleaseFunc , error ) {
187194 // Short circuit if it's genesis block.
188195 if block .NumberU64 () == 0 {
189196 return nil , vm.BlockContext {}, nil , nil , errors .New ("no transaction in genesis" )
@@ -195,7 +202,7 @@ func (eth *Ethereum) stateAtTransaction(block *types.Block, txIndex int, reexec
195202 }
196203 // Lookup the statedb of parent block from the live database,
197204 // otherwise regenerate it on the flight.
198- statedb , release , err := eth .StateAtBlock (parent , reexec , nil , true , false )
205+ statedb , release , err := eth .StateAtBlock (ctx , parent , reexec , nil , true , false )
199206 if err != nil {
200207 return nil , vm.BlockContext {}, nil , nil , err
201208 }
0 commit comments