Skip to content

Commit

Permalink
miner: optimize function pending and pendingBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
JukLee0ira authored and gzliudan committed Aug 19, 2024
1 parent f8d6e06 commit 1b2d023
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,34 +194,23 @@ func (self *worker) setExtra(extra []byte) {
self.extra = extra
}

func (self *worker) pending() (*types.Block, *state.StateDB) {
self.currentMu.Lock()
defer self.currentMu.Unlock()

if atomic.LoadInt32(&self.mining) == 0 {
return types.NewBlock(
self.current.header,
self.current.txs,
nil,
self.current.receipts,
), self.current.state.Copy()
// pending returns the pending state and corresponding block. The returned
// values can be nil in case the pending block is not initialized.
func (w *worker) pending() (*types.Block, *state.StateDB) {
w.snapshotMu.RLock()
defer w.snapshotMu.RUnlock()
if w.snapshotState == nil {
return nil, nil
}
return self.current.Block, self.current.state.Copy()
return w.snapshotBlock, w.snapshotState.Copy()
}

func (self *worker) pendingBlock() *types.Block {
self.currentMu.Lock()
defer self.currentMu.Unlock()

if atomic.LoadInt32(&self.mining) == 0 {
return types.NewBlock(
self.current.header,
self.current.txs,
nil,
self.current.receipts,
)
}
return self.current.Block
// pendingBlock returns pending block. The returned block can be nil in case the
// pending block is not initialized.
func (w *worker) pendingBlock() *types.Block {
w.snapshotMu.RLock()
defer w.snapshotMu.RUnlock()
return w.snapshotBlock
}

// pendingBlockAndReceipts returns pending block and corresponding receipts.
Expand Down

0 comments on commit 1b2d023

Please sign in to comment.