Skip to content

Commit

Permalink
fix(dot/network): decrease DHT find peers interval for gssmr nodes (C…
Browse files Browse the repository at this point in the history
  • Loading branch information
noot authored and timwu20 committed Dec 6, 2021
1 parent d0a6341 commit 7823c57
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 83 deletions.
1 change: 1 addition & 0 deletions chain/gssmr/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ grandpa-authority = true
port = 7001
nobootstrap = false
nomdns = false
discovery-interval = 10

[rpc]
enabled = false
Expand Down
5 changes: 5 additions & 0 deletions chain/gssmr/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package gssmr

import (
"time"

"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
log "github.com/ChainSafe/log15"
)
Expand Down Expand Up @@ -80,6 +82,9 @@ var (
// DefaultNoMDNS disables mDNS discovery
DefaultNoMDNS = false

// DefaultDiscoveryInterval is the default interval for searching for DHT peers
DefaultDiscoveryInterval = time.Second * 10

// RPCConfig

// DefaultRPCHTTPHost rpc host
Expand Down
8 changes: 8 additions & 0 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/chain/gssmr"
Expand Down Expand Up @@ -627,6 +628,12 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot
cfg.MaxPeers = tomlCfg.MaxPeers
cfg.PersistentPeers = tomlCfg.PersistentPeers

if tomlCfg.DiscoveryInterval > 0 {
cfg.DiscoveryInterval = time.Second * time.Duration(tomlCfg.DiscoveryInterval)
} else {
cfg.DiscoveryInterval = 0
}

// check --port flag and update node configuration
if port := ctx.GlobalUint(PortFlag.Name); port != 0 {
cfg.Port = uint32(port)
Expand Down Expand Up @@ -671,6 +678,7 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot
"minpeers", cfg.MinPeers,
"maxpeers", cfg.MaxPeers,
"persistent-peers", cfg.PersistentPeers,
"discovery-interval", cfg.DiscoveryInterval,
)
}

Expand Down
67 changes: 37 additions & 30 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"io/ioutil"
"testing"
"time"

"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/chain/gssmr"
Expand Down Expand Up @@ -383,59 +384,64 @@ func TestNetworkConfigFromFlags(t *testing.T) {
[]string{"config", "port"},
[]interface{}{testCfgFile.Name(), "1234"},
dot.NetworkConfig{
Port: 1234,
Bootnodes: testCfg.Network.Bootnodes,
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
Port: 1234,
Bootnodes: testCfg.Network.Bootnodes,
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
},
},
{
"Test gossamer --bootnodes",
[]string{"config", "bootnodes"},
[]interface{}{testCfgFile.Name(), "peer1,peer2"},
dot.NetworkConfig{
Port: testCfg.Network.Port,
Bootnodes: []string{"peer1", "peer2"},
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
Port: testCfg.Network.Port,
Bootnodes: []string{"peer1", "peer2"},
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
},
},
{
"Test gossamer --protocol",
[]string{"config", "protocol"},
[]interface{}{testCfgFile.Name(), "/gossamer/test/0"},
dot.NetworkConfig{
Port: testCfg.Network.Port,
Bootnodes: testCfg.Network.Bootnodes,
ProtocolID: "/gossamer/test/0",
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
Port: testCfg.Network.Port,
Bootnodes: testCfg.Network.Bootnodes,
ProtocolID: "/gossamer/test/0",
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
},
},
{
"Test gossamer --nobootstrap",
[]string{"config", "nobootstrap"},
[]interface{}{testCfgFile.Name(), "true"},
dot.NetworkConfig{
Port: testCfg.Network.Port,
Bootnodes: testCfg.Network.Bootnodes,
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: true,
NoMDNS: testCfg.Network.NoMDNS,
Port: testCfg.Network.Port,
Bootnodes: testCfg.Network.Bootnodes,
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: true,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
},
},
{
"Test gossamer --nomdns",
[]string{"config", "nomdns"},
[]interface{}{testCfgFile.Name(), "true"},
dot.NetworkConfig{
Port: testCfg.Network.Port,
Bootnodes: testCfg.Network.Bootnodes,
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: true,
Port: testCfg.Network.Port,
Bootnodes: testCfg.Network.Bootnodes,
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: true,
DiscoveryInterval: time.Second * 10,
},
},
}
Expand Down Expand Up @@ -804,11 +810,12 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {
Account: testCfg.Account,
Core: testCfg.Core,
Network: dot.NetworkConfig{
Port: testCfg.Network.Port,
Bootnodes: []string{}, // TODO: improve cmd tests #687
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
Port: testCfg.Network.Port,
Bootnodes: []string{}, // TODO: improve cmd tests #687
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: testCfg.Network.DiscoveryInterval,
},
RPC: testCfg.RPC,
System: testCfg.System,
Expand Down
12 changes: 7 additions & 5 deletions cmd/gossamer/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"fmt"
"time"

"github.com/ChainSafe/gossamer/dot"
ctoml "github.com/ChainSafe/gossamer/dot/config/toml"
Expand Down Expand Up @@ -117,11 +118,12 @@ func dotConfigToToml(dcfg *dot.Config) *ctoml.Config {
}

cfg.Network = ctoml.NetworkConfig{
Port: dcfg.Network.Port,
Bootnodes: dcfg.Network.Bootnodes,
ProtocolID: dcfg.Network.ProtocolID,
NoBootstrap: dcfg.Network.NoBootstrap,
NoMDNS: dcfg.Network.NoMDNS,
Port: dcfg.Network.Port,
Bootnodes: dcfg.Network.Bootnodes,
ProtocolID: dcfg.Network.ProtocolID,
NoBootstrap: dcfg.Network.NoBootstrap,
NoMDNS: dcfg.Network.NoMDNS,
DiscoveryInterval: int(dcfg.Network.DiscoveryInterval / time.Second),
}

cfg.RPC = ctoml.RPCConfig{
Expand Down
33 changes: 18 additions & 15 deletions cmd/gossamer/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ func TestExportCommand(t *testing.T) {
Account: testCfg.Account,
Core: testCfg.Core,
Network: dot.NetworkConfig{
Port: testCfg.Network.Port,
Bootnodes: testCfg.Network.Bootnodes, // TODO: improve cmd tests #687
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
Port: testCfg.Network.Port,
Bootnodes: testCfg.Network.Bootnodes, // TODO: improve cmd tests #687
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: testCfg.Network.DiscoveryInterval,
},
RPC: testCfg.RPC,
},
Expand All @@ -112,11 +113,12 @@ func TestExportCommand(t *testing.T) {
Account: testCfg.Account,
Core: testCfg.Core,
Network: dot.NetworkConfig{
Port: testCfg.Network.Port,
Bootnodes: []string{testBootnode},
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
Port: testCfg.Network.Port,
Bootnodes: []string{testBootnode},
ProtocolID: testCfg.Network.ProtocolID,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: testCfg.Network.DiscoveryInterval,
},
RPC: testCfg.RPC,
},
Expand All @@ -143,11 +145,12 @@ func TestExportCommand(t *testing.T) {
Account: testCfg.Account,
Core: testCfg.Core,
Network: dot.NetworkConfig{
Port: testCfg.Network.Port,
Bootnodes: []string{testBootnode}, // TODO: improve cmd tests #687
ProtocolID: testProtocol,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
Port: testCfg.Network.Port,
Bootnodes: []string{testBootnode}, // TODO: improve cmd tests #687
ProtocolID: testProtocol,
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: testCfg.Network.DiscoveryInterval,
},
RPC: testCfg.RPC,
},
Expand Down
27 changes: 15 additions & 12 deletions dot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package dot

import (
"encoding/json"
"time"

"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/chain/gssmr"
Expand Down Expand Up @@ -82,14 +83,15 @@ type AccountConfig struct {

// NetworkConfig is to marshal/unmarshal toml network config vars
type NetworkConfig struct {
Port uint32
Bootnodes []string
ProtocolID string
NoBootstrap bool
NoMDNS bool
MinPeers int
MaxPeers int
PersistentPeers []string
Port uint32
Bootnodes []string
ProtocolID string
NoBootstrap bool
NoMDNS bool
MinPeers int
MaxPeers int
PersistentPeers []string
DiscoveryInterval time.Duration
}

// CoreConfig is to marshal/unmarshal toml core config vars
Expand Down Expand Up @@ -166,10 +168,11 @@ func GssmrConfig() *Config {
WasmInterpreter: gssmr.DefaultWasmInterpreter,
},
Network: NetworkConfig{
Port: gssmr.DefaultNetworkPort,
Bootnodes: gssmr.DefaultNetworkBootnodes,
NoBootstrap: gssmr.DefaultNoBootstrap,
NoMDNS: gssmr.DefaultNoMDNS,
Port: gssmr.DefaultNetworkPort,
Bootnodes: gssmr.DefaultNetworkBootnodes,
NoBootstrap: gssmr.DefaultNoBootstrap,
NoMDNS: gssmr.DefaultNoMDNS,
DiscoveryInterval: gssmr.DefaultDiscoveryInterval,
},
RPC: RPCConfig{
Port: gssmr.DefaultRPCHTTPPort,
Expand Down
17 changes: 9 additions & 8 deletions dot/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ type AccountConfig struct {

// NetworkConfig is to marshal/unmarshal toml network config vars
type NetworkConfig struct {
Port uint32 `toml:"port,omitempty"`
Bootnodes []string `toml:"bootnodes,omitempty"`
ProtocolID string `toml:"protocol,omitempty"`
NoBootstrap bool `toml:"nobootstrap,omitempty"`
NoMDNS bool `toml:"nomdns,omitempty"`
MinPeers int `toml:"min-peers,omitempty"`
MaxPeers int `toml:"max-peers,omitempty"`
PersistentPeers []string `toml:"persistent-peers,omitempty"`
Port uint32 `toml:"port,omitempty"`
Bootnodes []string `toml:"bootnodes,omitempty"`
ProtocolID string `toml:"protocol,omitempty"`
NoBootstrap bool `toml:"nobootstrap,omitempty"`
NoMDNS bool `toml:"nomdns,omitempty"`
MinPeers int `toml:"min-peers,omitempty"`
MaxPeers int `toml:"max-peers,omitempty"`
PersistentPeers []string `toml:"persistent-peers,omitempty"`
DiscoveryInterval int `toml:"discovery-interval,omitempty"`
}

// CoreConfig is to marshal/unmarshal toml core config vars
Expand Down
5 changes: 5 additions & 0 deletions dot/network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const (

// DefaultMaxPeerCount is the default maximum peer count
DefaultMaxPeerCount = 50

// DefaultDiscoveryInterval is the default interval for searching for DHT peers
DefaultDiscoveryInterval = time.Minute * 5
)

// DefaultBootnodes the default value for Config.Bootnodes
Expand Down Expand Up @@ -86,6 +89,8 @@ type Config struct {
MinPeers int
MaxPeers int

DiscoveryInterval time.Duration

// PersistentPeers is a list of multiaddrs which the node should remain connected to
PersistentPeers []string

Expand Down
4 changes: 4 additions & 0 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func NewService(cfg *Config) (*Service, error) {
cfg.MaxPeers = DefaultMaxPeerCount
}

if cfg.DiscoveryInterval > 0 {
connectToPeersTimeout = cfg.DiscoveryInterval
}

// create a new host instance
host, err := newHost(ctx, cfg)
if err != nil {
Expand Down
27 changes: 14 additions & 13 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,20 @@ func createNetworkService(cfg *Config, stateSrvc *state.Service) (*network.Servi

// network service configuation
networkConfig := network.Config{
LogLvl: cfg.Log.NetworkLvl,
BlockState: stateSrvc.Block,
BasePath: cfg.Global.BasePath,
Roles: cfg.Core.Roles,
Port: cfg.Network.Port,
Bootnodes: cfg.Network.Bootnodes,
ProtocolID: cfg.Network.ProtocolID,
NoBootstrap: cfg.Network.NoBootstrap,
NoMDNS: cfg.Network.NoMDNS,
MinPeers: cfg.Network.MinPeers,
MaxPeers: cfg.Network.MaxPeers,
PublishMetrics: cfg.Global.PublishMetrics,
PersistentPeers: cfg.Network.PersistentPeers,
LogLvl: cfg.Log.NetworkLvl,
BlockState: stateSrvc.Block,
BasePath: cfg.Global.BasePath,
Roles: cfg.Core.Roles,
Port: cfg.Network.Port,
Bootnodes: cfg.Network.Bootnodes,
ProtocolID: cfg.Network.ProtocolID,
NoBootstrap: cfg.Network.NoBootstrap,
NoMDNS: cfg.Network.NoMDNS,
MinPeers: cfg.Network.MinPeers,
MaxPeers: cfg.Network.MaxPeers,
PublishMetrics: cfg.Global.PublishMetrics,
PersistentPeers: cfg.Network.PersistentPeers,
DiscoveryInterval: cfg.Network.DiscoveryInterval,
}

networkSrvc, err := network.NewService(&networkConfig)
Expand Down

0 comments on commit 7823c57

Please sign in to comment.