From b476485b7797df825c59c2d975489a027bea3797 Mon Sep 17 00:00:00 2001 From: Arya Date: Mon, 13 May 2024 20:36:46 -0400 Subject: [PATCH] Add a network magic config field for custom testnets --- zebra-network/src/config.rs | 29 +++++--- zebrad/tests/common/configs/v1.8.0.toml | 91 +++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 zebrad/tests/common/configs/v1.8.0.toml diff --git a/zebra-network/src/config.rs b/zebra-network/src/config.rs index 4c7956887c1..9ab27cc961c 100644 --- a/zebra-network/src/config.rs +++ b/zebra-network/src/config.rs @@ -16,7 +16,7 @@ use tracing::Span; use zebra_chain::parameters::{ testnet::{self, ConfiguredActivationHeights}, - Network, NetworkKind, + Magic, Network, NetworkKind, }; use crate::{ @@ -636,6 +636,7 @@ impl<'de> Deserialize<'de> for Config { { #[derive(Deserialize)] struct DTestnetParameters { + network_magic: Option<[u8; 4]>, network_name: Option, activation_heights: Option, } @@ -718,26 +719,34 @@ impl<'de> Deserialize<'de> for Config { NetworkKind::Testnet, Some(DTestnetParameters { network_name, + network_magic, activation_heights, }), ) => { let mut params_builder = testnet::Parameters::build(); + let should_avoid_default_peers = + network_magic.is_some() || activation_heights.is_some(); + + // Return an error if the initial testnet peers includes any of the default initial Mainnet or Testnet + // peers while activation heights or a custom network magic is configured. + if should_avoid_default_peers + && contains_default_initial_peers(&initial_testnet_peers) + { + return Err(de::Error::custom( + "cannot use default initials peers with incompatible testnet", + )); + } if let Some(network_name) = network_name { params_builder = params_builder.with_network_name(network_name) } + if let Some(network_magic) = network_magic { + params_builder = params_builder.with_network_magic(Magic(network_magic)); + } + // 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) } diff --git a/zebrad/tests/common/configs/v1.8.0.toml b/zebrad/tests/common/configs/v1.8.0.toml new file mode 100644 index 00000000000..6c70283715c --- /dev/null +++ b/zebrad/tests/common/configs/v1.8.0.toml @@ -0,0 +1,91 @@ +# 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 = [] +listen_addr = "0.0.0.0:8233" +max_connections_per_ip = 1 +network = "Testnet" +peerset_initial_target_size = 25 + +[network.testnet_parameters] +network_name = "ConfiguredTestnet_1" +network_magic = [0, 0, 0, 0] + +[network.testnet_parameters.activation_heights] +BeforeOverwinter = 1 +Overwinter = 207_500 +Sapling = 280_000 +Blossom = 584_000 +Heartwood = 903_800 +Canopy = 1_028_500 +NU5 = 1_842_420 + +[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