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
use std::{
    collections::{HashMap, HashSet},
    ops::BitXor,
};

use crate::{
    metrics::{Event, Metrics},
    Data, PublicKey,
};

/// Data about peers we know and whether we should connect to them or they to us. For the former
/// case also keeps the peers' addresses.
pub struct DirectedPeers<PK: PublicKey, A: Data> {
    own_id: PK,
    outgoing: HashMap<PK, A>,
    incoming: HashSet<PK>,
    metrics: Metrics,
}

/// Whether we should call the remote or the other way around. We xor the peer ids and based on the
/// parity of the sum of bits of the result decide whether the caller should be the smaller or
/// greated lexicographically. They are never equal, because cryptography.
fn should_we_call(own_id: &[u8], remote_id: &[u8]) -> bool {
    let xor_sum_parity = (own_id.iter().fold(0u8, BitXor::bitxor)
        ^ remote_id.iter().fold(0u8, BitXor::bitxor))
    .count_ones()
        % 2;
    match xor_sum_parity == 0 {
        true => own_id < remote_id,
        false => own_id > remote_id,
    }
}

impl<PK: PublicKey, A: Data> DirectedPeers<PK, A> {
    /// Create a new set of peers directed using our own peer id.
    pub fn new(own_id: PK, metrics: Metrics) -> Self {
        DirectedPeers {
            own_id,
            outgoing: HashMap::new(),
            incoming: HashSet::new(),
            metrics,
        }
    }

    /// Add a peer to the list of peers we want to stay connected to, or
    /// update the address if the peer was already added.
    /// Returns whether we should start attempts at connecting with the peer, which is the case
    /// exactly when the peer is one with which we should attempt connections AND it was added for
    /// the first time.
    pub fn add_peer(&mut self, peer_id: PK, address: A) -> bool {
        use Event::*;
        match should_we_call(self.own_id.as_ref(), peer_id.as_ref()) {
            true => match self.outgoing.insert(peer_id, address).is_none() {
                true => {
                    self.metrics.report_event(NewOutgoing);
                    true
                }
                false => false,
            },
            false => {
                // We discard the address here, as we will never want to call this peer anyway,
                // so we don't need it.
                if self.incoming.insert(peer_id) {
                    self.metrics.report_event(NewIncoming);
                }
                false
            }
        }
    }

    /// Return the address of the given peer, or None if we shouldn't attempt connecting with the peer.
    pub fn peer_address(&self, peer_id: &PK) -> Option<A> {
        self.outgoing.get(peer_id).cloned()
    }

    /// Whether we should be maintaining a connection with this peer.
    pub fn interested(&self, peer_id: &PK) -> bool {
        self.incoming.contains(peer_id) || self.outgoing.contains_key(peer_id)
    }

    /// Iterator over the peers we want connections from.
    pub fn incoming_peers(&self) -> impl Iterator<Item = &PK> {
        self.incoming.iter()
    }

    /// Iterator over the peers we want to connect to.
    pub fn outgoing_peers(&self) -> impl Iterator<Item = &PK> {
        self.outgoing.keys()
    }

    /// Remove a peer from the list of peers that we want to stay connected with, whether the
    /// connection was supposed to be incoming or outgoing.
    pub fn remove_peer(&mut self, peer_id: &PK) {
        use Event::*;
        if self.incoming.remove(peer_id) {
            self.metrics.report_event(DelIncoming);
        }
        if self.outgoing.remove(peer_id).is_some() {
            self.metrics.report_event(DelOutgoing);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::DirectedPeers;
    use crate::{
        metrics::Metrics,
        mock::{key, MockPublicKey},
    };

    type Address = String;

    fn container_with_id() -> (DirectedPeers<MockPublicKey, Address>, MockPublicKey) {
        let (id, _) = key();
        let container = DirectedPeers::new(id.clone(), Metrics::noop());
        (container, id)
    }

    fn some_address() -> Address {
        String::from("43.43.43.43:43000")
    }

    #[test]
    fn exactly_one_direction_attempts_connections() {
        let (mut container0, id0) = container_with_id();
        let (mut container1, id1) = container_with_id();
        let address = some_address();
        assert!(container0.add_peer(id1, address.clone()) != container1.add_peer(id0, address));
    }

    fn container_with_added_connecting_peer(
    ) -> (DirectedPeers<MockPublicKey, Address>, MockPublicKey) {
        let (mut container0, id0) = container_with_id();
        let (mut container1, id1) = container_with_id();
        let address = some_address();
        match container0.add_peer(id1.clone(), address.clone()) {
            true => (container0, id1),
            false => {
                container1.add_peer(id0.clone(), address);
                (container1, id0)
            }
        }
    }

    fn container_with_added_nonconnecting_peer(
    ) -> (DirectedPeers<MockPublicKey, Address>, MockPublicKey) {
        let (mut container0, id0) = container_with_id();
        let (mut container1, id1) = container_with_id();
        let address = some_address();
        match container0.add_peer(id1.clone(), address.clone()) {
            false => (container0, id1),
            true => {
                container1.add_peer(id0.clone(), address);
                (container1, id0)
            }
        }
    }

    #[test]
    fn no_connecting_on_subsequent_add() {
        let (mut container0, id1) = container_with_added_connecting_peer();
        let address = some_address();
        assert!(!container0.add_peer(id1, address));
    }

    #[test]
    fn peer_address_when_connecting() {
        let (container0, id1) = container_with_added_connecting_peer();
        assert!(container0.peer_address(&id1).is_some());
    }

    #[test]
    fn no_peer_address_when_nonconnecting() {
        let (container0, id1) = container_with_added_nonconnecting_peer();
        assert!(container0.peer_address(&id1).is_none());
    }

    #[test]
    fn interested_in_connecting() {
        let (container0, id1) = container_with_added_connecting_peer();
        assert!(container0.interested(&id1));
    }

    #[test]
    fn interested_in_nonconnecting() {
        let (container0, id1) = container_with_added_nonconnecting_peer();
        assert!(container0.interested(&id1));
    }

    #[test]
    fn uninterested_in_unknown() {
        let (container0, _) = container_with_id();
        let (_, id1) = container_with_id();
        assert!(!container0.interested(&id1));
    }

    #[test]
    fn connecting_are_outgoing() {
        let (container0, id1) = container_with_added_connecting_peer();
        assert_eq!(container0.outgoing_peers().collect::<Vec<_>>(), vec![&id1]);
        assert_eq!(container0.incoming_peers().next(), None);
    }

    #[test]
    fn nonconnecting_are_incoming() {
        let (container0, id1) = container_with_added_nonconnecting_peer();
        assert_eq!(container0.incoming_peers().collect::<Vec<_>>(), vec![&id1]);
        assert_eq!(container0.outgoing_peers().next(), None);
    }

    #[test]
    fn uninterested_in_removed() {
        let (mut container0, id1) = container_with_added_connecting_peer();
        assert!(container0.interested(&id1));
        container0.remove_peer(&id1);
        assert!(!container0.interested(&id1));
    }
}