Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing

This commit is contained in:
Arya 2024-03-22 19:35:00 -04:00
parent 06cf9474ce
commit 4900a20cad
4 changed files with 127 additions and 36 deletions

View File

@ -23,7 +23,7 @@ pub mod arbitrary;
pub use error::*;
pub use genesis::*;
pub use network::{Network, NetworkKind};
pub use network::{Network, NetworkKind, NetworkParameters};
pub use network_upgrade::*;
pub use transaction::*;

View File

@ -2,7 +2,6 @@
use std::{fmt, str::FromStr, sync::Arc};
use serde::{Deserialize, Deserializer};
use thiserror::Error;
use zcash_primitives::consensus::{Network as ZcashPrimitivesNetwork, Parameters as _};
@ -66,20 +65,28 @@ impl NetworkParameters {
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
/// An enum describing the kind of network, whether it's the production mainnet or a testnet.
pub enum NetworkKind {
/// The production mainnet.
#[default]
Mainnet,
/// A test network.
Testnet,
}
impl From<Network> for NetworkKind {
fn from(network: Network) -> Self {
network.kind()
}
}
/// An enum describing the possible network choices.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
#[serde(into = "NetworkKind")]
pub enum Network {
/// The production mainnet.
#[default]
@ -89,34 +96,6 @@ pub enum Network {
Testnet(Arc<NetworkParameters>),
}
impl<'de> Deserialize<'de> for Network {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
enum DNetwork {
#[default]
Mainnet,
#[serde(alias = "Testnet")]
DefaultTestnet,
Regtest,
#[serde(untagged)]
ConfiguredTestnet(NetworkParameters),
}
let network = match DNetwork::deserialize(deserializer)? {
DNetwork::Mainnet => Network::Mainnet,
DNetwork::DefaultTestnet => Network::new_default_testnet(),
DNetwork::Regtest => unimplemented!("Regtest is not yet implemented"),
DNetwork::ConfiguredTestnet(params) => Network::Testnet(Arc::new(params)),
};
Ok(network)
}
}
impl NetworkKind {
/// Returns the human-readable prefix for Base58Check-encoded transparent
/// pay-to-public-key-hash payment addresses for the network.
@ -184,11 +163,16 @@ impl fmt::Display for Network {
}
impl Network {
/// Creates a new [`Network::Testnet`] with the default Testnet network parameters.
/// Creates a new [`Network::Testnet`] with the default Testnet [`NetworkParameters`].
pub fn new_default_testnet() -> Self {
Self::Testnet(Arc::new(NetworkParameters::default()))
}
/// Creates a new configured [`Network::Testnet`] with the provided Testnet [`NetworkParameters`].
pub fn new_configured_testnet(params: NetworkParameters) -> Self {
Self::Testnet(Arc::new(params))
}
/// Returns true if the network is the default Testnet, or false otherwise.
pub fn is_default_testnet(&self) -> bool {
if let Self::Testnet(params) = self {
@ -206,6 +190,14 @@ impl Network {
}
}
/// Returns the default [`Network`] for a [`NetworkKind`].
pub fn from_kind(kind: NetworkKind) -> Self {
match kind {
NetworkKind::Mainnet => Self::Mainnet,
NetworkKind::Testnet => Self::new_default_testnet(),
}
}
/// Returns an iterator over [`Network`] variants.
pub fn iter() -> impl Iterator<Item = Self> {
// TODO: Use default values of `Testnet` variant when adding fields for #7845.

View File

@ -17,7 +17,7 @@ use tracing::Span;
use lazy_static::lazy_static;
use zebra_chain::parameters::Network;
use zebra_chain::parameters::{Network, NetworkKind, NetworkParameters};
use crate::{
constants::{
@ -633,7 +633,8 @@ impl<'de> Deserialize<'de> for Config {
#[serde(deny_unknown_fields, default)]
struct DConfig {
listen_addr: String,
network: Network,
network: NetworkKind,
testnet_parameters: Option<NetworkParameters>,
initial_mainnet_peers: IndexSet<String>,
initial_testnet_peers: IndexSet<String>,
cache_dir: CacheDir,
@ -648,7 +649,8 @@ impl<'de> Deserialize<'de> for Config {
let config = Config::default();
Self {
listen_addr: "0.0.0.0".to_string(),
network: config.network,
network: Default::default(),
testnet_parameters: None,
initial_mainnet_peers: config.initial_mainnet_peers,
initial_testnet_peers: config.initial_testnet_peers,
cache_dir: config.cache_dir,
@ -661,7 +663,8 @@ impl<'de> Deserialize<'de> for Config {
let DConfig {
listen_addr,
network,
network: network_kind,
testnet_parameters,
initial_mainnet_peers,
initial_testnet_peers,
cache_dir,
@ -670,6 +673,18 @@ impl<'de> Deserialize<'de> for Config {
max_connections_per_ip,
} = DConfig::deserialize(deserializer)?;
let network = if let Some(network_params) = testnet_parameters {
assert_eq!(
network_kind,
NetworkKind::Testnet,
"set network to 'Testnet' to use testnet parameters"
);
Network::new_configured_testnet(network_params)
} else {
Network::from_kind(network_kind)
};
let listen_addr = match listen_addr.parse::<SocketAddr>() {
Ok(socket) => Ok(socket),
Err(_) => match listen_addr.parse::<IpAddr>() {

View File

@ -0,0 +1,84 @@
# Default configuration for zebrad.
#
# This file can be used as a skeleton for custom configs.
#
# Unspecified fields use default values. Optional fields are Some(field) if the
# field is present and None if it is absent.
#
# This file is generated as an example using zebrad's current defaults.
# You should set only the config options you want to keep, and delete the rest.
# Only a subset of fields are present in the skeleton, since optional values
# whose default is None are omitted.
#
# The config format (including a complete list of sections and fields) is
# documented here:
# https://docs.rs/zebrad/latest/zebrad/config/struct.ZebradConfig.html
#
# zebrad attempts to load configs in the following order:
#
# 1. The -c flag on the command line, e.g., `zebrad -c myconfig.toml start`;
# 2. The file `zebrad.toml` in the users's preference directory (platform-dependent);
# 3. The default config.
#
# The user's preference directory and the default path to the `zebrad` config are platform dependent,
# based on `dirs::preference_dir`, see https://docs.rs/dirs/latest/dirs/fn.preference_dir.html :
#
# | Platform | Value | Example |
# | -------- | ------------------------------------- | ---------------------------------------------- |
# | Linux | `$XDG_CONFIG_HOME` or `$HOME/.config` | `/home/alice/.config/zebrad.toml` |
# | macOS | `$HOME/Library/Preferences` | `/Users/Alice/Library/Preferences/zebrad.toml` |
# | Windows | `{FOLDERID_RoamingAppData}` | `C:\Users\Alice\AppData\Local\zebrad.toml` |
[consensus]
checkpoint_sync = true
[mempool]
eviction_memory_time = "1h"
tx_cost_limit = 80000000
[metrics]
[mining]
debug_like_zcashd = true
[network]
cache_dir = true
crawl_new_peer_interval = "1m 1s"
initial_mainnet_peers = [
"dnsseed.z.cash:8233",
"dnsseed.str4d.xyz:8233",
"mainnet.seeder.zfnd.org:8233",
"mainnet.is.yolo.money:8233",
]
initial_testnet_peers = [
"dnsseed.testnet.z.cash:18233",
"testnet.seeder.zfnd.org:18233",
"testnet.is.yolo.money:18233",
]
listen_addr = "0.0.0.0:8233"
max_connections_per_ip = 1
network = "Testnet"
peerset_initial_target_size = 25
[network.testnet_parameters]
[rpc]
debug_force_finished_sync = false
parallel_cpu_threads = 0
[state]
cache_dir = "cache_dir"
delete_old_database = true
ephemeral = false
[sync]
checkpoint_verify_concurrency_limit = 1000
download_concurrency_limit = 50
full_verify_concurrency_limit = 20
parallel_cpu_threads = 0
[tracing]
buffer_limit = 128000
force_use_color = false
use_color = true
use_journald = false