Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consortium-v2: only check the current block in GetBestParentBlock #555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions consensus/consortium/v2/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -1474,33 +1474,30 @@ func (c *Consortium) readSignerAndContract() (
return c.val, c.signFn, c.signTxFn, c.contract
}

// GetBestParentBlock goes backward in the canonical chain to find if the miner can
// create a chain which has more difficulty than current chain. In case the miner
// cannot create a better chain, this function returns the head block of current
// canonical chain.
// GetBestParentBlock looks at the current block to see if the miner can create
// higher difficulty block than the current block. If it can, GetBestParentBlock
// returns the the parent of current block and true. Otherwise, this function
// returns current block and false.
func (c *Consortium) GetBestParentBlock(chain *core.BlockChain) (*types.Block, bool) {
signer, _, _, _ := c.readSignerAndContract()

currentBlock := chain.CurrentBlock()
block := currentBlock
prevBlock := chain.GetBlockByHash(block.ParentHash())
diffculty := block.Difficulty().Int64()
for diffculty < diffInTurn.Int64() {
snap, err := c.snapshot(chain, block.NumberU64()-1, block.ParentHash(), nil)
if currentBlock.Difficulty().Int64() < diffInTurn.Int64() {
snap, err := c.snapshot(chain, currentBlock.NumberU64()-1, currentBlock.ParentHash(), nil)
if err != nil {
return currentBlock, false
}
// Miner can create an inturn block which helps the chain to have
// greater diffculty
// higher diffculty
signer, _, _, _ := c.readSignerAndContract()
if snap.supposeValidator() == signer {
if !snap.IsRecentlySigned(signer) {
return prevBlock, true
parentBlock := chain.GetBlockByHash(currentBlock.ParentHash())
// This must never happen, still check for safety
if parentBlock == nil {
return currentBlock, false
}
return parentBlock, true
}
}

block = prevBlock
prevBlock = chain.GetBlockByHash(block.ParentHash())
diffculty += block.Difficulty().Int64()
}

return currentBlock, false
Expand Down
Loading