hopr_transport_p2p/
lib.rs1pub mod constants;
26
27pub mod errors;
29
30pub mod swarm;
32
33mod behavior;
35
36use std::fmt::Debug;
37
38pub use behavior::discovery::Event as DiscoveryEvent;
39use futures::{AsyncRead, AsyncWrite, Stream};
40use hopr_internal_types::prelude::*;
41use hopr_transport_identity::PeerId;
42use hopr_transport_protocol::PeerDiscovery;
43use libp2p::{StreamProtocol, autonat, swarm::NetworkBehaviour};
44use rand::rngs::OsRng;
45
46pub const MSG_ACK_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
47pub const NAT_SERVER_PROBE_INTERVAL: std::time::Duration = std::time::Duration::from_secs(30);
48
49#[derive(Clone)]
51pub struct HoprStreamProtocolControl {
52 control: libp2p_stream::Control,
53 protocol: StreamProtocol,
54}
55
56impl HoprStreamProtocolControl {
57 pub fn new(control: libp2p_stream::Control, protocol: &'static str) -> Self {
58 Self {
59 control,
60 protocol: StreamProtocol::new(protocol),
61 }
62 }
63}
64
65impl std::fmt::Debug for HoprStreamProtocolControl {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 f.debug_struct("HoprStreamProtocolControl")
68 .field("protocol", &self.protocol)
69 .finish()
70 }
71}
72
73#[async_trait::async_trait]
74impl hopr_transport_protocol::stream::BidirectionalStreamControl for HoprStreamProtocolControl {
75 fn accept(
76 mut self,
77 ) -> Result<impl Stream<Item = (PeerId, impl AsyncRead + AsyncWrite + Send)> + Send, impl std::error::Error> {
78 self.control.accept(self.protocol)
79 }
80
81 async fn open(mut self, peer: PeerId) -> Result<impl AsyncRead + AsyncWrite + Send, impl std::error::Error> {
82 self.control.open_stream(peer, self.protocol).await
83 }
84}
85
86#[derive(NetworkBehaviour)]
92#[behaviour(to_swarm = "HoprNetworkBehaviorEvent")]
93pub struct HoprNetworkBehavior {
94 discovery: behavior::discovery::Behaviour,
95 streams: libp2p_stream::Behaviour,
96 pub autonat_client: autonat::v2::client::Behaviour,
97 pub autonat_server: autonat::v2::server::Behaviour,
98}
99
100impl Debug for HoprNetworkBehavior {
101 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 f.debug_struct("HoprNetworkBehavior").finish()
103 }
104}
105
106impl HoprNetworkBehavior {
107 pub fn new<T>(me: PeerId, onchain_events: T) -> Self
108 where
109 T: Stream<Item = PeerDiscovery> + Send + 'static,
110 {
111 Self {
112 streams: libp2p_stream::Behaviour::new(),
113 discovery: behavior::discovery::Behaviour::new(me, onchain_events),
114 autonat_client: autonat::v2::client::Behaviour::new(
115 OsRng,
116 autonat::v2::client::Config::default().with_probe_interval(NAT_SERVER_PROBE_INTERVAL), ),
118 autonat_server: autonat::v2::server::Behaviour::new(OsRng),
119 }
120 }
121}
122
123#[derive(Debug)]
128pub enum HoprNetworkBehaviorEvent {
129 Discovery(behavior::discovery::Event),
130 TicketAggregation(
131 libp2p::request_response::Event<Vec<TransferableWinningTicket>, std::result::Result<Ticket, String>>,
132 ),
133 AutonatClient(autonat::v2::client::Event),
134 AutonatServer(autonat::v2::server::Event),
135}
136
137impl From<()> for HoprNetworkBehaviorEvent {
139 fn from(_: ()) -> Self {
140 panic!("Unexpected event: ()")
141 }
142}
143
144impl From<behavior::discovery::Event> for HoprNetworkBehaviorEvent {
145 fn from(event: behavior::discovery::Event) -> Self {
146 Self::Discovery(event)
147 }
148}
149
150impl From<libp2p::request_response::Event<Vec<TransferableWinningTicket>, std::result::Result<Ticket, String>>>
151 for HoprNetworkBehaviorEvent
152{
153 fn from(
154 event: libp2p::request_response::Event<Vec<TransferableWinningTicket>, std::result::Result<Ticket, String>>,
155 ) -> Self {
156 Self::TicketAggregation(event)
157 }
158}
159
160impl From<autonat::v2::client::Event> for HoprNetworkBehaviorEvent {
161 fn from(event: autonat::v2::client::Event) -> Self {
162 Self::AutonatClient(event)
163 }
164}
165
166impl From<autonat::v2::server::Event> for HoprNetworkBehaviorEvent {
167 fn from(event: autonat::v2::server::Event) -> Self {
168 Self::AutonatServer(event)
169 }
170}
171
172pub use swarm::HoprSwarm;