From cfb0413f3fa3fb509244a74235aea15eef7c0e63 Mon Sep 17 00:00:00 2001 From: Arya Date: Wed, 17 Apr 2024 22:49:03 -0400 Subject: [PATCH] adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods --- zebra-chain/src/parameters/network/testnet.rs | 4 ++-- .../src/parameters/network/tests/vectors.rs | 10 +++++----- zebra-network/src/config.rs | 19 +++++++++++++++---- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/zebra-chain/src/parameters/network/testnet.rs b/zebra-chain/src/parameters/network/testnet.rs index 6fd57e615e1..0b01c493bf4 100644 --- a/zebra-chain/src/parameters/network/testnet.rs +++ b/zebra-chain/src/parameters/network/testnet.rs @@ -79,7 +79,7 @@ impl Default for ParametersBuilder { impl ParametersBuilder { /// Sets the network name to be used in the [`Parameters`] being built. - pub fn network_name(mut self, network_name: impl fmt::Display) -> Self { + pub fn with_network_name(mut self, network_name: impl fmt::Display) -> Self { self.network_name = network_name.to_string(); assert!( @@ -104,7 +104,7 @@ impl ParametersBuilder { /// Checks that the provided network upgrade activation heights are in the correct order, then /// sets them as the new network upgrade activation heights. - pub fn activation_heights( + pub fn with_activation_heights( mut self, ConfiguredActivationHeights { // TODO: Find out if `BeforeOverwinter` is required at Height(1), allow for diff --git a/zebra-chain/src/parameters/network/tests/vectors.rs b/zebra-chain/src/parameters/network/tests/vectors.rs index 8815019055f..1d619aa5e5e 100644 --- a/zebra-chain/src/parameters/network/tests/vectors.rs +++ b/zebra-chain/src/parameters/network/tests/vectors.rs @@ -99,7 +99,7 @@ fn check_parameters_impl() { fn activates_network_upgrades_correctly() { let expected_activation_height = 1; let network = testnet::Parameters::build() - .activation_heights(ConfiguredActivationHeights { + .with_activation_heights(ConfiguredActivationHeights { nu5: Some(expected_activation_height), ..Default::default() }) @@ -137,7 +137,7 @@ fn check_network_name() { // Checks that reserved network names cannot be used for configured testnets. for reserved_network_name in RESERVED_NETWORK_NAMES { std::panic::catch_unwind(|| { - testnet::Parameters::build().network_name(reserved_network_name) + testnet::Parameters::build().with_network_name(reserved_network_name) }) .expect_err("should panic when attempting to set network name as a reserved name"); } @@ -148,7 +148,7 @@ fn check_network_name() { "!!!!non-alphanumeric-name".to_string(), ] { std::panic::catch_unwind(|| { - testnet::Parameters::build().network_name(invalid_network_name) + testnet::Parameters::build().with_network_name(invalid_network_name) }) .expect_err("should panic when setting network name that's too long or contains non-alphanumeric characters (except '_')"); } @@ -171,8 +171,8 @@ fn check_network_name() { let expected_name = "ConfiguredTestnet_1"; let network = testnet::Parameters::build() // Check that network name can contain `MAX_NETWORK_NAME_LENGTH` characters - .network_name("a".repeat(MAX_NETWORK_NAME_LENGTH)) - .network_name(expected_name) + .with_network_name("a".repeat(MAX_NETWORK_NAME_LENGTH)) + .with_network_name(expected_name) .to_network(); // Check that configured network name is displayed diff --git a/zebra-network/src/config.rs b/zebra-network/src/config.rs index 51395d55fd3..71ca3974c6e 100644 --- a/zebra-network/src/config.rs +++ b/zebra-network/src/config.rs @@ -630,8 +630,9 @@ impl<'de> Deserialize<'de> for Config { { #[derive(Deserialize)] struct DTestnetParameters { + network_name: Option, #[serde(default)] - pub(super) activation_heights: ConfiguredActivationHeights, + activation_heights: ConfiguredActivationHeights, } #[derive(Deserialize)] @@ -679,15 +680,25 @@ impl<'de> Deserialize<'de> for Config { } = DConfig::deserialize(deserializer)?; // TODO: Panic here if the initial testnet peers are the default initial testnet peers. - let network = if let Some(DTestnetParameters { activation_heights }) = testnet_parameters { + let network = if let Some(DTestnetParameters { + network_name, + activation_heights, + }) = testnet_parameters + { assert_eq!( network_kind, NetworkKind::Testnet, "set network to 'Testnet' to use configured testnet parameters" ); - testnet::Parameters::build() - .activation_heights(activation_heights) + let mut params_builder = testnet::Parameters::build(); + + if let Some(network_name) = network_name { + params_builder = params_builder.with_network_name(network_name) + } + + params_builder + .with_activation_heights(activation_heights) .to_network() } else { // Convert to default `Network` for a `NetworkKind` if there are no testnet parameters.