@@ -333,13 +333,20 @@ func (p *agent) Start(ctx context.Context) error {
333
333
return err
334
334
}
335
335
host .JoinOverlay ()
336
+ p .host = host
336
337
337
338
// connect to bootstrap nodes
338
- p .host = host
339
- if err := p .joinP2P (ctx ); err != nil {
340
- log .L ().Error ("fail to join p2p network" , zap .Error (err ))
339
+ if err := p .connectBootNode (ctx ); err != nil {
340
+ log .L ().Error ("fail to connect bootnode" , zap .Error (err ))
341
341
return err
342
342
}
343
+ if err := p .host .AdvertiseAsync (); err != nil {
344
+ return err
345
+ }
346
+ if err := p .host .FindPeersAsync (); err != nil {
347
+ return err
348
+ }
349
+
343
350
close (ready )
344
351
345
352
// check network connectivity every 60 blocks, and reconnect in case of disconnection
@@ -484,22 +491,10 @@ func (p *agent) BlockPeer(pidStr string) {
484
491
p .host .BlockPeer (pid )
485
492
}
486
493
487
- func (p * agent ) joinP2P (ctx context.Context ) error {
494
+ func (p * agent ) connectBootNode (ctx context.Context ) error {
488
495
if len (p .cfg .BootstrapNodes ) == 0 {
489
496
return nil
490
497
}
491
- if err := p .connectBootNode (ctx ); err != nil {
492
- return err
493
- }
494
- // it might take a few seconds to establish handshake with bootstrap
495
- if err := p .host .Advertise (); err != nil {
496
- return err
497
- }
498
- p .host .FindPeers (ctx )
499
- return nil
500
- }
501
-
502
- func (p * agent ) connectBootNode (ctx context.Context ) error {
503
498
var errNum , connNum , desiredConnNum int
504
499
conn := make (chan struct {}, len (p .cfg .BootstrapNodes ))
505
500
connErrChan := make (chan error , len (p .cfg .BootstrapNodes ))
@@ -550,13 +545,17 @@ func (p *agent) reconnect() {
550
545
if len (p .host .ConnectedPeers ()) == 0 || p .qosMetrics .lostConnection () {
551
546
log .L ().Info ("network lost, try re-connecting." )
552
547
p .host .ClearBlocklist ()
553
- if err := p .joinP2P (context .Background ()); err != nil {
554
- log .L ().Error ("fail to join p2p network" , zap .Error (err ))
548
+ if err := p .connectBootNode (context .Background ()); err != nil {
549
+ log .L ().Error ("fail to connect bootnode" , zap .Error (err ))
550
+ return
551
+ }
552
+ if err := p .host .AdvertiseAsync (); err != nil {
553
+ log .L ().Error ("fail to advertise" , zap .Error (err ))
554
+ return
555
555
}
556
- return
557
556
}
558
- if err := p .host .FindPeers ( context . Background () ); err != nil {
559
- log .L ().Error ("fail to find peers " , zap .Error (err ))
557
+ if err := p .host .FindPeersAsync ( ); err != nil {
558
+ log .L ().Error ("fail to find peer " , zap .Error (err ))
560
559
}
561
560
}
562
561
0 commit comments