Skip to content

Commit

Permalink
call updateSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
JukLee0ira committed Aug 14, 2024
1 parent cac2d79 commit 9551c98
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,39 +194,28 @@ 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.
// The returned values can be nil in case the pending block is not initialized.
func (w *worker) pendingBlockAndReceipts() (*types.Block, types.Receipts) {
// return a snapshot to avoid contention on currentMu mutex
w.snapshotMu.RLock()
defer w.snapshotMu.RUnlock()
return w.snapshotBlock, w.snapshotReceipts
Expand Down Expand Up @@ -338,7 +327,15 @@ func (self *worker) update() {
}
feeCapacity := state.GetTRC21FeeCapacityFromState(self.current.state)
txset, specialTxs := types.NewTransactionsByPriceAndNonce(self.current.signer, txs, nil, feeCapacity)

tcount := self.current.tcount
self.current.commitTransactions(self.mux, feeCapacity, txset, specialTxs, self.chain, self.coinbase, &self.pendingLogsFeed)

// Only update the snapshot if any new transactions were added
// to the pending block
if tcount != self.current.tcount {
self.updateSnapshot()
}
self.currentMu.Unlock()
} else {
// If we're mining, but nothing is being processed, wake on new transactions
Expand Down Expand Up @@ -855,6 +852,7 @@ func (self *worker) commitNewWork() {
self.lastParentBlockCommit = parent.Hash().Hex()
}
self.push(work)
self.updateSnapshot()
}

func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Address]*big.Int, txs *types.TransactionsByPriceAndNonce, specialTxs types.Transactions, bc *core.BlockChain, coinbase common.Address, pendingLogsFeed *event.Feed) {
Expand Down

0 comments on commit 9551c98

Please sign in to comment.