change(network): Allow custom testnets to make peer connections and configure more parameters (#8528)
* Allow custom testnets to define more parameters and to make peer connections based on the initial_testnet_peer config field * Updates latest stored test config * Update zebra-network/src/config.rs Co-authored-by: Arya <aryasolhi@gmail.com> --------- Co-authored-by: Marek <mail@marek.onl>
This commit is contained in:
parent
5ca40aa58b
commit
c494aa7bd1
|
@ -284,8 +284,12 @@ impl ParametersBuilder {
|
|||
|
||||
/// Sets the target difficulty limit to be used in the [`Parameters`] being built.
|
||||
// TODO: Accept a hex-encoded String instead?
|
||||
pub fn with_target_difficulty_limit(mut self, target_difficulty_limit: U256) -> Self {
|
||||
self.target_difficulty_limit = ExpandedDifficulty::from(target_difficulty_limit)
|
||||
pub fn with_target_difficulty_limit(
|
||||
mut self,
|
||||
target_difficulty_limit: impl Into<ExpandedDifficulty>,
|
||||
) -> Self {
|
||||
self.target_difficulty_limit = target_difficulty_limit
|
||||
.into()
|
||||
.to_compact()
|
||||
.to_expanded()
|
||||
.expect("difficulty limits are valid expanded values");
|
||||
|
|
|
@ -14,9 +14,12 @@ use tempfile::NamedTempFile;
|
|||
use tokio::{fs, io::AsyncWriteExt};
|
||||
use tracing::Span;
|
||||
|
||||
use zebra_chain::parameters::{
|
||||
use zebra_chain::{
|
||||
parameters::{
|
||||
testnet::{self, ConfiguredActivationHeights},
|
||||
Magic, Network, NetworkKind,
|
||||
},
|
||||
work::difficulty::U256,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -235,10 +238,8 @@ impl Config {
|
|||
pub fn initial_peer_hostnames(&self) -> IndexSet<String> {
|
||||
match &self.network {
|
||||
Network::Mainnet => self.initial_mainnet_peers.clone(),
|
||||
Network::Testnet(params) if params.is_default_testnet() => {
|
||||
self.initial_testnet_peers.clone()
|
||||
}
|
||||
// TODO: Add a `disable_peers` field to `Network` to check instead of `is_default_testnet()` (#8361)
|
||||
Network::Testnet(params) if !params.is_regtest() => self.initial_testnet_peers.clone(),
|
||||
// TODO: Add a `disable_peers` field to `Network` to check instead of `is_regtest()` (#8361)
|
||||
Network::Testnet(_params) => IndexSet::new(),
|
||||
}
|
||||
}
|
||||
|
@ -250,6 +251,11 @@ impl Config {
|
|||
///
|
||||
/// If a configured address is an invalid [`SocketAddr`] or DNS name.
|
||||
pub async fn initial_peers(&self) -> HashSet<PeerSocketAddr> {
|
||||
// Return early if network is regtest in case there are somehow any entries in the peer cache
|
||||
if self.network.is_regtest() {
|
||||
return HashSet::new();
|
||||
}
|
||||
|
||||
// TODO: do DNS and disk in parallel if startup speed becomes important
|
||||
let dns_peers =
|
||||
Config::resolve_peers(&self.initial_peer_hostnames().iter().cloned().collect()).await;
|
||||
|
@ -636,8 +642,11 @@ impl<'de> Deserialize<'de> for Config {
|
|||
{
|
||||
#[derive(Deserialize)]
|
||||
struct DTestnetParameters {
|
||||
network_magic: Option<[u8; 4]>,
|
||||
network_name: Option<String>,
|
||||
network_magic: Option<[u8; 4]>,
|
||||
slow_start_interval: Option<u32>,
|
||||
target_difficulty_limit: Option<String>,
|
||||
disable_pow: Option<bool>,
|
||||
activation_heights: Option<ConfiguredActivationHeights>,
|
||||
}
|
||||
|
||||
|
@ -720,12 +729,19 @@ impl<'de> Deserialize<'de> for Config {
|
|||
Some(DTestnetParameters {
|
||||
network_name,
|
||||
network_magic,
|
||||
slow_start_interval,
|
||||
target_difficulty_limit,
|
||||
disable_pow,
|
||||
activation_heights,
|
||||
}),
|
||||
) => {
|
||||
let mut params_builder = testnet::Parameters::build();
|
||||
let should_avoid_default_peers =
|
||||
network_magic.is_some() || activation_heights.is_some();
|
||||
// TODO: allow default peers when fields match default testnet values?
|
||||
let should_avoid_default_peers = network_magic.is_some()
|
||||
|| slow_start_interval.is_some()
|
||||
|| target_difficulty_limit.is_some()
|
||||
|| disable_pow == Some(true)
|
||||
|| 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.
|
||||
|
@ -745,6 +761,24 @@ impl<'de> Deserialize<'de> for Config {
|
|||
params_builder = params_builder.with_network_magic(Magic(network_magic));
|
||||
}
|
||||
|
||||
if let Some(slow_start_interval) = slow_start_interval {
|
||||
params_builder = params_builder.with_slow_start_interval(
|
||||
slow_start_interval.try_into().map_err(de::Error::custom)?,
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(target_difficulty_limit) = target_difficulty_limit {
|
||||
params_builder = params_builder.with_target_difficulty_limit(
|
||||
target_difficulty_limit
|
||||
.parse::<U256>()
|
||||
.map_err(de::Error::custom)?,
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(disable_pow) = disable_pow {
|
||||
params_builder = params_builder.with_disable_pow(disable_pow);
|
||||
}
|
||||
|
||||
// Retain default Testnet activation heights unless there's an empty [testnet_parameters.activation_heights] section.
|
||||
if let Some(activation_heights) = activation_heights {
|
||||
params_builder = params_builder.with_activation_heights(activation_heights)
|
||||
|
|
|
@ -59,6 +59,9 @@ peerset_initial_target_size = 25
|
|||
[network.testnet_parameters]
|
||||
network_name = "ConfiguredTestnet_1"
|
||||
network_magic = [0, 0, 0, 0]
|
||||
slow_start_interval = 0
|
||||
target_difficulty_limit = "0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f"
|
||||
disable_pow = true
|
||||
|
||||
[network.testnet_parameters.activation_heights]
|
||||
BeforeOverwinter = 1
|
||||
|
|
Loading…
Reference in New Issue