From 08516a048d99d80f8cc7ba58d435905110a94e68 Mon Sep 17 00:00:00 2001 From: noot <36753753+noot@users.noreply.github.com> Date: Fri, 23 Jul 2021 13:04:13 -0400 Subject: [PATCH] fix(dot/network): decrease DHT find peers interval for gssmr nodes (#1703) --- chain/gssmr/config.toml | 1 + chain/gssmr/defaults.go | 5 +++ cmd/gossamer/config.go | 8 +++++ cmd/gossamer/config_test.go | 67 ++++++++++++++++++++----------------- cmd/gossamer/export.go | 12 ++++--- cmd/gossamer/export_test.go | 33 +++++++++--------- dot/config.go | 27 ++++++++------- dot/config/toml/config.go | 17 +++++----- dot/network/config.go | 5 +++ dot/network/service.go | 4 +++ dot/services.go | 27 ++++++++------- 11 files changed, 123 insertions(+), 83 deletions(-) diff --git a/chain/gssmr/config.toml b/chain/gssmr/config.toml index f77d8064b9..893fd41a1d 100644 --- a/chain/gssmr/config.toml +++ b/chain/gssmr/config.toml @@ -29,6 +29,7 @@ grandpa-authority = true port = 7001 nobootstrap = false nomdns = false +discovery-interval = 10 [rpc] enabled = false diff --git a/chain/gssmr/defaults.go b/chain/gssmr/defaults.go index a0a5be6c95..b537f6b1a9 100644 --- a/chain/gssmr/defaults.go +++ b/chain/gssmr/defaults.go @@ -17,6 +17,8 @@ package gssmr import ( + "time" + "github.com/ChainSafe/gossamer/lib/runtime/wasmer" log "github.com/ChainSafe/log15" ) @@ -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 diff --git a/cmd/gossamer/config.go b/cmd/gossamer/config.go index 8ddd67902d..ce944d426d 100644 --- a/cmd/gossamer/config.go +++ b/cmd/gossamer/config.go @@ -20,6 +20,7 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/ChainSafe/gossamer/chain/dev" "github.com/ChainSafe/gossamer/chain/gssmr" @@ -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) @@ -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, ) } diff --git a/cmd/gossamer/config_test.go b/cmd/gossamer/config_test.go index bbe169c2a6..c308feb09a 100644 --- a/cmd/gossamer/config_test.go +++ b/cmd/gossamer/config_test.go @@ -19,6 +19,7 @@ package main import ( "io/ioutil" "testing" + "time" "github.com/ChainSafe/gossamer/chain/dev" "github.com/ChainSafe/gossamer/chain/gssmr" @@ -383,11 +384,12 @@ 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, }, }, { @@ -395,11 +397,12 @@ func TestNetworkConfigFromFlags(t *testing.T) { []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, }, }, { @@ -407,11 +410,12 @@ func TestNetworkConfigFromFlags(t *testing.T) { []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, }, }, { @@ -419,11 +423,12 @@ func TestNetworkConfigFromFlags(t *testing.T) { []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, }, }, { @@ -431,11 +436,12 @@ func TestNetworkConfigFromFlags(t *testing.T) { []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, }, }, } @@ -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, diff --git a/cmd/gossamer/export.go b/cmd/gossamer/export.go index b088137d2d..07c0a2a95e 100644 --- a/cmd/gossamer/export.go +++ b/cmd/gossamer/export.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "time" "github.com/ChainSafe/gossamer/dot" ctoml "github.com/ChainSafe/gossamer/dot/config/toml" @@ -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{ diff --git a/cmd/gossamer/export_test.go b/cmd/gossamer/export_test.go index ae247aabb4..2371638d82 100644 --- a/cmd/gossamer/export_test.go +++ b/cmd/gossamer/export_test.go @@ -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, }, @@ -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, }, @@ -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, }, diff --git a/dot/config.go b/dot/config.go index 4b626e9f4b..ef0b23ba41 100644 --- a/dot/config.go +++ b/dot/config.go @@ -18,6 +18,7 @@ package dot import ( "encoding/json" + "time" "github.com/ChainSafe/gossamer/chain/dev" "github.com/ChainSafe/gossamer/chain/gssmr" @@ -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 @@ -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, diff --git a/dot/config/toml/config.go b/dot/config/toml/config.go index cf6555f3b5..7cc270e590 100644 --- a/dot/config/toml/config.go +++ b/dot/config/toml/config.go @@ -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 diff --git a/dot/network/config.go b/dot/network/config.go index f9270207cf..8946a740d7 100644 --- a/dot/network/config.go +++ b/dot/network/config.go @@ -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 @@ -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 diff --git a/dot/network/service.go b/dot/network/service.go index 04cb19c749..fa89abf502 100644 --- a/dot/network/service.go +++ b/dot/network/service.go @@ -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 { diff --git a/dot/services.go b/dot/services.go index 170a6c2982..a74a85db8c 100644 --- a/dot/services.go +++ b/dot/services.go @@ -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)