Applies more suggestions from code review.

This commit is contained in:
Arya 2024-04-01 21:03:06 -04:00
parent d108ec6355
commit 574b1f9487
9 changed files with 32 additions and 37 deletions

View File

@ -372,8 +372,8 @@ impl NonEmptyHistoryTree {
}
/// Return the network where this tree is used.
pub fn network(&self) -> Network {
self.network.clone()
pub fn network(&self) -> &Network {
&self.network
}
}

View File

@ -29,9 +29,7 @@ pub fn funding_stream_values(
let mut results = HashMap::new();
if height >= canopy_height {
let range = FUNDING_STREAM_HEIGHT_RANGES
.get(&network.bip70_network_name())
.unwrap();
let range = FUNDING_STREAM_HEIGHT_RANGES.get(&network.kind()).unwrap();
if range.contains(&height) {
let block_subsidy = block_subsidy(height, network)?;
for (&receiver, &numerator) in FUNDING_STREAM_RECEIVER_NUMERATORS.iter() {
@ -86,7 +84,7 @@ fn funding_stream_address_index(height: Height, network: &Network) -> usize {
.expect("no overflow should happen in this sum")
.checked_sub(funding_stream_address_period(
FUNDING_STREAM_HEIGHT_RANGES
.get(&network.bip70_network_name())
.get(&network.kind())
.unwrap()
.start,
network,
@ -110,7 +108,7 @@ pub fn funding_stream_address(
) -> transparent::Address {
let index = funding_stream_address_index(height, network);
let address = &FUNDING_STREAM_ADDRESSES
.get(&network.bip70_network_name())
.get(&network.kind())
.expect("there is always another hash map as value for a given valid network")
.get(&receiver)
.expect("in the inner hash map there is always a vector of strings with addresses")[index];

View File

@ -44,9 +44,7 @@ fn test_funding_stream_values() -> Result<(), Report> {
);
// funding stream period is ending
let range = FUNDING_STREAM_HEIGHT_RANGES
.get(&network.bip70_network_name())
.unwrap();
let range = FUNDING_STREAM_HEIGHT_RANGES.get(&network.kind()).unwrap();
let end = range.end;
let last = end - 1;
@ -70,7 +68,7 @@ fn test_funding_stream_addresses() -> Result<(), Report> {
let address =
transparent::Address::from_str(address).expect("address should deserialize");
assert_eq!(
&address.network().bip70_network_name(),
&address.network(),
network,
"incorrect network for {receiver:?} funding stream address constant: {address}",
);

View File

@ -70,8 +70,7 @@ impl ParameterCheckpoint for Network {
// parse calls CheckpointList::from_list
// TODO:
// - Add a `genesis_hash` field to `NetworkParameters` and return it here (#8366)
// - Consider adding a `CUSTOM_TESTNET_CHECKPOINTS` constant to enable building with another checkpoints list
// when using a configured testnet?
// - Try to disable checkpoints entirely for regtest and custom testnets
let checkpoint_list: CheckpointList = match self {
Network::Mainnet => MAINNET_CHECKPOINTS
.parse()
@ -149,7 +148,8 @@ impl CheckpointList {
// Check that the list starts with the correct genesis block
match checkpoints.iter().next() {
// TODO: Move this check to `<Network as ParameterCheckpoint>::checkpoint_list(&network)` method above (#8366),
// TODO: If required (we may not need checkpoints at all in Regtest or custom testnets):
// move this check to `<Network as ParameterCheckpoint>::checkpoint_list(&network)` method above (#8366),
// See <https://github.com/ZcashFoundation/zebra/pull/7924#discussion_r1385865347>
Some((block::Height(0), hash))
if (hash == &Network::Mainnet.genesis_hash()

View File

@ -7,7 +7,7 @@ use lazy_static::lazy_static;
use zebra_chain::{
amount::COIN,
block::{Height, HeightDiff},
parameters::{Network, NetworkUpgrade},
parameters::{Network, NetworkKind, NetworkUpgrade},
};
/// An initial period from Genesis to this Height where the block subsidy is gradually incremented. [What is slow-start mining][slow-mining]
@ -105,16 +105,17 @@ lazy_static! {
///
/// [7.10.1]: https://zips.z.cash/protocol/protocol.pdf#zip214fundingstreams
// TODO: Move the value here to a field on `NetworkParameters` (#8367)
pub static ref FUNDING_STREAM_HEIGHT_RANGES: HashMap<String, std::ops::Range<Height>> = {
pub static ref FUNDING_STREAM_HEIGHT_RANGES: HashMap<NetworkKind, std::ops::Range<Height>> = {
let mut hash_map = HashMap::new();
hash_map.insert(Network::Mainnet.bip70_network_name(), Height(1_046_400)..Height(2_726_400));
hash_map.insert(Network::new_default_testnet().bip70_network_name(), Height(1_028_500)..Height(2_796_000));
hash_map.insert(NetworkKind::Mainnet, Height(1_046_400)..Height(2_726_400));
hash_map.insert(NetworkKind::Testnet, Height(1_028_500)..Height(2_796_000));
hash_map
};
/// Convenient storage for all addresses, for all receivers and networks
// TODO: Move the value here to a field on `NetworkParameters` (#8367)
pub static ref FUNDING_STREAM_ADDRESSES: HashMap<String, HashMap<FundingStreamReceiver, Vec<String>>> = {
// There are no funding stream addresses on Regtest in zcashd, zebrad should do the same for compatibility.
pub static ref FUNDING_STREAM_ADDRESSES: HashMap<NetworkKind, HashMap<FundingStreamReceiver, Vec<String>>> = {
let mut addresses_by_network = HashMap::with_capacity(2);
// Mainnet addresses
@ -122,14 +123,14 @@ lazy_static! {
mainnet_addresses.insert(FundingStreamReceiver::Ecc, FUNDING_STREAM_ECC_ADDRESSES_MAINNET.iter().map(|a| a.to_string()).collect());
mainnet_addresses.insert(FundingStreamReceiver::ZcashFoundation, FUNDING_STREAM_ZF_ADDRESSES_MAINNET.iter().map(|a| a.to_string()).collect());
mainnet_addresses.insert(FundingStreamReceiver::MajorGrants, FUNDING_STREAM_MG_ADDRESSES_MAINNET.iter().map(|a| a.to_string()).collect());
addresses_by_network.insert(Network::Mainnet.bip70_network_name(), mainnet_addresses);
addresses_by_network.insert(NetworkKind::Mainnet, mainnet_addresses);
// Testnet addresses
let mut testnet_addresses = HashMap::with_capacity(3);
testnet_addresses.insert(FundingStreamReceiver::Ecc, FUNDING_STREAM_ECC_ADDRESSES_TESTNET.iter().map(|a| a.to_string()).collect());
testnet_addresses.insert(FundingStreamReceiver::ZcashFoundation, FUNDING_STREAM_ZF_ADDRESSES_TESTNET.iter().map(|a| a.to_string()).collect());
testnet_addresses.insert(FundingStreamReceiver::MajorGrants, FUNDING_STREAM_MG_ADDRESSES_TESTNET.iter().map(|a| a.to_string()).collect());
addresses_by_network.insert(Network::new_default_testnet().bip70_network_name(), testnet_addresses);
addresses_by_network.insert(NetworkKind::Testnet, testnet_addresses);
addresses_by_network
};

View File

@ -15,8 +15,6 @@ use tempfile::NamedTempFile;
use tokio::{fs, io::AsyncWriteExt};
use tracing::Span;
use lazy_static::lazy_static;
use zebra_chain::parameters::{Network, NetworkKind, NetworkParameters};
use crate::{
@ -178,10 +176,6 @@ pub struct Config {
pub max_connections_per_ip: usize,
}
lazy_static! {
static ref EMPTY_INITIAL_REGTEST_PEERS: IndexSet<String> = IndexSet::new();
}
impl Config {
/// The maximum number of outbound connections that Zebra will open at the same time.
/// When this limit is reached, Zebra stops opening outbound connections.
@ -228,14 +222,16 @@ impl Config {
}
/// Returns the initial seed peer hostnames for the configured network.
pub fn initial_peer_hostnames(&self) -> &IndexSet<String> {
pub fn initial_peer_hostnames(&self) -> IndexSet<String> {
match &self.network {
Network::Mainnet => &self.initial_mainnet_peers,
Network::Testnet(params) if params.is_default_testnet() => &self.initial_testnet_peers,
Network::Mainnet => self.initial_mainnet_peers.clone(),
Network::Testnet(params) if params.is_default_testnet() => {
self.initial_testnet_peers.clone()
}
// TODO: Check if the network is an incompatible custom testnet (_not_ Regtest), then panic if `initial_testnet_peers`
// contains any of the default testnet peers, or return `initial_testnet_peers` otherwise. See:
// <https://github.com/ZcashFoundation/zebra/pull/7924#discussion_r1385881828>
Network::Testnet(_params) => &EMPTY_INITIAL_REGTEST_PEERS,
Network::Testnet(_params) => IndexSet::new(),
}
}
@ -674,10 +670,11 @@ impl<'de> Deserialize<'de> for Config {
} = DConfig::deserialize(deserializer)?;
let network = if let Some(network_params) = testnet_parameters {
// TODO: Panic here if the initial testnet peers are the default initial testnet peers.
assert_eq!(
network_kind,
NetworkKind::Testnet,
"set network to 'Testnet' to use testnet parameters"
"set network to 'Testnet' to use configured testnet parameters"
);
Network::new_configured_testnet(network_params)

View File

@ -16,6 +16,7 @@ use crate::protocol::external::types::*;
use zebra_chain::{
parameters::{
Network::{self, *},
NetworkKind,
NetworkUpgrade::*,
},
serialization::Duration32,
@ -393,11 +394,11 @@ lazy_static! {
/// The minimum network protocol version typically changes after Mainnet and
/// Testnet network upgrades.
// TODO: Move the value here to a field on `NetworkParameters` (#8367)
pub static ref INITIAL_MIN_NETWORK_PROTOCOL_VERSION: HashMap<String, Version> = {
pub static ref INITIAL_MIN_NETWORK_PROTOCOL_VERSION: HashMap<NetworkKind, Version> = {
let mut hash_map = HashMap::new();
hash_map.insert(Mainnet.bip70_network_name(), Version::min_specified_for_upgrade(&Mainnet, Nu5));
hash_map.insert(Network::new_default_testnet().bip70_network_name(), Version::min_specified_for_upgrade(&Network::new_default_testnet(), Nu5));
hash_map.insert(NetworkKind::Mainnet, Version::min_specified_for_upgrade(&Mainnet, Nu5));
hash_map.insert(NetworkKind::Testnet, Version::min_specified_for_upgrade(&Network::new_default_testnet(), Nu5));
hash_map
};

View File

@ -81,7 +81,7 @@ impl Version {
/// - after Zebra's local network is slow or shut down.
fn initial_min_for_network(network: &Network) -> Version {
*constants::INITIAL_MIN_NETWORK_PROTOCOL_VERSION
.get(&network.bip70_network_name())
.get(&network.kind())
.expect("We always have a value for testnet or mainnet")
}

View File

@ -318,7 +318,7 @@ fn snapshot_block_and_transaction_data(state: &FinalizedState) {
// Skip these checks for empty history trees.
if let Some(history_tree_at_tip) = history_tree_at_tip.as_ref().as_ref() {
assert_eq!(history_tree_at_tip.current_height(), max_height);
assert_eq!(history_tree_at_tip.network(), state.network());
assert_eq!(history_tree_at_tip.network(), &state.network());
}
}