From b12a68994df0490a886a84cc307724d1658acf7c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 31 Oct 2019 15:04:28 +0300 Subject: [PATCH] network: make minpeers configurable Without it node in privnet tries to reconnect to four existing nodes indefinitely for no good reason. --- config/config.go | 1 + config/protocol.mainnet.yml | 1 + config/protocol.privnet.docker.four.yml | 1 + config/protocol.privnet.docker.one.yml | 1 + config/protocol.privnet.docker.three.yml | 1 + config/protocol.privnet.docker.two.yml | 1 + config/protocol.privnet.yml | 1 + config/protocol.testnet.yml | 1 + config/protocol.unit_testnet.yml | 1 + pkg/network/server.go | 5 ++--- pkg/network/server_config.go | 6 ++++++ 11 files changed, 17 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index f7a3cad4d..e348bcf10 100644 --- a/config/config.go +++ b/config/config.go @@ -73,6 +73,7 @@ type ( DialTimeout time.Duration `yaml:"DialTimeout"` ProtoTickInterval time.Duration `yaml:"ProtoTickInterval"` MaxPeers int `yaml:"MaxPeers"` + MinPeers int `yaml:"MinPeers"` Monitoring metrics.PrometheusConfig `yaml:"Monitoring"` } diff --git a/config/protocol.mainnet.yml b/config/protocol.mainnet.yml index 9613e13ad..b20b5a1b2 100644 --- a/config/protocol.mainnet.yml +++ b/config/protocol.mainnet.yml @@ -48,6 +48,7 @@ ApplicationConfiguration: DialTimeout: 3 ProtoTickInterval: 2 MaxPeers: 50 + MinPeers: 5 Monitoring: Enabled: true Port: 2112 diff --git a/config/protocol.privnet.docker.four.yml b/config/protocol.privnet.docker.four.yml index 1d930a45f..c92117da0 100644 --- a/config/protocol.privnet.docker.four.yml +++ b/config/protocol.privnet.docker.four.yml @@ -36,6 +36,7 @@ ApplicationConfiguration: DialTimeout: 3 ProtoTickInterval: 2 MaxPeers: 50 + MinPeers: 3 Monitoring: Enabled: true Port: 2112 diff --git a/config/protocol.privnet.docker.one.yml b/config/protocol.privnet.docker.one.yml index 1a3731e12..7c8f94d1d 100644 --- a/config/protocol.privnet.docker.one.yml +++ b/config/protocol.privnet.docker.one.yml @@ -33,6 +33,7 @@ ApplicationConfiguration: DialTimeout: 3 ProtoTickInterval: 2 MaxPeers: 50 + MinPeers: 3 Monitoring: Enabled: true Port: 2112 diff --git a/config/protocol.privnet.docker.three.yml b/config/protocol.privnet.docker.three.yml index f8ef52f91..db6ac5436 100644 --- a/config/protocol.privnet.docker.three.yml +++ b/config/protocol.privnet.docker.three.yml @@ -33,6 +33,7 @@ ApplicationConfiguration: DialTimeout: 3 ProtoTickInterval: 2 MaxPeers: 50 + MinPeers: 3 Monitoring: Enabled: true Port: 2112 diff --git a/config/protocol.privnet.docker.two.yml b/config/protocol.privnet.docker.two.yml index 197d42fdd..85d07045d 100644 --- a/config/protocol.privnet.docker.two.yml +++ b/config/protocol.privnet.docker.two.yml @@ -33,6 +33,7 @@ ApplicationConfiguration: DialTimeout: 3 ProtoTickInterval: 2 MaxPeers: 50 + MinPeers: 3 Monitoring: Enabled: true Port: 2112 diff --git a/config/protocol.privnet.yml b/config/protocol.privnet.yml index 66ab3791d..ac804131b 100644 --- a/config/protocol.privnet.yml +++ b/config/protocol.privnet.yml @@ -39,6 +39,7 @@ ApplicationConfiguration: DialTimeout: 3 ProtoTickInterval: 2 MaxPeers: 50 + MinPeers: 3 Monitoring: Enabled: true Port: 2112 diff --git a/config/protocol.testnet.yml b/config/protocol.testnet.yml index 09401bb04..3663565da 100644 --- a/config/protocol.testnet.yml +++ b/config/protocol.testnet.yml @@ -48,6 +48,7 @@ ApplicationConfiguration: DialTimeout: 3 ProtoTickInterval: 2 MaxPeers: 50 + MinPeers: 5 Monitoring: Enabled: true Port: 2112 diff --git a/config/protocol.unit_testnet.yml b/config/protocol.unit_testnet.yml index 7217d26fe..eff0453fe 100644 --- a/config/protocol.unit_testnet.yml +++ b/config/protocol.unit_testnet.yml @@ -38,6 +38,7 @@ ApplicationConfiguration: DialTimeout: 3 ProtoTickInterval: 2 MaxPeers: 50 + MinPeers: 1 Monitoring: Enabled: false #since it's not useful for unit tests. Port: 2112 diff --git a/pkg/network/server.go b/pkg/network/server.go index 69d167613..23d1c37ea 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -18,7 +18,6 @@ import ( const ( // peer numbers are arbitrary at the moment. - minPeers = 5 maxPeers = 20 maxBlockBatch = 200 maxAddrsToSend = 200 @@ -71,7 +70,7 @@ func NewServer(config ServerConfig, chain core.Blockchainer) *Server { bQueue: newBlockQueue(maxBlockBatch, chain), id: rand.Uint32(), quit: make(chan struct{}), - addrReq: make(chan *Message, minPeers), + addrReq: make(chan *Message, config.MinPeers), register: make(chan Peer), unregister: make(chan peerDrop), peers: make(map[Peer]bool), @@ -129,7 +128,7 @@ func (s *Server) BadPeers() []string { func (s *Server) run() { for { c := s.PeerCount() - if c < minPeers { + if c < s.ServerConfig.MinPeers { s.discovery.RequestRemote(maxPeers - c) } if s.discovery.PoolCount() < minPoolCount { diff --git a/pkg/network/server_config.go b/pkg/network/server_config.go index 434ecbc96..797b45b82 100644 --- a/pkg/network/server_config.go +++ b/pkg/network/server_config.go @@ -10,6 +10,11 @@ import ( type ( // ServerConfig holds the server configuration. ServerConfig struct { + // MinPeers is the minimum number of peers for normal operation, + // when the node has less than this number of peers it tries to + // connect with some new ones. + MinPeers int + // MaxPeers it the maximum numbers of peers that can // be connected to the server. MaxPeers int @@ -59,5 +64,6 @@ func NewServerConfig(cfg config.Config) ServerConfig { DialTimeout: appConfig.DialTimeout * time.Second, ProtoTickInterval: appConfig.ProtoTickInterval * time.Second, MaxPeers: appConfig.MaxPeers, + MinPeers: appConfig.MinPeers, } }