Skip to content

Commit

Permalink
Check the validity of ECChain first in chain exchange (#837)
Browse files Browse the repository at this point in the history
Add `ECChain` validation to chain exchange pubsub validation function to
reject invalid chains as early as possible.

Change the ordering of validation rules such that cheaper checks are
done first before checking current progress.
  • Loading branch information
masih authored Jan 22, 2025
1 parent 9c11ba3 commit a61a42e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions chainexchange/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,21 @@ func (p *PubSubChainExchange) validatePubSubMessage(_ context.Context, _ peer.ID
log.Debugw("failed to decode message", "from", msg.GetFrom(), "err", err)
return pubsub.ValidationReject
}
if cmsg.Chain.IsZero() {
// No peer should broadcast a zero-length chain.
return pubsub.ValidationReject
}
if err := cmsg.Chain.Validate(); err != nil {
// Invalid chain.
log.Debugw("Invalid chain", "from", msg.GetFrom(), "err", err)
return pubsub.ValidationReject
}
switch current := p.progress(); {
case
cmsg.Instance < current.ID,
cmsg.Instance > current.ID+p.maxInstanceLookahead:
// Too far ahead or too far behind.
return pubsub.ValidationIgnore
case cmsg.Chain.IsZero():
// No peer should broadcast a zero-length chain.
return pubsub.ValidationReject
}
// TODO: wire in the current base chain from an on-going instance to further
// tighten up validation.
Expand Down

0 comments on commit a61a42e

Please sign in to comment.