mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
set block difficulty belong to creator turn
This commit is contained in:
parent
bc027c4fc0
commit
f5e8c999a2
3 changed files with 14 additions and 30 deletions
|
|
@ -906,7 +906,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
|
||||||
defer bc.mu.Unlock()
|
defer bc.mu.Unlock()
|
||||||
|
|
||||||
currentBlock := bc.CurrentBlock()
|
currentBlock := bc.CurrentBlock()
|
||||||
//localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64())
|
localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64())
|
||||||
externTd := new(big.Int).Add(block.Difficulty(), ptd)
|
externTd := new(big.Int).Add(block.Difficulty(), ptd)
|
||||||
|
|
||||||
// Irrelevant of the canonical status, write the block itself to the database
|
// Irrelevant of the canonical status, write the block itself to the database
|
||||||
|
|
@ -981,8 +981,13 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
|
||||||
// Second clause in the if statement reduces the vulnerability to selfish mining.
|
// Second clause in the if statement reduces the vulnerability to selfish mining.
|
||||||
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
|
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
|
||||||
|
|
||||||
reorg := block.NumberU64() > currentBlock.NumberU64()
|
reorg := externTd.Cmp(localTd) > 0
|
||||||
if reorg {
|
currentBlock = bc.CurrentBlock()
|
||||||
|
if !reorg && externTd.Cmp(localTd) == 0 {
|
||||||
|
// Split same-difficulty blocks by number
|
||||||
|
reorg = block.NumberU64() > currentBlock.NumberU64()
|
||||||
|
}
|
||||||
|
if reorg {
|
||||||
// Reorganise the chain if the parent is not the head block
|
// Reorganise the chain if the parent is not the head block
|
||||||
if block.ParentHash() != currentBlock.Hash() {
|
if block.ParentHash() != currentBlock.Hash() {
|
||||||
if err := bc.reorg(currentBlock, block); err != nil {
|
if err := bc.reorg(currentBlock, block); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -657,18 +657,15 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
|
||||||
trueTD = new(big.Int).Sub(request.TD, request.Block.Difficulty())
|
trueTD = new(big.Int).Sub(request.TD, request.Block.Difficulty())
|
||||||
)
|
)
|
||||||
// Update the peers total difficulty if better than the previous
|
// Update the peers total difficulty if better than the previous
|
||||||
_, td := p.Head()
|
if _, td := p.Head(); trueTD.Cmp(td) > 0 {
|
||||||
currentBlock := pm.blockchain.CurrentBlock()
|
|
||||||
currentTd := pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())
|
|
||||||
log.Debug("NewBlockMsg", "p", p, "number", request.Block.NumberU64(), "trueTD", trueTD, "td", td, "currentTd", currentTd)
|
|
||||||
if trueTD.Cmp(td) > 0 {
|
|
||||||
p.SetHead(trueHead, trueTD)
|
p.SetHead(trueHead, trueTD)
|
||||||
|
|
||||||
// Schedule a sync if above ours. Note, this will not fire a sync for a gap of
|
// Schedule a sync if above ours. Note, this will not fire a sync for a gap of
|
||||||
// a singe block (as the true TD is below the propagated block), however this
|
// a singe block (as the true TD is below the propagated block), however this
|
||||||
// scenario should easily be covered by the fetcher.
|
// scenario should easily be covered by the fetcher.
|
||||||
if trueTD.Cmp(currentTd) > 0 {
|
currentBlock := pm.blockchain.CurrentBlock()
|
||||||
go pm.synchronise(p)
|
if trueTD.Cmp(pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())) > 0 {
|
||||||
|
go pm.synchronise(p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -472,17 +472,6 @@ func abs(x int64) int64 {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
func hop(len, pre, cur int) int {
|
|
||||||
switch {
|
|
||||||
case pre < cur:
|
|
||||||
return cur - (pre + 1)
|
|
||||||
case pre > cur:
|
|
||||||
return (len - pre) + (cur - 1)
|
|
||||||
default:
|
|
||||||
return len - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *worker) commitNewWork() {
|
func (self *worker) commitNewWork() {
|
||||||
self.mu.Lock()
|
self.mu.Lock()
|
||||||
defer self.mu.Unlock()
|
defer self.mu.Unlock()
|
||||||
|
|
@ -508,14 +497,7 @@ func (self *worker) commitNewWork() {
|
||||||
if self.config.XDPoS != nil {
|
if self.config.XDPoS != nil {
|
||||||
// get masternodes set from latest checkpoint
|
// get masternodes set from latest checkpoint
|
||||||
c := self.engine.(*XDPoS.XDPoS)
|
c := self.engine.(*XDPoS.XDPoS)
|
||||||
masternodes := c.GetMasternodes(self.chain, parent.Header())
|
len, preIndex, curIndex, ok, err := c.YourTurn(self.chain, parent.Header())
|
||||||
snap, err := c.GetSnapshot(self.chain, parent.Header())
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Failed when trying to commit new work", "err", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
signers = snap.Signers
|
|
||||||
preIndex, curIndex, ok, err := XDPoS.YourTurn(masternodes, snap, parent.Header(), self.coinbase)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed when trying to commit new work", "err", err)
|
log.Error("Failed when trying to commit new work", "err", err)
|
||||||
return
|
return
|
||||||
|
|
@ -531,7 +513,7 @@ func (self *worker) commitNewWork() {
|
||||||
// you're not allowed to create this block
|
// you're not allowed to create this block
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h := hop(len(masternodes), preIndex, curIndex)
|
h := XDPoS.Hop(len, preIndex, curIndex)
|
||||||
gap := waitPeriod * int64(h)
|
gap := waitPeriod * int64(h)
|
||||||
// Check nearest checkpoint block in hop range.
|
// Check nearest checkpoint block in hop range.
|
||||||
nearest := self.config.XDPoS.Epoch - (parent.Header().Number.Uint64() % self.config.XDPoS.Epoch)
|
nearest := self.config.XDPoS.Epoch - (parent.Header().Number.Uint64() % self.config.XDPoS.Epoch)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue