Skip to content

Commit

Permalink
adds config field for setting network name, adds "with_" prefix to Pa…
Browse files Browse the repository at this point in the history
…rameterBuilder setter methods
  • Loading branch information
arya2 committed Apr 18, 2024
1 parent 384d6d9 commit cfb0413
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions zebra-chain/src/parameters/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions zebra-chain/src/parameters/network/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down Expand Up @@ -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");
}
Expand All @@ -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 '_')");
}
Expand All @@ -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
Expand Down
19 changes: 15 additions & 4 deletions zebra-network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,9 @@ impl<'de> Deserialize<'de> for Config {
{
#[derive(Deserialize)]
struct DTestnetParameters {
network_name: Option<String>,
#[serde(default)]
pub(super) activation_heights: ConfiguredActivationHeights,
activation_heights: ConfiguredActivationHeights,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit cfb0413

Please sign in to comment.