Skip to content

Commit

Permalink
p2p: enable discovery after all local protocols were registered (#4546)
Browse files Browse the repository at this point in the history
related: #4542

libp2p communicates the set of enabled protocols after completing secure handshake.
if some of the protocols were not registered before completing initial identify protocol round, it will result in errors
as in attached github issue.
  • Loading branch information
dshulyak committed Jun 19, 2023
1 parent 2bd60cb commit e1f02d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,9 @@ func (app *App) initServices(ctx context.Context, poetClients []activation.PoetP
peersync.WithConfig(app.Config.TIME.Peersync),
)
}

if err := app.host.Start(); err != nil {
return err
}
return nil
}

Expand Down
10 changes: 8 additions & 2 deletions p2p/peerexchange/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Discovery struct {
host host.Host
cfg Config

ctx context.Context
cancel context.CancelFunc
eg errgroup.Group

Expand All @@ -60,6 +61,7 @@ func New(logger log.Log, h host.Host, config Config) (*Discovery, error) {
cfg: config,
logger: logger,
host: h,
ctx: ctx,
cancel: cancel,
book: book.New(),
}
Expand Down Expand Up @@ -103,10 +105,15 @@ func New(logger log.Log, h host.Host, config Config) (*Discovery, error) {
return nil, err
}
}
d.scanPeers(ctx)
return d, nil
}

// StartScan starts background goroutine to connect with known peers
// and scan for new ones.
func (d *Discovery) StartScan() {
d.scanPeers(d.ctx)
}

func (d *Discovery) recovery(ctx context.Context) error {
fpath := filepath.Join(d.cfg.DataDir, peersFile)
f, err := os.Open(fpath)
Expand Down Expand Up @@ -203,7 +210,6 @@ func (d *Discovery) scanPeers(ctx context.Context) {
}
return err
}

}
})
}
Expand Down
1 change: 1 addition & 0 deletions p2p/peerexchange/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestDiscovery_CrawlMesh(t *testing.T) {
cfg.Bootnodes = append(cfg.Bootnodes, bootnode.String())
instance, err := New(logger, h, cfg)
require.NoError(t, err)
instance.StartScan()
t.Cleanup(instance.Stop)
}

Expand Down
23 changes: 23 additions & 0 deletions p2p/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package p2p

import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/libp2p/go-libp2p/core/host"
Expand Down Expand Up @@ -53,6 +55,11 @@ type Host struct {
cfg Config
logger log.Log

closed struct {
sync.Mutex
closed bool
}

host.Host
*pubsub.PubSub

Expand Down Expand Up @@ -131,8 +138,24 @@ func (fh *Host) PeerCount() uint64 {
return uint64(len(fh.Host.Network().Peers()))
}

func (fh *Host) Start() error {
fh.closed.Lock()
defer fh.closed.Unlock()
if fh.closed.closed {
return errors.New("p2p: closed")
}
fh.discovery.StartScan()
return nil
}

// Stop background workers and release external resources.
func (fh *Host) Stop() error {
fh.closed.Lock()
defer fh.closed.Unlock()
if fh.closed.closed {
return errors.New("p2p: closed")
}
fh.closed.closed = true
fh.discovery.Stop()
if err := fh.Host.Close(); err != nil {
return fmt.Errorf("failed to close libp2p host: %w", err)
Expand Down

0 comments on commit e1f02d7

Please sign in to comment.