libp2p-gossipsub 0.49.4

Gossipsub protocol for libp2p
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
// Copyright 2020 Sigma Prime Pty Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

/// A collection of unit tests mostly ported from the go implementation.
use super::*;
use crate::{types::RawMessage, IdentTopic as Topic, Message};

// estimates a value within variance
fn within_variance(value: f64, expected: f64, variance: f64) -> bool {
    if expected >= 0.0 {
        return value > expected * (1.0 - variance) && value < expected * (1.0 + variance);
    }
    value > expected * (1.0 + variance) && value < expected * (1.0 - variance)
}

// generates a random gossipsub message with sequence number i
fn make_test_message(seq: u64) -> (MessageId, RawMessage) {
    let raw_message = RawMessage {
        source: Some(PeerId::random()),
        data: vec![12, 34, 56],
        sequence_number: Some(seq),
        topic: Topic::new("test").hash(),
        signature: None,
        key: None,
        validated: true,
    };

    let message = Message {
        source: raw_message.source,
        data: raw_message.data.clone(),
        sequence_number: raw_message.sequence_number,
        topic: raw_message.topic.clone(),
    };

    let id = default_message_id()(&message);
    (id, raw_message)
}

fn default_message_id() -> fn(&Message) -> MessageId {
    |message| {
        // default message id is: source + sequence number
        // NOTE: If either the peer_id or source is not provided, we set to 0;
        let mut source_string = if let Some(peer_id) = message.source.as_ref() {
            peer_id.to_base58()
        } else {
            PeerId::from_bytes(&[0, 1, 0])
                .expect("Valid peer id")
                .to_base58()
        };
        source_string.push_str(&message.sequence_number.unwrap_or_default().to_string());
        MessageId::from(source_string)
    }
}

#[test]
fn test_score_time_in_mesh() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams {
        topic_score_cap: 1000.0,
        ..Default::default()
    };

    let topic_params = TopicScoreParams {
        topic_weight: 0.5,
        time_in_mesh_weight: 1.0,
        time_in_mesh_quantum: Duration::from_millis(1),
        time_in_mesh_cap: 3600.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());

    let peer_id = PeerId::random();

    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());
    // Peer score should start at 0
    peer_score.add_peer(peer_id);

    let score = peer_score.score_report(&peer_id).score;
    assert!(
        score == 0.0,
        "expected score to start at zero. Score found: {score}"
    );

    // The time in mesh depends on how long the peer has been grafted
    peer_score.graft(&peer_id, topic);
    let elapsed = topic_params.time_in_mesh_quantum * 200;
    std::thread::sleep(elapsed);
    peer_score.refresh_scores();

    let score = peer_score.score_report(&peer_id).score;
    let expected = topic_params.topic_weight
        * topic_params.time_in_mesh_weight
        * (elapsed.as_millis() / topic_params.time_in_mesh_quantum.as_millis()) as f64;
    assert!(
        score >= expected,
        "The score: {score} should be greater than or equal to: {expected}"
    );
}

#[test]
fn test_score_time_in_mesh_cap() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        topic_weight: 0.5,
        time_in_mesh_weight: 1.0,
        time_in_mesh_quantum: Duration::from_millis(1),
        time_in_mesh_cap: 10.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());

    let peer_id = PeerId::random();

    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());
    // Peer score should start at 0
    peer_score.add_peer(peer_id);

    let score = peer_score.score_report(&peer_id).score;
    assert!(
        score == 0.0,
        "expected score to start at zero. Score found: {score}"
    );

    // The time in mesh depends on how long the peer has been grafted
    peer_score.graft(&peer_id, topic);
    let elapsed = topic_params.time_in_mesh_quantum * 40;
    std::thread::sleep(elapsed);
    peer_score.refresh_scores();

    let score = peer_score.score_report(&peer_id).score;
    let expected = topic_params.topic_weight
        * topic_params.time_in_mesh_weight
        * topic_params.time_in_mesh_cap;
    let variance = 0.5;
    assert!(
        within_variance(score, expected, variance),
        "The score: {} should be within {} of {}",
        score,
        score * variance,
        expected
    );
}

#[test]
fn test_score_first_message_deliveries() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        first_message_deliveries_weight: 1.0,
        first_message_deliveries_decay: 1.0,
        first_message_deliveries_cap: 2000.0,
        time_in_mesh_weight: 0.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());

    let peer_id = PeerId::random();

    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());
    // Peer score should start at 0
    peer_score.add_peer(peer_id);
    peer_score.graft(&peer_id, topic);

    // deliver a bunch of messages from the peer
    let messages = 100;
    for seq in 0..messages {
        let (id, msg) = make_test_message(seq);
        peer_score.validate_message(&peer_id, &id, &msg.topic);
        peer_score.deliver_message(&peer_id, &id, &msg.topic);
    }

    peer_score.refresh_scores();

    let score = peer_score.score_report(&peer_id).score;
    let expected =
        topic_params.topic_weight * topic_params.first_message_deliveries_weight * messages as f64;
    assert!(score == expected, "The score: {score} should be {expected}");
}

#[test]
fn test_score_first_message_deliveries_cap() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        first_message_deliveries_weight: 1.0,
        first_message_deliveries_decay: 1.0, // test without decay
        first_message_deliveries_cap: 50.0,
        time_in_mesh_weight: 0.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());

    let peer_id = PeerId::random();

    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());
    // Peer score should start at 0
    peer_score.add_peer(peer_id);
    peer_score.graft(&peer_id, topic);

    // deliver a bunch of messages from the peer
    let messages = 100;
    for seq in 0..messages {
        let (id, msg) = make_test_message(seq);
        peer_score.validate_message(&peer_id, &id, &msg.topic);
        peer_score.deliver_message(&peer_id, &id, &msg.topic);
    }

    peer_score.refresh_scores();
    let score = peer_score.score_report(&peer_id).score;
    let expected = topic_params.topic_weight
        * topic_params.first_message_deliveries_weight
        * topic_params.first_message_deliveries_cap;
    assert!(score == expected, "The score: {score} should be {expected}");
}

#[test]
fn test_score_first_message_deliveries_decay() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        first_message_deliveries_weight: 1.0,
        first_message_deliveries_decay: 0.9, // decay 10% per decay interval
        first_message_deliveries_cap: 2000.0,
        time_in_mesh_weight: 0.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());
    let peer_id = PeerId::random();
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());
    peer_score.add_peer(peer_id);
    peer_score.graft(&peer_id, topic);

    // deliver a bunch of messages from the peer
    let messages = 100;
    for seq in 0..messages {
        let (id, msg) = make_test_message(seq);
        peer_score.validate_message(&peer_id, &id, &msg.topic);
        peer_score.deliver_message(&peer_id, &id, &msg.topic);
    }

    peer_score.refresh_scores();
    let score = peer_score.score_report(&peer_id).score;
    let mut expected = topic_params.topic_weight
        * topic_params.first_message_deliveries_weight
        * topic_params.first_message_deliveries_decay
        * messages as f64;
    assert!(score == expected, "The score: {score} should be {expected}");

    // refreshing the scores applies the decay param
    let decay_intervals = 10;
    for _ in 0..decay_intervals {
        peer_score.refresh_scores();
        expected *= topic_params.first_message_deliveries_decay;
    }
    let score = peer_score.score_report(&peer_id).score;
    assert!(score == expected, "The score: {score} should be {expected}");
}

#[test]
fn test_score_mesh_message_deliveries() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        mesh_message_deliveries_weight: -1.0,
        mesh_message_deliveries_activation: Duration::from_secs(1),
        mesh_message_deliveries_window: Duration::from_millis(10),
        mesh_message_deliveries_threshold: 20.0,
        mesh_message_deliveries_cap: 100.0,
        mesh_message_deliveries_decay: 1.0,
        first_message_deliveries_weight: 0.0,
        time_in_mesh_weight: 0.0,
        mesh_failure_penalty_weight: 0.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    // peer A always delivers the message first.
    // peer B delivers next (within the delivery window).
    // peer C delivers outside the delivery window.
    // we expect peers A and B to have a score of zero, since all other parameter weights are zero.
    // Peer C should have a negative score.
    let peer_id_a = PeerId::random();
    let peer_id_b = PeerId::random();
    let peer_id_c = PeerId::random();

    let peers = vec![peer_id_a, peer_id_b, peer_id_c];

    for peer_id in &peers {
        peer_score.add_peer(*peer_id);
        peer_score.graft(peer_id, topic.clone());
    }

    // assert that nobody has been penalized yet for not delivering messages before activation time
    peer_score.refresh_scores();
    for peer_id in &peers {
        let score = peer_score.score_report(peer_id).score;
        assert!(
            score >= 0.0,
            "expected no mesh delivery penalty before activation time, got score {score}"
        );
    }

    // wait for the activation time to kick in
    std::thread::sleep(topic_params.mesh_message_deliveries_activation);

    // deliver a bunch of messages from peer A, with duplicates within the window from peer B,
    // and duplicates outside the window from peer C.
    let messages = 100;
    let mut messages_to_send = Vec::new();
    for seq in 0..messages {
        let (id, msg) = make_test_message(seq);
        peer_score.validate_message(&peer_id_a, &id, &msg.topic);
        peer_score.deliver_message(&peer_id_a, &id, &msg.topic);

        peer_score.duplicated_message(&peer_id_b, &id, &msg.topic);
        messages_to_send.push((id, msg));
    }

    std::thread::sleep(topic_params.mesh_message_deliveries_window + Duration::from_millis(20));

    for (id, msg) in messages_to_send {
        peer_score.duplicated_message(&peer_id_c, &id, &msg.topic);
    }

    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    let score_b = peer_score.score_report(&peer_id_b).score;
    let score_c = peer_score.score_report(&peer_id_c).score;

    assert!(
        score_a >= 0.0,
        "expected non-negative score for Peer A, got score {score_a}"
    );
    assert!(
        score_b >= 0.0,
        "expected non-negative score for Peer B, got score {score_b}"
    );

    // the penalty is the difference between the threshold and the actual mesh deliveries, squared.
    // since we didn't deliver anything, this is just the value of the threshold
    let penalty = topic_params.mesh_message_deliveries_threshold
        * topic_params.mesh_message_deliveries_threshold;
    let expected =
        topic_params.topic_weight * topic_params.mesh_message_deliveries_weight * penalty;

    assert!(score_c == expected, "Score: {score_c}. Expected {expected}");
}

#[test]
fn test_score_mesh_message_deliveries_decay() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        mesh_message_deliveries_weight: -1.0,
        mesh_message_deliveries_activation: Duration::from_secs(0),
        mesh_message_deliveries_window: Duration::from_millis(10),
        mesh_message_deliveries_threshold: 20.0,
        mesh_message_deliveries_cap: 100.0,
        mesh_message_deliveries_decay: 0.9,
        first_message_deliveries_weight: 0.0,
        time_in_mesh_weight: 0.0,
        time_in_mesh_quantum: Duration::from_secs(1),
        mesh_failure_penalty_weight: 0.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    let peer_id_a = PeerId::random();
    peer_score.add_peer(peer_id_a);
    peer_score.graft(&peer_id_a, topic);

    // deliver a bunch of messages from peer A
    let messages = 100;
    for seq in 0..messages {
        let (id, msg) = make_test_message(seq);
        peer_score.validate_message(&peer_id_a, &id, &msg.topic);
        peer_score.deliver_message(&peer_id_a, &id, &msg.topic);
    }

    // we should have a positive score, since we delivered more messages than the threshold
    peer_score.refresh_scores();

    let score_a = peer_score.score_report(&peer_id_a).score;
    assert!(
        score_a >= 0.0,
        "expected non-negative score for Peer A, got score {score_a}"
    );

    let mut decayed_delivery_count = (messages as f64) * topic_params.mesh_message_deliveries_decay;
    for _ in 0..20 {
        peer_score.refresh_scores();
        decayed_delivery_count *= topic_params.mesh_message_deliveries_decay;
    }

    let score_a = peer_score.score_report(&peer_id_a).score;
    // the penalty is the difference between the threshold and the (decayed)
    // mesh deliveries, squared.
    let deficit = topic_params.mesh_message_deliveries_threshold - decayed_delivery_count;
    let penalty = deficit * deficit;
    let expected =
        topic_params.topic_weight * topic_params.mesh_message_deliveries_weight * penalty;

    assert_eq!(score_a, expected, "Invalid score");
}

#[test]
fn test_score_mesh_failure_penalty() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        // the mesh failure penalty is applied when a peer is pruned while their
        // mesh deliveries are under the threshold.
        // for this test, we set the mesh delivery threshold, but set
        // mesh_message_deliveries to zero, so the only affect on the score
        // is from the mesh failure penalty
        topic_weight: 1.0,
        mesh_message_deliveries_weight: 0.0,
        mesh_message_deliveries_activation: Duration::from_secs(0),
        mesh_message_deliveries_window: Duration::from_millis(10),
        mesh_message_deliveries_threshold: 20.0,
        mesh_message_deliveries_cap: 100.0,
        mesh_message_deliveries_decay: 1.0,
        first_message_deliveries_weight: 0.0,
        time_in_mesh_weight: 0.0,
        time_in_mesh_quantum: Duration::from_secs(1),
        mesh_failure_penalty_weight: -1.0,
        mesh_failure_penalty_decay: 1.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    let peer_id_a = PeerId::random();
    let peer_id_b = PeerId::random();

    let peers = vec![peer_id_a, peer_id_b];

    for peer_id in &peers {
        peer_score.add_peer(*peer_id);
        peer_score.graft(peer_id, topic.clone());
    }

    // deliver a bunch of messages from peer A
    let messages = 100;
    for seq in 0..messages {
        let (id, msg) = make_test_message(seq);

        peer_score.validate_message(&peer_id_a, &id, &msg.topic);
        peer_score.deliver_message(&peer_id_a, &id, &msg.topic);
    }

    // peers A and B should both have zero scores, since the failure penalty hasn't been applied yet
    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    let score_b = peer_score.score_report(&peer_id_b).score;
    assert!(
        score_a >= 0.0,
        "expected non-negative score for Peer A, got score {score_a}"
    );
    assert!(
        score_b >= 0.0,
        "expected non-negative score for Peer B, got score {score_b}"
    );

    // prune peer B to apply the penalty
    peer_score.prune(&peer_id_b, topic.hash());
    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;

    assert_eq!(score_a, 0.0, "expected Peer A to have a 0");

    // penalty calculation is the same as for mesh_message_deliveries, but multiplied by
    // mesh_failure_penalty_weigh
    // instead of mesh_message_deliveries_weight
    let penalty = topic_params.mesh_message_deliveries_threshold
        * topic_params.mesh_message_deliveries_threshold;
    let expected = topic_params.topic_weight * topic_params.mesh_failure_penalty_weight * penalty;

    let score_b = peer_score.score_report(&peer_id_b).score;

    assert_eq!(score_b, expected, "Peer B should have expected score",);
}

#[test]
fn test_score_invalid_message_deliveries() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        mesh_message_deliveries_weight: 0.0,
        mesh_message_deliveries_activation: Duration::from_secs(1),
        mesh_message_deliveries_window: Duration::from_millis(10),
        mesh_message_deliveries_threshold: 20.0,
        mesh_message_deliveries_cap: 100.0,
        mesh_message_deliveries_decay: 1.0,
        first_message_deliveries_weight: 0.0,
        time_in_mesh_weight: 0.0,
        mesh_failure_penalty_weight: 0.0,
        invalid_message_deliveries_weight: -1.0,
        invalid_message_deliveries_decay: 1.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    let peer_id_a = PeerId::random();
    peer_score.add_peer(peer_id_a);
    peer_score.graft(&peer_id_a, topic);

    // reject a bunch of messages from peer A
    let messages = 100;
    for seq in 0..messages {
        let (id, msg) = make_test_message(seq);
        peer_score.reject_message(&peer_id_a, &id, &msg.topic, RejectReason::ValidationFailed);
    }

    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;

    let expected = topic_params.topic_weight
        * topic_params.invalid_message_deliveries_weight
        * (messages * messages) as f64;

    assert_eq!(score_a, expected, "Peer has unexpected score",);
}

#[test]
fn test_score_invalid_message_deliveris_decay() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        mesh_message_deliveries_weight: 0.0,
        mesh_message_deliveries_activation: Duration::from_secs(1),
        mesh_message_deliveries_window: Duration::from_millis(10),
        mesh_message_deliveries_threshold: 20.0,
        mesh_message_deliveries_cap: 100.0,
        mesh_message_deliveries_decay: 1.0,
        first_message_deliveries_weight: 0.0,
        time_in_mesh_weight: 0.0,
        mesh_failure_penalty_weight: 0.0,
        invalid_message_deliveries_weight: -1.0,
        invalid_message_deliveries_decay: 0.9,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params.clone());
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    let peer_id_a = PeerId::random();
    peer_score.add_peer(peer_id_a);
    peer_score.graft(&peer_id_a, topic);

    // reject a bunch of messages from peer A
    let messages = 100;
    for seq in 0..messages {
        let (id, msg) = make_test_message(seq);
        peer_score.reject_message(&peer_id_a, &id, &msg.topic, RejectReason::ValidationFailed);
    }

    peer_score.refresh_scores();

    let decay = topic_params.invalid_message_deliveries_decay * messages as f64;

    let mut expected =
        topic_params.topic_weight * topic_params.invalid_message_deliveries_weight * decay * decay;

    let score_a = peer_score.score_report(&peer_id_a).score;
    assert_eq!(score_a, expected, "Peer has unexpected score");

    // refresh scores a few times to apply decay
    for _ in 0..10 {
        peer_score.refresh_scores();
        expected *= topic_params.invalid_message_deliveries_decay
            * topic_params.invalid_message_deliveries_decay;
    }

    let score_a = peer_score.score_report(&peer_id_a).score;
    assert_eq!(score_a, expected, "Peer has unexpected score");
}

#[test]
fn test_score_reject_message_deliveries() {
    // This tests adds coverage for the dark corners of rejection tracing

    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams::default();

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        mesh_message_deliveries_weight: 0.0,
        first_message_deliveries_weight: 0.0,
        mesh_failure_penalty_weight: 0.0,
        time_in_mesh_weight: 0.0,
        time_in_mesh_quantum: Duration::from_secs(1),
        invalid_message_deliveries_weight: -1.0,
        invalid_message_deliveries_decay: 1.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params);
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    let peer_id_a = PeerId::random();
    let peer_id_b = PeerId::random();

    let peers = vec![peer_id_a, peer_id_b];

    for peer_id in &peers {
        peer_score.add_peer(*peer_id);
    }

    let (id, msg) = make_test_message(1);

    // these should have no effect in the score
    peer_score.reject_message(&peer_id_a, &id, &msg.topic, RejectReason::BlackListedPeer);
    peer_score.reject_message(&peer_id_a, &id, &msg.topic, RejectReason::BlackListedSource);
    peer_score.reject_message(&peer_id_a, &id, &msg.topic, RejectReason::ValidationIgnored);

    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    let score_b = peer_score.score_report(&peer_id_b).score;

    assert_eq!(score_a, 0.0, "Should have no effect on the score");
    assert_eq!(score_b, 0.0, "Should have no effect on the score");

    // insert a record in the message deliveries
    peer_score.validate_message(&peer_id_a, &id, &msg.topic);

    // this should have no effect in the score, and subsequent duplicate messages should have no
    // effect either
    peer_score.reject_message(&peer_id_a, &id, &msg.topic, RejectReason::ValidationIgnored);
    peer_score.duplicated_message(&peer_id_b, &id, &msg.topic);

    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    let score_b = peer_score.score_report(&peer_id_b).score;

    assert_eq!(score_a, 0.0, "Should have no effect on the score");
    assert_eq!(score_b, 0.0, "Should have no effect on the score");

    // now clear the delivery record
    peer_score.deliveries.clear();

    // insert a record in the message deliveries
    peer_score.validate_message(&peer_id_a, &id, &msg.topic);

    // this should have no effect in the score, and subsequent duplicate messages should have no
    // effect either
    peer_score.reject_message(&peer_id_a, &id, &msg.topic, RejectReason::ValidationIgnored);
    peer_score.duplicated_message(&peer_id_b, &id, &msg.topic);

    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    let score_b = peer_score.score_report(&peer_id_b).score;

    assert_eq!(score_a, 0.0, "Should have no effect on the score");
    assert_eq!(score_b, 0.0, "Should have no effect on the score");

    // now clear the delivery record
    peer_score.deliveries.clear();

    // insert a new record in the message deliveries
    peer_score.validate_message(&peer_id_a, &id, &msg.topic);

    // and reject the message to make sure duplicates are also penalized
    peer_score.reject_message(&peer_id_a, &id, &msg.topic, RejectReason::ValidationFailed);
    peer_score.duplicated_message(&peer_id_b, &id, &msg.topic);

    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    let score_b = peer_score.score_report(&peer_id_b).score;

    assert_eq!(score_a, -1.0, "Score should be effected");
    assert_eq!(score_b, -1.0, "Score should be effected");

    // now clear the delivery record again
    peer_score.deliveries.clear();

    // insert a new record in the message deliveries
    peer_score.validate_message(&peer_id_a, &id, &msg.topic);

    // and reject the message after a duplicate has arrived
    peer_score.duplicated_message(&peer_id_b, &id, &msg.topic);
    peer_score.reject_message(&peer_id_a, &id, &msg.topic, RejectReason::ValidationFailed);

    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    let score_b = peer_score.score_report(&peer_id_b).score;

    assert_eq!(score_a, -4.0, "Score should be effected");
    assert_eq!(score_b, -4.0, "Score should be effected");
}

#[test]
fn test_application_score() {
    // Create parameters with reasonable default values
    let app_specific_weight = 0.5;
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams {
        app_specific_weight,
        ..Default::default()
    };

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        mesh_message_deliveries_weight: 0.0,
        first_message_deliveries_weight: 0.0,
        mesh_failure_penalty_weight: 0.0,
        time_in_mesh_weight: 0.0,
        time_in_mesh_quantum: Duration::from_secs(1),
        invalid_message_deliveries_weight: 0.0,
        invalid_message_deliveries_decay: 1.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params);
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    let peer_id_a = PeerId::random();
    peer_score.add_peer(peer_id_a);
    peer_score.graft(&peer_id_a, topic);

    let messages = 100;
    for i in -100..messages {
        let app_score_value = i as f64;
        peer_score.set_application_score(&peer_id_a, app_score_value);
        peer_score.refresh_scores();
        let score_a = peer_score.score_report(&peer_id_a).score;
        let expected = (i as f64) * app_specific_weight;
        assert_eq!(score_a, expected, "Peer has unexpected score");
    }
}

#[test]
fn test_score_ip_colocation() {
    // Create parameters with reasonable default values
    let ip_colocation_factor_weight = -1.0;
    let ip_colocation_factor_threshold = 1.0;
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams {
        ip_colocation_factor_weight,
        ip_colocation_factor_threshold,
        ..Default::default()
    };

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        mesh_message_deliveries_weight: 0.0,
        first_message_deliveries_weight: 0.0,
        mesh_failure_penalty_weight: 0.0,
        time_in_mesh_weight: 0.0,
        time_in_mesh_quantum: Duration::from_secs(1),
        invalid_message_deliveries_weight: 0.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params);
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    let peer_id_a = PeerId::random();
    let peer_id_b = PeerId::random();
    let peer_id_c = PeerId::random();
    let peer_id_d = PeerId::random();

    let peers = vec![peer_id_a, peer_id_b, peer_id_c, peer_id_d];
    for peer_id in &peers {
        peer_score.add_peer(*peer_id);
        peer_score.graft(peer_id, topic.clone());
    }

    // peerA should have no penalty, but B, C, and D should be penalized for sharing an IP
    peer_score.add_ip(&peer_id_a, "1.2.3.4".parse().unwrap());
    peer_score.add_ip(&peer_id_b, "2.3.4.5".parse().unwrap());
    peer_score.add_ip(&peer_id_c, "2.3.4.5".parse().unwrap());
    peer_score.add_ip(&peer_id_c, "3.4.5.6".parse().unwrap());
    peer_score.add_ip(&peer_id_d, "2.3.4.5".parse().unwrap());

    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    let score_b = peer_score.score_report(&peer_id_b).score;
    let score_c = peer_score.score_report(&peer_id_c).score;
    let score_d = peer_score.score_report(&peer_id_d).score;

    assert_eq!(score_a, 0.0, "Peer A should be unaffected");

    let n_shared = 3.0;
    let ip_surplus = n_shared - ip_colocation_factor_threshold;
    let penalty = ip_surplus * ip_surplus;
    let expected = ip_colocation_factor_weight * penalty;

    assert_eq!(score_b, expected, "Peer B should have expected score");
    assert_eq!(score_c, expected, "Peer C should have expected score");
    assert_eq!(score_d, expected, "Peer D should have expected score");
}

#[test]
fn test_score_behaviour_penalty() {
    // Create parameters with reasonable default values
    let behaviour_penalty_weight = -1.0;
    let behaviour_penalty_decay = 0.99;

    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let mut params = PeerScoreParams {
        behaviour_penalty_decay,
        behaviour_penalty_weight,
        ..Default::default()
    };

    let topic_params = TopicScoreParams {
        topic_weight: 1.0,
        mesh_message_deliveries_weight: 0.0,
        first_message_deliveries_weight: 0.0,
        mesh_failure_penalty_weight: 0.0,
        time_in_mesh_weight: 0.0,
        time_in_mesh_quantum: Duration::from_secs(1),
        invalid_message_deliveries_weight: 0.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params);
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    let peer_id_a = PeerId::random();

    // add a penalty to a non-existent peer.
    peer_score.add_penalty(&peer_id_a, 1);

    let score_a = peer_score.score_report(&peer_id_a).score;
    assert_eq!(score_a, 0.0, "Peer A should be unaffected");

    // add the peer and test penalties
    peer_score.add_peer(peer_id_a);
    assert_eq!(score_a, 0.0, "Peer A should be unaffected");

    peer_score.add_penalty(&peer_id_a, 1);

    let score_a = peer_score.score_report(&peer_id_a).score;
    assert_eq!(score_a, -1.0, "Peer A should have been penalized");

    peer_score.add_penalty(&peer_id_a, 1);
    let score_a = peer_score.score_report(&peer_id_a).score;
    assert_eq!(score_a, -4.0, "Peer A should have been penalized");

    peer_score.refresh_scores();

    let score_a = peer_score.score_report(&peer_id_a).score;
    assert_eq!(score_a, -3.9204, "Peer A should have been penalized");
}

#[test]
fn test_score_retention() {
    // Create parameters with reasonable default values
    let topic = Topic::new("test");
    let topic_hash = topic.hash();
    let app_specific_weight = 1.0;
    let app_score_value = -1000.0;
    let retain_score = Duration::from_secs(1);
    let mut params = PeerScoreParams {
        app_specific_weight,
        retain_score,
        ..Default::default()
    };

    let topic_params = TopicScoreParams {
        topic_weight: 0.0,
        mesh_message_deliveries_weight: 0.0,
        mesh_message_deliveries_activation: Duration::from_secs(0),
        first_message_deliveries_weight: 0.0,
        time_in_mesh_weight: 0.0,
        ..Default::default()
    };

    params.topics.insert(topic_hash, topic_params);
    let mut peer_score = PeerScore::new(params, PeerScoreThresholds::default());

    let peer_id_a = PeerId::random();
    peer_score.add_peer(peer_id_a);
    peer_score.graft(&peer_id_a, topic);

    peer_score.set_application_score(&peer_id_a, app_score_value);

    // score should equal -1000 (app specific score)
    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    assert_eq!(
        score_a, app_score_value,
        "Score should be the application specific score"
    );

    // disconnect & wait half of RetainScore time. Should still have negative score
    peer_score.remove_peer(&peer_id_a);
    std::thread::sleep(retain_score / 2);
    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    assert_eq!(
        score_a, app_score_value,
        "Score should be the application specific score"
    );

    // wait remaining time (plus a little slop) and the score should reset to zero
    std::thread::sleep(retain_score / 2 + Duration::from_millis(50));
    peer_score.refresh_scores();
    let score_a = peer_score.score_report(&peer_id_a).score;
    assert_eq!(
        score_a, 0.0,
        "Score should be the application specific score"
    );
}