@@ -333,13 +333,20 @@ func (p *agent) Start(ctx context.Context) error {
333333 return err
334334 }
335335 host .JoinOverlay ()
336+ p .host = host
336337
337338 // 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 ))
341341 return err
342342 }
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+
343350 close (ready )
344351
345352 // check network connectivity every 60 blocks, and reconnect in case of disconnection
@@ -484,22 +491,10 @@ func (p *agent) BlockPeer(pidStr string) {
484491 p .host .BlockPeer (pid )
485492}
486493
487- func (p * agent ) joinP2P (ctx context.Context ) error {
494+ func (p * agent ) connectBootNode (ctx context.Context ) error {
488495 if len (p .cfg .BootstrapNodes ) == 0 {
489496 return nil
490497 }
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 {
503498 var errNum , connNum , desiredConnNum int
504499 conn := make (chan struct {}, len (p .cfg .BootstrapNodes ))
505500 connErrChan := make (chan error , len (p .cfg .BootstrapNodes ))
@@ -550,13 +545,17 @@ func (p *agent) reconnect() {
550545 if len (p .host .ConnectedPeers ()) == 0 || p .qosMetrics .lostConnection () {
551546 log .L ().Info ("network lost, try re-connecting." )
552547 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
555555 }
556- return
557556 }
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 ))
560559 }
561560}
562561
0 commit comments