miner: optimize function pending and pendingBlock

This commit is contained in:
JukLee0ira 2024-08-15 13:40:08 +08:00 committed by Daniel Liu
parent f8d6e064dc
commit 1b2d023da2

View file

@ -194,34 +194,23 @@ func (self *worker) setExtra(extra []byte) {
self.extra = extra self.extra = extra
} }
func (self *worker) pending() (*types.Block, *state.StateDB) { // pending returns the pending state and corresponding block. The returned
self.currentMu.Lock() // values can be nil in case the pending block is not initialized.
defer self.currentMu.Unlock() func (w *worker) pending() (*types.Block, *state.StateDB) {
w.snapshotMu.RLock()
if atomic.LoadInt32(&self.mining) == 0 { defer w.snapshotMu.RUnlock()
return types.NewBlock( if w.snapshotState == nil {
self.current.header, return nil, nil
self.current.txs,
nil,
self.current.receipts,
), self.current.state.Copy()
} }
return self.current.Block, self.current.state.Copy() return w.snapshotBlock, w.snapshotState.Copy()
} }
func (self *worker) pendingBlock() *types.Block { // pendingBlock returns pending block. The returned block can be nil in case the
self.currentMu.Lock() // pending block is not initialized.
defer self.currentMu.Unlock() func (w *worker) pendingBlock() *types.Block {
w.snapshotMu.RLock()
if atomic.LoadInt32(&self.mining) == 0 { defer w.snapshotMu.RUnlock()
return types.NewBlock( return w.snapshotBlock
self.current.header,
self.current.txs,
nil,
self.current.receipts,
)
}
return self.current.Block
} }
// pendingBlockAndReceipts returns pending block and corresponding receipts. // pendingBlockAndReceipts returns pending block and corresponding receipts.