@@ -60,7 +60,7 @@ impl Discovery {
60
60
)
61
61
. await ;
62
62
63
- task. discovery ( None ) ;
63
+ task. publish_discovery ( ) ;
64
64
65
65
let _handle = executor. spawn_abortable ( task) ;
66
66
Self {
@@ -508,6 +508,10 @@ struct DiscoveryTask {
508
508
509
509
broadcast_tx : broadcast:: Sender < DID > ,
510
510
511
+ discovery_publish_confirmed : bool ,
512
+
513
+ discovery_publish_fut : Option < BoxFuture < ' static , DiscoveryPublish > > ,
514
+
511
515
waker : Option < Waker > ,
512
516
}
513
517
@@ -537,6 +541,8 @@ impl DiscoveryTask {
537
541
command_rx,
538
542
connection_event,
539
543
discovery_request_st,
544
+ discovery_publish_confirmed : false ,
545
+ discovery_publish_fut : None ,
540
546
discovery_fut : None ,
541
547
waker : None ,
542
548
}
@@ -556,6 +562,12 @@ enum DiscoveryResponse {
556
562
InvalidRequest ,
557
563
}
558
564
565
+ enum DiscoveryPublish {
566
+ Dht ,
567
+ Rz ,
568
+ None ,
569
+ }
570
+
559
571
impl DiscoveryTask {
560
572
#[ allow( dead_code) ]
561
573
pub fn discovery ( & mut self , ns : Option < String > ) {
@@ -591,10 +603,68 @@ impl DiscoveryTask {
591
603
}
592
604
}
593
605
606
+ pub fn publish_discovery ( & mut self ) {
607
+ let ipfs = self . ipfs . clone ( ) ;
608
+ let fut = match self . config {
609
+ DiscoveryConfig :: Shuttle { .. } => {
610
+ futures:: future:: ready ( DiscoveryPublish :: None ) . boxed ( )
611
+ }
612
+ DiscoveryConfig :: Namespace {
613
+ ref namespace,
614
+ ref discovery_type,
615
+ } => {
616
+ let namespace = namespace. clone ( ) . unwrap_or ( "satellite-warp" . to_string ( ) ) ;
617
+ match discovery_type {
618
+ DiscoveryType :: DHT => async move {
619
+ if let Err ( e) = ipfs. dht_provide ( namespace. as_bytes ( ) . to_vec ( ) ) . await {
620
+ tracing:: error!( error = %e, "cannot provide {namespace}" ) ;
621
+ return DiscoveryPublish :: None ;
622
+ }
623
+ DiscoveryPublish :: Dht
624
+ }
625
+ . boxed ( ) ,
626
+ DiscoveryType :: RzPoint { addresses } => {
627
+ let peers = addresses
628
+ . iter ( )
629
+ . filter_map ( |addr| addr. clone ( ) . extract_peer_id ( ) )
630
+ . collect :: < IndexSet < _ > > ( ) ;
631
+
632
+ if peers. is_empty ( ) {
633
+ return ;
634
+ }
635
+
636
+ // We will use the first instance instead of the whole set for now
637
+ let peer_id = peers. get_index ( 0 ) . copied ( ) . expect ( "should not fail" ) ;
638
+
639
+ async move {
640
+ if let Err ( e) = ipfs
641
+ . rendezvous_register_namespace ( & namespace, None , peer_id)
642
+ . await
643
+ {
644
+ tracing:: error!( error = %e, "cannot provide {namespace}" ) ;
645
+ return DiscoveryPublish :: None ;
646
+ }
647
+ DiscoveryPublish :: Rz
648
+ }
649
+ . boxed ( )
650
+ }
651
+ }
652
+ }
653
+ DiscoveryConfig :: None => futures:: future:: ready ( DiscoveryPublish :: None ) . boxed ( ) ,
654
+ } ;
655
+
656
+ self . discovery_publish_fut = Some ( fut) ;
657
+ }
658
+
594
659
pub fn dht_discovery ( & mut self , namespace : String ) {
595
660
if self . discovery_fut . is_some ( ) {
596
661
return ;
597
662
}
663
+
664
+ if !self . discovery_publish_confirmed {
665
+ return ;
666
+ }
667
+
598
668
let ipfs = self . ipfs . clone ( ) ;
599
669
let fut = async move {
600
670
let bytes = namespace. as_bytes ( ) ;
@@ -613,11 +683,12 @@ impl DiscoveryTask {
613
683
return ;
614
684
}
615
685
616
- // TODO: show that we are registered so we dont repeat multiple registration to the namespace when discovering peers
686
+ if !self . discovery_publish_confirmed {
687
+ return ;
688
+ }
689
+
617
690
let ipfs = self . ipfs . clone ( ) ;
618
691
let fut = async move {
619
- ipfs. rendezvous_register_namespace ( & namespace, None , rz_peer_id)
620
- . await ?;
621
692
let peers = ipfs
622
693
. rendezvous_namespace_discovery ( & namespace, None , rz_peer_id)
623
694
. await ?;
@@ -697,6 +768,15 @@ impl Future for DiscoveryTask {
697
768
}
698
769
}
699
770
771
+ if let Some ( fut) = self . discovery_publish_fut . as_mut ( ) {
772
+ if let Poll :: Ready ( discovery_publish_type) = fut. poll_unpin ( cx) {
773
+ self . discovery_publish_fut . take ( ) ;
774
+ self . discovery_publish_confirmed =
775
+ !matches ! ( discovery_publish_type, DiscoveryPublish :: None ) ;
776
+ self . discovery ( None ) ;
777
+ }
778
+ }
779
+
700
780
while let Poll :: Ready ( Some ( ( peer_id, request, response) ) ) =
701
781
self . discovery_request_st . poll_next_unpin ( cx)
702
782
{
@@ -723,7 +803,10 @@ impl Future for DiscoveryTask {
723
803
pl. to_bytes ( ) . expect ( "valid payload" )
724
804
}
725
805
} ;
726
- _ = response. send ( bytes) ;
806
+
807
+ if response. send ( bytes) . is_err ( ) {
808
+ tracing:: warn!( %peer_id, "unable to respond to peer due to request being dropped." ) ;
809
+ }
727
810
}
728
811
729
812
if let Some ( fut) = self . discovery_fut . as_mut ( ) {
0 commit comments