Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - p2p: add configuration for direct peers #4769

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
if err != nil {
return nil, err
}
direct, err := parseIntoAddr(fh.cfg.Bootnodes)
if err != nil {
return nil, err
}

Check warning on line 99 in p2p/upgrade.go

View check run for this annotation

Codecov / codecov/patch

p2p/upgrade.go#L98-L99

Added lines #L98 - L99 were not covered by tests
for _, peer := range direct {
h.ConnManager().Protect(peer.ID, "direct")
}

Check warning on line 102 in p2p/upgrade.go

View check run for this annotation

Codecov / codecov/patch

p2p/upgrade.go#L101-L102

Added lines #L101 - L102 were not covered by tests
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 @@
dopts := []discovery.Opt{
discovery.WithDir(cfg.DataDir),
discovery.WithBootnodes(bootnodes),
discovery.WithDirect(direct),
discovery.WithLogger(fh.logger.Zap()),
}
if cfg.PrivateNetwork {
Expand Down