Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose test network cfg #3573

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions network/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"time"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/genesis"
Expand Down Expand Up @@ -89,12 +90,24 @@ func ExampleNewTestNetwork() {
handler := &testExternalHandler{
log: log,
}

network, err := NewTestNetwork(
log,
metrics := prometheus.NewRegistry()
cfg, err := NewTestNetworkConfig(
metrics,
constants.FujiID,
validators,
trackedSubnets,
)
if err != nil {
log.Fatal(
"failed to create test network config",
zap.Error(err),
)
return
}
network, err := NewTestNetwork(
log,
metrics,
cfg,
handler,
)
if err != nil {
Expand Down
230 changes: 118 additions & 112 deletions network/test_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,12 @@ func (*noopListener) Addr() net.Addr {
}
}

func NewTestNetwork(
log logging.Logger,
func NewTestNetworkConfig(
metrics prometheus.Registerer,
networkID uint32,
currentValidators validators.Manager,
trackedSubnets set.Set[ids.ID],
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
}

) (*Config, error) {
tlsCert, err := staking.NewTLSCert()
if err != nil {
return nil, err
Expand All @@ -113,110 +101,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,
},
BandwidthThrottlerConfig: throttling.BandwidthThrottlerConfig{
RefillRate: constants.DefaultInboundThrottlerBandwidthRefillRate,
MaxBurstSize: constants.DefaultInboundThrottlerBandwidthMaxBurstSize,
},
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,
CPUThrottlerConfig: throttling.SystemThrottlerConfig{
MaxRecheckDelay: constants.DefaultInboundThrottlerCPUMaxRecheckDelay,
},
OutboundMsgThrottlerConfig: throttling.MsgByteThrottlerConfig{
VdrAllocSize: constants.DefaultOutboundThrottlerVdrAllocSize,
AtLargeAllocSize: constants.DefaultOutboundThrottlerAtLargeAllocSize,
NodeMaxAtLargeBytes: constants.DefaultOutboundThrottlerNodeMaxAtLargeBytes,
DiskThrottlerConfig: throttling.SystemThrottlerConfig{
MaxRecheckDelay: constants.DefaultInboundThrottlerDiskMaxRecheckDelay,
},
MaxInboundConnsPerSec: constants.DefaultInboundThrottlerMaxConnsPerSec,
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,
metrics prometheus.Registerer,
cfg *Config,
router router.ExternalHandler,
) (Network, error) {
msgCreator, err := message.NewCreator(
logging.NoLog{},
metrics,
constants.DefaultNetworkCompressionType,
constants.DefaultNetworkMaximumInboundTimeout,
)
if err != nil {
return nil, err
}

return NewNetwork(
cfg,
upgrade.InitiallyActiveTime,
msgCreator,
metrics,
Expand Down
Loading