From 71ab9c1d7378fd87b14b9d12b6fee401912792e9 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 27 Mar 2020 11:38:48 +0200 Subject: [PATCH] use a struct for aggregating score thresholds --- gossipsub.go | 35 ++++++++--------------------------- score_params.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/gossipsub.go b/gossipsub.go index 4529540..86b6cff 100644 --- a/gossipsub.go +++ b/gossipsub.go @@ -78,22 +78,7 @@ func NewGossipSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, er } // WithPeerScore is a gossipsub router option that enables peer scoring. -// -// gossipThreshold is the score threshold below which gossip propagation is supressed. -// -// publishThreshold is the score threshold below which we shouldn't publish when using flood -// publishing (also applies to fanout and floodsub peers). -// -// graylistThreshold is the score threshold below which message processing is supressed altogether, -// implementing an effective graylist according to peer score. -// -// These thresholds should generally be negative, allowing some information to disseminate from low -// scoring peers. -// -// acceptPXThreshold is the score threshold below which PX will be ignored; this should be positive -// and limited to scores attainable by bootstrappers and other trusted nodes. -// -func WithPeerScore(params *PeerScoreParams, gossipThreshold, publishThreshold, graylistThreshold, acceptPXThreshold float64) Option { +func WithPeerScore(params *PeerScoreParams, thresholds *PeerScoreThresholds) Option { return func(ps *PubSub) error { gs, ok := ps.rt.(*GossipSubRouter) if !ok { @@ -107,20 +92,16 @@ func WithPeerScore(params *PeerScoreParams, gossipThreshold, publishThreshold, g } // sanity check: validate the threshold values - if gossipThreshold > 0 { - return fmt.Errorf("invalid gossip threshold; it must be <= 0") - } - if publishThreshold > 0 || publishThreshold > gossipThreshold { - return fmt.Errorf("invalid publish threshold; it must be <= 0 and <= gossip threshold") - } - if graylistThreshold > 0 || graylistThreshold > publishThreshold { - return fmt.Errorf("invalid graylist threshold; it must be <= 0 and <= publish threshold") + err = thresholds.validate() + if err != nil { + return err } gs.score = newPeerScore(params) - gs.gossipThreshold = gossipThreshold - gs.publishThreshold = publishThreshold - gs.graylistThreshold = graylistThreshold + gs.gossipThreshold = thresholds.GossipThreshold + gs.publishThreshold = thresholds.PublishThreshold + gs.graylistThreshold = thresholds.GraylistThreshold + gs.acceptPXThreshold = thresholds.AcceptPXThreshold // hook the tracer if ps.tracer != nil { diff --git a/score_params.go b/score_params.go index 423c901..a1d56cc 100644 --- a/score_params.go +++ b/score_params.go @@ -7,6 +7,40 @@ import ( "github.com/libp2p/go-libp2p-core/peer" ) +type PeerScoreThresholds struct { + // GossipThreshold is the score threshold below which gossip propagation is supressed; + // should be negative. + GossipThreshold float64 + + // PublishThreshold is the score threshold below which we shouldn't publish when using flood + // publishing (also applies to fanout and floodsub peers); should be negative and <= GossipThreshold. + PublishThreshold float64 + + // GraylistThreshold is the score threshold below which message processing is supressed altogether, + // implementing an effective graylist according to peer score; should be negative and <= PublisThreshold. + GraylistThreshold float64 + + // acceptPXThreshold is the score threshold below which PX will be ignored; this should be positive + // and limited to scores attainable by bootstrappers and other trusted nodes. + AcceptPXThreshold float64 +} + +func (p *PeerScoreThresholds) validate() error { + if p.GossipThreshold > 0 { + return fmt.Errorf("invalid gossip threshold; it must be <= 0") + } + if p.PublishThreshold > 0 || p.PublishThreshold > p.GossipThreshold { + return fmt.Errorf("invalid publish threshold; it must be <= 0 and <= gossip threshold") + } + if p.GraylistThreshold > 0 || p.GraylistThreshold > p.PublishThreshold { + return fmt.Errorf("invalid graylist threshold; it must be <= 0 and <= publish threshold") + } + if p.AcceptPXThreshold < 0 { + return fmt.Errorf("invalid accept PX threshold; it must be >= 0") + } + return nil +} + type PeerScoreParams struct { // Score parameters per topic. Topics map[string]*TopicScoreParams