From 551b50acfa30e8dc9a8ff0a6abc937e07703b3db Mon Sep 17 00:00:00 2001 From: tycho garen Date: Thu, 16 Jun 2022 17:02:55 -0400 Subject: [PATCH] more fixes --- config/config.go | 8 ++++++++ internal/p2p/peermanager.go | 2 +- node/setup.go | 12 ++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 73ffe49c8..bebbf8d50 100644 --- a/config/config.go +++ b/config/config.go @@ -712,6 +712,10 @@ type P2PConfig struct { //nolint: maligned // outbound). MaxConnections uint16 `mapstructure:"max-connections"` + // MaxOutgoingConnections defines the maximum number of connected peers (inbound and + // outbound). + MaxOutgoingConnections uint16 `mapstructure:"max-outgoing-connections"` + // MaxIncomingConnectionAttempts rate limits the number of incoming connection // attempts per IP address. MaxIncomingConnectionAttempts uint `mapstructure:"max-incoming-connection-attempts"` @@ -774,6 +778,7 @@ func DefaultP2PConfig() *P2PConfig { MaxNumInboundPeers: 40, MaxNumOutboundPeers: 10, MaxConnections: 64, + MaxOutgoingConnections: 32, MaxIncomingConnectionAttempts: 100, PersistentPeersMaxDialPeriod: 0 * time.Second, FlushThrottleTimeout: 100 * time.Millisecond, @@ -833,6 +838,9 @@ func (cfg *P2PConfig) ValidateBasic() error { if cfg.RecvRate < 0 { return errors.New("recv-rate can't be negative") } + if cfg.MaxOutgoingConnections > cfg.MaxConnections { + return errors.New("max-outgoing-connections cannot be larger than max-connections") + } return nil } diff --git a/internal/p2p/peermanager.go b/internal/p2p/peermanager.go index f3b2fcf76..ed3b117ea 100644 --- a/internal/p2p/peermanager.go +++ b/internal/p2p/peermanager.go @@ -1005,7 +1005,7 @@ func (m *PeerManager) Advertise(peerID types.NodeID, limit uint16) []NodeAddress // peer. // nolint:gosec // G404: Use of weak random number generator - if numAddresses <= int(limit) || rand.Intn(totalScore+1) <= int(scores[peer.ID]+1) || rand.Intn((idx+1)*10) <= idx+1 { + if numAddresses <= int(limit) || rand.Intn(totalScore+1) <= scores[peer.ID]+1 || rand.Intn((idx+1)*10) <= idx+1 { addresses = append(addresses, addressInfo.Address) addedLastIteration = true seenAddresses[addressInfo.Address] = struct{}{} diff --git a/node/setup.go b/node/setup.go index 212254657..28a61a0d4 100644 --- a/node/setup.go +++ b/node/setup.go @@ -488,6 +488,14 @@ func createPeerManager( maxConns = 64 } + var maxOutgoingConns uint16 + switch { + case cfg.P2P.MaxOutgoingConnections > 0: + maxOutgoingConns = cfg.P2P.MaxOutgoingConnections + default: + maxOutgoingConns = maxConns / 2 + } + privatePeerIDs := make(map[types.NodeID]struct{}) for _, id := range tmstrings.SplitAndTrimEmpty(cfg.P2P.PrivatePeerIDs, ",", " ") { privatePeerIDs[types.NodeID(id)] = struct{}{} @@ -498,9 +506,9 @@ func createPeerManager( options := p2p.PeerManagerOptions{ SelfAddress: selfAddr, MaxConnected: maxConns, - MaxOutgoingConnections: maxConns / 2, + MaxOutgoingConnections: maxOutgoingConns, MaxConnectedUpgrade: maxUpgradeConns, - MaxPeers: maxUpgradeConns + 2*maxConns, + MaxPeers: maxUpgradeConns + 4*maxConns, MinRetryTime: 250 * time.Millisecond, MaxRetryTime: 30 * time.Minute, MaxRetryTimePersistent: 5 * time.Minute,