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
crate::ix!();

#[derive(PartialEq,Eq)]
pub struct PeerMisbehavior {

    /**
      | Accumulated misbehavior score for
      | this peer
      | 
      |
      */
    pub score: i32,

    /**
      | Whether this peer should be disconnected
      | and marked as discouraged (unless it
      | has NetPermissionFlags::NoBan permission).
      | 
      |
      */
    pub should_discourage: bool,
}

impl Default for PeerMisbehavior {

    fn default() -> Self {
        Self {
            score: 0,
            should_discourage: false,
        }
    }
}

impl Misbehaving for PeerManager {

    fn misbehaving(&self,
        pnode:   NodeId,
        howmuch: i32,
        message: &str)  {
        
        assert!(howmuch > 0);

        let peer: Amo<Peer> = self.get_peer_ref(pnode);

        if peer.is_none() {
            return;
        }

        let peer = peer.get();

        let mut guard = peer.misbehavior.lock();

        let score_before: i32 = guard.score;

        guard.score += howmuch;

        let score_now: i32 = guard.score;

        let message_prefixed: String = match message.is_empty() {
            true   => "".to_string(),
            false  => format!(": {}", message)
        };

        let mut warning = String::default();

        if score_now >= DISCOURAGEMENT_THRESHOLD && score_before < DISCOURAGEMENT_THRESHOLD {

            warning = " DISCOURAGE THRESHOLD EXCEEDED".to_string();

            guard.should_discourage = true;
        }

        log_print!(
            LogFlags::NET, 
            "Misbehaving: peer=%d (%d -> %d)%s%s\n", 
            pnode, 
            score_before, 
            score_now, 
            warning, 
            message_prefixed
        );
    }
}