use a struct for aggregating score thresholds

This commit is contained in:
vyzo 2020-03-27 11:38:48 +02:00
parent f6f34cfc99
commit 71ab9c1d73
2 changed files with 42 additions and 27 deletions

View File

@ -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 {

View File

@ -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