From a067399c4e703fc86ce0ac85268a538e86d499c5 Mon Sep 17 00:00:00 2001 From: NguyenNguyen Date: Fri, 18 Jan 2019 17:53:44 +0700 Subject: [PATCH 1/2] Won't grasp txs at checkpoint --- consensus/posv/posv.go | 3 ++- miner/worker.go | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/consensus/posv/posv.go b/consensus/posv/posv.go index 254a568176..51cab6ea5e 100644 --- a/consensus/posv/posv.go +++ b/consensus/posv/posv.go @@ -900,7 +900,8 @@ func (c *Posv) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan return nil, errUnknownBlock } // For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing) - if c.config.Period == 0 && len(block.Transactions()) == 0 { + // checkpoint blocks have no tx + if c.config.Period == 0 && len(block.Transactions()) == 0 && number % c.config.Epoch != 0 { return nil, errWaitTransactions } // Don't hold the signer fields for the entire sealing procedure diff --git a/miner/worker.go b/miner/worker.go index 25af63faa5..fc915ff617 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -583,13 +583,18 @@ func (self *worker) commitNewWork() { if self.config.DAOForkSupport && self.config.DAOForkBlock != nil && self.config.DAOForkBlock.Cmp(header.Number) == 0 { misc.ApplyDAOHardFork(work.state) } - pending, err := self.eth.TxPool().Pending() - if err != nil { - log.Error("Failed to fetch pending transactions", "err", err) - return + specialTxCount := 0 + // won't grasp txs at checkpoint + if self.config.Posv != nil && header.Number.Uint64() % self.config.Posv.Epoch != 0 { + pending, err := self.eth.TxPool().Pending() + if err != nil { + log.Error("Failed to fetch pending transactions", "err", err) + return + } + txs, specialTxs := types.NewTransactionsByPriceAndNonce(self.current.signer, pending, signers) + specialTxCount = len(specialTxs) + work.commitTransactions(self.mux, txs, specialTxs, self.chain, self.coinbase) } - txs, specialTxs := types.NewTransactionsByPriceAndNonce(self.current.signer, pending, signers) - work.commitTransactions(self.mux, txs, specialTxs, self.chain, self.coinbase) // compute uncles for the new block. var ( @@ -621,7 +626,7 @@ func (self *worker) commitNewWork() { return } if atomic.LoadInt32(&self.mining) == 1 { - log.Info("Committing new block", "number", work.Block.Number(), "txs", work.tcount, "special txs", len(specialTxs), "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart))) + log.Info("Committing new block", "number", work.Block.Number(), "txs", work.tcount, "special-txs", specialTxCount, "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart))) self.unconfirmed.Shift(work.Block.NumberU64() - 1) self.lastParentBlockCommit = parent.Hash().Hex() } From f12023908a39918711be08c0ba77cf2b5ddfd2a2 Mon Sep 17 00:00:00 2001 From: NguyenNguyen Date: Mon, 21 Jan 2019 17:06:09 +0700 Subject: [PATCH 2/2] refactoring code --- miner/worker.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/miner/worker.go b/miner/worker.go index fc915ff617..d567e7bb41 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -583,18 +583,21 @@ func (self *worker) commitNewWork() { if self.config.DAOForkSupport && self.config.DAOForkBlock != nil && self.config.DAOForkBlock.Cmp(header.Number) == 0 { misc.ApplyDAOHardFork(work.state) } - specialTxCount := 0 // won't grasp txs at checkpoint + var ( + txs *types.TransactionsByPriceAndNonce + specialTxs types.Transactions + ) if self.config.Posv != nil && header.Number.Uint64() % self.config.Posv.Epoch != 0 { pending, err := self.eth.TxPool().Pending() if err != nil { log.Error("Failed to fetch pending transactions", "err", err) return } - txs, specialTxs := types.NewTransactionsByPriceAndNonce(self.current.signer, pending, signers) - specialTxCount = len(specialTxs) - work.commitTransactions(self.mux, txs, specialTxs, self.chain, self.coinbase) + txs, specialTxs = types.NewTransactionsByPriceAndNonce(self.current.signer, pending, signers) } + work.commitTransactions(self.mux, txs, specialTxs, self.chain, self.coinbase) + // compute uncles for the new block. var ( @@ -626,7 +629,7 @@ func (self *worker) commitNewWork() { return } if atomic.LoadInt32(&self.mining) == 1 { - log.Info("Committing new block", "number", work.Block.Number(), "txs", work.tcount, "special-txs", specialTxCount, "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart))) + log.Info("Committing new block", "number", work.Block.Number(), "txs", work.tcount, "special-txs", len(specialTxs), "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart))) self.unconfirmed.Shift(work.Block.NumberU64() - 1) self.lastParentBlockCommit = parent.Hash().Hex() } @@ -702,6 +705,10 @@ func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsB log.Trace("Not enough gas for further transactions", "gp", gp) break } + if txs == nil { + log.Info("this block has no transaction") + break + } // Retrieve the next transaction and abort if all done tx := txs.Peek() if tx == nil {