From 1c4f0920fed5e42e73aec7ebc0fd95e3f523df06 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 27 Mar 2020 20:31:31 +0200 Subject: [PATCH] use named constants for tracing message rejection reasons --- pubsub.go | 6 +++--- score.go | 13 ++++++------- tracer.go | 11 +++++++++++ validation.go | 14 +++++++------- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/pubsub.go b/pubsub.go index df5970c..985ad8a 100644 --- a/pubsub.go +++ b/pubsub.go @@ -814,21 +814,21 @@ func (p *PubSub) pushMsg(msg *Message) { // reject messages from blacklisted peers if p.blacklist.Contains(src) { log.Warningf("dropping message from blacklisted peer %s", src) - p.tracer.RejectMessage(msg, "blacklisted peer") + p.tracer.RejectMessage(msg, rejectBlacklstedPeer) return } // even if they are forwarded by good peers if p.blacklist.Contains(msg.GetFrom()) { log.Warningf("dropping message from blacklisted source %s", src) - p.tracer.RejectMessage(msg, "blacklisted source") + p.tracer.RejectMessage(msg, rejectBlacklistedSource) return } // reject unsigned messages when strict before we even process the id if p.signStrict && msg.Signature == nil { log.Debugf("dropping unsigned message from %s", src) - p.tracer.RejectMessage(msg, "missing signature") + p.tracer.RejectMessage(msg, rejectMissingSignature) return } diff --git a/score.go b/score.go index e567cf9..08e3a13 100644 --- a/score.go +++ b/score.go @@ -505,22 +505,21 @@ func (ps *peerScore) RejectMessage(msg *Message, reason string) { ps.Lock() defer ps.Unlock() - // 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": + case rejectMissingSignature: fallthrough - case "invalid signature": + case rejectInvalidSignature: ps.markInvalidMessageDelivery(msg.ReceivedFrom, msg) return // we ignore those messages, so do nothing. - case "blacklisted peer": + case rejectBlacklstedPeer: fallthrough - case "blacklisted source": + case rejectBlacklistedSource: return - case "validation queue full": + case rejectValidationQueueFull: // the message was rejected before it entered the validation pipeline; // we don't know if this message has a valid signature, and thus we also don't know if // it has a valid message ID; all we can do is ignore it. @@ -529,7 +528,7 @@ func (ps *peerScore) RejectMessage(msg *Message, reason string) { drec := ps.deliveries.getRecord(ps.msgID(msg.Message)) - if reason == "validation throttled" { + if reason == rejectValidationThrottled { // if we reject with "validation throttled" we don't penalize the peer(s) that forward it // because we don't know if it was valid. drec.status = delivery_throttled diff --git a/tracer.go b/tracer.go index a379c89..613d70e 100644 --- a/tracer.go +++ b/tracer.go @@ -24,6 +24,17 @@ import ( var TraceBufferSize = 1 << 16 // 64K ought to be enough for everyone; famous last words. var MinTraceBatchSize = 16 +// rejection reasons +const ( + rejectBlacklstedPeer = "blacklisted peer" + rejectBlacklistedSource = "blacklisted source" + rejectMissingSignature = "missing signature" + rejectInvalidSignature = "invalid signature" + rejectValidationQueueFull = "validation queue full" + rejectValidationThrottled = "validation throttled" + rejectValidationFailed = "validation failed" +) + type basicTracer struct { ch chan struct{} mx sync.Mutex diff --git a/validation.go b/validation.go index 96e8be0..d83be65 100644 --- a/validation.go +++ b/validation.go @@ -152,7 +152,7 @@ func (v *validation) Push(src peer.ID, msg *Message) bool { case v.validateQ <- &validateReq{vals, src, msg}: default: log.Warningf("message validation throttled: queue full; dropping message from %s", src) - v.tracer.RejectMessage(msg, "validation queue full") + v.tracer.RejectMessage(msg, rejectValidationQueueFull) } return false } @@ -195,7 +195,7 @@ func (v *validation) validate(vals []*topicVal, src peer.ID, msg *Message) { if msg.Signature != nil { if !v.validateSignature(msg) { log.Warningf("message signature validation failed; dropping message from %s", src) - v.tracer.RejectMessage(msg, "invalid signature") + v.tracer.RejectMessage(msg, rejectInvalidSignature) return } } @@ -223,7 +223,7 @@ func (v *validation) validate(vals []*topicVal, src peer.ID, msg *Message) { for _, val := range inline { if !val.validateMsg(v.p.ctx, src, msg) { log.Debugf("message validation failed; dropping message from %s", src) - v.tracer.RejectMessage(msg, "validation failed") + v.tracer.RejectMessage(msg, rejectValidationFailed) return } } @@ -238,7 +238,7 @@ func (v *validation) validate(vals []*topicVal, src peer.ID, msg *Message) { }() default: log.Warningf("message validation throttled; dropping message from %s", src) - v.tracer.RejectMessage(msg, "validation throttled") + v.tracer.RejectMessage(msg, rejectValidationThrottled) } return } @@ -260,7 +260,7 @@ func (v *validation) validateSignature(msg *Message) bool { func (v *validation) doValidateTopic(vals []*topicVal, src peer.ID, msg *Message) { if !v.validateTopic(vals, src, msg) { log.Warningf("message validation failed; dropping message from %s", src) - v.tracer.RejectMessage(msg, "validation failed") + v.tracer.RejectMessage(msg, rejectValidationFailed) return } @@ -298,7 +298,7 @@ loop: } if throttle { - v.tracer.RejectMessage(msg, "validation throttled") + v.tracer.RejectMessage(msg, rejectValidationThrottled) return false } @@ -323,7 +323,7 @@ func (v *validation) validateSingleTopic(val *topicVal, src peer.ID, msg *Messag default: log.Debugf("validation throttled for topic %s", val.topic) - v.tracer.RejectMessage(msg, "validation throttled") + v.tracer.RejectMessage(msg, rejectValidationThrottled) return false } }