Skip to content

Commit

Permalink
p2p: add configuration for direct peers (#4769)
Browse files Browse the repository at this point in the history
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": []
}
```
  • Loading branch information
dshulyak committed Aug 3, 2023
1 parent 05e1351 commit 8d291aa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
16 changes: 12 additions & 4 deletions p2p/dhtdiscovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
8 changes: 8 additions & 0 deletions p2p/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 8d291aa

Please sign in to comment.