From 8d291aa8cea99c531b5ada756291dd82db085048 Mon Sep 17 00:00:00 2001 From: Dmitry Shulyak Date: Thu, 3 Aug 2023 09:38:21 +0000 Subject: [PATCH] p2p: add configuration for direct peers (#4769) direct peers are not pruned, and node will always try to establish connections with them regardless of the current number of connections. configuration is in p2p section, same format for address as bootnodes ```json "p2p": { "direct": [] } ``` --- p2p/dhtdiscovery/discovery.go | 16 ++++++++++++---- p2p/host.go | 1 + p2p/upgrade.go | 8 ++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/p2p/dhtdiscovery/discovery.go b/p2p/dhtdiscovery/discovery.go index 2c5c87bc8de..cfbf67fcfe8 100644 --- a/p2p/dhtdiscovery/discovery.go +++ b/p2p/dhtdiscovery/discovery.go @@ -54,6 +54,12 @@ func WithBackup(backup []peer.AddrInfo) Opt { } } +func WithDirect(direct []peer.AddrInfo) Opt { + return func(d *Discovery) { + d.direct = direct + } +} + func WithLogger(logger *zap.Logger) Opt { return func(d *Discovery) { d.logger = logger @@ -121,10 +127,10 @@ type Discovery struct { // how often to check if we have enough peers period time.Duration // timeout used for connections - timeout time.Duration - bootstrapDuration time.Duration - minPeers int - backup, bootnodes []peer.AddrInfo + timeout time.Duration + bootstrapDuration time.Duration + minPeers int + direct, backup, bootnodes []peer.AddrInfo } func (d *Discovery) Start() { @@ -151,6 +157,8 @@ func (d *Discovery) Start() { case <-ticker.C: case <-disconnected: } + // ensure we are always connected with direct peers + d.connect(&connEg, d.direct) if connected := len(d.h.Network().Peers()); connected >= d.minPeers { d.backup = nil // once got enough peers no need to keep backup, they are either already connected or unavailable d.logger.Debug("node is connected with required number of peers. skipping bootstrap", diff --git a/p2p/host.go b/p2p/host.go index 7f7f9bec893..1d8860ba727 100644 --- a/p2p/host.go +++ b/p2p/host.go @@ -63,6 +63,7 @@ type Config struct { Flood bool `mapstructure:"flood"` Listen string `mapstructure:"listen"` Bootnodes []string `mapstructure:"bootnodes"` + Direct []string `mapstructure:"direct"` MinPeers int `mapstructure:"min-peers"` LowPeers int `mapstructure:"low-peers"` HighPeers int `mapstructure:"high-peers"` diff --git a/p2p/upgrade.go b/p2p/upgrade.go index 0bf6ba52bb6..038ade55b8c 100644 --- a/p2p/upgrade.go +++ b/p2p/upgrade.go @@ -93,6 +93,13 @@ func Upgrade(h host.Host, opts ...Opt) (*Host, error) { if err != nil { return nil, err } + direct, err := parseIntoAddr(fh.cfg.Bootnodes) + if err != nil { + return nil, err + } + for _, peer := range direct { + h.ConnManager().Protect(peer.ID, "direct") + } if fh.PubSub, err = pubsub.New(fh.ctx, fh.logger, h, pubsub.Config{ Flood: cfg.Flood, IsBootnode: cfg.Bootnode, @@ -117,6 +124,7 @@ func Upgrade(h host.Host, opts ...Opt) (*Host, error) { dopts := []discovery.Opt{ discovery.WithDir(cfg.DataDir), discovery.WithBootnodes(bootnodes), + discovery.WithDirect(direct), discovery.WithLogger(fh.logger.Zap()), } if cfg.PrivateNetwork {