Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`

This commit is contained in:
Arya 2024-04-18 02:02:41 -04:00
parent 32d0758702
commit e816c6616c
1 changed files with 28 additions and 34 deletions

View File

@ -697,44 +697,38 @@ impl<'de> Deserialize<'de> for Config {
.is_some() .is_some()
} }
let network = if let Some(DTestnetParameters { let network = match (network_kind, testnet_parameters) {
network_name, (NetworkKind::Mainnet, _) => Network::Mainnet,
activation_heights, (NetworkKind::Testnet, None) => Network::new_default_testnet(),
}) = testnet_parameters (NetworkKind::Regtest, _) => Network::new_regtest(regtest_activation_heights),
{ (
if network_kind != NetworkKind::Testnet { NetworkKind::Testnet,
return Err(de::Error::custom( Some(DTestnetParameters {
"network must be 'Testnet' to use configured testnet parameters", network_name,
)); activation_heights,
} }),
) => {
let mut params_builder = testnet::Parameters::build();
let mut params_builder = testnet::Parameters::build(); if let Some(network_name) = network_name {
params_builder = params_builder.with_network_name(network_name)
if let Some(network_name) = network_name {
params_builder = params_builder.with_network_name(network_name)
}
// Retain default Testnet activation heights unless there's an empty [testnet_parameters.activation_heights] section.
if let Some(activation_heights) = activation_heights {
// Return an error if the initial testnet peers includes any of the default initial Mainnet or Testnet
// peers while activation heights are configured.
// TODO: Check that the network magic is different from the default Mainnet/Testnet network magic too?
if contains_default_initial_peers(&initial_testnet_peers) {
return Err(de::Error::custom(
"cannot use default initial testnet peers with configured activation heights",
));
} }
params_builder = params_builder.with_activation_heights(activation_heights) // Retain default Testnet activation heights unless there's an empty [testnet_parameters.activation_heights] section.
} if let Some(activation_heights) = activation_heights {
// Return an error if the initial testnet peers includes any of the default initial Mainnet or Testnet
// peers while activation heights are configured.
// TODO: Check that the network magic is different from the default Mainnet/Testnet network magic too?
if contains_default_initial_peers(&initial_testnet_peers) {
return Err(de::Error::custom(
"cannot use default initial testnet peers with configured activation heights",
));
}
params_builder.to_network() params_builder = params_builder.with_activation_heights(activation_heights)
} else { }
// Convert to default `Network` for a `NetworkKind` if there are no testnet parameters.
match network_kind { params_builder.to_network()
NetworkKind::Mainnet => Network::Mainnet,
NetworkKind::Testnet => Network::new_default_testnet(),
NetworkKind::Regtest => Network::new_regtest(regtest_activation_heights),
} }
}; };