Skip to content

Commit

Permalink
[p2p] Fix p2p agent start (#3580)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liuhaai authored and dustinxie committed Aug 3, 2022
1 parent 2048392 commit 7db8eaf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/iotexproject/go-fsm v1.0.0
github.com/iotexproject/go-p2p v0.3.4
github.com/iotexproject/go-p2p v0.3.5
github.com/iotexproject/go-pkgs v0.1.12
github.com/iotexproject/iotex-address v0.2.8
github.com/iotexproject/iotex-antenna-go/v2 v2.5.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ github.com/iotexproject/go-ethereum v0.4.0 h1:3GX+vZTI6KeazPabaw3oyLpPl1dOuyJyqq
github.com/iotexproject/go-ethereum v0.4.0/go.mod h1:pJNuIUYfX5+JKzSD/BTdNsvJSZ1TJqmz0dVyXMAbf6M=
github.com/iotexproject/go-fsm v1.0.0 h1:Zrg9JnNDUZg4Anpj6oa0Tk4+sXbHTpJzI0v5/Cj5N6A=
github.com/iotexproject/go-fsm v1.0.0/go.mod h1:t3aYXtCCcQxyS7oduQZyuUpPnVI4ddFTwbAagHN7fT0=
github.com/iotexproject/go-p2p v0.3.4 h1:V+Hq4K4lbEI9gAjHqKLpzS4lqCPqjeosRY/LnIpCVhU=
github.com/iotexproject/go-p2p v0.3.4/go.mod h1:P32N/mwLUWMQ/okMcNLlfBO7iD5ITyBOvN41roKdPdg=
github.com/iotexproject/go-p2p v0.3.5 h1:F71XxYQtR25youD+dCXnMgtfiCKGUFh8KDIqU7u5xOk=
github.com/iotexproject/go-p2p v0.3.5/go.mod h1:P32N/mwLUWMQ/okMcNLlfBO7iD5ITyBOvN41roKdPdg=
github.com/iotexproject/go-pkgs v0.1.5-0.20210604060651-be5ee19f2575/go.mod h1:ttXhcwrtODyh7JozpJlCml09CjP0pcKqTe2B0MbTGc8=
github.com/iotexproject/go-pkgs v0.1.6/go.mod h1:E+Fl0XQZz56EzXFajFET2yFoRGsIbL6wQooVIkSim3o=
github.com/iotexproject/go-pkgs v0.1.12 h1:WQcGDHxC9d2gv7r+8aBK7IgechL0XflimRkh+xN+ybI=
Expand Down
41 changes: 20 additions & 21 deletions p2p/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,20 @@ func (p *agent) Start(ctx context.Context) error {
return err
}
host.JoinOverlay()
p.host = host

// connect to bootstrap nodes
p.host = host
if err := p.joinP2P(ctx); err != nil {
log.L().Error("fail to join p2p network", zap.Error(err))
if err := p.connectBootNode(ctx); err != nil {
log.L().Error("fail to connect bootnode", zap.Error(err))
return err
}
if err := p.host.AdvertiseAsync(); err != nil {
return err
}
if err := p.host.FindPeersAsync(); err != nil {
return err
}

close(ready)

// check network connectivity every 60 blocks, and reconnect in case of disconnection
Expand Down Expand Up @@ -484,22 +491,10 @@ func (p *agent) BlockPeer(pidStr string) {
p.host.BlockPeer(pid)
}

func (p *agent) joinP2P(ctx context.Context) error {
func (p *agent) connectBootNode(ctx context.Context) error {
if len(p.cfg.BootstrapNodes) == 0 {
return nil
}
if err := p.connectBootNode(ctx); err != nil {
return err
}
// it might take a few seconds to establish handshake with bootstrap
if err := p.host.Advertise(); err != nil {
return err
}
p.host.FindPeers(ctx)
return nil
}

func (p *agent) connectBootNode(ctx context.Context) error {
var errNum, connNum, desiredConnNum int
conn := make(chan struct{}, len(p.cfg.BootstrapNodes))
connErrChan := make(chan error, len(p.cfg.BootstrapNodes))
Expand Down Expand Up @@ -550,13 +545,17 @@ func (p *agent) reconnect() {
if len(p.host.ConnectedPeers()) == 0 || p.qosMetrics.lostConnection() {
log.L().Info("network lost, try re-connecting.")
p.host.ClearBlocklist()
if err := p.joinP2P(context.Background()); err != nil {
log.L().Error("fail to join p2p network", zap.Error(err))
if err := p.connectBootNode(context.Background()); err != nil {
log.L().Error("fail to connect bootnode", zap.Error(err))
return
}
if err := p.host.AdvertiseAsync(); err != nil {
log.L().Error("fail to advertise", zap.Error(err))
return
}
return
}
if err := p.host.FindPeers(context.Background()); err != nil {
log.L().Error("fail to find peers", zap.Error(err))
if err := p.host.FindPeersAsync(); err != nil {
log.L().Error("fail to find peer", zap.Error(err))
}
}

Expand Down

0 comments on commit 7db8eaf

Please sign in to comment.