From 9ea7f7caafcc39a7dc2051b457763e6cb1266f47 Mon Sep 17 00:00:00 2001 From: fudongbai <296179868@qq.com> Date: Tue, 10 Aug 2021 10:59:51 +0800 Subject: [PATCH] fix pending block null issue --- miner/miner.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/miner/miner.go b/miner/miner.go index 8c9d249f6c..efae1abd70 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -184,19 +184,21 @@ func (miner *Miner) SetRecommitInterval(interval time.Duration) { // Pending returns the currently pending block and associated state. func (miner *Miner) Pending() (*types.Block, *state.StateDB) { if miner.worker.isRunning() { - return miner.worker.pending() - } else { - // fallback to latest block - block := miner.worker.chain.CurrentBlock() - if block == nil { - return nil, nil + pendingBlock, pendingState := miner.worker.pending() + if pendingState != nil && pendingBlock != nil { + return pendingBlock, pendingState } - stateDb, err := miner.worker.chain.StateAt(block.Root()) - if err != nil { - return nil, nil - } - return block, stateDb } + // fallback to latest block + block := miner.worker.chain.CurrentBlock() + if block == nil { + return nil, nil + } + stateDb, err := miner.worker.chain.StateAt(block.Root()) + if err != nil { + return nil, nil + } + return block, stateDb } // PendingBlock returns the currently pending block. @@ -206,11 +208,13 @@ func (miner *Miner) Pending() (*types.Block, *state.StateDB) { // change between multiple method calls func (miner *Miner) PendingBlock() *types.Block { if miner.worker.isRunning() { - return miner.worker.pendingBlock() - } else { - // fallback to latest block - return miner.worker.chain.CurrentBlock() + pendingBlock := miner.worker.pendingBlock() + if pendingBlock != nil { + return pendingBlock + } } + // fallback to latest block + return miner.worker.chain.CurrentBlock() } func (miner *Miner) SetEtherbase(addr common.Address) {