Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P7: Behaviour pattern penalties #318

Merged
merged 5 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions score.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type peerStats struct {

// IP tracking; store as string for easy processing
ips []string

// behavioural pattern penalties (applied by the router)
behaviourPenalty float64
}

type topicStats struct {
Expand Down Expand Up @@ -251,9 +254,30 @@ func (ps *peerScore) score(p peer.ID) float64 {
}
}

// P7: behavioural pattern penalty
p7 := pstats.behaviourPenalty * pstats.behaviourPenalty
score += p7 * ps.params.BehaviourPenaltyWeight

return score
}

// behavioural pattern penalties
func (ps *peerScore) AddPenalty(p peer.ID, count int) {
if ps == nil {
return
}

ps.Lock()
defer ps.Unlock()

pstats, ok := ps.peerStats[p]
if !ok {
return
}

pstats.behaviourPenalty += float64(count)
}

// periodic maintenance
func (ps *peerScore) background(ctx context.Context) {
refreshScores := time.NewTicker(ps.params.DecayInterval)
Expand Down Expand Up @@ -366,6 +390,12 @@ func (ps *peerScore) refreshScores() {
}
}
}

// decay P7 counter
pstats.behaviourPenalty *= ps.params.BehaviourPenaltyDecay
if pstats.behaviourPenalty < ps.params.DecayToZero {
pstats.behaviourPenalty = 0
}
}
}

Expand Down
17 changes: 16 additions & 1 deletion score_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 square of the counter, which decays with BehaviourPenaltyDecay.
// The weight of the parameter MUST be negative (or zero to disable).
BehaviourPenaltyWeight, BehaviourPenaltyDecay float64

// the decay interval for parameter counters.
DecayInterval time.Duration

Expand Down Expand Up @@ -157,10 +164,18 @@ func (p *PeerScoreParams) validate() error {
if p.IPColocationFactorWeight > 0 {
return fmt.Errorf("invalid IPColocationFactorWeight; must be negative (or 0 to disable)")
}
if p.IPColocationFactorWeight < 0 && p.IPColocationFactorThreshold < 1 {
if p.IPColocationFactorWeight != 0 && p.IPColocationFactorThreshold < 1 {
return fmt.Errorf("invalid IPColocationFactorThreshold; must be at least 1")
}

// check the behaviour penalty
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")
Expand Down
24 changes: 23 additions & 1 deletion score_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,42 @@ 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}).validate() == nil {
t.Fatal("expected validation error")
}
if (&PeerScoreParams{AppSpecificScore: appScore, DecayInterval: time.Second, DecayToZero: 0.01, BehaviourPenaltyWeight: -1}).validate() == nil {
t.Fatal("expected validation error")
}
if (&PeerScoreParams{AppSpecificScore: appScore, DecayInterval: time.Second, DecayToZero: 0.01, BehaviourPenaltyWeight: -1, BehaviourPenaltyDecay: 2}).validate() == nil {
t.Fatal("expected validation error")
}

// don't use these params in production!
if (&PeerScoreParams{
AppSpecificScore: appScore,
DecayInterval: time.Second,
DecayToZero: 0.01,
IPColocationFactorWeight: -1,
IPColocationFactorThreshold: 1,
BehaviourPenaltyWeight: -1,
BehaviourPenaltyDecay: 0.999,
}).validate() != nil {
t.Fatal("expected validation success")
}

if (&PeerScoreParams{
TopicScoreCap: 1,
AppSpecificScore: appScore,
DecayInterval: time.Second,
DecayToZero: 0.01,
IPColocationFactorWeight: -1,
IPColocationFactorThreshold: 1,
BehaviourPenaltyWeight: -1,
BehaviourPenaltyDecay: 0.999,
}).validate() != nil {
t.Fatal("expected validation success")
}

// don't use these params in production!
if (&PeerScoreParams{
TopicScoreCap: 1,
AppSpecificScore: appScore,
Expand Down
56 changes: 56 additions & 0 deletions score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,62 @@ func TestScoreIPColocation(t *testing.T) {
}
}

func TestScoreBehaviourPenalty(t *testing.T) {
params := &PeerScoreParams{
AppSpecificScore: func(peer.ID) float64 { return 0 },
BehaviourPenaltyWeight: -1,
BehaviourPenaltyDecay: 0.99,
}

peerA := peer.ID("A")

var ps *peerScore

// first check AddPenalty on a nil peerScore
ps.AddPenalty(peerA, 1)
aScore := ps.Score(peerA)
if aScore != 0 {
t.Errorf("expected peer score to be 0, got %f", aScore)
}

// instantiate the peerScore
ps = newPeerScore(params)

// next AddPenalty on a non-existent peer
ps.AddPenalty(peerA, 1)
aScore = ps.Score(peerA)
if aScore != 0 {
t.Errorf("expected peer score to be 0, got %f", aScore)
}

// add the peer and test penalties
ps.AddPeer(peerA, "myproto")

aScore = ps.Score(peerA)
if aScore != 0 {
t.Errorf("expected peer score to be 0, got %f", aScore)
}

ps.AddPenalty(peerA, 1)
aScore = ps.Score(peerA)
if aScore != -1 {
t.Errorf("expected peer score to be -1, got %f", aScore)
}

ps.AddPenalty(peerA, 1)
aScore = ps.Score(peerA)
if aScore != -4 {
t.Errorf("expected peer score to be -4, got %f", aScore)
}

ps.refreshScores()

aScore = ps.Score(peerA)
if aScore != -3.9204 {
t.Errorf("expected peer score to be -3.9204, got %f", aScore)
}
}

func TestScoreRetention(t *testing.T) {
// Create parameters with reasonable default values
mytopic := "mytopic"
Expand Down