Skip to content

Commit

Permalink
Standardize config keys (ava-labs#1370)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur committed May 18, 2023
1 parent 49b71b4 commit 85c1d24
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 70 deletions.
8 changes: 4 additions & 4 deletions chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ type ManagerConfig struct {
MeterVMEnabled bool // Should each VM be wrapped with a MeterVM
Metrics metrics.MultiGatherer

ConsensusGossipFrequency time.Duration
ConsensusAppConcurrency int
AcceptedFrontierGossipFrequency time.Duration
ConsensusAppConcurrency int

// Max Time to spend fetching a container and its
// ancestors when responding to a GetAncestors
Expand Down Expand Up @@ -817,7 +817,7 @@ func (m *manager) createAvalancheChain(
ctx,
vdrs,
msgChan,
m.ConsensusGossipFrequency,
m.AcceptedFrontierGossipFrequency,
m.ConsensusAppConcurrency,
m.ResourceTracker,
validators.UnhandledSubnetConnector, // avalanche chains don't use subnet connector
Expand Down Expand Up @@ -1151,7 +1151,7 @@ func (m *manager) createSnowmanChain(
ctx,
vdrs,
msgChan,
m.ConsensusGossipFrequency,
m.AcceptedFrontierGossipFrequency,
m.ConsensusAppConcurrency,
m.ResourceTracker,
subnetConnector,
Expand Down
101 changes: 62 additions & 39 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,38 @@ const (
var (
// Deprecated key --> deprecation message (i.e. which key replaces it)
deprecatedKeys = map[string]string{
NetworkCompressionEnabledKey: fmt.Sprintf("use --%s instead", NetworkCompressionTypeKey),
}

errInvalidStakerWeights = errors.New("staking weights must be positive")
errStakingDisableOnPublicNetwork = errors.New("staking disabled on public network")
errAuthPasswordTooWeak = errors.New("API auth password is not strong enough")
errInvalidUptimeRequirement = errors.New("uptime requirement must be in the range [0, 1]")
errMinValidatorStakeAboveMax = errors.New("minimum validator stake can't be greater than maximum validator stake")
errInvalidDelegationFee = errors.New("delegation fee must be in the range [0, 1,000,000]")
errInvalidMinStakeDuration = errors.New("min stake duration must be > 0")
errMinStakeDurationAboveMax = errors.New("max stake duration can't be less than min stake duration")
errStakeMaxConsumptionTooLarge = fmt.Errorf("max stake consumption must be less than or equal to %d", reward.PercentDenominator)
errStakeMaxConsumptionBelowMin = errors.New("stake max consumption can't be less than min stake consumption")
errStakeMintingPeriodBelowMin = errors.New("stake minting period can't be less than max stake duration")
errCannotTrackPrimaryNetwork = errors.New("cannot track primary network")
errStakingKeyContentUnset = fmt.Errorf("%s key not set but %s set", StakingTLSKeyContentKey, StakingCertContentKey)
errStakingCertContentUnset = fmt.Errorf("%s key set but %s not set", StakingTLSKeyContentKey, StakingCertContentKey)
errMissingStakingSigningKeyFile = errors.New("missing staking signing key file")
errTracingEndpointEmpty = fmt.Errorf("%s cannot be empty", TracingEndpointKey)
errPluginDirNotADirectory = errors.New("plugin dir is not a directory")
errCannotReadDirectory = errors.New("cannot read directory")
errUnmarshalling = errors.New("unmarshalling failed")
errFileDoesNotExist = errors.New("file does not exist")
NetworkCompressionEnabledKey: fmt.Sprintf("use --%s instead", NetworkCompressionTypeKey),
GenesisConfigFileKey: fmt.Sprintf("use --%s instead", GenesisFileKey),
GenesisConfigContentKey: fmt.Sprintf("use --%s instead", GenesisFileContentKey),
InboundConnUpgradeThrottlerCooldownKey: fmt.Sprintf("use --%s instead", NetworkInboundConnUpgradeThrottlerCooldownKey),
InboundThrottlerMaxConnsPerSecKey: fmt.Sprintf("use --%s instead", NetworkInboundThrottlerMaxConnsPerSecKey),
OutboundConnectionThrottlingRpsKey: fmt.Sprintf("use --%s instead", NetworkOutboundConnectionThrottlingRpsKey),
OutboundConnectionTimeoutKey: fmt.Sprintf("use --%s instead", NetworkOutboundConnectionTimeoutKey),
StakingEnabledKey: fmt.Sprintf("use --%s instead", SybilProtectionEnabledKey),
StakingDisabledWeightKey: fmt.Sprintf("use --%s instead", SybilProtectionDisabledWeightKey),
ConsensusGossipFrequencyKey: fmt.Sprintf("use --%s instead", ConsensusAcceptedFrontierGossipFrequencyKey),
}

errSybilProtectionDisabledStakerWeights = errors.New("sybil protection disabled weights must be positive")
errSybilProtectionDisabledOnPublicNetwork = errors.New("sybil protection disabled on public network")
errAuthPasswordTooWeak = errors.New("API auth password is not strong enough")
errInvalidUptimeRequirement = errors.New("uptime requirement must be in the range [0, 1]")
errMinValidatorStakeAboveMax = errors.New("minimum validator stake can't be greater than maximum validator stake")
errInvalidDelegationFee = errors.New("delegation fee must be in the range [0, 1,000,000]")
errInvalidMinStakeDuration = errors.New("min stake duration must be > 0")
errMinStakeDurationAboveMax = errors.New("max stake duration can't be less than min stake duration")
errStakeMaxConsumptionTooLarge = fmt.Errorf("max stake consumption must be less than or equal to %d", reward.PercentDenominator)
errStakeMaxConsumptionBelowMin = errors.New("stake max consumption can't be less than min stake consumption")
errStakeMintingPeriodBelowMin = errors.New("stake minting period can't be less than max stake duration")
errCannotTrackPrimaryNetwork = errors.New("cannot track primary network")
errStakingKeyContentUnset = fmt.Errorf("%s key not set but %s set", StakingTLSKeyContentKey, StakingCertContentKey)
errStakingCertContentUnset = fmt.Errorf("%s key set but %s not set", StakingTLSKeyContentKey, StakingCertContentKey)
errMissingStakingSigningKeyFile = errors.New("missing staking signing key file")
errTracingEndpointEmpty = fmt.Errorf("%s cannot be empty", TracingEndpointKey)
errPluginDirNotADirectory = errors.New("plugin dir is not a directory")
errCannotReadDirectory = errors.New("cannot read directory")
errUnmarshalling = errors.New("unmarshalling failed")
errFileDoesNotExist = errors.New("file does not exist")
)

func getConsensusConfig(v *viper.Viper) snowball.Parameters {
Expand Down Expand Up @@ -310,8 +319,8 @@ func getNetworkConfig(
) (network.Config, error) {
// Set the max number of recent inbound connections upgraded to be
// equal to the max number of inbound connections per second.
maxInboundConnsPerSec := v.GetFloat64(InboundThrottlerMaxConnsPerSecKey)
upgradeCooldown := v.GetDuration(InboundConnUpgradeThrottlerCooldownKey)
maxInboundConnsPerSec := v.GetFloat64(getRenamedKey(v, InboundThrottlerMaxConnsPerSecKey, NetworkInboundThrottlerMaxConnsPerSecKey))
upgradeCooldown := v.GetDuration(getRenamedKey(v, InboundConnUpgradeThrottlerCooldownKey, NetworkInboundConnUpgradeThrottlerCooldownKey))
upgradeCooldownInSeconds := upgradeCooldown.Seconds()
maxRecentConnsUpgraded := int(math.Ceil(maxInboundConnsPerSec * upgradeCooldownInSeconds))

Expand Down Expand Up @@ -384,8 +393,8 @@ func getNetworkConfig(
ProxyReadHeaderTimeout: v.GetDuration(NetworkTCPProxyReadTimeoutKey),

DialerConfig: dialer.Config{
ThrottleRps: v.GetUint32(OutboundConnectionThrottlingRpsKey),
ConnectionTimeout: v.GetDuration(OutboundConnectionTimeoutKey),
ThrottleRps: v.GetUint32(getRenamedKey(v, OutboundConnectionThrottlingRpsKey, NetworkOutboundConnectionThrottlingRpsKey)),
ConnectionTimeout: v.GetDuration(getRenamedKey(v, OutboundConnectionTimeoutKey, NetworkOutboundConnectionTimeoutKey)),
},

TLSKeyLogFile: v.GetString(NetworkTLSKeyLogFileKey),
Expand Down Expand Up @@ -430,7 +439,7 @@ func getNetworkConfig(
case config.HealthConfig.MaxPortionSendQueueBytesFull < 0 || config.HealthConfig.MaxPortionSendQueueBytesFull > 1:
return network.Config{}, fmt.Errorf("%s must be in [0,1]", NetworkHealthMaxPortionSendQueueFillKey)
case config.DialerConfig.ConnectionTimeout < 0:
return network.Config{}, fmt.Errorf("%q must be >= 0", OutboundConnectionTimeoutKey)
return network.Config{}, fmt.Errorf("%q must be >= 0", NetworkOutboundConnectionTimeoutKey)
case config.PeerListGossipFreq < 0:
return network.Config{}, fmt.Errorf("%s must be >= 0", NetworkPeerListGossipFreqKey)
case config.ThrottlerConfig.InboundMsgThrottlerConfig.CPUThrottlerConfig.MaxRecheckDelay < constants.MinInboundThrottlerMaxRecheckDelay:
Expand Down Expand Up @@ -784,18 +793,21 @@ func getStakingSigner(v *viper.Viper) (*bls.SecretKey, error) {

func getStakingConfig(v *viper.Viper, networkID uint32) (node.StakingConfig, error) {
config := node.StakingConfig{
EnableStaking: v.GetBool(StakingEnabledKey),
DisabledStakingWeight: v.GetUint64(StakingDisabledWeightKey),
// We use SybilProtectionEnabledKey for CLI flags that is shown to the user
// and EnableStaking in the rest of the codebase. This is to avoid confusion
// with the name "staking" like in "AVAX staking".
EnableStaking: v.GetBool(getRenamedKey(v, StakingEnabledKey, SybilProtectionEnabledKey)),
DisabledStakingWeight: v.GetUint64(getRenamedKey(v, StakingDisabledWeightKey, SybilProtectionDisabledWeightKey)),
StakingKeyPath: GetExpandedArg(v, StakingTLSKeyPathKey),
StakingCertPath: GetExpandedArg(v, StakingCertPathKey),
StakingSignerPath: GetExpandedArg(v, StakingSignerKeyPathKey),
}
if !config.EnableStaking && config.DisabledStakingWeight == 0 {
return node.StakingConfig{}, errInvalidStakerWeights
return node.StakingConfig{}, errSybilProtectionDisabledStakerWeights
}

if !config.EnableStaking && (networkID == constants.MainnetID || networkID == constants.FujiID) {
return node.StakingConfig{}, errStakingDisableOnPublicNetwork
return node.StakingConfig{}, errSybilProtectionDisabledOnPublicNetwork
}

var err error
Expand Down Expand Up @@ -862,14 +874,16 @@ func getTxFeeConfig(v *viper.Viper, networkID uint32) genesis.TxFeeConfig {

func getGenesisData(v *viper.Viper, networkID uint32, stakingCfg *genesis.StakingConfig) ([]byte, ids.ID, error) {
// try first loading genesis content directly from flag/env-var
if v.IsSet(GenesisConfigContentKey) {
genesisData := v.GetString(GenesisConfigContentKey)
configContentKey := getRenamedKey(v, GenesisConfigContentKey, GenesisFileContentKey)
if v.IsSet(configContentKey) {
genesisData := v.GetString(configContentKey)
return genesis.FromFlag(networkID, genesisData, stakingCfg)
}

configFileKey := getRenamedKey(v, GenesisConfigFileKey, GenesisFileKey)
// if content is not specified go for the file
if v.IsSet(GenesisConfigFileKey) {
genesisFileName := GetExpandedArg(v, GenesisConfigFileKey)
if v.IsSet(configFileKey) {
genesisFileName := GetExpandedArg(v, configFileKey)
return genesis.FromFile(networkID, genesisFileName, stakingCfg)
}

Expand Down Expand Up @@ -1298,9 +1312,9 @@ func GetNodeConfig(v *viper.Viper) (node.Config, error) {
}

// Gossiping
nodeConfig.ConsensusGossipFrequency = v.GetDuration(ConsensusGossipFrequencyKey)
if nodeConfig.ConsensusGossipFrequency < 0 {
return node.Config{}, fmt.Errorf("%s must be >= 0", ConsensusGossipFrequencyKey)
nodeConfig.AcceptedFrontierGossipFrequency = v.GetDuration(getRenamedKey(v, ConsensusGossipFrequencyKey, ConsensusAcceptedFrontierGossipFrequencyKey))
if nodeConfig.AcceptedFrontierGossipFrequency < 0 {
return node.Config{}, fmt.Errorf("%s must be >= 0", ConsensusAcceptedFrontierGossipFrequencyKey)
}

// App handling
Expand Down Expand Up @@ -1513,3 +1527,12 @@ func providedFlags(v *viper.Viper) map[string]interface{} {
}
return customSettings
}

// getRenamedKey returns the new key if it is set, otherwise it returns the deprecated key.
func getRenamedKey(v *viper.Viper, deprecatedKey string, newKey string) string {
if v.IsSet(newKey) {
return newKey
}

return deprecatedKey
}
39 changes: 25 additions & 14 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ func addNodeFlags(fs *pflag.FlagSet) {
fs.String(ConfigContentTypeKey, "json", "Specifies the format of the base64 encoded config content. Available values: 'json', 'yaml', 'toml'")

// Genesis
fs.String(GenesisFileKey, "", fmt.Sprintf("Specifies a genesis config file path. Ignored when running standard networks or if %s is specified",
GenesisFileContentKey))
fs.String(GenesisFileContentKey, "", "Specifies base64 encoded genesis content")
// TODO: Remove this flag in the future
fs.String(GenesisConfigFileKey, "", fmt.Sprintf("Specifies a genesis config file (ignored when running standard networks or if %s is specified)",
GenesisConfigContentKey))
// TODO: Remove this flag in the future
fs.String(GenesisConfigContentKey, "", "Specifies base64 encoded genesis content")

// Network ID
Expand Down Expand Up @@ -116,30 +121,30 @@ func addNodeFlags(fs *pflag.FlagSet) {
fs.Bool(LogDisableDisplayPluginLogsKey, false, "Disables displaying plugin logs in stdout.")

// Peer List Gossip
gossipHelpMsg := fmt.Sprintf(
"Gossip [%s] validator IPs to [%s] validators, [%s] non-validators, and [%s] validating or non-validating peers every [%s]",
NetworkPeerListNumValidatorIPsKey,
NetworkPeerListValidatorGossipSizeKey,
NetworkPeerListNonValidatorGossipSizeKey,
NetworkPeerListPeersGossipSizeKey,
NetworkPeerListGossipFreqKey,
)
fs.Uint(NetworkPeerListNumValidatorIPsKey, constants.DefaultNetworkPeerListNumValidatorIPs, gossipHelpMsg)
fs.Uint(NetworkPeerListValidatorGossipSizeKey, constants.DefaultNetworkPeerListValidatorGossipSize, gossipHelpMsg)
fs.Uint(NetworkPeerListNonValidatorGossipSizeKey, constants.DefaultNetworkPeerListNonValidatorGossipSize, gossipHelpMsg)
fs.Uint(NetworkPeerListPeersGossipSizeKey, constants.DefaultNetworkPeerListPeersGossipSize, gossipHelpMsg)
fs.Duration(NetworkPeerListGossipFreqKey, constants.DefaultNetworkPeerListGossipFreq, gossipHelpMsg)
fs.Uint(NetworkPeerListNumValidatorIPsKey, constants.DefaultNetworkPeerListNumValidatorIPs, "Number of validator IPs to gossip to other nodes")
fs.Uint(NetworkPeerListValidatorGossipSizeKey, constants.DefaultNetworkPeerListValidatorGossipSize, "Number of validators that the node will gossip peer list to")
fs.Uint(NetworkPeerListNonValidatorGossipSizeKey, constants.DefaultNetworkPeerListNonValidatorGossipSize, "Number of non-validators that the node will gossip peer list to")
fs.Uint(NetworkPeerListPeersGossipSizeKey, constants.DefaultNetworkPeerListPeersGossipSize, "Number of total peers (including non-validators and validators) that the node will gossip peer list to")
fs.Duration(NetworkPeerListGossipFreqKey, constants.DefaultNetworkPeerListGossipFreq, "Frequency to gossip peers to other nodes")

// Public IP Resolution
fs.String(PublicIPKey, "", "Public IP of this node for P2P communication. If empty, try to discover with NAT")
fs.Duration(PublicIPResolutionFreqKey, 5*time.Minute, "Frequency at which this node resolves/updates its public IP and renew NAT mappings, if applicable")
fs.String(PublicIPResolutionServiceKey, "", fmt.Sprintf("Only acceptable values are 'ifconfigco', 'opendns' or 'ifconfigme'. When provided, the node will use that service to periodically resolve/update its public IP. Ignored if %s is set", PublicIPKey))

// Inbound Connection Throttling
fs.Duration(NetworkInboundConnUpgradeThrottlerCooldownKey, constants.DefaultInboundConnUpgradeThrottlerCooldown, "Upgrade an inbound connection from a given IP at most once per this duration. If 0, don't rate-limit inbound connection upgrades")
fs.Float64(NetworkInboundThrottlerMaxConnsPerSecKey, constants.DefaultInboundThrottlerMaxConnsPerSec, "Max number of inbound connections to accept (from all peers) per second")
// TODO: Remove this flag in the future
fs.Duration(InboundConnUpgradeThrottlerCooldownKey, constants.DefaultInboundConnUpgradeThrottlerCooldown, "Upgrade an inbound connection from a given IP at most once per this duration. If 0, don't rate-limit inbound connection upgrades")
// TODO: Remove this flag in the future
fs.Float64(InboundThrottlerMaxConnsPerSecKey, constants.DefaultInboundThrottlerMaxConnsPerSec, "Max number of inbound connections to accept (from all peers) per second")
// Outbound Connection Throttling
fs.Uint(NetworkOutboundConnectionThrottlingRpsKey, constants.DefaultOutboundConnectionThrottlingRps, "Make at most this number of outgoing peer connection attempts per second")
fs.Duration(NetworkOutboundConnectionTimeoutKey, constants.DefaultOutboundConnectionTimeout, "Timeout when dialing a peer")
// TODO: Remove this flag in the future
fs.Uint(OutboundConnectionThrottlingRpsKey, constants.DefaultOutboundConnectionThrottlingRps, "Make at most this number of outgoing peer connection attempts per second")
// TODO: Remove this flag in the future
fs.Duration(OutboundConnectionTimeoutKey, constants.DefaultOutboundConnectionTimeout, "Timeout when dialing a peer")
// Timeouts
fs.Duration(NetworkInitialTimeoutKey, constants.DefaultNetworkInitialTimeout, "Initial timeout value of the adaptive timeout manager")
Expand Down Expand Up @@ -177,7 +182,9 @@ func addNodeFlags(fs *pflag.FlagSet) {
fs.Duration(BenchlistMinFailingDurationKey, constants.DefaultBenchlistMinFailingDuration, "Minimum amount of time messages to a peer must be failing before the peer is benched")

// Router
fs.Duration(ConsensusGossipFrequencyKey, constants.DefaultConsensusGossipFrequency, "Frequency of gossiping accepted frontiers")
// TODO: Remove this flag in the future
fs.Duration(ConsensusGossipFrequencyKey, constants.DefaultAcceptedFrontierGossipFrequency, "Frequency of gossiping accepted frontiers")
fs.Duration(ConsensusAcceptedFrontierGossipFrequencyKey, constants.DefaultAcceptedFrontierGossipFrequency, "Frequency of gossiping accepted frontiers")
fs.Uint(ConsensusAppConcurrencyKey, constants.DefaultConsensusAppConcurrency, "Maximum number of goroutines to use when handling App messages on a chain")
fs.Duration(ConsensusShutdownTimeoutKey, constants.DefaultConsensusShutdownTimeout, "Timeout before killing an unresponsive chain")
fs.Uint(ConsensusGossipAcceptedFrontierValidatorSizeKey, constants.DefaultConsensusGossipAcceptedFrontierValidatorSize, "Number of validators to gossip to when gossiping accepted frontier")
Expand Down Expand Up @@ -250,6 +257,7 @@ func addNodeFlags(fs *pflag.FlagSet) {

// Staking
fs.Uint(StakingPortKey, DefaultStakingPort, "Port of the consensus server")
// TODO: Remove this flag in the future
fs.Bool(StakingEnabledKey, true, "Enable staking. If enabled, Network TLS is required")
fs.Bool(StakingEphemeralCertEnabledKey, false, "If true, the node uses an ephemeral staking TLS key and certificate, and has an ephemeral node ID")
fs.String(StakingTLSKeyPathKey, defaultStakingTLSKeyPath, fmt.Sprintf("Path to the TLS private key for staking. Ignored if %s is specified", StakingTLSKeyContentKey))
Expand All @@ -260,7 +268,10 @@ func addNodeFlags(fs *pflag.FlagSet) {
fs.String(StakingSignerKeyPathKey, defaultStakingSignerKeyPath, fmt.Sprintf("Path to the signer private key for staking. Ignored if %s is specified", StakingSignerKeyContentKey))
fs.String(StakingSignerKeyContentKey, "", "Specifies base64 encoded signer private key for staking")

// TODO: Remove this flag in the future
fs.Uint64(StakingDisabledWeightKey, 100, "Weight to provide to each peer when staking is disabled")
fs.Bool(SybilProtectionEnabledKey, true, "Enables sybil protection. If enabled, Network TLS is required")
fs.Uint64(SybilProtectionDisabledWeightKey, 100, "Weight to provide to each peer when sybil protection is disabled")
// Uptime Requirement
fs.Float64(UptimeRequirementKey, genesis.LocalParams.UptimeRequirement, "Fraction of time a validator must be online to receive rewards")
// Minimum Stake required to validate the Primary Network
Expand Down

0 comments on commit 85c1d24

Please sign in to comment.