From f8d6e064dc18bc09e6025a44da215f2518369755 Mon Sep 17 00:00:00 2001 From: JukLee0ira Date: Thu, 15 Aug 2024 13:37:00 +0800 Subject: [PATCH] miner: implement function updateSnapshot --- miner/worker.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/miner/worker.go b/miner/worker.go index 64b195b30f..447f5a1b88 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -131,6 +131,7 @@ type worker struct { snapshotMu sync.RWMutex // The lock used to protect the block snapshot and state snapshot snapshotBlock *types.Block snapshotReceipts types.Receipts + snapshotState *state.StateDB currentMu sync.Mutex current *Work @@ -337,7 +338,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 @@ -477,6 +486,32 @@ func (self *worker) push(work *Work) { } } +// copyReceipts makes a deep copy of the given receipts. +func copyReceipts(receipts []*types.Receipt) []*types.Receipt { + result := make([]*types.Receipt, len(receipts)) + for i, l := range receipts { + cpy := *l + result[i] = &cpy + } + return result +} + +// updateSnapshot updates pending snapshot block and state. +// Note this function assumes the current variable is thread safe. +func (w *worker) updateSnapshot() { + w.snapshotMu.Lock() + defer w.snapshotMu.Unlock() + + w.snapshotBlock = types.NewBlock( + w.current.header, + w.current.txs, + nil, + w.current.receipts, + ) + w.snapshotReceipts = copyReceipts(w.current.receipts) + w.snapshotState = w.current.state.Copy() +} + // makeCurrent creates a new environment for the current cycle. func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error { // Retrieve the parent state to execute on top and start a prefetcher for @@ -814,6 +849,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) {