From e17e266aa9fbd9549e9a1babc898419df41d39db Mon Sep 17 00:00:00 2001 From: vyzo Date: Sun, 8 Mar 2020 21:43:28 +0200 Subject: [PATCH] track message delivery time within the validation pipeline --- score.go | 23 +++++++++++++++++++++-- trace.go | 11 +++++++++++ validation.go | 2 ++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/score.go b/score.go index 0253394..ac17d0c 100644 --- a/score.go +++ b/score.go @@ -378,13 +378,32 @@ func (ps *peerScore) DeliverMessage(msg *Message) { } } +func (ps *peerScore) ValidateMessage(msg *Message) { + ps.Lock() + defer ps.Unlock() + + // the pubsub subsystem is beginning validation; create a record to track time in + // the validation pipeline with an accurate firstSeen time. + _ = ps.deliveries.getRecord(ps.msgID(msg.Message)) +} + func (ps *peerScore) RejectMessage(msg *Message, reason string) { ps.Lock() defer ps.Unlock() - if reason == "invalid signature" { + // TODO: the reasons should become named strings; good enough for now. + switch reason { + // we don't track those messages, but we penalize the peer as they are clearly invalid + case "missing signature": + fallthrough + case "invalid signature": ps.markInvalidMessageDelivery(msg.ReceivedFrom, msg) - // if we reject with "invalid signature" we don't track this message delivery. + return + + // we ignore those messages, so do nothing. + case "blacklisted peer": + fallthrough + case "blacklisted source": return } diff --git a/trace.go b/trace.go index 1924b4e..f888836 100644 --- a/trace.go +++ b/trace.go @@ -22,6 +22,7 @@ type scoreTracer interface { Leave(topic string) Graft(p peer.ID, topic string) Prune(p peer.ID, topic string) + ValidateMessage(msg *Message) DeliverMessage(msg *Message) RejectMessage(msg *Message, reason string) DuplicateMessage(msg *Message) @@ -58,6 +59,16 @@ func (t *pubsubTracer) PublishMessage(msg *Message) { t.tracer.Trace(evt) } +func (t *pubsubTracer) ValidateMessage(msg *Message) { + if t == nil { + return + } + + if t.score != nil && msg.ReceivedFrom != t.pid { + t.score.ValidateMessage(msg) + } +} + func (t *pubsubTracer) RejectMessage(msg *Message, reason string) { if t == nil { return diff --git a/validation.go b/validation.go index d6ae546..fb18b38 100644 --- a/validation.go +++ b/validation.go @@ -206,6 +206,8 @@ func (v *validation) validate(vals []*topicVal, src peer.ID, msg *Message) { if !v.p.markSeen(id) { v.tracer.DuplicateMessage(msg) return + } else { + v.tracer.ValidateMessage(msg) } var inline, async []*topicVal