From 6ca8dc2c50c325b8ccd48621c8b325df16f7f6bf Mon Sep 17 00:00:00 2001 From: "Masih H. Derkani" Date: Tue, 21 Jan 2025 17:01:48 +0000 Subject: [PATCH] Check the validity of ECChain first in chain exchange 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. --- chainexchange/pubsub.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/chainexchange/pubsub.go b/chainexchange/pubsub.go index 83f15ca5..61249235 100644 --- a/chainexchange/pubsub.go +++ b/chainexchange/pubsub.go @@ -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.