diff --git a/score_params.go b/score_params.go index 813c167..84f5a49 100644 --- a/score_params.go +++ b/score_params.go @@ -73,6 +73,13 @@ type PeerScoreParams struct { IPColocationFactorThreshold int IPColocationFactorWhitelist map[string]struct{} + // P7: behavioural pattern penalties. + // This parameter has an associated counter which tracks misbehaviour as detected by the + // router. + // The value of the parameter is the counter, decaying with BehaviourPatternPenaltyDecay. + // The weight of the parameter MUST be negative (or zero to disable). + BehaviourPenaltyWeight, BehaviourPenaltyDecay float64 + // the decay interval for parameter counters. DecayInterval time.Duration @@ -161,6 +168,13 @@ func (p *PeerScoreParams) validate() error { return fmt.Errorf("invalid IPColocationFactorThreshold; must be at least 1") } + if p.BehaviourPenaltyWeight > 0 { + return fmt.Errorf("invalid BehaviourPenaltyWeight; must be negative (or 0 to disable)") + } + if p.BehaviourPenaltyWeight != 0 && (p.BehaviourPenaltyDecay <= 0 || p.BehaviourPenaltyDecay >= 1) { + return fmt.Errorf("invalid BehaviourPenaltyDecay; must be between 0 and 1") + } + // check the decay parameters if p.DecayInterval < time.Second { return fmt.Errorf("invalid DecayInterval; must be at least 1s") diff --git a/score_params_test.go b/score_params_test.go index b46c36d..e617098 100644 --- a/score_params_test.go +++ b/score_params_test.go @@ -153,6 +153,15 @@ func TestPeerScoreParamsValidation(t *testing.T) { if (&PeerScoreParams{TopicScoreCap: 1, AppSpecificScore: appScore, DecayInterval: time.Second, DecayToZero: 2, IPColocationFactorWeight: -1, IPColocationFactorThreshold: 1}).validate() == nil { t.Fatal("expected validation error") } + if (&PeerScoreParams{AppSpecificScore: appScore, DecayInterval: time.Second, DecayToZero: 0.01, BehaviourPenaltyWeight: 1}) == nil { + t.Fatal("expected validation error") + } + if (&PeerScoreParams{AppSpecificScore: appScore, DecayInterval: time.Second, DecayToZero: 0.01, BehaviourPenaltyWeight: -1}) == nil { + t.Fatal("expected validation error") + } + if (&PeerScoreParams{AppSpecificScore: appScore, DecayInterval: time.Second, DecayToZero: 0.01, BehaviourPenaltyWeight: -1, BehaviourPenaltyDecay: 2}) == nil { + t.Fatal("expected validation error") + } // don't use these params in production! if (&PeerScoreParams{ @@ -162,6 +171,8 @@ func TestPeerScoreParamsValidation(t *testing.T) { DecayToZero: 0.01, IPColocationFactorWeight: -1, IPColocationFactorThreshold: 1, + BehaviourPenaltyWeight: -1, + BehaviourPenaltyDecay: 0.999, }).validate() != nil { t.Fatal("expected validation success") }