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

use crate::{network::PeerId, SessionId};

/// Keeps track of connections we should maintain taking into account data from many sessions.
pub struct Connections<PID: PeerId> {
    associated_sessions: HashMap<PID, HashSet<SessionId>>,
    peers_by_session: HashMap<SessionId, HashSet<PID>>,
}

impl<PID: PeerId> Connections<PID> {
    /// Creates a new object, initially without any connections.
    pub fn new() -> Self {
        Connections {
            associated_sessions: HashMap::new(),
            peers_by_session: HashMap::new(),
        }
    }

    /// Mark the specified peers as ones we should be connected to for the given session.
    pub fn add_peers(&mut self, session_id: SessionId, peers: impl IntoIterator<Item = PID>) {
        for peer in peers {
            self.associated_sessions
                .entry(peer.clone())
                .or_default()
                .insert(session_id);
            self.peers_by_session
                .entry(session_id)
                .or_default()
                .insert(peer);
        }
    }

    /// Assume we no longer need to be connected to peers from the given session.
    /// Returns the peers we no longer have any reason to be connected to.
    pub fn remove_session(&mut self, session_id: SessionId) -> HashSet<PID> {
        let mut result = HashSet::new();
        if let Some(peers) = self.peers_by_session.remove(&session_id) {
            for peer in peers {
                if let Some(mut sessions) = self.associated_sessions.remove(&peer) {
                    sessions.remove(&session_id);
                    if !sessions.is_empty() {
                        self.associated_sessions.insert(peer, sessions);
                    } else {
                        result.insert(peer);
                    }
                }
            }
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use network_clique::mock::{random_keys, MockPublicKey};

    use super::Connections;
    use crate::SessionId;

    fn random_peer_ids(num: usize) -> HashSet<MockPublicKey> {
        random_keys(num).into_keys().collect()
    }

    #[test]
    fn removes_peer_after_single_session() {
        let session_id = SessionId(43);
        let peer_ids = random_peer_ids(1);
        let mut connections = Connections::new();
        connections.add_peers(session_id, peer_ids.clone());
        let to_remove = connections.remove_session(session_id);
        assert_eq!(to_remove, peer_ids);
    }

    #[test]
    fn does_not_remove_peer_if_still_in_session() {
        let session_id = SessionId(43);
        let other_session_id = SessionId(2137);
        let peer_ids = random_peer_ids(1);
        let mut connections = Connections::new();
        connections.add_peers(session_id, peer_ids.clone());
        connections.add_peers(other_session_id, peer_ids);
        let to_remove = connections.remove_session(session_id);
        assert!(to_remove.is_empty());
    }

    #[test]
    fn removes_peer_only_after_all_sessions_pass() {
        let start = 43;
        let end = 50;
        let peer_ids = random_peer_ids(1);
        let mut connections = Connections::new();
        for i in start..end + 1 {
            connections.add_peers(SessionId(i), peer_ids.clone());
        }
        for i in start..end {
            assert!(connections.remove_session(SessionId(i)).is_empty());
        }
        let to_remove = connections.remove_session(SessionId(end));
        assert_eq!(to_remove, peer_ids);
    }
}