diff --git a/score.go b/score.go index b22f4e9..5e2b58b 100644 --- a/score.go +++ b/score.go @@ -274,8 +274,11 @@ func (ps *peerScore) score(p peer.ID) float64 { score += p6 * ps.params.IPColocationFactorWeight // P7: behavioural pattern penalty - p7 := pstats.behaviourPenalty * pstats.behaviourPenalty - score += p7 * ps.params.BehaviourPenaltyWeight + if pstats.behaviourPenalty > ps.params.BehaviourPenaltyThreshold { + excess := pstats.behaviourPenalty - ps.params.BehaviourPenaltyThreshold + p7 := excess * excess + score += p7 * ps.params.BehaviourPenaltyWeight + } return score } diff --git a/score_params.go b/score_params.go index d004de3..3a20584 100644 --- a/score_params.go +++ b/score_params.go @@ -79,9 +79,10 @@ type PeerScoreParams struct { // - attempting to re-graft before the prune backoff time has elapsed. // - not following up in IWANT requests for messages advertised with IHAVE. // - // The value of the parameter is the square of the counter, which decays with BehaviourPenaltyDecay. + // The value of the parameter is the square of the counter over the threshold, which decays with + // BehaviourPenaltyDecay. // The weight of the parameter MUST be negative (or zero to disable). - BehaviourPenaltyWeight, BehaviourPenaltyDecay float64 + BehaviourPenaltyWeight, BehaviourPenaltyThreshold, BehaviourPenaltyDecay float64 // the decay interval for parameter counters. DecayInterval time.Duration @@ -179,6 +180,9 @@ func (p *PeerScoreParams) validate() error { if p.BehaviourPenaltyWeight != 0 && (p.BehaviourPenaltyDecay <= 0 || p.BehaviourPenaltyDecay >= 1) { return fmt.Errorf("invalid BehaviourPenaltyDecay; must be between 0 and 1") } + if p.BehaviourPenaltyThreshold < 0 { + return fmt.Errorf("invalid BehaviourPenaltyThreshold; must be >= 0") + } // check the decay parameters if p.DecayInterval < time.Second {