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

use futures::channel::mpsc;
use log::{debug, info};

use crate::{
    abft::Recipient,
    crypto::{AuthorityPen, AuthorityVerifier},
    network::{
        address_cache::{ValidatorAddressCacheUpdater, ValidatorAddressingInfo},
        session::{
            data::DataInSession, Authentication, Connections, Discovery, DiscoveryMessage,
            SessionHandler, SessionHandlerError,
        },
        AddressingInformation, Data, NetworkIdentity, PeerId,
    },
    NodeIndex, SessionId,
};

/// Commands for manipulating the reserved peers set.
#[derive(Debug, PartialEq, Eq)]
pub enum ConnectionCommand<A: AddressingInformation> {
    AddReserved(HashSet<A>),
    DelReserved(HashSet<A::PeerId>),
}

// In practice D: Data and P: PeerId, but we cannot require that in type aliases.
pub type AddressedData<D, P> = (D, P);

struct Session<D: Data, A: AddressingInformation> {
    handler: SessionHandler<A>,
    discovery: Discovery<A>,
    data_for_user: Option<mpsc::UnboundedSender<D>>,
}

#[derive(Clone)]
/// Stores all data needed for starting validator session
pub struct PreValidatorSession {
    pub session_id: SessionId,
    pub verifier: AuthorityVerifier,
    pub node_id: NodeIndex,
    pub pen: AuthorityPen,
}

#[derive(Clone)]
/// Stores all data needed for starting non-validator session
pub struct PreNonvalidatorSession {
    pub session_id: SessionId,
    pub verifier: AuthorityVerifier,
}

/// Actions that the manager wants to take as the result of some information. Might contain a
/// command for connecting to or disconnecting from some peers or a message to broadcast for
/// discovery  purposes.
pub struct ManagerActions<A: AddressingInformation> {
    pub maybe_command: Option<ConnectionCommand<A>>,
    pub maybe_message: Option<Authentication<A>>,
}

impl<A: AddressingInformation> ManagerActions<A> {
    fn noop() -> Self {
        ManagerActions {
            maybe_command: None,
            maybe_message: None,
        }
    }
}

/// The connection manager. It handles the abstraction over the network we build to support
/// separate sessions. This includes:
/// 1. Starting and ending specific sessions on user demand.
/// 2. Forwarding in-session user messages to the network using session handlers for address
///    translation.
/// 3. Handling network messages:
///    1. In-session messages are forwarded to the user.
///    2. Authentication messages forwarded to session handlers.
/// 4. Running periodic maintenance, mostly related to node discovery.
pub struct Manager<NI: NetworkIdentity, D: Data, VCU: ValidatorAddressCacheUpdater> {
    network_identity: NI,
    connections: Connections<NI::PeerId>,
    sessions: HashMap<SessionId, Session<D, NI::AddressingInformation>>,
    validator_address_cache_updater: VCU,
    discovery_cooldown: Duration,
}

/// Error when trying to forward data from the network to the user, should never be fatal.
#[derive(Debug, PartialEq, Eq)]
pub enum SendError {
    UserSend,
    NoSession,
}

impl<NI: NetworkIdentity, D: Data, VCU: ValidatorAddressCacheUpdater> Manager<NI, D, VCU> {
    /// Create a new connection manager.
    pub fn new(
        network_identity: NI,
        validator_address_cache_updater: VCU,
        discovery_cooldown: Duration,
    ) -> Self {
        Manager {
            network_identity,
            connections: Connections::new(),
            sessions: HashMap::new(),
            validator_address_cache_updater,
            discovery_cooldown,
        }
    }

    fn delete_reserved(
        to_remove: HashSet<NI::PeerId>,
    ) -> Option<ConnectionCommand<NI::AddressingInformation>> {
        match to_remove.is_empty() {
            true => None,
            false => Some(ConnectionCommand::DelReserved(to_remove)),
        }
    }

    /// Ends a session.
    pub fn finish_session(
        &mut self,
        session_id: SessionId,
    ) -> ManagerActions<NI::AddressingInformation> {
        self.sessions.remove(&session_id);
        ManagerActions {
            maybe_command: Self::delete_reserved(self.connections.remove_session(session_id)),
            maybe_message: None,
        }
    }

    fn discover_authorities(
        &mut self,
        session_id: &SessionId,
    ) -> Option<Authentication<NI::AddressingInformation>> {
        self.sessions.get_mut(session_id).and_then(
            |Session {
                 handler, discovery, ..
             }| { discovery.discover_authorities(handler) },
        )
    }

    /// Returns all the network messages that should be sent as part of discovery at this moment.
    pub fn discovery(&mut self) -> Vec<Authentication<NI::AddressingInformation>> {
        let sessions: Vec<_> = self.sessions.keys().cloned().collect();
        sessions
            .iter()
            .flat_map(|session_id| self.discover_authorities(session_id))
            .collect()
    }

    fn start_validator_session(
        &mut self,
        pre_session: PreValidatorSession,
        address: NI::AddressingInformation,
    ) -> (
        Option<Authentication<NI::AddressingInformation>>,
        mpsc::UnboundedReceiver<D>,
    ) {
        let PreValidatorSession {
            session_id,
            verifier,
            node_id,
            pen,
        } = pre_session;
        let handler = SessionHandler::new(Some((node_id, pen)), verifier, session_id, address);
        let discovery = Discovery::new(self.discovery_cooldown);
        let (data_for_user, data_from_network) = mpsc::unbounded();
        let data_for_user = Some(data_for_user);
        self.sessions.insert(
            session_id,
            Session {
                handler,
                discovery,
                data_for_user,
            },
        );
        (self.discover_authorities(&session_id), data_from_network)
    }

    /// Starts or updates a validator session.
    pub fn update_validator_session(
        &mut self,
        pre_session: PreValidatorSession,
    ) -> Result<
        (
            ManagerActions<NI::AddressingInformation>,
            mpsc::UnboundedReceiver<D>,
        ),
        SessionHandlerError,
    > {
        let address = self.network_identity.identity();
        println!("address: {:?}", address);
        println!("pre_session: {:?}", pre_session.session_id);
        let session = match self.sessions.get_mut(&pre_session.session_id) {
            Some(session) => session,
            None => {
                let (maybe_message, data_from_network) =
                    self.start_validator_session(pre_session, address);
                return Ok((
                    ManagerActions {
                        maybe_command: None,
                        maybe_message,
                    },
                    data_from_network,
                ));
            }
        };
        let PreValidatorSession {
            session_id,
            verifier,
            node_id,
            pen,
        } = pre_session;
        self.validator_address_cache_updater.update(
            node_id,
            ValidatorAddressingInfo {
                session: session_id,
                network_level_address: address.address(),
                validator_network_peer_id: address.peer_id().to_string(),
            },
        );

        let peers_to_stay = session
            .handler
            .update(Some((node_id, pen)), verifier, address)?
            .iter()
            .map(|address| address.peer_id())
            .collect();
        let maybe_command = Self::delete_reserved(
            self.connections
                .remove_session(session_id)
                .difference(&peers_to_stay)
                .cloned()
                .collect(),
        );
        let (data_for_user, data_from_network) = mpsc::unbounded();
        session.data_for_user = Some(data_for_user);
        self.connections.add_peers(session_id, peers_to_stay);
        Ok((
            ManagerActions {
                maybe_command,
                maybe_message: self.discover_authorities(&session_id),
            },
            data_from_network,
        ))
    }

    fn start_nonvalidator_session(
        &mut self,
        pre_session: PreNonvalidatorSession,
        address: NI::AddressingInformation,
    ) {
        let PreNonvalidatorSession {
            session_id,
            verifier,
        } = pre_session;
        let handler = SessionHandler::new(None, verifier, session_id, address);
        let discovery = Discovery::new(self.discovery_cooldown);
        self.sessions.insert(
            session_id,
            Session {
                handler,
                discovery,
                data_for_user: None,
            },
        );
    }

    /// Starts or updates a nonvalidator session.
    pub fn update_nonvalidator_session(
        &mut self,
        pre_session: PreNonvalidatorSession,
    ) -> Result<ManagerActions<NI::AddressingInformation>, SessionHandlerError> {
        let address = self.network_identity.identity();
        match self.sessions.get_mut(&pre_session.session_id) {
            Some(session) => {
                session
                    .handler
                    .update(None, pre_session.verifier, address)?;
            }
            None => {
                self.start_nonvalidator_session(pre_session, address);
            }
        };
        Ok(ManagerActions::noop())
    }

    /// Handle a user request for sending data.
    /// Returns a list of data to be sent over the network.
    pub fn on_user_message(
        &self,
        data: D,
        session_id: SessionId,
        recipient: Recipient,
    ) -> Vec<AddressedData<DataInSession<D>, NI::PeerId>> {
        if let Some(handler) = self
            .sessions
            .get(&session_id)
            .map(|session| &session.handler)
        {
            let to_send = DataInSession { data, session_id };
            match recipient {
                Recipient::Everyone => (0..handler.node_count().0)
                    .map(NodeIndex)
                    .flat_map(|node_id| handler.peer_id(&node_id))
                    .map(|peer_id| (to_send.clone(), peer_id))
                    .collect(),
                Recipient::Node(node_id) => handler
                    .peer_id(&node_id)
                    .into_iter()
                    .map(|peer_id| (to_send.clone(), peer_id))
                    .collect(),
            }
        } else {
            Vec::new()
        }
    }

    /// Handle a discovery message.
    /// Returns actions the manager wants to take.
    pub fn on_discovery_message(
        &mut self,
        message: DiscoveryMessage<NI::AddressingInformation>,
    ) -> ManagerActions<NI::AddressingInformation> {
        let session_id = message.session_id();
        let creator = message.0.creator();
        match self.sessions.get_mut(&session_id) {
            Some(Session {
                handler, discovery, ..
            }) => {
                let (maybe_address, maybe_message) =
                    discovery.handle_authentication(message, handler);
                let mut maybe_command = None;
                if let Some(address) = maybe_address {
                    self.validator_address_cache_updater.update(
                        creator,
                        ValidatorAddressingInfo {
                            session: session_id,
                            network_level_address: address.address(),
                            validator_network_peer_id: address.peer_id().to_string(),
                        },
                    );
                    if handler.is_validator() {
                        debug!(target: "phron-network", "Adding addresses for session {:?} to reserved: {:?}", session_id, address);
                        self.connections.add_peers(session_id, [address.peer_id()]);
                        maybe_command = Some(ConnectionCommand::AddReserved([address].into()));
                    }
                }
                ManagerActions {
                    maybe_command,
                    maybe_message,
                }
            }
            None => {
                debug!(target: "phron-network", "Received message from unknown session: {:?}", message);
                ManagerActions::noop()
            }
        }
    }

    /// Sends the data to the identified session.
    pub fn send_session_data(&self, session_id: &SessionId, data: D) -> Result<(), SendError> {
        match self
            .sessions
            .get(session_id)
            .and_then(|session| session.data_for_user.as_ref())
        {
            Some(data_for_user) => data_for_user
                .unbounded_send(data)
                .map_err(|_| SendError::UserSend),
            None => Err(SendError::NoSession),
        }
    }

    pub fn status_report(&self) {
        let mut status = String::from("Connection Manager status report: ");

        let mut authenticated: Vec<_> = self
            .sessions
            .iter()
            .filter(|(_, session)| session.handler.authentication().is_some())
            .map(|(session_id, session)| {
                let mut peers = session
                    .handler
                    .peers()
                    .into_iter()
                    .map(|(node_id, peer_id)| (node_id.0, peer_id))
                    .collect::<Vec<_>>();
                peers.sort_by(|x, y| x.0.cmp(&y.0));
                (session_id.0, session.handler.node_count().0, peers)
            })
            .collect();
        authenticated.sort_by(|x, y| x.0.cmp(&y.0));
        if !authenticated.is_empty() {
            let authenticated_status = authenticated
                .iter()
                .map(|(session_id, node_count, peers)| {
                    let peer_ids = peers
                        .iter()
                        .map(|(node_id, peer_id)| {
                            format!("{:?}: {}", node_id, peer_id.to_short_string())
                        })
                        .collect::<Vec<_>>()
                        .join(", ");

                    format!(
                        "{:?}: {}/{} {{{}}}",
                        session_id,
                        peers.len() + 1,
                        node_count,
                        peer_ids
                    )
                })
                .collect::<Vec<_>>()
                .join(", ");
            status.push_str(&format!(
                "authenticated authorities: {authenticated_status}; "
            ));
        }

        let mut missing: Vec<_> = self
            .sessions
            .iter()
            .filter(|(_, session)| session.handler.authentication().is_some())
            .map(|(session_id, session)| {
                (
                    session_id.0,
                    session
                        .handler
                        .missing_nodes()
                        .iter()
                        .map(|id| id.0)
                        .collect::<Vec<_>>(),
                )
            })
            .filter(|(_, missing)| !missing.is_empty())
            .collect();
        missing.sort_by(|x, y| x.0.cmp(&y.0));
        if !missing.is_empty() {
            let missing_status = missing
                .iter()
                .map(|(session_id, missing)| format!("{session_id:?}: {missing:?}"))
                .collect::<Vec<_>>()
                .join(", ");
            status.push_str(&format!("missing authorities: {missing_status}; "));
        }

        if !authenticated.is_empty() || !missing.is_empty() {
            info!(target: "phron-network", "{}", status);
        }
    }
}

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

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

    use super::{
        ConnectionCommand, Manager, ManagerActions, PreNonvalidatorSession, PreValidatorSession,
        SendError,
    };
    use crate::{
        network::{
            address_cache::{test::noop_updater, ValidatorAddressCacheUpdater},
            mock::crypto_basics,
            session::data::DataInSession,
        },
        Recipient, SessionId,
    };

    const NUM_NODES: usize = 7;
    const DISCOVERY_PERIOD: Duration = Duration::from_secs(60);

    fn build() -> Manager<MockAddressingInformation, i32, impl ValidatorAddressCacheUpdater> {
        Manager::new(random_address(), noop_updater(), DISCOVERY_PERIOD)
    }

    #[test]
    fn starts_nonvalidator_session() {
        let mut manager = build();
        let (_, verifier) = crypto_basics(NUM_NODES);
        let session_id = SessionId(43);
        let ManagerActions {
            maybe_command,
            maybe_message,
        } = manager
            .update_nonvalidator_session(PreNonvalidatorSession {
                session_id,
                verifier,
            })
            .unwrap();
        assert!(maybe_command.is_none());
        assert!(maybe_message.is_none());
        assert_eq!(
            manager.send_session_data(&session_id, -43),
            Err(SendError::NoSession)
        );
    }

    #[test]
    fn starts_validator_session() {
        let mut manager = build();
        let (validator_data, verifier) = crypto_basics(NUM_NODES);
        let (node_id, pen) = validator_data[0].clone();
        let session_id = SessionId(43);
        let (
            ManagerActions {
                maybe_command,
                maybe_message,
            },
            _data_from_network,
        ) = manager
            .update_validator_session(PreValidatorSession {
                session_id,
                verifier,
                node_id,
                pen,
            })
            .unwrap();
        assert!(maybe_command.is_none());
        assert!(maybe_message.is_some());
        assert_eq!(manager.send_session_data(&session_id, -43), Ok(()));
    }

    #[tokio::test]
    async fn stops_session() {
        let mut manager = build();
        let (validator_data, verifier) = crypto_basics(NUM_NODES);
        let (node_id, pen) = validator_data[0].clone();
        let session_id = SessionId(43);
        let (
            ManagerActions {
                maybe_command,
                maybe_message,
            },
            mut data_from_network,
        ) = manager
            .update_validator_session(PreValidatorSession {
                session_id,
                verifier,
                node_id,
                pen,
            })
            .unwrap();
        assert!(maybe_command.is_none());
        assert!(maybe_message.is_some());
        assert_eq!(manager.send_session_data(&session_id, -43), Ok(()));
        assert_eq!(data_from_network.next().await, Some(-43));
        let ManagerActions {
            maybe_command,
            maybe_message,
        } = manager.finish_session(session_id);
        assert!(maybe_command.is_none());
        assert!(maybe_message.is_none());
        assert_eq!(
            manager.send_session_data(&session_id, -43),
            Err(SendError::NoSession)
        );
        assert!(data_from_network.next().await.is_none());
    }

    #[test]
    fn handles_broadcast() {
        let mut manager = build();
        let (validator_data, verifier) = crypto_basics(NUM_NODES);
        let (node_id, pen) = validator_data[0].clone();
        let session_id = SessionId(43);
        manager
            .update_validator_session(PreValidatorSession {
                session_id,
                verifier: verifier.clone(),
                node_id,
                pen,
            })
            .unwrap();
        let mut other_manager = build();
        let (node_id, pen) = validator_data[1].clone();
        let (ManagerActions { maybe_message, .. }, _) = other_manager
            .update_validator_session(PreValidatorSession {
                session_id,
                verifier,
                node_id,
                pen,
            })
            .unwrap();
        let message = maybe_message.expect("there should be a discovery message");
        let (address, message) = (message.0.address(), message);
        let ManagerActions {
            maybe_command,
            maybe_message,
        } = manager.on_discovery_message(message);
        assert_eq!(
            maybe_command,
            Some(ConnectionCommand::AddReserved(
                iter::once(address).collect()
            ))
        );
        assert!(maybe_message.is_some());
    }

    #[test]
    fn sends_user_data() {
        let mut manager = build();
        let (validator_data, verifier) = crypto_basics(NUM_NODES);
        let (node_id, pen) = validator_data[0].clone();
        let session_id = SessionId(43);
        manager
            .update_validator_session(PreValidatorSession {
                session_id,
                verifier: verifier.clone(),
                node_id,
                pen,
            })
            .unwrap();
        let mut other_manager = build();
        let (node_id, pen) = validator_data[1].clone();
        let (ManagerActions { maybe_message, .. }, _) = other_manager
            .update_validator_session(PreValidatorSession {
                session_id,
                verifier,
                node_id,
                pen,
            })
            .unwrap();
        let message = maybe_message.expect("there should be a discovery message");
        manager.on_discovery_message(message);
        let messages = manager.on_user_message(2137, session_id, Recipient::Everyone);
        assert_eq!(messages.len(), 1);
        let (network_data, _) = &messages[0];
        assert_eq!(
            network_data,
            &DataInSession {
                data: 2137,
                session_id
            }
        );
    }
}