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
use std::{
    collections::HashMap,
    marker::PhantomData,
    time::{Duration, Instant},
};

use log::{debug, info, trace};

use crate::{
    network::{
        session::{Authentication, SessionHandler},
        AddressingInformation,
    },
    NodeIndex,
};

/// Handles creating and rebroadcasting discovery messages.
pub struct Discovery<A: AddressingInformation> {
    cooldown: Duration,
    last_broadcast: HashMap<NodeIndex, Instant>,
    _phantom: PhantomData<A>,
}

impl<A: AddressingInformation> Discovery<A> {
    /// Create a new discovery handler with the given response/broadcast cooldown.
    pub fn new(cooldown: Duration) -> Self {
        Discovery {
            cooldown,
            last_broadcast: HashMap::new(),
            _phantom: PhantomData,
        }
    }

    /// Returns a message that should be sent as part of authority discovery at this moment.
    pub fn discover_authorities(
        &mut self,
        handler: &SessionHandler<A>,
    ) -> Option<Authentication<A>> {
        let authentication = match handler.authentication() {
            Some(authentication) => authentication,
            None => return None,
        };

        let missing_authorities = handler.missing_nodes();
        let node_count = handler.node_count();
        info!(target: "phron-network", "{}/{} authorities known for session {}.", node_count.0-missing_authorities.len(), node_count.0, handler.session_id().0);
        Some(authentication)
    }

    fn should_rebroadcast(&self, node_id: &NodeIndex) -> bool {
        match self.last_broadcast.get(node_id) {
            Some(instant) => Instant::now() > *instant + self.cooldown,
            None => true,
        }
    }

    /// Processes the provided authentication and returns any new address we should
    /// be connected to if we want to stay connected to the committee and an optional
    /// message that we should send as a result of it.
    pub fn handle_authentication(
        &mut self,
        authentication: Authentication<A>,
        handler: &mut SessionHandler<A>,
    ) -> (Option<A>, Option<Authentication<A>>) {
        debug!(target: "phron-network", "Handling broadcast with authentication {:?}.", authentication);
        let address = match handler.handle_authentication(authentication.clone()) {
            Some(address) => Some(address),
            None => return (None, None),
        };
        let node_id = authentication.0.creator();
        if !self.should_rebroadcast(&node_id) {
            return (address, None);
        }
        trace!(target: "phron-network", "Rebroadcasting {:?}.", authentication);
        self.last_broadcast.insert(node_id, Instant::now());
        (address, Some(authentication))
    }
}

#[cfg(test)]
mod tests {
    use std::{thread::sleep, time::Duration};

    use network_clique::mock::{random_address, MockAddressingInformation};

    use super::Discovery;
    use crate::{
        network::{
            mock::crypto_basics,
            session::{authentication, Authentication, SessionHandler},
        },
        SessionId,
    };

    const NUM_NODES: u8 = 7;
    const MS_COOLDOWN: u64 = 200;

    fn addresses() -> Vec<MockAddressingInformation> {
        (0..NUM_NODES).map(|_| random_address()).collect()
    }

    fn build_number(
        num_nodes: u8,
    ) -> (
        Discovery<MockAddressingInformation>,
        Vec<SessionHandler<MockAddressingInformation>>,
        SessionHandler<MockAddressingInformation>,
    ) {
        let crypto_basics = crypto_basics(num_nodes.into());
        let mut handlers = Vec::new();
        for (authority_index_and_pen, address) in crypto_basics.0.into_iter().zip(addresses()) {
            handlers.push(SessionHandler::new(
                Some(authority_index_and_pen),
                crypto_basics.1.clone(),
                SessionId(43),
                address,
            ));
        }
        let non_validator =
            SessionHandler::new(None, crypto_basics.1, SessionId(43), random_address());
        (
            Discovery::new(Duration::from_millis(MS_COOLDOWN)),
            handlers,
            non_validator,
        )
    }

    fn build() -> (
        Discovery<MockAddressingInformation>,
        Vec<SessionHandler<MockAddressingInformation>>,
        SessionHandler<MockAddressingInformation>,
    ) {
        build_number(NUM_NODES)
    }

    #[test]
    fn broadcasts_when_clueless() {
        for num_nodes in 2..NUM_NODES {
            let (mut discovery, mut handlers, _) = build_number(num_nodes);
            let handler = &mut handlers[0];
            let maybe_authentication = discovery.discover_authorities(handler);
            assert_eq!(
                maybe_authentication.expect("there is an authentication"),
                handler
                    .authentication()
                    .expect("the handler has an authentication"),
            );
        }
    }

    #[test]
    fn non_validator_discover_authorities_returns_empty_vector() {
        let (mut discovery, _, non_validator) = build();
        let maybe_authentication = discovery.discover_authorities(&non_validator);
        assert!(maybe_authentication.is_none());
    }

    #[test]
    fn rebroadcasts_and_accepts_addresses() {
        let (mut discovery, mut handlers, _) = build();
        let authentication = authentication(&handlers[1]);
        let handler = &mut handlers[0];
        let (address, command) = discovery.handle_authentication(authentication.clone(), handler);
        assert_eq!(address, Some(authentication.0.address()));
        assert!(matches!(command, Some(
                rebroadcast_authentication,
            ) if rebroadcast_authentication == authentication));
    }

    #[test]
    fn non_validator_rebroadcasts() {
        let (mut discovery, handlers, mut non_validator) = build();
        let authentication = authentication(&handlers[1]);
        let (address, command) =
            discovery.handle_authentication(authentication.clone(), &mut non_validator);
        assert_eq!(address, Some(authentication.0.address()));
        assert!(matches!(command, Some(
                rebroadcast_authentication,
            ) if rebroadcast_authentication == authentication));
    }

    #[test]
    fn does_not_rebroadcast_wrong_authentications() {
        let (mut discovery, mut handlers, _) = build();
        let Authentication(auth_data, _) = authentication(&handlers[1]);
        let Authentication(_, signature) = authentication(&handlers[2]);
        let authentication = Authentication(auth_data, signature);
        let handler = &mut handlers[0];
        let (address, command) = discovery.handle_authentication(authentication, handler);
        assert!(address.is_none());
        assert!(command.is_none());
    }

    #[test]
    fn rebroadcasts_after_cooldown() {
        let (mut discovery, mut handlers, _) = build();
        let authentication = authentication(&handlers[1]);
        let handler = &mut handlers[0];
        discovery.handle_authentication(authentication.clone(), handler);
        sleep(Duration::from_millis(MS_COOLDOWN + 5));
        let (address, command) = discovery.handle_authentication(authentication.clone(), handler);
        assert_eq!(address, Some(authentication.0.address()));
        assert!(matches!(command, Some(
                rebroadcast_authentication,
            ) if rebroadcast_authentication == authentication));
    }
}