diff --git a/score.go b/score.go index bcb7916..fd6405e 100644 --- a/score.go +++ b/score.go @@ -216,6 +216,11 @@ func (ps *peerScore) score(p peer.ID) float64 { score += topicScore * topicParams.TopicWeight } + // apply the topic score cap, if any + if ps.params.TopicScoreCap > 0 && score > ps.params.TopicScoreCap { + score = ps.params.TopicScoreCap + } + // P5: application-specific score p5 := ps.params.AppSpecificScore(p) score += p5 * ps.params.AppSpecificWeight diff --git a/score_params.go b/score_params.go index 8851799..98d312b 100644 --- a/score_params.go +++ b/score_params.go @@ -45,6 +45,10 @@ type PeerScoreParams struct { // Score parameters per topic. Topics map[string]*TopicScoreParams + // Aggregate topic score cap; this limits the total contribution of topics towards a positive + // score. It must be positive (or 0 for no cap). + TopicScoreCap float64 + // P5: Application-specific peer scoring AppSpecificScore func(p peer.ID) float64 AppSpecificWeight float64 @@ -130,6 +134,11 @@ func (p *PeerScoreParams) validate() error { } } + // check that the topic score is 0 or something positive + if p.TopicScoreCap < 0 { + return fmt.Errorf("invalid topic score cap; must be positive (or 0 for no cap)") + } + // check that we have an app specific score; the weight can be anything (but expected positive) if p.AppSpecificScore == nil { return fmt.Errorf("missing application specific score function")