hopr_transport_p2p/
lib.rs

1//! # P2P
2//!
3//! The underlying technology for managing the peer-to-peer networking used by this package is the [`rust-libp2p`](https://github.com/libp2p/rust-libp2p) library ([documentation](https://docs.libp2p.io/)).
4//!
5//! ## Modularity
6//!
7//! `rust-libp2p` is highly modular allowing for reimplmenting expected behavior using custom implementations for API
8//! traits.
9//!
10//! This way it is possible to experiment with and combine different components of the library in order to construct a
11//! specific targeted use case.
12//!
13//! ## `rust-libp2p` connectivity
14//!
15//! As per the [official documentation](https://connectivity.libp2p.io/), the connectivity types in the library are divided into the `standalone` (implementation of network over host) and `browser` (implementation of network over browser).
16//!
17//! Nodes that are not located behind a blocking firewall or NAT are designated as **public nodes** and can utilize the
18//! `TCP` or `QUIC` connectivity, with the recommendation to use QUIC if possible.
19//!
20//! Browser based solutions are almost always located behind a private network or a blocking firewall and to open a
21//! connection towards the standalone nodes these utilize either the `WebSocket` approach (by hijacking the `TCP`
22//! connection) or the (not yet fully speced up) `WebTransport` (by hijacking the `QUIC` connection).
23
24/// Constants exported by the crate.
25pub mod constants;
26
27/// Errors generated by the crate.
28pub mod errors;
29
30/// Raw swarm definition for the HOPR network.
31pub mod swarm;
32
33/// P2P behavior definitions for the transport level interactions not related to the HOPR protocol
34mod behavior;
35
36use std::fmt::Debug;
37
38use futures::{AsyncRead, AsyncWrite, Stream};
39use hopr_internal_types::prelude::*;
40use hopr_transport_identity::PeerId;
41use hopr_transport_protocol::PeerDiscovery;
42use libp2p::{StreamProtocol, autonat, swarm::NetworkBehaviour};
43use rand::rngs::OsRng;
44
45pub const MSG_ACK_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
46pub const NAT_SERVER_PROBE_INTERVAL: std::time::Duration = std::time::Duration::from_secs(30);
47
48// Control object for the streams over the HOPR protocols
49#[derive(Clone)]
50pub struct HoprStreamProtocolControl {
51    control: libp2p_stream::Control,
52    protocol: StreamProtocol,
53}
54
55impl HoprStreamProtocolControl {
56    pub fn new(control: libp2p_stream::Control, protocol: &'static str) -> Self {
57        Self {
58            control,
59            protocol: StreamProtocol::new(protocol),
60        }
61    }
62}
63
64impl std::fmt::Debug for HoprStreamProtocolControl {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        f.debug_struct("HoprStreamProtocolControl")
67            .field("protocol", &self.protocol)
68            .finish()
69    }
70}
71
72#[async_trait::async_trait]
73impl hopr_transport_protocol::stream::BidirectionalStreamControl for HoprStreamProtocolControl {
74    fn accept(
75        mut self,
76    ) -> Result<impl Stream<Item = (PeerId, impl AsyncRead + AsyncWrite + Send)> + Send, impl std::error::Error> {
77        self.control.accept(self.protocol)
78    }
79
80    async fn open(mut self, peer: PeerId) -> Result<impl AsyncRead + AsyncWrite + Send, impl std::error::Error> {
81        self.control.open_stream(peer, self.protocol).await
82    }
83}
84
85/// Network Behavior definition for aggregated HOPR network functionality.
86///
87/// Individual network behaviors from the libp2p perspectives are aggregated
88/// under this type in order to create an aggregated network behavior capable
89/// of generating events for all component behaviors.
90#[derive(NetworkBehaviour)]
91#[behaviour(to_swarm = "HoprNetworkBehaviorEvent")]
92pub struct HoprNetworkBehavior {
93    discovery: behavior::discovery::Behaviour,
94    streams: libp2p_stream::Behaviour,
95    pub autonat_client: autonat::v2::client::Behaviour,
96    pub autonat_server: autonat::v2::server::Behaviour,
97}
98
99impl Debug for HoprNetworkBehavior {
100    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101        f.debug_struct("HoprNetworkBehavior").finish()
102    }
103}
104
105impl HoprNetworkBehavior {
106    pub fn new<T>(me: PeerId, onchain_events: T) -> Self
107    where
108        T: Stream<Item = PeerDiscovery> + Send + 'static,
109    {
110        Self {
111            streams: libp2p_stream::Behaviour::new(),
112            discovery: behavior::discovery::Behaviour::new(me, onchain_events),
113            autonat_client: autonat::v2::client::Behaviour::new(
114                OsRng,
115                autonat::v2::client::Config::default().with_probe_interval(NAT_SERVER_PROBE_INTERVAL), /* TODO (jean): make this configurable */
116            ),
117            autonat_server: autonat::v2::server::Behaviour::new(OsRng),
118        }
119    }
120}
121
122/// Aggregated network behavior event inheriting the component behaviors' events.
123///
124/// Necessary to allow the libp2p handler to properly distribute the events for
125/// processing in the business logic loop.
126#[derive(Debug)]
127pub enum HoprNetworkBehaviorEvent {
128    Discovery(behavior::discovery::Event),
129    TicketAggregation(
130        libp2p::request_response::Event<Vec<TransferableWinningTicket>, std::result::Result<Ticket, String>>,
131    ),
132    AutonatClient(autonat::v2::client::Event),
133    AutonatServer(autonat::v2::server::Event),
134}
135
136// Unexpected libp2p_stream event
137impl From<()> for HoprNetworkBehaviorEvent {
138    fn from(_: ()) -> Self {
139        panic!("Unexpected event: ()")
140    }
141}
142
143impl From<behavior::discovery::Event> for HoprNetworkBehaviorEvent {
144    fn from(event: behavior::discovery::Event) -> Self {
145        Self::Discovery(event)
146    }
147}
148
149impl From<libp2p::request_response::Event<Vec<TransferableWinningTicket>, std::result::Result<Ticket, String>>>
150    for HoprNetworkBehaviorEvent
151{
152    fn from(
153        event: libp2p::request_response::Event<Vec<TransferableWinningTicket>, std::result::Result<Ticket, String>>,
154    ) -> Self {
155        Self::TicketAggregation(event)
156    }
157}
158
159impl From<autonat::v2::client::Event> for HoprNetworkBehaviorEvent {
160    fn from(event: autonat::v2::client::Event) -> Self {
161        Self::AutonatClient(event)
162    }
163}
164
165impl From<autonat::v2::server::Event> for HoprNetworkBehaviorEvent {
166    fn from(event: autonat::v2::server::Event) -> Self {
167        Self::AutonatServer(event)
168    }
169}
170
171pub use swarm::HoprSwarm;