aleph_bft_types/network.rs
1use crate::NodeIndex;
2
3use codec::{Decode, Encode};
4
5/// A recipient of a message, either a specific node or everyone.
6#[derive(Clone, Eq, PartialEq, Hash, Debug, Decode, Encode)]
7pub enum Recipient {
8 Everyone,
9 Node(NodeIndex),
10}
11
12/// Network represents an interface for sending and receiving NetworkData.
13///
14/// Note on Rate Control: it is assumed that Network implements a rate control mechanism guaranteeing
15/// that no node is allowed to spam messages without limits. We do not specify details yet, but in
16/// future releases we plan to publish recommended upper bounds for the amounts of bandwidth and
17/// number of messages allowed per node per a unit of time. These bounds must be carefully crafted
18/// based upon the number of nodes N and the configured delays between subsequent Dag rounds, so
19/// that at the same time spammers are cut off but honest nodes are able function correctly within
20/// these bounds.
21///
22/// Note on Network Reliability: it is not assumed that each message that AlephBFT orders to send
23/// reaches its intended recipient, there are some built-in reliability mechanisms within AlephBFT
24/// that will automatically detect certain failures and resend messages as needed. Clearly, the less
25/// reliable the network is, the worse the performance of AlephBFT will be (generally slower to
26/// produce output). Also, not surprisingly if the percentage of dropped messages is too high
27/// AlephBFT might stop making progress, but from what we observe in tests, this happens only when
28/// the reliability is extremely bad, i.e., drops below 50% (which means there is some significant
29/// issue with the network).
30///
31/// We refer to the documentation https://cardinal-cryptography.github.io/AlephBFT/aleph_bft_api.html
32/// Section 3.1.2 for a discussion of the required guarantees of this trait's implementation.
33#[async_trait::async_trait]
34pub trait Network<D>: Send + 'static {
35 /// Send a message to a single node or everyone, depending on the value of the recipient
36 /// argument.
37 ///
38 /// Note on the implementation: this function should be implemented in a non-blocking manner.
39 /// Otherwise, the performance might be affected negatively or the execution may end up in a deadlock.
40 fn send(&self, data: D, recipient: Recipient);
41 /// Receive a message from the network.
42 async fn next_event(&mut self) -> Option<D>;
43}