Skip to main content

everscale_network/overlay/
mod.rs

1//! ## Overlay - virtual subnetwork
2//!
3//! An overlay (sub)network is simply a (virtual) network implemented inside some larger network.
4//! Only some nodes of the larger network participate in the overlay subnetwork,
5//! and only some "links" between these nodes, physical or virtual, are part of the overlay
6//! sub-network.
7//!
8//! TODO
9
10pub use overlay_id::{IdFull, IdShort};
11
12mod overlay_id;
13
14#[cfg(feature = "overlay")]
15mod broadcast_receiver;
16#[cfg(feature = "overlay")]
17mod node;
18#[cfg(feature = "overlay")]
19#[allow(clippy::module_inception)]
20mod overlay;
21
22#[cfg(feature = "overlay")]
23mod node_impl {
24    use std::sync::Arc;
25
26    use anyhow::Result;
27    use frunk_core::hlist::{HCons, HList, IntoTuple2, Selector};
28    use frunk_core::indices::There;
29
30    pub use super::node::Node;
31    pub use super::overlay::{
32        BroadcastTarget, ExistingPeersFilter, IncomingBroadcastInfo, OutgoingBroadcastInfo,
33        Overlay, OverlayMetrics, OverlayOptions, ReceivedPeersMap,
34    };
35
36    use crate::rldp;
37    use crate::util::{DeferredInitialization, NetworkBuilder};
38
39    pub(crate) type Deferred = Result<Arc<Node>>;
40
41    impl DeferredInitialization for Deferred {
42        type Initialized = Arc<Node>;
43
44        fn initialize(self) -> Result<Self::Initialized> {
45            self
46        }
47    }
48
49    impl<L, A, R> NetworkBuilder<L, (A, R)>
50    where
51        L: HList + Selector<rldp::Deferred, R>,
52        HCons<Deferred, L>: IntoTuple2,
53    {
54        /// Creates overlay network layer.
55        ///
56        /// NOTE: RLDP network layer must be present before calling this method.
57        ///
58        /// # Examples
59        ///
60        /// ```
61        /// # use anyhow::Result;
62        /// # use everscale_network::{adnl, rldp, NetworkBuilder};
63        /// #[tokio::main]
64        /// async fn main() -> Result<()> {
65        ///     const OVERLAY_KEY_TAG: usize = 0;
66        ///
67        ///     let keystore = adnl::Keystore::builder()
68        ///         .with_tagged_key([0; 32], OVERLAY_KEY_TAG)?
69        ///         .build();
70        ///
71        ///     let adnl_options = adnl::NodeOptions::default();
72        ///     let rldp_options = rldp::NodeOptions::default();
73        ///
74        ///     let (adnl, rldp, overlay) =
75        ///         NetworkBuilder::with_adnl("127.0.0.1:10000", keystore, adnl_options)
76        ///             .with_rldp(rldp_options)
77        ///             .with_overlay(OVERLAY_KEY_TAG)
78        ///             .build()?;
79        ///     Ok(())
80        /// }
81        /// ```
82        #[allow(clippy::type_complexity)]
83        pub fn with_overlay(
84            mut self,
85            key_tag: usize,
86        ) -> NetworkBuilder<HCons<Deferred, L>, (There<A>, There<R>)> {
87            let deferred = match self.0.get_mut() {
88                Ok((adnl, subscribers, _)) => {
89                    let overlay = Node::new(adnl.clone(), key_tag);
90                    if let Ok(overlay) = &overlay {
91                        subscribers.push(overlay.query_subscriber());
92                    }
93                    overlay
94                }
95                Err(_) => Err(anyhow::anyhow!("ADNL was not initialized")),
96            };
97
98            NetworkBuilder(self.0.prepend(deferred), Default::default())
99        }
100    }
101
102    /// Max allowed known peer count
103    pub const MAX_OVERLAY_PEERS: u32 = 65536;
104}
105
106#[cfg(feature = "overlay")]
107pub use node_impl::*;