hopr_transport_p2p/
lib.rs1pub mod constants;
26
27pub mod errors;
29
30pub mod swarm;
32
33mod 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#[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#[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), ),
117 autonat_server: autonat::v2::server::Behaviour::new(OsRng),
118 }
119 }
120}
121
122#[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
136impl 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;