From 7c56c0a597013384b5c1e5f60c3949af24f296da Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Thu, 9 Dec 2021 11:24:22 +0800 Subject: [PATCH] miner: recover state if parent is too old --- miner/miner.go | 4 +++- miner/worker.go | 14 ++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/miner/miner.go b/miner/miner.go index 399768999bbba..149765c58f315 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -35,10 +35,12 @@ import ( "github.com/ethereum/go-ethereum/params" ) -// Backend wraps all methods required for mining. +// Backend wraps all methods required for mining. Only full node is capable +// to offer all the functions here. type Backend interface { BlockChain() *core.BlockChain TxPool() *core.TxPool + StateAtBlock(block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (statedb *state.StateDB, err error) } // Config is the configuration parameters of mining. diff --git a/miner/worker.go b/miner/worker.go index 1786a631c758f..ff46d74e76d72 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -749,11 +749,17 @@ func (w *worker) resultLoop() { // makeEnv creates a new environment for the sealing block. func (w *worker) makeEnv(parent *types.Block, header *types.Header, coinbase common.Address) (*environment, error) { // Retrieve the parent state to execute on top and start a prefetcher for - // the miner to speed block sealing up a bit. Note since the sealing block - // can be created upon the arbitrary parent block, but the state of parent - // block may already be pruned, so the necessary state recovery is needed - // here in the future. TODO(rjl493456442). + // the miner to speed block sealing up a bit. state, err := w.chain.StateAt(parent.Root()) + if err != nil { + // Note since the sealing block can be created upon the arbitrary parent + // block, but the state of parent block may already be pruned, so the necessary + // state recovery is needed here in the future. + // + // The maximum acceptable reorg depth can be limited by the finalised block + // somehow. TODO(rjl493456442) fix the hard-coded number here later. + state, err = w.eth.StateAtBlock(parent, 1024, nil, false, false) + } if err != nil { return nil, err }