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

Implement postponed validation of proposed blocks #735

Merged
merged 2 commits into from
Jul 13, 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
2 changes: 1 addition & 1 deletion consensus/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (engine *Engine) proposeBlock(proof []byte) *types.Block {
engine.pm.ProposeProof(proofProposal)
engine.pm.ProposeBlock(proposal)

engine.proposals.AddProposedBlock(proposal, "", time.Now().UTC(), nil)
engine.proposals.AddProposedBlock(proposal, "", time.Now().UTC())
engine.proposals.AddProposeProof(proofProposal)

return proposal.Block
Expand Down
19 changes: 8 additions & 11 deletions pengings/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,28 +196,21 @@ func (proposals *Proposals) ProcessPendingProofs() []*types.ProofProposal {
func (proposals *Proposals) ProcessPendingBlocks() []*types.BlockProposal {
var result []*types.BlockProposal

checkState, err := proposals.appState.ForCheck(proposals.chain.Head.Height())
if err != nil {
proposals.log.Warn("failed to create checkState", "err", err)
}
proposals.pendingBlocks.Range(func(key, value interface{}) bool {

blockPeer := value.(*blockPeer)
if added, pending := proposals.AddProposedBlock(blockPeer.proposal, blockPeer.peerId, blockPeer.receivingTime, checkState); added {
if added, pending := proposals.AddProposedBlock(blockPeer.proposal, blockPeer.peerId, blockPeer.receivingTime); added {
result = append(result, blockPeer.proposal)
} else if !pending {
proposals.pendingBlocks.Delete(key)
}
if checkState != nil {
checkState.Reset()
}
return true
})

return result
}

func (proposals *Proposals) AddProposedBlock(proposal *types.BlockProposal, peerId peer.ID, receivingTime time.Time, checkState *appstate.AppState) (added bool, pending bool) {
func (proposals *Proposals) AddProposedBlock(proposal *types.BlockProposal, peerId peer.ID, receivingTime time.Time) (added bool, pending bool) {
block := proposal.Block
currentRound := proposals.chain.Round()
if currentRound == block.Height() {
Expand Down Expand Up @@ -258,8 +251,9 @@ func (proposals *Proposals) AddProposedBlock(proposal *types.BlockProposal, peer
if _, ok := round.Load(block.Hash()); ok {
return false, false
}
if _, err := proposals.chain.ValidateBlock(block, checkState, nil); err != nil {
log.Warn("Failed proposed block validation", "err", err)

if err := proposals.chain.ValidateHeader(block.Header, proposals.chain.Head); err != nil {
log.Warn("Failed proposed block header validation", "err", err)
// it might be a signal about a fork
if err == blockchain.ParentHashIsInvalid && peerId != "" {
proposals.potentialForkedPeers.Add(peerId)
Expand Down Expand Up @@ -303,6 +297,9 @@ func (proposals *Proposals) GetProposedBlock(round uint64, proposerPubKey []byte
return true
})
if result != nil {
if _, err := proposals.chain.ValidateBlock(result, nil, nil); err != nil {
return nil, err
}
return result, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (h *IdenaGossipHandler) handle(p *protoPeer) error {
}
// if peer proposes this msg it should be on `query.Round-1` height
p.setHeight(proposal.Block.Height() - 1)
if ok, _ := h.proposals.AddProposedBlock(proposal, p.id, time.Now().UTC(), nil); ok {
if ok, _ := h.proposals.AddProposedBlock(proposal, p.id, time.Now().UTC()); ok {
h.ProposeBlock(proposal)
}
case Vote:
Expand Down