@@ -15,9 +15,12 @@ use crate::portalnet::{
15
15
} ,
16
16
} ;
17
17
18
- use crate :: utp:: {
19
- stream:: { UtpListenerRequest , UtpSocket , BUF_SIZE } ,
20
- trin_helpers:: { UtpAccept , UtpMessage } ,
18
+ use crate :: {
19
+ portalnet:: types:: content_key:: RawContentKey ,
20
+ utp:: {
21
+ stream:: { UtpListenerRequest , UtpStream , BUF_SIZE } ,
22
+ trin_helpers:: { UtpAccept , UtpMessage , UtpStreamId } ,
23
+ } ,
21
24
} ;
22
25
use discv5:: {
23
26
enr:: NodeId ,
@@ -277,48 +280,42 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
277
280
enr : Enr ,
278
281
conn_id : u16 ,
279
282
) -> Result < Content , OverlayRequestError > {
280
- let utp_request = UtpListenerRequest :: FindContentStream ( conn_id) ;
281
- if let Err ( err) = self . utp_listener_tx . send ( utp_request) {
282
- return Err ( OverlayRequestError :: UtpError ( format ! (
283
- "Unable to send uTP FindContent stream request: {err}"
284
- ) ) ) ;
285
- }
286
-
287
283
// initiate the connection to the acceptor
288
- let ( tx, rx) = tokio:: sync:: oneshot:: channel :: < anyhow:: Result < UtpSocket > > ( ) ;
289
-
290
- let _ = self
291
- . utp_listener_tx
292
- . send ( UtpListenerRequest :: Connect ( conn_id, enr. node_id ( ) , tx) ) ;
284
+ let ( tx, rx) = tokio:: sync:: oneshot:: channel :: < UtpStream > ( ) ;
285
+ let utp_request = UtpListenerRequest :: Connect (
286
+ conn_id,
287
+ enr,
288
+ self . protocol . clone ( ) ,
289
+ UtpStreamId :: FindContentStream ,
290
+ tx,
291
+ ) ;
292
+
293
+ self . utp_listener_tx . send ( utp_request) . map_err ( |err| {
294
+ OverlayRequestError :: UtpError ( format ! (
295
+ "Unable to send Connect request with FindContent stream to UtpListener: {err}"
296
+ ) )
297
+ } ) ?;
293
298
294
299
match rx. await {
295
- Ok ( conn) => {
296
- match conn {
297
- Ok ( mut conn) => {
298
- let mut result = Vec :: new ( ) ;
299
- // Loop and receive all DATA packets, similar to `read_to_end`
300
- loop {
301
- let mut buf = [ 0 ; BUF_SIZE ] ;
302
- match conn. recv_from ( & mut buf) . await {
303
- Ok ( ( 0 , _) ) => {
304
- break ;
305
- }
306
- Ok ( ( bytes, _) ) => {
307
- result. extend_from_slice ( & mut buf[ ..bytes] ) ;
308
- }
309
- Err ( err) => {
310
- warn ! ( "Unable to receive content via uTP: {err}" ) ;
311
- return Err ( OverlayRequestError :: UtpError ( err. to_string ( ) ) ) ;
312
- }
313
- }
300
+ Ok ( mut conn) => {
301
+ let mut result = Vec :: new ( ) ;
302
+ // Loop and receive all DATA packets, similar to `read_to_end`
303
+ loop {
304
+ let mut buf = [ 0 ; BUF_SIZE ] ;
305
+ match conn. recv_from ( & mut buf) . await {
306
+ Ok ( ( 0 , _) ) => {
307
+ break ;
308
+ }
309
+ Ok ( ( bytes, _) ) => {
310
+ result. extend_from_slice ( & mut buf[ ..bytes] ) ;
311
+ }
312
+ Err ( err) => {
313
+ warn ! ( "Unable to receive content via uTP: {err}" ) ;
314
+ return Err ( OverlayRequestError :: UtpError ( err. to_string ( ) ) ) ;
314
315
}
315
- Ok ( Content :: Content ( VariableList :: from ( result) ) )
316
- }
317
- Err ( err) => {
318
- warn ! ( "Unable to initiate uTP stream with remote node. Error initializing uTP socket: {err}" ) ;
319
- Err ( OverlayRequestError :: UtpError ( err. to_string ( ) ) )
320
316
}
321
317
}
318
+ Ok ( Content :: Content ( VariableList :: from ( result) ) )
322
319
}
323
320
Err ( err) => {
324
321
warn ! ( "Unable to receive from uTP listener channel: {err}" ) ;
@@ -331,7 +328,7 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
331
328
/// Offer is also sent to nodes after FindContent (POKE)
332
329
pub async fn send_offer (
333
330
& self ,
334
- content_keys : Vec < Vec < u8 > > ,
331
+ content_keys : Vec < RawContentKey > ,
335
332
enr : Enr ,
336
333
) -> Result < Accept , OverlayRequestError > {
337
334
// Construct the request.
@@ -384,49 +381,44 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
384
381
return Ok ( response) ;
385
382
}
386
383
387
- let utp_request = UtpListenerRequest :: OfferStream ( conn_id) ;
388
- if let Err ( err) = self . utp_listener_tx . send ( utp_request) {
389
- return Err ( anyhow ! ( "Unable to send uTP Offer stream request: {err}" ) ) ;
390
- }
391
-
392
384
// initiate the connection to the acceptor
393
- let ( tx, rx) = tokio:: sync:: oneshot:: channel :: < anyhow:: Result < UtpSocket > > ( ) ;
394
-
395
- let _ = self
396
- . utp_listener_tx
397
- . send ( UtpListenerRequest :: Connect ( conn_id, enr. node_id ( ) , tx) ) ;
385
+ let ( tx, rx) = tokio:: sync:: oneshot:: channel :: < UtpStream > ( ) ;
386
+ let utp_request = UtpListenerRequest :: Connect (
387
+ conn_id,
388
+ enr,
389
+ self . protocol . clone ( ) ,
390
+ UtpStreamId :: OfferStream ,
391
+ tx,
392
+ ) ;
393
+
394
+ self . utp_listener_tx
395
+ . send ( utp_request) . map_err ( |err| anyhow ! ( "Unable to send Connect request to UtpListener when processing ACCEPT message: {err}" ) ) ?;
396
+
397
+ let mut conn = rx. await ?;
398
+ // Handle STATE packet for SYN
399
+ let mut buf = [ 0 ; BUF_SIZE ] ;
400
+ conn. recv ( & mut buf) . await ?;
401
+
402
+ let content_items = self . provide_requested_content ( & response, content_keys_offered) ;
403
+
404
+ let content_message = UtpAccept {
405
+ message : content_items,
406
+ } ;
398
407
399
- match rx. await ? {
400
- Ok ( mut conn) => {
401
- // Handle STATE packet for SYN
402
- let mut buf = [ 0 ; BUF_SIZE ] ;
403
- conn. recv ( & mut buf) . await ?;
404
-
405
- let content_items = self . provide_requested_content ( & response, content_keys_offered) ;
406
-
407
- let content_message = UtpAccept {
408
- message : content_items,
409
- } ;
410
-
411
- tokio:: spawn ( async move {
412
- // send the content to the acceptor over a uTP stream
413
- if let Err ( err) = conn
414
- . send_to ( & UtpMessage :: new ( content_message. as_ssz_bytes ( ) ) . encode ( ) [ ..] )
415
- . await
416
- {
417
- warn ! ( "Error sending content {err}" ) ;
418
- } ;
419
- // Close uTP connection
420
- if let Err ( err) = conn. close ( ) . await {
421
- warn ! ( "Unable to close uTP connection!: {err}" )
422
- } ;
423
- } ) ;
424
- Ok ( response)
425
- }
426
- Err ( err) => Err ( anyhow ! (
427
- "Unable to initialize Offer uTP stream with remote node: {err}"
428
- ) ) ,
429
- }
408
+ tokio:: spawn ( async move {
409
+ // send the content to the acceptor over a uTP stream
410
+ if let Err ( err) = conn
411
+ . send_to ( & UtpMessage :: new ( content_message. as_ssz_bytes ( ) ) . encode ( ) [ ..] )
412
+ . await
413
+ {
414
+ warn ! ( "Error sending content {err}" ) ;
415
+ } ;
416
+ // Close uTP connection
417
+ if let Err ( err) = conn. close ( ) . await {
418
+ warn ! ( "Unable to close uTP connection!: {err}" )
419
+ } ;
420
+ } ) ;
421
+ Ok ( response)
430
422
}
431
423
432
424
/// Provide the requested content key and content value for the acceptor
0 commit comments