forked from berty/go-libp2p-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.go
1081 lines (891 loc) · 27 KB
/
score.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package pubsub
import (
"context"
"fmt"
"net"
"sync"
"time"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
manet "github.com/multiformats/go-multiaddr/net"
)
type peerStats struct {
// true if the peer is currently connected
connected bool
// expiration time of the score stats for disconnected peers
expire time.Time
// per topc stats
topics map[string]*topicStats
// IP tracking; store as string for easy processing
ips []string
// IP whitelisting cache
ipWhitelist map[string]bool
// behavioural pattern penalties (applied by the router)
behaviourPenalty float64
}
type topicStats struct {
// true if the peer is in the mesh
inMesh bool
// time when the peer was (last) GRAFTed; valid only when in mesh
graftTime time.Time
// time in mesh (updated during refresh/decay to avoid calling gettimeofday on
// every score invocation)
meshTime time.Duration
// first message deliveries
firstMessageDeliveries float64
// mesh message deliveries
meshMessageDeliveries float64
// true if the peer has been enough time in the mesh to activate mess message deliveries
meshMessageDeliveriesActive bool
// sticky mesh rate failure penalty counter
meshFailurePenalty float64
// invalid message counter
invalidMessageDeliveries float64
}
type peerScore struct {
sync.Mutex
// the score parameters
params *PeerScoreParams
// per peer stats for score calculation
peerStats map[peer.ID]*peerStats
// IP colocation tracking; maps IP => set of peers.
peerIPs map[string]map[peer.ID]struct{}
// message delivery tracking
deliveries *messageDeliveries
idGen *msgIDGenerator
host host.Host
// debugging inspection
inspect PeerScoreInspectFn
inspectEx ExtendedPeerScoreInspectFn
inspectPeriod time.Duration
}
var _ RawTracer = (*peerScore)(nil)
type messageDeliveries struct {
seenMsgTTL time.Duration
records map[string]*deliveryRecord
// queue for cleaning up old delivery records
head *deliveryEntry
tail *deliveryEntry
}
type deliveryRecord struct {
status int
firstSeen time.Time
validated time.Time
peers map[peer.ID]struct{}
}
type deliveryEntry struct {
id string
expire time.Time
next *deliveryEntry
}
// delivery record status
const (
deliveryUnknown = iota // we don't know (yet) if the message is valid
deliveryValid // we know the message is valid
deliveryInvalid // we know the message is invalid
deliveryIgnored // we were intructed by the validator to ignore the message
deliveryThrottled // we can't tell if it is valid because validation throttled
)
type (
PeerScoreInspectFn = func(map[peer.ID]float64)
ExtendedPeerScoreInspectFn = func(map[peer.ID]*PeerScoreSnapshot)
)
type PeerScoreSnapshot struct {
Score float64
Topics map[string]*TopicScoreSnapshot
AppSpecificScore float64
IPColocationFactor float64
BehaviourPenalty float64
}
type TopicScoreSnapshot struct {
TimeInMesh time.Duration
FirstMessageDeliveries float64
MeshMessageDeliveries float64
InvalidMessageDeliveries float64
}
// WithPeerScoreInspect is a gossipsub router option that enables peer score debugging.
// When this option is enabled, the supplied function will be invoked periodically to allow
// the application to inspect or dump the scores for connected peers.
// The supplied function can have one of two signatures:
// - PeerScoreInspectFn, which takes a map of peer IDs to score.
// - ExtendedPeerScoreInspectFn, which takes a map of peer IDs to
// PeerScoreSnapshots and allows inspection of individual score
// components for debugging peer scoring.
//
// This option must be passed _after_ the WithPeerScore option.
func WithPeerScoreInspect(inspect interface{}, period time.Duration) Option {
return func(ps *PubSub) error {
gs, ok := ps.rt.(*GossipSubRouter)
if !ok {
return fmt.Errorf("pubsub router is not gossipsub")
}
if gs.score == nil {
return fmt.Errorf("peer scoring is not enabled")
}
if gs.score.inspect != nil || gs.score.inspectEx != nil {
return fmt.Errorf("duplicate peer score inspector")
}
switch i := inspect.(type) {
case PeerScoreInspectFn:
gs.score.inspect = i
case ExtendedPeerScoreInspectFn:
gs.score.inspectEx = i
default:
return fmt.Errorf("unknown peer score insector type: %v", inspect)
}
gs.score.inspectPeriod = period
return nil
}
}
// implementation
func newPeerScore(params *PeerScoreParams) *peerScore {
seenMsgTTL := params.SeenMsgTTL
if seenMsgTTL == 0 {
seenMsgTTL = TimeCacheDuration
}
return &peerScore{
params: params,
peerStats: make(map[peer.ID]*peerStats),
peerIPs: make(map[string]map[peer.ID]struct{}),
deliveries: &messageDeliveries{seenMsgTTL: seenMsgTTL, records: make(map[string]*deliveryRecord)},
idGen: newMsgIdGenerator(),
}
}
// SetTopicScoreParams sets new score parameters for a topic.
// If the topic previously had parameters and the parameters are lowering delivery caps,
// then the score counters are recapped appropriately.
// Note: assumes that the topic score parameters have already been validated
func (ps *peerScore) SetTopicScoreParams(topic string, p *TopicScoreParams) error {
ps.Lock()
defer ps.Unlock()
old, exist := ps.params.Topics[topic]
ps.params.Topics[topic] = p
if !exist {
return nil
}
// check to see if the counter Caps are being lowered; if that's the case we need to recap them
recap := false
if p.FirstMessageDeliveriesCap < old.FirstMessageDeliveriesCap {
recap = true
}
if p.MeshMessageDeliveriesCap < old.MeshMessageDeliveriesCap {
recap = true
}
if !recap {
return nil
}
// recap counters for topic
for _, pstats := range ps.peerStats {
tstats, ok := pstats.topics[topic]
if !ok {
continue
}
if tstats.firstMessageDeliveries > p.FirstMessageDeliveriesCap {
tstats.firstMessageDeliveries = p.FirstMessageDeliveriesCap
}
if tstats.meshMessageDeliveries > p.MeshMessageDeliveriesCap {
tstats.meshMessageDeliveries = p.MeshMessageDeliveriesCap
}
}
return nil
}
// router interface
func (ps *peerScore) Start(gs *GossipSubRouter) {
if ps == nil {
return
}
ps.idGen = gs.p.idGen
ps.host = gs.p.host
go ps.background(gs.p.ctx)
}
func (ps *peerScore) Score(p peer.ID) float64 {
if ps == nil {
return 0
}
ps.Lock()
defer ps.Unlock()
return ps.score(p)
}
func (ps *peerScore) score(p peer.ID) float64 {
pstats, ok := ps.peerStats[p]
if !ok {
return 0
}
var score float64
// topic scores
for topic, tstats := range pstats.topics {
// the topic parameters
topicParams, ok := ps.params.Topics[topic]
if !ok {
// we are not scoring this topic
continue
}
// the topic score
var topicScore float64
// P1: time in Mesh
if tstats.inMesh {
p1 := float64(tstats.meshTime / topicParams.TimeInMeshQuantum)
if p1 > topicParams.TimeInMeshCap {
p1 = topicParams.TimeInMeshCap
}
topicScore += p1 * topicParams.TimeInMeshWeight
}
// P2: first message deliveries
p2 := tstats.firstMessageDeliveries
topicScore += p2 * topicParams.FirstMessageDeliveriesWeight
// P3: mesh message deliveries
if tstats.meshMessageDeliveriesActive {
if tstats.meshMessageDeliveries < topicParams.MeshMessageDeliveriesThreshold {
deficit := topicParams.MeshMessageDeliveriesThreshold - tstats.meshMessageDeliveries
p3 := deficit * deficit
topicScore += p3 * topicParams.MeshMessageDeliveriesWeight
}
}
// P3b:
// NOTE: the weight of P3b is negative (validated in TopicScoreParams.validate), so this detracts.
p3b := tstats.meshFailurePenalty
topicScore += p3b * topicParams.MeshFailurePenaltyWeight
// P4: invalid messages
// NOTE: the weight of P4 is negative (validated in TopicScoreParams.validate), so this detracts.
p4 := (tstats.invalidMessageDeliveries * tstats.invalidMessageDeliveries)
topicScore += p4 * topicParams.InvalidMessageDeliveriesWeight
// update score, mixing with topic weight
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
// P6: IP collocation factor
p6 := ps.ipColocationFactor(p)
score += p6 * ps.params.IPColocationFactorWeight
// P7: behavioural pattern penalty
if pstats.behaviourPenalty > ps.params.BehaviourPenaltyThreshold {
excess := pstats.behaviourPenalty - ps.params.BehaviourPenaltyThreshold
p7 := excess * excess
score += p7 * ps.params.BehaviourPenaltyWeight
}
return score
}
func (ps *peerScore) ipColocationFactor(p peer.ID) float64 {
pstats, ok := ps.peerStats[p]
if !ok {
return 0
}
var result float64
loop:
for _, ip := range pstats.ips {
if len(ps.params.IPColocationFactorWhitelist) > 0 {
if pstats.ipWhitelist == nil {
pstats.ipWhitelist = make(map[string]bool)
}
whitelisted, ok := pstats.ipWhitelist[ip]
if !ok {
ipObj := net.ParseIP(ip)
for _, ipNet := range ps.params.IPColocationFactorWhitelist {
if ipNet.Contains(ipObj) {
pstats.ipWhitelist[ip] = true
continue loop
}
}
pstats.ipWhitelist[ip] = false
}
if whitelisted {
continue loop
}
}
// P6 has a cliff (IPColocationFactorThreshold); it's only applied iff
// at least that many peers are connected to us from that source IP
// addr. It is quadratic, and the weight is negative (validated by
// PeerScoreParams.validate).
peersInIP := len(ps.peerIPs[ip])
if peersInIP > ps.params.IPColocationFactorThreshold {
surpluss := float64(peersInIP - ps.params.IPColocationFactorThreshold)
result += surpluss * surpluss
}
}
return result
}
// 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)
defer refreshScores.Stop()
refreshIPs := time.NewTicker(time.Minute)
defer refreshIPs.Stop()
gcDeliveryRecords := time.NewTicker(time.Minute)
defer gcDeliveryRecords.Stop()
var inspectScores <-chan time.Time
if ps.inspect != nil || ps.inspectEx != nil {
ticker := time.NewTicker(ps.inspectPeriod)
defer ticker.Stop()
// also dump at exit for one final sample
defer ps.inspectScores()
inspectScores = ticker.C
}
for {
select {
case <-refreshScores.C:
ps.refreshScores()
case <-refreshIPs.C:
ps.refreshIPs()
case <-gcDeliveryRecords.C:
ps.gcDeliveryRecords()
case <-inspectScores:
ps.inspectScores()
case <-ctx.Done():
return
}
}
}
// inspectScores dumps all tracked scores into the inspect function.
func (ps *peerScore) inspectScores() {
if ps.inspect != nil {
ps.inspectScoresSimple()
}
if ps.inspectEx != nil {
ps.inspectScoresExtended()
}
}
func (ps *peerScore) inspectScoresSimple() {
ps.Lock()
scores := make(map[peer.ID]float64, len(ps.peerStats))
for p := range ps.peerStats {
scores[p] = ps.score(p)
}
ps.Unlock()
// Since this is a user-injected function, it could be performing I/O, and
// we don't want to block the scorer's background loop. Therefore, we launch
// it in a separate goroutine. If the function needs to synchronise, it
// should do so locally.
go ps.inspect(scores)
}
func (ps *peerScore) inspectScoresExtended() {
ps.Lock()
scores := make(map[peer.ID]*PeerScoreSnapshot, len(ps.peerStats))
for p, pstats := range ps.peerStats {
pss := new(PeerScoreSnapshot)
pss.Score = ps.score(p)
if len(pstats.topics) > 0 {
pss.Topics = make(map[string]*TopicScoreSnapshot, len(pstats.topics))
for t, ts := range pstats.topics {
tss := &TopicScoreSnapshot{
FirstMessageDeliveries: ts.firstMessageDeliveries,
MeshMessageDeliveries: ts.meshMessageDeliveries,
InvalidMessageDeliveries: ts.invalidMessageDeliveries,
}
if ts.inMesh {
tss.TimeInMesh = ts.meshTime
}
pss.Topics[t] = tss
}
}
pss.AppSpecificScore = ps.params.AppSpecificScore(p)
pss.IPColocationFactor = ps.ipColocationFactor(p)
pss.BehaviourPenalty = pstats.behaviourPenalty
scores[p] = pss
}
ps.Unlock()
go ps.inspectEx(scores)
}
// refreshScores decays scores, and purges score records for disconnected peers,
// once their expiry has elapsed.
func (ps *peerScore) refreshScores() {
ps.Lock()
defer ps.Unlock()
now := time.Now()
for p, pstats := range ps.peerStats {
if !pstats.connected {
// has the retention period expired?
if now.After(pstats.expire) {
// yes, throw it away (but clean up the IP tracking first)
ps.removeIPs(p, pstats.ips)
delete(ps.peerStats, p)
}
// we don't decay retained scores, as the peer is not active.
// this way the peer cannot reset a negative score by simply disconnecting and reconnecting,
// unless the retention period has ellapsed.
// similarly, a well behaved peer does not lose its score by getting disconnected.
continue
}
for topic, tstats := range pstats.topics {
// the topic parameters
topicParams, ok := ps.params.Topics[topic]
if !ok {
// we are not scoring this topic
continue
}
// decay counters
tstats.firstMessageDeliveries *= topicParams.FirstMessageDeliveriesDecay
if tstats.firstMessageDeliveries < ps.params.DecayToZero {
tstats.firstMessageDeliveries = 0
}
tstats.meshMessageDeliveries *= topicParams.MeshMessageDeliveriesDecay
if tstats.meshMessageDeliveries < ps.params.DecayToZero {
tstats.meshMessageDeliveries = 0
}
tstats.meshFailurePenalty *= topicParams.MeshFailurePenaltyDecay
if tstats.meshFailurePenalty < ps.params.DecayToZero {
tstats.meshFailurePenalty = 0
}
tstats.invalidMessageDeliveries *= topicParams.InvalidMessageDeliveriesDecay
if tstats.invalidMessageDeliveries < ps.params.DecayToZero {
tstats.invalidMessageDeliveries = 0
}
// update mesh time and activate mesh message delivery parameter if need be
if tstats.inMesh {
tstats.meshTime = now.Sub(tstats.graftTime)
if tstats.meshTime > topicParams.MeshMessageDeliveriesActivation {
tstats.meshMessageDeliveriesActive = true
}
}
}
// decay P7 counter
pstats.behaviourPenalty *= ps.params.BehaviourPenaltyDecay
if pstats.behaviourPenalty < ps.params.DecayToZero {
pstats.behaviourPenalty = 0
}
}
}
// refreshIPs refreshes IPs we know of peers we're tracking.
func (ps *peerScore) refreshIPs() {
ps.Lock()
defer ps.Unlock()
// peer IPs may change, so we periodically refresh them
//
// TODO: it could be more efficient to collect connections for all peers
// from the Network, populate a new map, and replace it in place. We are
// incurring in those allocs anyway, and maybe even in more, in the form of
// slices.
for p, pstats := range ps.peerStats {
if pstats.connected {
ips := ps.getIPs(p)
ps.setIPs(p, ips, pstats.ips)
pstats.ips = ips
}
}
}
func (ps *peerScore) gcDeliveryRecords() {
ps.Lock()
defer ps.Unlock()
ps.deliveries.gc()
}
// tracer interface
func (ps *peerScore) AddPeer(p peer.ID, proto protocol.ID) {
ps.Lock()
defer ps.Unlock()
pstats, ok := ps.peerStats[p]
if !ok {
pstats = &peerStats{topics: make(map[string]*topicStats)}
ps.peerStats[p] = pstats
}
pstats.connected = true
ips := ps.getIPs(p)
ps.setIPs(p, ips, pstats.ips)
pstats.ips = ips
}
func (ps *peerScore) RemovePeer(p peer.ID) {
ps.Lock()
defer ps.Unlock()
pstats, ok := ps.peerStats[p]
if !ok {
return
}
// decide whether to retain the score; this currently only retains non-positive scores
// to dissuade attacks on the score function.
if ps.score(p) > 0 {
ps.removeIPs(p, pstats.ips)
delete(ps.peerStats, p)
return
}
// furthermore, when we decide to retain the score, the firstMessageDelivery counters are
// reset to 0 and mesh delivery penalties applied.
for topic, tstats := range pstats.topics {
tstats.firstMessageDeliveries = 0
threshold := ps.params.Topics[topic].MeshMessageDeliveriesThreshold
if tstats.inMesh && tstats.meshMessageDeliveriesActive && tstats.meshMessageDeliveries < threshold {
deficit := threshold - tstats.meshMessageDeliveries
tstats.meshFailurePenalty += deficit * deficit
}
tstats.inMesh = false
}
pstats.connected = false
pstats.expire = time.Now().Add(ps.params.RetainScore)
}
func (ps *peerScore) Join(topic string) {}
func (ps *peerScore) Leave(topic string) {}
func (ps *peerScore) Graft(p peer.ID, topic string) {
ps.Lock()
defer ps.Unlock()
pstats, ok := ps.peerStats[p]
if !ok {
return
}
tstats, ok := pstats.getTopicStats(topic, ps.params)
if !ok {
return
}
tstats.inMesh = true
tstats.graftTime = time.Now()
tstats.meshTime = 0
tstats.meshMessageDeliveriesActive = false
}
func (ps *peerScore) Prune(p peer.ID, topic string) {
ps.Lock()
defer ps.Unlock()
pstats, ok := ps.peerStats[p]
if !ok {
return
}
tstats, ok := pstats.getTopicStats(topic, ps.params)
if !ok {
return
}
// sticky mesh delivery rate failure penalty
threshold := ps.params.Topics[topic].MeshMessageDeliveriesThreshold
if tstats.meshMessageDeliveriesActive && tstats.meshMessageDeliveries < threshold {
deficit := threshold - tstats.meshMessageDeliveries
tstats.meshFailurePenalty += deficit * deficit
}
tstats.inMesh = false
}
func (ps *peerScore) ValidateMessage(msg *Message) {
ps.Lock()
defer ps.Unlock()
// the pubsub subsystem is beginning validation; create a record to track time in
// the validation pipeline with an accurate firstSeen time.
_ = ps.deliveries.getRecord(ps.idGen.ID(msg))
}
func (ps *peerScore) DeliverMessage(msg *Message) {
ps.Lock()
defer ps.Unlock()
ps.markFirstMessageDelivery(msg.ReceivedFrom, msg)
drec := ps.deliveries.getRecord(ps.idGen.ID(msg))
// defensive check that this is the first delivery trace -- delivery status should be unknown
if drec.status != deliveryUnknown {
log.Debugf("unexpected delivery trace: message from %s was first seen %s ago and has delivery status %d", msg.ReceivedFrom, time.Since(drec.firstSeen), drec.status)
return
}
// mark the message as valid and reward mesh peers that have already forwarded it to us
drec.status = deliveryValid
drec.validated = time.Now()
for p := range drec.peers {
// this check is to make sure a peer can't send us a message twice and get a double count
// if it is a first delivery.
if p != msg.ReceivedFrom {
ps.markDuplicateMessageDelivery(p, msg, time.Time{})
}
}
}
func (ps *peerScore) RejectMessage(msg *Message, reason string) {
ps.Lock()
defer ps.Unlock()
switch reason {
// we don't track those messages, but we penalize the peer as they are clearly invalid
case RejectMissingSignature:
fallthrough
case RejectInvalidSignature:
fallthrough
case RejectUnexpectedSignature:
fallthrough
case RejectUnexpectedAuthInfo:
fallthrough
case RejectSelfOrigin:
ps.markInvalidMessageDelivery(msg.ReceivedFrom, msg)
return
// we ignore those messages, so do nothing.
case RejectBlacklstedPeer:
fallthrough
case RejectBlacklistedSource:
return
case RejectValidationQueueFull:
// the message was rejected before it entered the validation pipeline;
// we don't know if this message has a valid signature, and thus we also don't know if
// it has a valid message ID; all we can do is ignore it.
return
}
drec := ps.deliveries.getRecord(ps.idGen.ID(msg))
// defensive check that this is the first rejection trace -- delivery status should be unknown
if drec.status != deliveryUnknown {
log.Debugf("unexpected rejection trace: message from %s was first seen %s ago and has delivery status %d", msg.ReceivedFrom, time.Since(drec.firstSeen), drec.status)
return
}
switch reason {
case RejectValidationThrottled:
// if we reject with "validation throttled" we don't penalize the peer(s) that forward it
// because we don't know if it was valid.
drec.status = deliveryThrottled
// release the delivery time tracking map to free some memory early
drec.peers = nil
return
case RejectValidationIgnored:
// we were explicitly instructed by the validator to ignore the message but not penalize
// the peer
drec.status = deliveryIgnored
drec.peers = nil
return
}
// mark the message as invalid and penalize peers that have already forwarded it.
drec.status = deliveryInvalid
ps.markInvalidMessageDelivery(msg.ReceivedFrom, msg)
for p := range drec.peers {
ps.markInvalidMessageDelivery(p, msg)
}
// release the delivery time tracking map to free some memory early
drec.peers = nil
}
func (ps *peerScore) DuplicateMessage(msg *Message) {
ps.Lock()
defer ps.Unlock()
drec := ps.deliveries.getRecord(ps.idGen.ID(msg))
_, ok := drec.peers[msg.ReceivedFrom]
if ok {
// we have already seen this duplicate!
return
}
switch drec.status {
case deliveryUnknown:
// the message is being validated; track the peer delivery and wait for
// the Deliver/Reject notification.
drec.peers[msg.ReceivedFrom] = struct{}{}
case deliveryValid:
// mark the peer delivery time to only count a duplicate delivery once.
drec.peers[msg.ReceivedFrom] = struct{}{}
ps.markDuplicateMessageDelivery(msg.ReceivedFrom, msg, drec.validated)
case deliveryInvalid:
// we no longer track delivery time
ps.markInvalidMessageDelivery(msg.ReceivedFrom, msg)
case deliveryThrottled:
// the message was throttled; do nothing (we don't know if it was valid)
case deliveryIgnored:
// the message was ignored; do nothing
}
}
func (ps *peerScore) ThrottlePeer(p peer.ID) {}
func (ps *peerScore) RecvRPC(rpc *RPC) {}
func (ps *peerScore) SendRPC(rpc *RPC, p peer.ID) {}
func (ps *peerScore) DropRPC(rpc *RPC, p peer.ID) {}
func (ps *peerScore) UndeliverableMessage(msg *Message) {}
// message delivery records
func (d *messageDeliveries) getRecord(id string) *deliveryRecord {
rec, ok := d.records[id]
if ok {
return rec
}
now := time.Now()
rec = &deliveryRecord{peers: make(map[peer.ID]struct{}), firstSeen: now}
d.records[id] = rec
entry := &deliveryEntry{id: id, expire: now.Add(d.seenMsgTTL)}
if d.tail != nil {
d.tail.next = entry
d.tail = entry
} else {
d.head = entry
d.tail = entry
}
return rec
}
func (d *messageDeliveries) gc() {
if d.head == nil {
return
}
now := time.Now()
for d.head != nil && now.After(d.head.expire) {
delete(d.records, d.head.id)
d.head = d.head.next
}
if d.head == nil {
d.tail = nil
}
}
// getTopicStats returns existing topic stats for a given a given (peer, topic)
// tuple, or initialises a new topicStats object and inserts it in the
// peerStats, iff the topic is scored.
func (pstats *peerStats) getTopicStats(topic string, params *PeerScoreParams) (*topicStats, bool) {
tstats, ok := pstats.topics[topic]
if ok {
return tstats, true
}
_, scoredTopic := params.Topics[topic]
if !scoredTopic {
return nil, false
}
tstats = &topicStats{}
pstats.topics[topic] = tstats
return tstats, true
}
// markInvalidMessageDelivery increments the "invalid message deliveries"
// counter for all scored topics the message is published in.
func (ps *peerScore) markInvalidMessageDelivery(p peer.ID, msg *Message) {
pstats, ok := ps.peerStats[p]
if !ok {
return
}
topic := msg.GetTopic()
tstats, ok := pstats.getTopicStats(topic, ps.params)
if !ok {
return
}
tstats.invalidMessageDeliveries += 1
}
// markFirstMessageDelivery increments the "first message deliveries" counter
// for all scored topics the message is published in, as well as the "mesh
// message deliveries" counter, if the peer is in the mesh for the topic.
func (ps *peerScore) markFirstMessageDelivery(p peer.ID, msg *Message) {
pstats, ok := ps.peerStats[p]
if !ok {
return
}
topic := msg.GetTopic()
tstats, ok := pstats.getTopicStats(topic, ps.params)
if !ok {
return
}
cap := ps.params.Topics[topic].FirstMessageDeliveriesCap
tstats.firstMessageDeliveries += 1
if tstats.firstMessageDeliveries > cap {
tstats.firstMessageDeliveries = cap
}
if !tstats.inMesh {
return
}
cap = ps.params.Topics[topic].MeshMessageDeliveriesCap
tstats.meshMessageDeliveries += 1
if tstats.meshMessageDeliveries > cap {
tstats.meshMessageDeliveries = cap
}
}
// markDuplicateMessageDelivery increments the "mesh message deliveries" counter
// for messages we've seen before, as long the message was received within the
// P3 window.
func (ps *peerScore) markDuplicateMessageDelivery(p peer.ID, msg *Message, validated time.Time) {
pstats, ok := ps.peerStats[p]
if !ok {
return
}
topic := msg.GetTopic()
tstats, ok := pstats.getTopicStats(topic, ps.params)
if !ok {
return
}
if !tstats.inMesh {
return
}
tparams := ps.params.Topics[topic]
// check against the mesh delivery window -- if the validated time is passed as 0, then
// the message was received before we finished validation and thus falls within the mesh
// delivery window.
if !validated.IsZero() && time.Since(validated) > tparams.MeshMessageDeliveriesWindow {
return
}
cap := tparams.MeshMessageDeliveriesCap
tstats.meshMessageDeliveries += 1
if tstats.meshMessageDeliveries > cap {
tstats.meshMessageDeliveries = cap
}
}
// getIPs gets the current IPs for a peer.
func (ps *peerScore) getIPs(p peer.ID) []string {
// in unit tests this can be nil
if ps.host == nil {
return nil
}
conns := ps.host.Network().ConnsToPeer(p)
res := make([]string, 0, 1)
for _, c := range conns {
if c.Stat().Transient {
// ignore transient
continue
}
remote := c.RemoteMultiaddr()
ip, err := manet.ToIP(remote)
if err != nil {