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

feat: override NetworkConfig from JSON config #8871

Merged
merged 3 commits into from
Apr 4, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* State sync is disabled by default [#8730](https://github.com/near/nearcore/pull/8730)
* Node can restart if State Sync gets interrupted. [#8732](https://github.com/near/nearcore/pull/8732)
* Merged two `neard view-state` commands: `apply-state-parts` and `dump-state-parts` into a single `state-parts` command. [#8739](https://github.com/near/nearcore/pull/8739)
* Add config.network.experimental.network_config_overrides to the JSON config. [#8871](https://github.com/near/nearcore/pull/8871)

## 1.32.2

Expand Down
131 changes: 130 additions & 1 deletion chain/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,47 @@ 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) {
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 }
}
}

pub fn new(
cfg: crate::config_json::Config,
node_key: SecretKey,
Expand Down Expand Up @@ -205,7 +246,7 @@ impl NetworkConfig {
}
}
}
let this = Self {
let mut this = Self {
node_key,
validator: validator_signer.map(|signer| ValidatorConfig {
signer,
Expand Down Expand Up @@ -293,6 +334,7 @@ impl NetworkConfig {
},
event_sink: Sink::null(),
};
this.override_config(cfg.experimental.network_config_overrides);
Ok(this)
}

Expand Down Expand Up @@ -438,6 +480,7 @@ impl std::ops::Deref for VerifiedConfig {
mod test {
use super::UPDATE_INTERVAL_LAST_TIME_RECEIVED_MESSAGE;
use crate::config;
use crate::config_json::NetworkConfigOverrides;
use crate::network_protocol;
use crate::network_protocol::testonly as data;
use crate::network_protocol::{AccountData, VersionedAccountData};
Expand Down Expand Up @@ -467,6 +510,92 @@ mod test {
assert!(nc.verify().is_err());
}

#[test]
fn test_network_config_override() {
fn check_override_field<T: std::cmp::PartialEq>(
before: &T,
after: &T,
override_val: &Option<T>,
) -> bool {
if let Some(val) = override_val {
return after == val;
} else {
return after == before;
}
}
let check_fields = |before: &config::NetworkConfig,
after: &config::NetworkConfig,
overrides: &NetworkConfigOverrides| {
assert!(check_override_field(
&before.connect_to_reliable_peers_on_startup,
&after.connect_to_reliable_peers_on_startup,
&overrides.connect_to_reliable_peers_on_startup
));
assert!(check_override_field(
&before.max_send_peers,
&after.max_send_peers,
&overrides.max_send_peers
));
assert!(check_override_field(
&before.routed_message_ttl,
&after.routed_message_ttl,
&overrides.routed_message_ttl
));
assert!(check_override_field(
&before.max_routes_to_store,
&after.max_routes_to_store,
&overrides.max_routes_to_store
));
assert!(check_override_field(
&before.highest_peer_horizon,
&after.highest_peer_horizon,
&overrides.highest_peer_horizon
));
assert!(check_override_field(
&before.push_info_period,
&after.push_info_period,
&overrides
.push_info_period_millis
.map(|millis| time::Duration::milliseconds(millis))
));
assert!(check_override_field(
&before.outbound_disabled,
&after.outbound_disabled,
&overrides.outbound_disabled
));
assert!(check_override_field(
&before.accounts_data_broadcast_rate_limit.burst,
&after.accounts_data_broadcast_rate_limit.burst,
&overrides.accounts_data_broadcast_rate_limit_burst
));
assert!(check_override_field(
&before.accounts_data_broadcast_rate_limit.qps,
&after.accounts_data_broadcast_rate_limit.qps,
&overrides.accounts_data_broadcast_rate_limit_qps
));
};
let no_overrides = NetworkConfigOverrides::default();
let mut overrides = NetworkConfigOverrides::default();
overrides.connect_to_reliable_peers_on_startup = Some(false);
overrides.max_send_peers = Some(42);
overrides.routed_message_ttl = Some(43);
overrides.accounts_data_broadcast_rate_limit_burst = Some(44);
overrides.accounts_data_broadcast_rate_limit_qps = Some(45.0);

let nc_before =
config::NetworkConfig::from_seed("123", tcp::ListenerAddr::reserve_for_test());

let mut nc_after = nc_before.clone();
nc_after.override_config(no_overrides.clone());
check_fields(&nc_before, &nc_after, &no_overrides);
assert!(nc_after.verify().is_ok());

nc_after = nc_before.clone();
nc_after.override_config(overrides.clone());
check_fields(&nc_before, &nc_after, &overrides);
assert!(nc_after.verify().is_ok());
}

// Check that MAX_PEER_ADDRS limit is consistent with the
// network_protocol::MAX_ACCOUNT_DATA_SIZE_BYTES limit
#[test]
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