From 48495c59fbb652903210d16bab166a6de08019e1 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Tue, 26 Nov 2024 12:07:11 -0600 Subject: [PATCH 1/3] expose test network cfg --- network/example_test.go | 15 ++- network/test_network.go | 227 +++++++++++++++++++++------------------- 2 files changed, 129 insertions(+), 113 deletions(-) diff --git a/network/example_test.go b/network/example_test.go index 0fef075f8101..c3ce25fa0fe9 100644 --- a/network/example_test.go +++ b/network/example_test.go @@ -89,12 +89,21 @@ func ExampleNewTestNetwork() { handler := &testExternalHandler{ log: log, } - - network, err := NewTestNetwork( - log, + cfg, err := NewTestNetworkConfig( constants.FujiID, validators, trackedSubnets, + ) + if err != nil { + log.Fatal( + "failed to create test network config", + zap.Error(err), + ) + return + } + network, err := NewTestNetwork( + log, + cfg, handler, ) if err != nil { diff --git a/network/test_network.go b/network/test_network.go index e0647210ee5a..e63580bebf00 100644 --- a/network/test_network.go +++ b/network/test_network.go @@ -73,23 +73,12 @@ func (*noopListener) Addr() net.Addr { } } -func NewTestNetwork( - log logging.Logger, +func NewTestNetworkConfig( networkID uint32, currentValidators validators.Manager, trackedSubnets set.Set[ids.ID], - router router.ExternalHandler, -) (Network, error) { +) (*Config, error) { metrics := prometheus.NewRegistry() - msgCreator, err := message.NewCreator( - logging.NoLog{}, - metrics, - constants.DefaultNetworkCompressionType, - constants.DefaultNetworkMaximumInboundTimeout, - ) - if err != nil { - return nil, err - } tlsCert, err := staking.NewTLSCert() if err != nil { @@ -113,110 +102,128 @@ func NewTestNetwork( if err != nil { return nil, err } - - return NewNetwork( - &Config{ - HealthConfig: HealthConfig{ - Enabled: true, - MinConnectedPeers: constants.DefaultNetworkHealthMinPeers, - MaxTimeSinceMsgReceived: constants.DefaultNetworkHealthMaxTimeSinceMsgReceived, - MaxTimeSinceMsgSent: constants.DefaultNetworkHealthMaxTimeSinceMsgSent, - MaxPortionSendQueueBytesFull: constants.DefaultNetworkHealthMaxPortionSendQueueFill, - MaxSendFailRate: constants.DefaultNetworkHealthMaxSendFailRate, - SendFailRateHalflife: constants.DefaultHealthCheckAveragerHalflife, - }, - PeerListGossipConfig: PeerListGossipConfig{ - PeerListNumValidatorIPs: constants.DefaultNetworkPeerListNumValidatorIPs, - PeerListPullGossipFreq: constants.DefaultNetworkPeerListPullGossipFreq, - PeerListBloomResetFreq: constants.DefaultNetworkPeerListBloomResetFreq, - }, - TimeoutConfig: TimeoutConfig{ - PingPongTimeout: constants.DefaultPingPongTimeout, - ReadHandshakeTimeout: constants.DefaultNetworkReadHandshakeTimeout, - }, - DelayConfig: DelayConfig{ - InitialReconnectDelay: constants.DefaultNetworkInitialReconnectDelay, - MaxReconnectDelay: constants.DefaultNetworkMaxReconnectDelay, + return &Config{ + HealthConfig: HealthConfig{ + Enabled: true, + MinConnectedPeers: constants.DefaultNetworkHealthMinPeers, + MaxTimeSinceMsgReceived: constants.DefaultNetworkHealthMaxTimeSinceMsgReceived, + MaxTimeSinceMsgSent: constants.DefaultNetworkHealthMaxTimeSinceMsgSent, + MaxPortionSendQueueBytesFull: constants.DefaultNetworkHealthMaxPortionSendQueueFill, + MaxSendFailRate: constants.DefaultNetworkHealthMaxSendFailRate, + SendFailRateHalflife: constants.DefaultHealthCheckAveragerHalflife, + }, + PeerListGossipConfig: PeerListGossipConfig{ + PeerListNumValidatorIPs: constants.DefaultNetworkPeerListNumValidatorIPs, + PeerListPullGossipFreq: constants.DefaultNetworkPeerListPullGossipFreq, + PeerListBloomResetFreq: constants.DefaultNetworkPeerListBloomResetFreq, + }, + TimeoutConfig: TimeoutConfig{ + PingPongTimeout: constants.DefaultPingPongTimeout, + ReadHandshakeTimeout: constants.DefaultNetworkReadHandshakeTimeout, + }, + DelayConfig: DelayConfig{ + InitialReconnectDelay: constants.DefaultNetworkInitialReconnectDelay, + MaxReconnectDelay: constants.DefaultNetworkMaxReconnectDelay, + }, + ThrottlerConfig: ThrottlerConfig{ + InboundConnUpgradeThrottlerConfig: throttling.InboundConnUpgradeThrottlerConfig{ + UpgradeCooldown: constants.DefaultInboundConnUpgradeThrottlerCooldown, + MaxRecentConnsUpgraded: int(math.Ceil(constants.DefaultInboundThrottlerMaxConnsPerSec * constants.DefaultInboundConnUpgradeThrottlerCooldown.Seconds())), }, - ThrottlerConfig: ThrottlerConfig{ - InboundConnUpgradeThrottlerConfig: throttling.InboundConnUpgradeThrottlerConfig{ - UpgradeCooldown: constants.DefaultInboundConnUpgradeThrottlerCooldown, - MaxRecentConnsUpgraded: int(math.Ceil(constants.DefaultInboundThrottlerMaxConnsPerSec * constants.DefaultInboundConnUpgradeThrottlerCooldown.Seconds())), + InboundMsgThrottlerConfig: throttling.InboundMsgThrottlerConfig{ + MsgByteThrottlerConfig: throttling.MsgByteThrottlerConfig{ + VdrAllocSize: constants.DefaultInboundThrottlerVdrAllocSize, + AtLargeAllocSize: constants.DefaultInboundThrottlerAtLargeAllocSize, + NodeMaxAtLargeBytes: constants.DefaultInboundThrottlerNodeMaxAtLargeBytes, }, - InboundMsgThrottlerConfig: throttling.InboundMsgThrottlerConfig{ - MsgByteThrottlerConfig: throttling.MsgByteThrottlerConfig{ - VdrAllocSize: constants.DefaultInboundThrottlerVdrAllocSize, - AtLargeAllocSize: constants.DefaultInboundThrottlerAtLargeAllocSize, - NodeMaxAtLargeBytes: constants.DefaultInboundThrottlerNodeMaxAtLargeBytes, - }, - BandwidthThrottlerConfig: throttling.BandwidthThrottlerConfig{ - RefillRate: constants.DefaultInboundThrottlerBandwidthRefillRate, - MaxBurstSize: constants.DefaultInboundThrottlerBandwidthMaxBurstSize, - }, - CPUThrottlerConfig: throttling.SystemThrottlerConfig{ - MaxRecheckDelay: constants.DefaultInboundThrottlerCPUMaxRecheckDelay, - }, - DiskThrottlerConfig: throttling.SystemThrottlerConfig{ - MaxRecheckDelay: constants.DefaultInboundThrottlerDiskMaxRecheckDelay, - }, - MaxProcessingMsgsPerNode: constants.DefaultInboundThrottlerMaxProcessingMsgsPerNode, + BandwidthThrottlerConfig: throttling.BandwidthThrottlerConfig{ + RefillRate: constants.DefaultInboundThrottlerBandwidthRefillRate, + MaxBurstSize: constants.DefaultInboundThrottlerBandwidthMaxBurstSize, }, - OutboundMsgThrottlerConfig: throttling.MsgByteThrottlerConfig{ - VdrAllocSize: constants.DefaultOutboundThrottlerVdrAllocSize, - AtLargeAllocSize: constants.DefaultOutboundThrottlerAtLargeAllocSize, - NodeMaxAtLargeBytes: constants.DefaultOutboundThrottlerNodeMaxAtLargeBytes, + CPUThrottlerConfig: throttling.SystemThrottlerConfig{ + MaxRecheckDelay: constants.DefaultInboundThrottlerCPUMaxRecheckDelay, }, - MaxInboundConnsPerSec: constants.DefaultInboundThrottlerMaxConnsPerSec, + DiskThrottlerConfig: throttling.SystemThrottlerConfig{ + MaxRecheckDelay: constants.DefaultInboundThrottlerDiskMaxRecheckDelay, + }, + MaxProcessingMsgsPerNode: constants.DefaultInboundThrottlerMaxProcessingMsgsPerNode, }, - ProxyEnabled: constants.DefaultNetworkTCPProxyEnabled, - ProxyReadHeaderTimeout: constants.DefaultNetworkTCPProxyReadTimeout, - DialerConfig: dialer.Config{ - ThrottleRps: constants.DefaultOutboundConnectionThrottlingRps, - ConnectionTimeout: constants.DefaultOutboundConnectionTimeout, + OutboundMsgThrottlerConfig: throttling.MsgByteThrottlerConfig{ + VdrAllocSize: constants.DefaultOutboundThrottlerVdrAllocSize, + AtLargeAllocSize: constants.DefaultOutboundThrottlerAtLargeAllocSize, + NodeMaxAtLargeBytes: constants.DefaultOutboundThrottlerNodeMaxAtLargeBytes, }, - TLSConfig: peer.TLSConfig(*tlsCert, nil), - MyIPPort: utils.NewAtomic(netip.AddrPortFrom( - netip.IPv4Unspecified(), - 1, - )), - NetworkID: networkID, - MaxClockDifference: constants.DefaultNetworkMaxClockDifference, - PingFrequency: constants.DefaultPingFrequency, - AllowPrivateIPs: !constants.ProductionNetworkIDs.Contains(networkID), - CompressionType: constants.DefaultNetworkCompressionType, - TLSKey: tlsCert.PrivateKey.(crypto.Signer), - BLSKey: blsKey, - TrackedSubnets: trackedSubnets, - Beacons: validators.NewManager(), - Validators: currentValidators, - UptimeCalculator: uptime.NoOpCalculator, - UptimeMetricFreq: constants.DefaultUptimeMetricFreq, - RequireValidatorToConnect: constants.DefaultNetworkRequireValidatorToConnect, - MaximumInboundMessageTimeout: constants.DefaultNetworkMaximumInboundTimeout, - PeerReadBufferSize: constants.DefaultNetworkPeerReadBufferSize, - PeerWriteBufferSize: constants.DefaultNetworkPeerWriteBufferSize, - ResourceTracker: resourceTracker, - CPUTargeter: tracker.NewTargeter( - logging.NoLog{}, - &tracker.TargeterConfig{ - VdrAlloc: float64(runtime.NumCPU()), - MaxNonVdrUsage: .8 * float64(runtime.NumCPU()), - MaxNonVdrNodeUsage: float64(runtime.NumCPU()) / 8, - }, - currentValidators, - resourceTracker.CPUTracker(), - ), - DiskTargeter: tracker.NewTargeter( - logging.NoLog{}, - &tracker.TargeterConfig{ - VdrAlloc: 1000 * units.GiB, - MaxNonVdrUsage: 1000 * units.GiB, - MaxNonVdrNodeUsage: 1000 * units.GiB, - }, - currentValidators, - resourceTracker.DiskTracker(), - ), + MaxInboundConnsPerSec: constants.DefaultInboundThrottlerMaxConnsPerSec, }, + ProxyEnabled: constants.DefaultNetworkTCPProxyEnabled, + ProxyReadHeaderTimeout: constants.DefaultNetworkTCPProxyReadTimeout, + DialerConfig: dialer.Config{ + ThrottleRps: constants.DefaultOutboundConnectionThrottlingRps, + ConnectionTimeout: constants.DefaultOutboundConnectionTimeout, + }, + TLSConfig: peer.TLSConfig(*tlsCert, nil), + MyIPPort: utils.NewAtomic(netip.AddrPortFrom( + netip.IPv4Unspecified(), + 1, + )), + NetworkID: networkID, + MaxClockDifference: constants.DefaultNetworkMaxClockDifference, + PingFrequency: constants.DefaultPingFrequency, + AllowPrivateIPs: !constants.ProductionNetworkIDs.Contains(networkID), + CompressionType: constants.DefaultNetworkCompressionType, + TLSKey: tlsCert.PrivateKey.(crypto.Signer), + BLSKey: blsKey, + TrackedSubnets: trackedSubnets, + Beacons: validators.NewManager(), + Validators: currentValidators, + UptimeCalculator: uptime.NoOpCalculator, + UptimeMetricFreq: constants.DefaultUptimeMetricFreq, + RequireValidatorToConnect: constants.DefaultNetworkRequireValidatorToConnect, + MaximumInboundMessageTimeout: constants.DefaultNetworkMaximumInboundTimeout, + PeerReadBufferSize: constants.DefaultNetworkPeerReadBufferSize, + PeerWriteBufferSize: constants.DefaultNetworkPeerWriteBufferSize, + ResourceTracker: resourceTracker, + CPUTargeter: tracker.NewTargeter( + logging.NoLog{}, + &tracker.TargeterConfig{ + VdrAlloc: float64(runtime.NumCPU()), + MaxNonVdrUsage: .8 * float64(runtime.NumCPU()), + MaxNonVdrNodeUsage: float64(runtime.NumCPU()) / 8, + }, + currentValidators, + resourceTracker.CPUTracker(), + ), + DiskTargeter: tracker.NewTargeter( + logging.NoLog{}, + &tracker.TargeterConfig{ + VdrAlloc: 1000 * units.GiB, + MaxNonVdrUsage: 1000 * units.GiB, + MaxNonVdrNodeUsage: 1000 * units.GiB, + }, + currentValidators, + resourceTracker.DiskTracker(), + ), + }, nil +} + +func NewTestNetwork( + log logging.Logger, + cfg *Config, + router router.ExternalHandler, +) (Network, error) { + metrics := prometheus.NewRegistry() + msgCreator, err := message.NewCreator( + logging.NoLog{}, + metrics, + constants.DefaultNetworkCompressionType, + constants.DefaultNetworkMaximumInboundTimeout, + ) + if err != nil { + return nil, err + } + + return NewNetwork( + cfg, upgrade.InitiallyActiveTime, msgCreator, metrics, From 9077256d5cc7a0ee0c0c58753a0752b21d874518 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Tue, 3 Dec 2024 16:05:55 -0600 Subject: [PATCH 2/3] user provides metrics --- network/example_test.go | 4 ++++ network/test_network.go | 5 ++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/network/example_test.go b/network/example_test.go index c3ce25fa0fe9..c0fe2ca74159 100644 --- a/network/example_test.go +++ b/network/example_test.go @@ -19,6 +19,7 @@ import ( "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/version" + "github.com/prometheus/client_golang/prometheus" ) var _ router.ExternalHandler = (*testExternalHandler)(nil) @@ -89,7 +90,9 @@ func ExampleNewTestNetwork() { handler := &testExternalHandler{ log: log, } + metrics := prometheus.NewRegistry() cfg, err := NewTestNetworkConfig( + metrics, constants.FujiID, validators, trackedSubnets, @@ -103,6 +106,7 @@ func ExampleNewTestNetwork() { } network, err := NewTestNetwork( log, + metrics, cfg, handler, ) diff --git a/network/test_network.go b/network/test_network.go index e63580bebf00..306325d44fcb 100644 --- a/network/test_network.go +++ b/network/test_network.go @@ -74,12 +74,11 @@ func (*noopListener) Addr() net.Addr { } func NewTestNetworkConfig( + metrics prometheus.Registerer, networkID uint32, currentValidators validators.Manager, trackedSubnets set.Set[ids.ID], ) (*Config, error) { - metrics := prometheus.NewRegistry() - tlsCert, err := staking.NewTLSCert() if err != nil { return nil, err @@ -208,10 +207,10 @@ func NewTestNetworkConfig( func NewTestNetwork( log logging.Logger, + metrics prometheus.Registerer, cfg *Config, router router.ExternalHandler, ) (Network, error) { - metrics := prometheus.NewRegistry() msgCreator, err := message.NewCreator( logging.NoLog{}, metrics, From 13e07a1b3a1efa93c19a350ec214a9d88fa3d368 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Tue, 3 Dec 2024 16:30:23 -0600 Subject: [PATCH 3/3] lint --- network/example_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/example_test.go b/network/example_test.go index c0fe2ca74159..4aaa8aa9ed07 100644 --- a/network/example_test.go +++ b/network/example_test.go @@ -8,6 +8,7 @@ import ( "os" "time" + "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" "github.com/ava-labs/avalanchego/genesis" @@ -19,7 +20,6 @@ import ( "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/version" - "github.com/prometheus/client_golang/prometheus" ) var _ router.ExternalHandler = (*testExternalHandler)(nil)