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

[R4R] the miner module should propose block on a proper fork #355

Merged
merged 2 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ type PoSA interface {
IsSystemTransaction(tx *types.Transaction, header *types.Header) (bool, error)
IsSystemContract(to *common.Address) bool
EnoughDistance(chain ChainReader, header *types.Header) bool
IsLocalBlock(header *types.Header) bool
}
28 changes: 28 additions & 0 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,34 @@ func (p *Parlia) EnoughDistance(chain consensus.ChainReader, header *types.Heade
return snap.enoughDistance(p.val, header)
}

func (p *Parlia) IsLocalBlock(header *types.Header) bool {
return p.val == header.Coinbase
}

func (p *Parlia) SignRecently(chain consensus.ChainReader, parent *types.Header) (bool, error) {
snap, err := p.snapshot(chain, parent.Number.Uint64(), parent.ParentHash, nil)
if err != nil {
return true, err
}

// Bail out if we're unauthorized to sign a block
if _, authorized := snap.Validators[p.val]; !authorized {
return true, errUnauthorizedValidator
}

// If we're amongst the recent signers, wait for the next block
number := parent.Number.Uint64() + 1
for seen, recent := range snap.Recents {
if recent == p.val {
// Signer is among recents, only wait if the current block doesn't shift it out
if limit := uint64(len(snap.Validators)/2 + 1); number < limit || seen > number-limit {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put the "limit := " expression before the if expression, it can make the 'if' expression more concise.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if {statement}; {condition} is a widely used grammar, I think it is concise enough for reviewers.

return true, nil
}
}
}
return false, nil
}

// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
// that a new block should have based on the previous blocks in the chain and the
// current signer.
Expand Down
4 changes: 3 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,9 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
if !reorg && externTd.Cmp(localTd) == 0 {
// Split same-difficulty blocks by number, then preferentially select
// the block generated by the local miner as the canonical block.
if block.NumberU64() < currentBlock.NumberU64() {
if block.NumberU64() < currentBlock.NumberU64() || block.Time() < currentBlock.Time() {
reorg = true
} else if p, ok := bc.engine.(consensus.PoSA); ok && p.IsLocalBlock(currentBlock.Header()) {
reorg = true
} else if block.NumberU64() == currentBlock.NumberU64() {
var currentPreserve, blockPreserve bool
Expand Down
11 changes: 11 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
}
clearPending(head.Block.NumberU64())
timestamp = time.Now().Unix()
if p, ok := w.engine.(*parlia.Parlia); ok {
signedRecent, err := p.SignRecently(w.chain, head.Block.Header())
if err != nil {
log.Info("Not allowed to propose block", "err", err)
continue
}
if signedRecent {
log.Info("Signed recently, must wait")
continue
}
}
commit(true, commitInterruptNewHead)

case <-timer.C:
Expand Down