Skip to content

Commit a75c6d9

Browse files
committed
miner: implement function updateSnapshot
1 parent 02d2646 commit a75c6d9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

miner/worker.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ type worker struct {
131131
snapshotMu sync.RWMutex // The lock used to protect the block snapshot and state snapshot
132132
snapshotBlock *types.Block
133133
snapshotReceipts types.Receipts
134+
snapshotState *state.StateDB
134135

135136
currentMu sync.Mutex
136137
current *Work
@@ -337,7 +338,15 @@ func (self *worker) update() {
337338
}
338339
feeCapacity := state.GetTRC21FeeCapacityFromState(self.current.state)
339340
txset, specialTxs := types.NewTransactionsByPriceAndNonce(self.current.signer, txs, nil, feeCapacity)
341+
342+
tcount := self.current.tcount
340343
self.current.commitTransactions(self.mux, feeCapacity, txset, specialTxs, self.chain, self.coinbase, &self.pendingLogsFeed)
344+
345+
// Only update the snapshot if any new transactions were added
346+
// to the pending block
347+
if tcount != self.current.tcount {
348+
self.updateSnapshot()
349+
}
341350
self.currentMu.Unlock()
342351
} else {
343352
// If we're mining, but nothing is being processed, wake on new transactions
@@ -477,6 +486,32 @@ func (self *worker) push(work *Work) {
477486
}
478487
}
479488

489+
// copyReceipts makes a deep copy of the given receipts.
490+
func copyReceipts(receipts []*types.Receipt) []*types.Receipt {
491+
result := make([]*types.Receipt, len(receipts))
492+
for i, l := range receipts {
493+
cpy := *l
494+
result[i] = &cpy
495+
}
496+
return result
497+
}
498+
499+
// updateSnapshot updates pending snapshot block and state.
500+
// Note this function assumes the current variable is thread safe.
501+
func (w *worker) updateSnapshot() {
502+
w.snapshotMu.Lock()
503+
defer w.snapshotMu.Unlock()
504+
505+
w.snapshotBlock = types.NewBlock(
506+
w.current.header,
507+
w.current.txs,
508+
nil,
509+
w.current.receipts,
510+
)
511+
w.snapshotReceipts = copyReceipts(w.current.receipts)
512+
w.snapshotState = w.current.state.Copy()
513+
}
514+
480515
// makeCurrent creates a new environment for the current cycle.
481516
func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error {
482517
// Retrieve the parent state to execute on top and start a prefetcher for
@@ -814,6 +849,7 @@ func (self *worker) commitNewWork() {
814849
self.lastParentBlockCommit = parent.Hash().Hex()
815850
}
816851
self.push(work)
852+
self.updateSnapshot()
817853
}
818854

819855
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) {

0 commit comments

Comments
 (0)