@@ -303,7 +303,7 @@ type RoutingBehaviour struct {
303
303
explore coordt.StateMachine [routing.ExploreEvent , routing.ExploreState ]
304
304
305
305
// crawl is the state machine that can crawl the network from a set of seed nodes
306
- crawl coordt.StateMachine [routing.ExploreEvent , routing.ExploreState ]
306
+ crawl coordt.StateMachine [routing.CrawlEvent , routing.CrawlState ]
307
307
308
308
pendingMu sync.Mutex
309
309
pending []BehaviourEvent
@@ -374,9 +374,14 @@ func NewRoutingBehaviour(self kadt.PeerID, rt routing.RoutingTableCpl[kadt.Key,
374
374
return nil , fmt .Errorf ("explore: %w" , err )
375
375
}
376
376
377
- // crawl, err := routing.NewCrawl(self )
377
+ crawlCfg := routing .DefaultCrawlConfig ( )
378
378
379
- return ComposeRoutingBehaviour (self , bootstrap , include , probe , explore , cfg )
379
+ crawl , err := routing .NewCrawl (self , cplutil .GenRandPeerID , crawlCfg )
380
+ if err != nil {
381
+ return nil , fmt .Errorf ("crawl: %w" , err )
382
+ }
383
+
384
+ return ComposeRoutingBehaviour (self , bootstrap , include , probe , explore , crawl , cfg )
380
385
}
381
386
382
387
// ComposeRoutingBehaviour creates a [RoutingBehaviour] composed of the supplied state machines.
@@ -387,6 +392,7 @@ func ComposeRoutingBehaviour(
387
392
include coordt.StateMachine [routing.IncludeEvent , routing.IncludeState ],
388
393
probe coordt.StateMachine [routing.ProbeEvent , routing.ProbeState ],
389
394
explore coordt.StateMachine [routing.ExploreEvent , routing.ExploreState ],
395
+ crawl coordt.StateMachine [routing.CrawlEvent , routing.CrawlState ],
390
396
cfg * RoutingConfig ,
391
397
) (* RoutingBehaviour , error ) {
392
398
if cfg == nil {
@@ -402,6 +408,7 @@ func ComposeRoutingBehaviour(
402
408
include : include ,
403
409
probe : probe ,
404
410
explore : explore ,
411
+ crawl : crawl ,
405
412
ready : make (chan struct {}, 1 ),
406
413
}
407
414
return r , nil
@@ -418,12 +425,11 @@ func (r *RoutingBehaviour) Notify(ctx context.Context, ev BehaviourEvent) {
418
425
419
426
// notify must only be called while r.pendingMu is held
420
427
func (r * RoutingBehaviour ) notify (ctx context.Context , ev BehaviourEvent ) {
421
- ctx , span := r .cfg .Tracer .Start (ctx , "RoutingBehaviour.notify" , trace .WithAttributes (attribute . String ( "event" , fmt . Sprintf ( "%T" , ev ) )))
428
+ ctx , span := r .cfg .Tracer .Start (ctx , "RoutingBehaviour.notify" , trace .WithAttributes (tele . AttrInEvent ( ev )))
422
429
defer span .End ()
423
430
424
431
switch ev := ev .(type ) {
425
432
case * EventStartBootstrap :
426
- span .SetAttributes (attribute .String ("event" , "EventStartBootstrap" ))
427
433
cmd := & routing.EventBootstrapStart [kadt.Key , kadt.PeerID ]{
428
434
KnownClosestNodes : ev .SeedNodes ,
429
435
}
@@ -433,8 +439,17 @@ func (r *RoutingBehaviour) notify(ctx context.Context, ev BehaviourEvent) {
433
439
r .pending = append (r .pending , next )
434
440
}
435
441
442
+ case * EventStartCrawl :
443
+ cmd := & routing.EventCrawlStart [kadt.Key , kadt.PeerID ]{
444
+ Seed : ev .Seed ,
445
+ }
446
+ // attempt to advance the bootstrap
447
+ next , ok := r .advanceCrawl (ctx , cmd )
448
+ if ok {
449
+ r .pending = append (r .pending , next )
450
+ }
451
+
436
452
case * EventAddNode :
437
- span .SetAttributes (attribute .String ("event" , "EventAddAddrInfo" ))
438
453
// Ignore self
439
454
if r .self .Equal (ev .NodeID ) {
440
455
break
@@ -450,7 +465,7 @@ func (r *RoutingBehaviour) notify(ctx context.Context, ev BehaviourEvent) {
450
465
}
451
466
452
467
case * EventRoutingUpdated :
453
- span .SetAttributes (attribute .String ("event" , "EventRoutingUpdated" ), attribute . String ( " nodeid" , ev .NodeID .String ()))
468
+ span .SetAttributes (attribute .String ("nodeid" , ev .NodeID .String ()))
454
469
cmd := & routing.EventProbeAdd [kadt.Key , kadt.PeerID ]{
455
470
NodeID : ev .NodeID ,
456
471
}
@@ -533,9 +548,23 @@ func (r *RoutingBehaviour) notify(ctx context.Context, ev BehaviourEvent) {
533
548
r .pending = append (r .pending , next )
534
549
}
535
550
551
+ case routing .CrawlQueryID :
552
+ cmd := & routing.EventCrawlNodeResponse [kadt.Key , kadt.PeerID ]{
553
+ NodeID : ev .To ,
554
+ Target : ev .Target ,
555
+ CloserNodes : ev .CloserNodes ,
556
+ }
557
+
558
+ // attempt to advance the crawl
559
+ next , ok := r .advanceCrawl (ctx , cmd )
560
+ if ok {
561
+ r .pending = append (r .pending , next )
562
+ }
563
+
536
564
default :
537
565
panic (fmt .Sprintf ("unexpected query id: %s" , ev .QueryID ))
538
566
}
567
+
539
568
case * EventGetCloserNodesFailure :
540
569
span .SetAttributes (attribute .String ("event" , "EventGetCloserNodesFailure" ), attribute .String ("queryid" , string (ev .QueryID )), attribute .String ("nodeid" , ev .To .String ()))
541
570
span .RecordError (ev .Err )
@@ -580,10 +609,22 @@ func (r *RoutingBehaviour) notify(ctx context.Context, ev BehaviourEvent) {
580
609
if ok {
581
610
r .pending = append (r .pending , next )
582
611
}
612
+ case routing .CrawlQueryID :
613
+ cmd := & routing.EventCrawlNodeFailure [kadt.Key , kadt.PeerID ]{
614
+ NodeID : ev .To ,
615
+ Target : ev .Target ,
616
+ Error : ev .Err ,
617
+ }
618
+ // attempt to advance the crawl
619
+ next , ok := r .advanceCrawl (ctx , cmd )
620
+ if ok {
621
+ r .pending = append (r .pending , next )
622
+ }
583
623
584
624
default :
585
625
panic (fmt .Sprintf ("unexpected query id: %s" , ev .QueryID ))
586
626
}
627
+
587
628
case * EventNotifyConnectivity :
588
629
span .SetAttributes (attribute .String ("event" , "EventNotifyConnectivity" ), attribute .String ("nodeid" , ev .NodeID .String ()))
589
630
// ignore self
@@ -609,6 +650,7 @@ func (r *RoutingBehaviour) notify(ctx context.Context, ev BehaviourEvent) {
609
650
if ok {
610
651
r .pending = append (r .pending , nextProbe )
611
652
}
653
+
612
654
case * EventNotifyNonConnectivity :
613
655
span .SetAttributes (attribute .String ("event" , "EventNotifyConnectivity" ), attribute .String ("nodeid" , ev .NodeID .String ()))
614
656
@@ -620,6 +662,7 @@ func (r *RoutingBehaviour) notify(ctx context.Context, ev BehaviourEvent) {
620
662
if ok {
621
663
r .pending = append (r .pending , nextProbe )
622
664
}
665
+
623
666
case * EventRoutingPoll :
624
667
r .pollChildren (ctx )
625
668
@@ -693,6 +736,11 @@ func (r *RoutingBehaviour) pollChildren(ctx context.Context) {
693
736
if ok {
694
737
r .pending = append (r .pending , ev )
695
738
}
739
+
740
+ ev , ok = r .advanceCrawl (ctx , & routing.EventCrawlPoll {})
741
+ if ok {
742
+ r .pending = append (r .pending , ev )
743
+ }
696
744
}
697
745
698
746
func (r * RoutingBehaviour ) advanceBootstrap (ctx context.Context , ev routing.BootstrapEvent ) (BehaviourEvent , bool ) {
@@ -817,9 +865,9 @@ func (r *RoutingBehaviour) advanceProbe(ctx context.Context, ev routing.ProbeEve
817
865
func (r * RoutingBehaviour ) advanceExplore (ctx context.Context , ev routing.ExploreEvent ) (BehaviourEvent , bool ) {
818
866
ctx , span := r .cfg .Tracer .Start (ctx , "RoutingBehaviour.advanceExplore" )
819
867
defer span .End ()
868
+
820
869
bstate := r .explore .Advance (ctx , ev )
821
870
switch st := bstate .(type ) {
822
-
823
871
case * routing.StateExploreFindCloser [kadt.Key , kadt.PeerID ]:
824
872
r .cfg .Logger .Debug ("starting explore" , slog .Int ("cpl" , st .Cpl ), tele .LogAttrPeerID (st .NodeID ))
825
873
return & EventOutboundGetCloserNodes {
@@ -845,3 +893,32 @@ func (r *RoutingBehaviour) advanceExplore(ctx context.Context, ev routing.Explor
845
893
846
894
return nil , false
847
895
}
896
+
897
+ func (r * RoutingBehaviour ) advanceCrawl (ctx context.Context , ev routing.CrawlEvent ) (BehaviourEvent , bool ) {
898
+ ctx , span := r .cfg .Tracer .Start (ctx , "RoutingBehaviour.advanceCrawl" )
899
+ defer span .End ()
900
+
901
+ cstate := r .crawl .Advance (ctx , ev )
902
+ switch st := cstate .(type ) {
903
+ case * routing.StateCrawlFindCloser [kadt.Key , kadt.PeerID ]:
904
+ return & EventOutboundGetCloserNodes {
905
+ QueryID : routing .CrawlQueryID ,
906
+ To : st .NodeID ,
907
+ Target : st .Target ,
908
+ Notify : r ,
909
+ }, true
910
+
911
+ case * routing.StateCrawlWaitingWithCapacity :
912
+ // crawl waiting for a message response but has capacity to do more
913
+ case * routing.StateCrawlWaitingAtCapacity :
914
+ // crawl waiting for a message response but has no capacity to do more
915
+ case * routing.StateCrawlFinished :
916
+ r .cfg .Logger .Info ("crawl finished" )
917
+ case * routing.StateCrawlIdle :
918
+ // bootstrap not running, nothing to do
919
+ default :
920
+ panic (fmt .Sprintf ("unexpected explore state: %T" , st ))
921
+ }
922
+
923
+ return nil , false
924
+ }
0 commit comments