Skip to content

Commit

Permalink
Implement postponed validation of proposed blocks (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidenaio authored Jul 13, 2021
1 parent c7d8462 commit dbd5bc2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
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

0 comments on commit dbd5bc2

Please sign in to comment.