From 3fe54e28d3da96bcbad3b0289cbf23ee89f4d25c Mon Sep 17 00:00:00 2001 From: Wanwiset Peerapatanapokin Date: Tue, 23 Apr 2024 09:44:49 +0400 Subject: [PATCH] remove uncle block handling (#523) --- consensus/XDPoS/XDPoS.go | 2 +- consensus/XDPoS/engines/engine_v2/engine.go | 10 ++++ miner/worker.go | 56 ++------------------- 3 files changed, 14 insertions(+), 54 deletions(-) diff --git a/consensus/XDPoS/XDPoS.go b/consensus/XDPoS/XDPoS.go index c97a709d1de1..6d5477a1ee61 100644 --- a/consensus/XDPoS/XDPoS.go +++ b/consensus/XDPoS/XDPoS.go @@ -240,7 +240,7 @@ func (x *XDPoS) VerifyHeaders(chain consensus.ChainReader, headers []*types.Head func (x *XDPoS) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { switch x.config.BlockConsensusVersion(block.Number(), block.Extra(), ExtraFieldCheck) { case params.ConsensusEngineVersion2: - return nil + return x.EngineV2.VerifyUncles(chain, block) default: // Default "v1" return x.EngineV1.VerifyUncles(chain, block) } diff --git a/consensus/XDPoS/engines/engine_v2/engine.go b/consensus/XDPoS/engines/engine_v2/engine.go index 693f9fca8119..9cc25d7fab76 100644 --- a/consensus/XDPoS/engines/engine_v2/engine.go +++ b/consensus/XDPoS/engines/engine_v2/engine.go @@ -2,6 +2,7 @@ package engine_v2 import ( "encoding/json" + "errors" "fmt" "math/big" "os" @@ -515,6 +516,15 @@ func (x *XDPoS_v2) UpdateMasternodes(chain consensus.ChainReader, header *types. return nil } +// VerifyUncles implements consensus.Engine, always returning an error for any +// uncles as this consensus mechanism doesn't permit uncles. +func (x *XDPoS_v2) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { + if len(block.Uncles()) > 0 { + return errors.New("uncles not allowed in XDPoS_v2") + } + return nil +} + func (x *XDPoS_v2) VerifyHeader(chain consensus.ChainReader, header *types.Header, fullVerify bool) error { err := x.verifyHeader(chain, header, nil, fullVerify) if err != nil { diff --git a/miner/worker.go b/miner/worker.go index 688d7556d85a..823cd742580b 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -19,7 +19,6 @@ package miner import ( "bytes" "encoding/binary" - "fmt" "github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate" "github.com/XinFinOrg/XDPoSChain/accounts" @@ -307,12 +306,7 @@ func (self *worker) update() { timeout.Reset(time.Duration(minePeriod) * time.Second) // Handle ChainSideEvent - case ev := <-self.chainSideCh: - if self.config.XDPoS == nil { - self.uncleMu.Lock() - self.possibleUncles[ev.Block.Hash()] = ev.Block - self.uncleMu.Unlock() - } + case <-self.chainSideCh: // Handle NewTxsEvent case ev := <-self.txsCh: @@ -508,17 +502,6 @@ func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error createdAt: time.Now(), } - if self.config.XDPoS == nil { - // when 08 is processed ancestors contain 07 (quick block) - for _, ancestor := range self.chain.GetBlocksFromHash(parent.Hash(), 7) { - for _, uncle := range ancestor.Uncles() { - work.family.Add(uncle.Hash()) - } - work.family.Add(ancestor.Hash()) - work.ancestors.Add(ancestor.Hash()) - } - } - // Keep track of transactions which return errors so they can be removed work.tcount = 0 self.current = work @@ -800,33 +783,15 @@ func (self *worker) commitNewWork() { work.commitTransactions(self.mux, feeCapacity, txs, specialTxs, self.chain, self.coinbase) // compute uncles for the new block. var ( - uncles []*types.Header - badUncles []common.Hash + uncles []*types.Header ) - if self.config.XDPoS == nil { - for hash, uncle := range self.possibleUncles { - if len(uncles) == 2 { - break - } - if err := self.commitUncle(work, uncle.Header()); err != nil { - log.Trace("Bad uncle found and will be removed", "hash", hash) - log.Trace(fmt.Sprint(uncle)) - badUncles = append(badUncles, hash) - } else { - log.Debug("Committing new uncle to block", "hash", hash) - uncles = append(uncles, uncle.Header()) - } - } - for _, hash := range badUncles { - delete(self.possibleUncles, hash) - } - } // Create the new block to seal with the consensus engine if work.Block, err = self.engine.Finalize(self.chain, header, work.state, work.parentState, work.txs, uncles, work.receipts); err != nil { log.Error("Failed to finalize block for sealing", "err", err) 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))) self.unconfirmed.Shift(work.Block.NumberU64() - 1) @@ -835,21 +800,6 @@ func (self *worker) commitNewWork() { self.push(work) } -func (self *worker) commitUncle(work *Work, uncle *types.Header) error { - hash := uncle.Hash() - if work.uncles.Contains(hash) { - return fmt.Errorf("uncle not unique") - } - if !work.ancestors.Contains(uncle.ParentHash) { - return fmt.Errorf("uncle's parent unknown (%x)", uncle.ParentHash[0:4]) - } - if work.family.Contains(hash) { - return fmt.Errorf("uncle already in family (%x)", hash) - } - work.uncles.Add(uncle.Hash()) - return nil -} - 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) { gp := new(core.GasPool).AddGas(env.header.GasLimit) balanceUpdated := map[common.Address]*big.Int{}