Skip to content

Commit 2e8a7f2

Browse files
committed
Remove redundant listening hashmap from uTP listener and clear dead code
1 parent 5c04aa2 commit 2e8a7f2

File tree

10 files changed

+27
-231
lines changed

10 files changed

+27
-231
lines changed

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub async fn run_trin(
5252
};
5353

5454
// Initialize and spawn UTP listener
55-
let (utp_events_tx, utp_listener_tx, utp_listener_rx, mut utp_listener) =
55+
let (utp_events_tx, utp_listener_tx, _, mut utp_listener) =
5656
UtpListener::new(Arc::clone(&discovery));
5757
tokio::spawn(async move { utp_listener.start().await });
5858

@@ -67,7 +67,6 @@ pub async fn run_trin(
6767
initialize_state_network(
6868
&discovery,
6969
utp_listener_tx.clone(),
70-
utp_listener_rx.clone(),
7170
portalnet_config.clone(),
7271
storage_config.clone(),
7372
)
@@ -86,7 +85,6 @@ pub async fn run_trin(
8685
initialize_history_network(
8786
&discovery,
8887
utp_listener_tx,
89-
utp_listener_rx,
9088
portalnet_config.clone(),
9189
storage_config.clone(),
9290
)

trin-core/src/portalnet/overlay.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::portalnet::{
1818
use crate::{
1919
portalnet::types::content_key::RawContentKey,
2020
utp::{
21-
stream::{UtpListenerEvent, UtpListenerRequest, UtpSocket, BUF_SIZE},
21+
stream::{UtpListenerRequest, UtpSocket, BUF_SIZE},
2222
trin_helpers::{UtpAccept, UtpMessage, UtpStreamId},
2323
},
2424
};
@@ -32,10 +32,7 @@ use futures::channel::oneshot;
3232
use parking_lot::RwLock;
3333
use ssz::Encode;
3434
use ssz_types::VariableList;
35-
use tokio::sync::{
36-
mpsc::{UnboundedReceiver, UnboundedSender},
37-
RwLock as TRwLock,
38-
};
35+
use tokio::sync::mpsc::UnboundedSender;
3936
use tracing::{debug, warn};
4037

4138
pub use super::overlay_service::{OverlayRequestError, RequestDirection};
@@ -101,7 +98,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
10198
config: OverlayConfig,
10299
discovery: Arc<Discovery>,
103100
utp_listener_tx: UnboundedSender<UtpListenerRequest>,
104-
utp_listener_rx: Arc<TRwLock<UnboundedReceiver<UtpListenerEvent>>>,
105101
storage: Arc<RwLock<PortalStorage>>,
106102
data_radius: U256,
107103
protocol: ProtocolId,
@@ -124,7 +120,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
124120
Arc::clone(&data_radius),
125121
protocol.clone(),
126122
utp_listener_tx.clone(),
127-
utp_listener_rx,
128123
config.enable_metrics,
129124
)
130125
.await
@@ -285,13 +280,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
285280
enr: Enr,
286281
conn_id: u16,
287282
) -> Result<Content, OverlayRequestError> {
288-
let utp_request = UtpListenerRequest::FindContentStream(conn_id);
289-
if let Err(err) = self.utp_listener_tx.send(utp_request) {
290-
return Err(OverlayRequestError::UtpError(format!(
291-
"Unable to send uTP FindContent stream request: {err}"
292-
)));
293-
}
294-
295283
// initiate the connection to the acceptor
296284
let (tx, rx) = tokio::sync::oneshot::channel::<anyhow::Result<UtpSocket>>();
297285
self.utp_listener_tx
@@ -401,11 +389,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
401389
return Ok(response);
402390
}
403391

404-
let utp_request = UtpListenerRequest::OfferStream(conn_id);
405-
if let Err(err) = self.utp_listener_tx.send(utp_request) {
406-
return Err(anyhow!("Unable to send uTP Offer stream request: {err}"));
407-
}
408-
409392
// initiate the connection to the acceptor
410393
let (tx, rx) = tokio::sync::oneshot::channel::<anyhow::Result<UtpSocket>>();
411394

trin-core/src/portalnet/overlay_service.rs

Lines changed: 6 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ use crate::{
1919
utp::stream::UtpListenerRequest,
2020
};
2121

22-
use crate::{
23-
portalnet::types::content_key::RawContentKey,
24-
utp::{
25-
stream::{UtpListenerEvent, UtpPayload},
26-
trin_helpers::UtpStreamId,
27-
},
28-
};
22+
use crate::utp::trin_helpers::UtpStreamId;
2923
use delay_map::HashSetDelay;
3024
use discv5::{
3125
enr::NodeId,
@@ -43,10 +37,7 @@ use rand::seq::SliceRandom;
4337
use ssz::Encode;
4438
use ssz_types::{BitList, VariableList};
4539
use thiserror::Error;
46-
use tokio::sync::{
47-
mpsc::{self, UnboundedReceiver, UnboundedSender},
48-
RwLock as TRwLock,
49-
};
40+
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
5041

5142
/// Maximum number of ENRs in response to FindNodes.
5243
pub const FIND_NODES_MAX_NODES: usize = 32;
@@ -265,8 +256,6 @@ pub struct OverlayService<TContentKey, TMetric> {
265256
response_tx: UnboundedSender<OverlayResponse>,
266257
/// The sender half of a channel to send requests to uTP listener
267258
utp_listener_tx: UnboundedSender<UtpListenerRequest>,
268-
/// Receiver for UtpListener emitted events
269-
utp_listener_rx: Arc<TRwLock<UnboundedReceiver<UtpListenerEvent>>>,
270259
/// Phantom content key.
271260
phantom_content_key: PhantomData<TContentKey>,
272261
/// Phantom metric (distance function).
@@ -292,7 +281,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
292281
data_radius: Arc<U256>,
293282
protocol: ProtocolId,
294283
utp_listener_sender: UnboundedSender<UtpListenerRequest>,
295-
utp_listener_receiver: Arc<TRwLock<UnboundedReceiver<UtpListenerEvent>>>,
296284
enable_metrics: bool,
297285
) -> Result<UnboundedSender<OverlayRequest>, String> {
298286
let (request_tx, request_rx) = mpsc::unbounded_channel();
@@ -325,7 +313,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
325313
response_rx,
326314
response_tx,
327315
utp_listener_tx: utp_listener_sender,
328-
utp_listener_rx: utp_listener_receiver,
329316
phantom_content_key: PhantomData,
330317
phantom_metric: PhantomData,
331318
metrics,
@@ -397,9 +384,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
397384
tokio::time::interval(Duration::from_secs(BUCKET_REFRESH_INTERVAL));
398385

399386
loop {
400-
// let utp_listener_rx = self.utp_listener_rx.clone();
401-
// let mut utp_listener_lock = utp_listener_rx.write_with_warn().await;
402-
403387
tokio::select! {
404388
Some(request) = self.request_rx.recv() => self.process_request(request),
405389
Some(response) = self.response_rx.recv() => {
@@ -428,14 +412,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
428412
self.peers_to_ping.insert(node_id);
429413
}
430414
}
431-
// Some(utp_event) = utp_listener_lock.recv() => {
432-
// match utp_event {
433-
// UtpListenerEvent::ProcessedClosedStreams(processed_streams) =>
434-
// {
435-
// self.handle_utp_payload(processed_streams);
436-
// }
437-
// }
438-
// }
439415
_ = OverlayService::<TContentKey, TMetric>::bucket_maintenance_poll(self.protocol.clone(), &self.kbuckets) => {}
440416
_ = bucket_refresh_interval.tick() => {
441417
debug!("[{:?}] Overlay bucket refresh lookup", self.protocol);
@@ -666,26 +642,11 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
666642
} else {
667643
let conn_id: u16 = crate::utp::stream::rand();
668644

669-
// listen for incoming connection request on conn_id, as part of utp handshake and
670-
// temporarily storing content data, so we can send it right after we receive
645+
// Listen for incoming uTP connection request on as part of uTP handshake and
646+
// storing content data, so we can send it inside UtpListener right after we receive
671647
// SYN packet from the requester
672-
let utp_request = UtpListenerRequest::FindContentData(conn_id, content.clone());
673-
if let Err(err) = self.utp_listener_tx.send(utp_request) {
674-
return Err(OverlayRequestError::UtpError(format!(
675-
"Unable to send uTP FindContentData stream request: {err}"
676-
)));
677-
}
678-
679-
// also listen on conn_id + 1 because this is the receive path
680648
let conn_id_recv = conn_id.wrapping_add(1);
681649

682-
let utp_request = UtpListenerRequest::FindContentStream(conn_id_recv);
683-
if let Err(err) = self.utp_listener_tx.send(utp_request) {
684-
return Err(OverlayRequestError::UtpError(format!(
685-
"Unable to send uTP FindContent stream request: {err}"
686-
)));
687-
}
688-
689650
self.add_utp_connection(
690651
source,
691652
conn_id_recv,
@@ -704,8 +665,7 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
704665
}
705666
}
706667
Err(msg) => Err(OverlayRequestError::Failure(format!(
707-
"Unable to respond to FindContent: {}",
708-
msg
668+
"Unable to respond to FindContent: {msg}",
709669
))),
710670
}
711671
}
@@ -748,24 +708,8 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
748708
})?;
749709
}
750710

751-
// listen for incoming connection request on conn_id, as part of utp handshake
711+
// Listen for incoming connection request on conn_id, as part of utp handshake
752712
let conn_id: u16 = crate::utp::stream::rand();
753-
let utp_request = UtpListenerRequest::OfferStream(conn_id);
754-
if let Err(err) = self.utp_listener_tx.send(utp_request) {
755-
return Err(OverlayRequestError::UtpError(format!(
756-
"Unable to send uTP Offer stream request: {err}"
757-
)));
758-
}
759-
760-
// also listen on conn_id + 1 because this is the actual receive path for acceptor
761-
let conn_id_recv = conn_id.wrapping_add(1);
762-
763-
let utp_request = UtpListenerRequest::AcceptStream(conn_id_recv, accept_keys.clone());
764-
if let Err(err) = self.utp_listener_tx.send(utp_request) {
765-
return Err(OverlayRequestError::UtpError(format!(
766-
"Unable to send uTP Accept stream request: {err}"
767-
)));
768-
}
769713

770714
self.add_utp_connection(source, conn_id, UtpStreamId::AcceptStream(accept_keys))?;
771715

@@ -777,19 +721,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
777721
Ok(accept)
778722
}
779723

780-
/// Handle all closed uTP streams, currently we process only AcceptStream here.
781-
/// FindContent payload is processed explicitly when we send FindContent request.
782-
fn handle_utp_payload(&self, streams: Vec<(UtpPayload, UtpStreamId)>) {
783-
for stream in streams {
784-
match stream {
785-
(payload, UtpStreamId::AcceptStream(content_keys)) => {
786-
self.process_accept_utp_payload(content_keys, payload);
787-
}
788-
_ => {}
789-
}
790-
}
791-
}
792-
793724
/// Sends a TALK request via Discovery v5 to some destination node.
794725
fn send_talk_req(&self, request: Request, request_id: OverlayRequestId, destination: Enr) {
795726
let discovery = Arc::clone(&self.discovery);
@@ -821,12 +752,6 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
821752
});
822753
}
823754

824-
/// Process accepted uTP payload of the OFFER?ACCEPT stream
825-
fn process_accept_utp_payload(&self, content_keys: Vec<RawContentKey>, payload: UtpPayload) {
826-
// TODO: Verify the payload, store the content and propagate gossip.
827-
debug!("DEBUG: Processing content keys: {content_keys:?}, with payload: {payload:?}, protocol: {:?}", self.protocol);
828-
}
829-
830755
/// Processes an incoming request from some source node.
831756
fn process_incoming_request(&mut self, request: Request, _id: RequestId, source: NodeId) {
832757
// Look up the node in the routing table.
@@ -1354,7 +1279,6 @@ mod tests {
13541279
let discovery = Arc::new(Discovery::new(portal_config).unwrap());
13551280

13561281
let (utp_listener_tx, _) = unbounded_channel::<UtpListenerRequest>();
1357-
let (_, utp_listener_rx) = unbounded_channel::<UtpListenerEvent>();
13581282

13591283
// Initialize DB config
13601284
let storage_capacity: u32 = DEFAULT_STORAGE_CAPACITY.parse().unwrap();
@@ -1392,7 +1316,6 @@ mod tests {
13921316
response_tx,
13931317
response_rx,
13941318
utp_listener_tx,
1395-
utp_listener_rx: Arc::new(TRwLock::new(utp_listener_rx)),
13961319
phantom_content_key: PhantomData,
13971320
phantom_metric: PhantomData,
13981321
metrics,

0 commit comments

Comments
 (0)