Skip to content

Commit

Permalink
remove uncle block handling (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanwiset25 committed Apr 23, 2024
1 parent 78bad64 commit 3fe54e2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 54 deletions.
2 changes: 1 addition & 1 deletion consensus/XDPoS/XDPoS.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions consensus/XDPoS/engines/engine_v2/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine_v2

import (
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
Expand Down Expand Up @@ -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 {
Expand Down
56 changes: 3 additions & 53 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package miner
import (
"bytes"
"encoding/binary"
"fmt"

"github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
"github.com/XinFinOrg/XDPoSChain/accounts"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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{}
Expand Down

0 comments on commit 3fe54e2

Please sign in to comment.