Skip to content

Commit

Permalink
feat: override NetworkConfig from JSON config
Browse files Browse the repository at this point in the history
Added config.experimental.network_config_overrides field.
It contains the overrides for the currently default values from NetworkConfig.

The JSON config override is done before the CLI overrides.
  • Loading branch information
VanBarbascu committed Apr 3, 2023
1 parent 83436c7 commit 3fbe8a2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
44 changes: 43 additions & 1 deletion chain/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,48 @@ pub struct NetworkConfig {
}

impl NetworkConfig {
/// Overrides values of NetworkConfig with values for the JSON config.
/// We need all the values from NetworkConfig to be configurable.
/// We need this in case of emergency. It is faster to change the config than to recompile.
fn override_config(mut self, overrides: crate::config_json::NetworkConfigOverrides) -> Self {
if let Some(connect_to_reliable_peers_on_startup) =
overrides.connect_to_reliable_peers_on_startup
{
self.connect_to_reliable_peers_on_startup = connect_to_reliable_peers_on_startup
}
if let Some(max_send_peers) = overrides.max_send_peers {
self.max_send_peers = max_send_peers
}
if let Some(routed_message_ttl) = overrides.routed_message_ttl {
self.routed_message_ttl = routed_message_ttl
}
if let Some(max_routes_to_store) = overrides.max_routes_to_store {
self.max_routes_to_store = max_routes_to_store
}
if let Some(highest_peer_horizon) = overrides.highest_peer_horizon {
self.highest_peer_horizon = highest_peer_horizon
}
if let Some(millis) = overrides.push_info_period_millis {
self.push_info_period = time::Duration::milliseconds(millis)
}
if let Some(outbound_disabled) = overrides.outbound_disabled {
self.outbound_disabled = outbound_disabled
}
if let (Some(qps), Some(burst)) = (
overrides.accounts_data_broadcast_rate_limit_qps,
overrides.accounts_data_broadcast_rate_limit_burst,
) {
self.accounts_data_broadcast_rate_limit = rate::Limit { qps, burst }
}
if let (Some(qps), Some(burst)) = (
overrides.routing_table_update_rate_limit_qps,
overrides.routing_table_update_rate_limit_burst,
) {
self.routing_table_update_rate_limit = rate::Limit { qps, burst }
}
self
}

pub fn new(
cfg: crate::config_json::Config,
node_key: SecretKey,
Expand Down Expand Up @@ -293,7 +335,7 @@ impl NetworkConfig {
},
event_sink: Sink::null(),
};
Ok(this)
Ok(this.override_config(cfg.experimental.network_config_overrides))
}

pub fn node_id(&self) -> PeerId {
Expand Down
23 changes: 23 additions & 0 deletions chain/network/src/config_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,28 @@ pub struct ExperimentalConfig {
/// See `near_network::config::Tier1::new_connections_per_attempt`.
#[serde(default = "default_tier1_new_connections_per_attempt")]
pub tier1_new_connections_per_attempt: u64,

/// See `NetworkConfig`.
/// Fields set here will override the NetworkConfig fields.
#[serde(default)]
pub network_config_overrides: NetworkConfigOverrides,
}

/// Overrides values from NetworkConfig.
/// This enables the user to override the hardcoded values.
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default)]
pub struct NetworkConfigOverrides {
pub connect_to_reliable_peers_on_startup: Option<bool>,
pub max_send_peers: Option<u32>,
pub routed_message_ttl: Option<u8>,
pub max_routes_to_store: Option<usize>,
pub highest_peer_horizon: Option<u64>,
pub push_info_period_millis: Option<i64>,
pub outbound_disabled: Option<bool>,
pub accounts_data_broadcast_rate_limit_burst: Option<u64>,
pub accounts_data_broadcast_rate_limit_qps: Option<f64>,
pub routing_table_update_rate_limit_burst: Option<u64>,
pub routing_table_update_rate_limit_qps: Option<f64>,
}

impl Default for ExperimentalConfig {
Expand All @@ -255,6 +277,7 @@ impl Default for ExperimentalConfig {
tier1_enable_outbound: default_tier1_enable_outbound(),
tier1_connect_interval: default_tier1_connect_interval(),
tier1_new_connections_per_attempt: default_tier1_new_connections_per_attempt(),
network_config_overrides: Default::default(),
}
}
}
Expand Down

0 comments on commit 3fbe8a2

Please sign in to comment.