chain: move Network, NetworkUpgrade to parameters

Also, avoid using star-imports of the enum variants, which pollutes the
namespace.
This commit is contained in:
Henry de Valence 2020-08-15 15:45:37 -07:00
parent 64d9d55992
commit 948b067808
31 changed files with 102 additions and 94 deletions

View File

@ -12,8 +12,8 @@ use proptest::prelude::*;
use crate::{
keys::sapling,
parameters::Network,
serialization::{ReadZcashExt, SerializationError},
Network,
};
/// Human-Readable Parts for input to bech32 encoding.

View File

@ -7,8 +7,8 @@ use proptest::{arbitrary::Arbitrary, array, prelude::*};
use crate::{
keys::sprout,
parameters::Network,
serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize},
Network,
};
/// Magic numbers used to identify what networks Sprout Shielded

View File

@ -10,9 +10,9 @@ use sha2::Sha256;
use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*};
use crate::{
parameters::Network,
serialization::{SerializationError, ZcashDeserialize, ZcashSerialize},
types::Script,
Network,
};
/// Magic numbers used to identify what networks Transparent Addresses

View File

@ -16,8 +16,8 @@ use std::{error, sync::Arc};
#[cfg(test)]
use proptest_derive::Arbitrary;
use crate::parameters::Network;
use crate::transaction::Transaction;
use crate::Network;
pub use hash::BlockHeaderHash;
pub use header::BlockHeader;

View File

@ -1,7 +1,7 @@
//! The LightClientRootHash enum, used for the corresponding block header field.
use crate::parameters::{Network, NetworkUpgrade, NetworkUpgrade::*};
use crate::treestate::note_commitment_tree::SaplingNoteTreeRootHash;
use crate::{Network, NetworkUpgrade, NetworkUpgrade::*};
use super::BlockHeight;

View File

@ -1,6 +1,6 @@
use crate::merkle_tree::MerkleTreeRootHash;
use crate::parameters::Network;
use crate::work::{difficulty::CompactDifficulty, equihash};
use crate::Network;
use super::super::*;

View File

@ -3,8 +3,8 @@ use std::io::ErrorKind;
use proptest::{arbitrary::any, prelude::*, test_runner::Config};
use crate::parameters::Network;
use crate::serialization::{SerializationError, ZcashDeserializeInto, ZcashSerialize};
use crate::Network;
use super::super::*;

View File

@ -30,11 +30,11 @@ use rand_core::{CryptoRng, RngCore};
use proptest_derive::Arbitrary;
use crate::{
parameters::Network,
redjubjub::{self, SpendAuth},
serialization::{
serde_helpers, ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize,
},
Network,
};
/// The [Randomness Beacon][1] ("URS").

View File

@ -18,8 +18,8 @@ use proptest::{array, prelude::*};
use proptest_derive::Arbitrary;
use crate::{
parameters::Network,
serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize},
Network,
};
/// Magic numbers used to identify with what networks Sprout Spending

View File

@ -29,24 +29,3 @@ pub mod work;
pub use ed25519_zebra;
pub use redjubjub;
pub use parameters::NetworkUpgrade;
#[cfg(test)]
use proptest_derive::Arbitrary;
/// An enum describing the possible network choices.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[cfg_attr(test, derive(Arbitrary))]
pub enum Network {
/// The production mainnet.
Mainnet,
/// The testnet.
Testnet,
}
impl Default for Network {
fn default() -> Self {
Network::Mainnet
}
}

View File

@ -12,8 +12,10 @@
//! Typically, consensus parameters are accessed via a function that takes a
//! `Network` and `BlockHeight`.
pub mod network_upgrade;
mod network;
mod network_upgrade;
pub use network::Network;
pub use network_upgrade::*;
#[cfg(test)]

View File

@ -0,0 +1,18 @@
#[cfg(test)]
use proptest_derive::Arbitrary;
/// An enum describing the possible network choices.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[cfg_attr(test, derive(Arbitrary))]
pub enum Network {
/// The production mainnet.
Mainnet,
/// The testnet.
Testnet,
}
impl Default for Network {
fn default() -> Self {
Network::Mainnet
}
}

View File

@ -3,7 +3,7 @@
use NetworkUpgrade::*;
use crate::block::BlockHeight;
use crate::{Network, Network::*};
use crate::parameters::{Network, Network::*};
use std::collections::{BTreeMap, HashMap};
use std::ops::Bound::*;

View File

@ -1,12 +1,13 @@
//! Consensus parameter tests for Zebra.
use super::*;
use NetworkUpgrade::*;
use std::collections::HashSet;
use crate::block::BlockHeight;
use crate::{Network, Network::*};
use std::collections::HashSet;
use super::*;
use Network::*;
use NetworkUpgrade::*;
/// Check that the activation heights and network upgrades are unique.
#[test]

View File

@ -29,7 +29,7 @@ use tracing_futures::Instrument;
use zebra_chain::block::BlockHeight;
use zebra_chain::block::{Block, BlockHeaderHash};
use zebra_chain::{Network, NetworkUpgrade::Sapling};
use zebra_chain::parameters::{Network, NetworkUpgrade::Sapling};
/// The maximum expected gap between blocks.
///

View File

@ -1,23 +1,26 @@
//! Tests for chain verification
use super::*;
use crate::checkpoint::CheckpointList;
use std::{collections::BTreeMap, mem::drop, sync::Arc, time::Duration};
use color_eyre::eyre::eyre;
use color_eyre::eyre::Report;
use futures::{future::TryFutureExt, stream::FuturesUnordered};
use once_cell::sync::Lazy;
use std::{collections::BTreeMap, mem::drop, sync::Arc, time::Duration};
use tokio::{stream::StreamExt, time::timeout};
use tower::{layer::Layer, timeout::TimeoutLayer, Service, ServiceExt};
use tracing_futures::Instrument;
use zebra_chain::block::{Block, BlockHeader};
use zebra_chain::serialization::ZcashDeserialize;
use zebra_chain::Network::{self, *};
use zebra_chain::{
block::{Block, BlockHeader},
parameters::Network,
serialization::ZcashDeserialize,
};
use zebra_test::transcript::{TransError, Transcript};
use crate::checkpoint::CheckpointList;
use super::*;
/// The timeout we apply to each verify future during testing.
///
/// The checkpoint verifier uses `tokio::sync::oneshot` channels as futures.
@ -213,7 +216,7 @@ async fn verify_block() -> Result<(), Report> {
checkpoint_data.iter().cloned().collect();
let checkpoint_list = CheckpointList::from_list(checkpoint_list).map_err(|e| eyre!(e))?;
let (chain_verifier, _) = verifiers_from_checkpoint_list(Mainnet, checkpoint_list);
let (chain_verifier, _) = verifiers_from_checkpoint_list(Network::Mainnet, checkpoint_list);
let transcript = Transcript::from(BLOCK_VERIFY_TRANSCRIPT_GENESIS_TO_BLOCK_1.iter().cloned());
transcript.check(chain_verifier).await.unwrap();
@ -235,7 +238,7 @@ async fn verify_checkpoint() -> Result<(), Report> {
// Test that the chain::init function works. Most of the other tests use
// init_from_verifiers.
let chain_verifier = super::init(Mainnet, zebra_state::in_memory::init()).await;
let chain_verifier = super::init(Network::Mainnet, zebra_state::in_memory::init()).await;
// Add a timeout layer
let chain_verifier =
@ -260,7 +263,7 @@ async fn verify_fail_no_coinbase_test() -> Result<(), Report> {
async fn verify_fail_no_coinbase() -> Result<(), Report> {
zebra_test::init();
let (chain_verifier, state_service) = verifiers_from_network(Mainnet);
let (chain_verifier, state_service) = verifiers_from_network(Network::Mainnet);
// Add a timeout layer
let chain_verifier =
@ -285,7 +288,7 @@ async fn round_trip_checkpoint_test() -> Result<(), Report> {
async fn round_trip_checkpoint() -> Result<(), Report> {
zebra_test::init();
let (chain_verifier, state_service) = verifiers_from_network(Mainnet);
let (chain_verifier, state_service) = verifiers_from_network(Network::Mainnet);
// Add a timeout layer
let chain_verifier =
@ -310,7 +313,7 @@ async fn verify_fail_add_block_checkpoint_test() -> Result<(), Report> {
async fn verify_fail_add_block_checkpoint() -> Result<(), Report> {
zebra_test::init();
let (chain_verifier, state_service) = verifiers_from_network(Mainnet);
let (chain_verifier, state_service) = verifiers_from_network(Network::Mainnet);
// Add a timeout layer
let chain_verifier =
@ -409,7 +412,7 @@ async fn continuous_blockchain(restart_height: Option<BlockHeight>) -> Result<()
let block_verifier = crate::block::init(state_service.clone());
let mut chain_verifier = super::init_from_verifiers(
Mainnet,
Network::Mainnet,
block_verifier,
Some(checkpoint_list),
state_service.clone(),

View File

@ -38,9 +38,10 @@ use std::{
use tokio::sync::oneshot;
use tower::Service;
use zebra_chain::block::BlockHeight;
use zebra_chain::block::{Block, BlockHeaderHash};
use zebra_chain::Network;
use zebra_chain::{
block::{Block, BlockHeaderHash, BlockHeight},
parameters::Network,
};
/// The inner error type for CheckpointVerifier.
// TODO(jlusby): Error = Report ?

View File

@ -19,7 +19,7 @@ use std::{
use zebra_chain::block::BlockHeaderHash;
use zebra_chain::block::BlockHeight;
use zebra_chain::Network::{self, *};
use zebra_chain::parameters::Network;
const MAINNET_CHECKPOINTS: &str = include_str!("main-checkpoints.txt");
const TESTNET_CHECKPOINTS: &str = include_str!("test-checkpoints.txt");
@ -70,10 +70,10 @@ impl CheckpointList {
pub fn new(network: Network) -> Self {
// parse calls CheckpointList::from_list
let checkpoint_list: CheckpointList = match network {
Mainnet => MAINNET_CHECKPOINTS
Network::Mainnet => MAINNET_CHECKPOINTS
.parse()
.expect("Hard-coded Mainnet checkpoint list parses and validates"),
Testnet => TESTNET_CHECKPOINTS
Network::Testnet => TESTNET_CHECKPOINTS
.parse()
.expect("Hard-coded Testnet checkpoint list parses and validates"),
};
@ -108,8 +108,8 @@ impl CheckpointList {
// Check that the list starts with the correct genesis block
match checkpoints.iter().next() {
Some((BlockHeight(0), hash))
if (hash == &parameters::genesis_hash(Mainnet)
|| hash == &parameters::genesis_hash(Testnet)) => {}
if (hash == &parameters::genesis_hash(Network::Mainnet)
|| hash == &parameters::genesis_hash(Network::Testnet)) => {}
Some((BlockHeight(0), _)) => {
Err("the genesis checkpoint does not match the Mainnet or Testnet genesis hash")?
}

View File

@ -4,8 +4,8 @@ use super::*;
use std::sync::Arc;
use zebra_chain::parameters::{Network, NetworkUpgrade::Sapling};
use zebra_chain::{block::Block, serialization::ZcashDeserialize};
use zebra_chain::{Network, NetworkUpgrade::Sapling};
/// Make a checkpoint list containing only the genesis block
#[test]
@ -231,20 +231,20 @@ fn checkpoint_list_load_hard_coded() -> Result<(), Error> {
.parse()
.expect("hard-coded Testnet checkpoint list should parse");
let _ = CheckpointList::new(Mainnet);
let _ = CheckpointList::new(Testnet);
let _ = CheckpointList::new(Network::Mainnet);
let _ = CheckpointList::new(Network::Testnet);
Ok(())
}
#[test]
fn checkpoint_list_hard_coded_sapling_mainnet() -> Result<(), Error> {
checkpoint_list_hard_coded_sapling(Mainnet)
checkpoint_list_hard_coded_sapling(Network::Mainnet)
}
#[test]
fn checkpoint_list_hard_coded_sapling_testnet() -> Result<(), Error> {
checkpoint_list_hard_coded_sapling(Testnet)
checkpoint_list_hard_coded_sapling(Network::Testnet)
}
/// Check that the hard-coded lists cover the Sapling network upgrade

View File

@ -1,7 +1,6 @@
//! Genesis consensus parameters for each Zcash network.
use zebra_chain::block::BlockHeaderHash;
use zebra_chain::{Network, Network::*};
use zebra_chain::{block::BlockHeaderHash, parameters::Network};
/// The previous block hash for the genesis block.
///
@ -13,9 +12,9 @@ pub const GENESIS_PREVIOUS_BLOCK_HASH: BlockHeaderHash = BlockHeaderHash([0; 32]
pub fn genesis_hash(network: Network) -> BlockHeaderHash {
match network {
// zcash-cli getblockhash 0 | zebrad revhex
Mainnet => "08ce3d9731b000c08338455c8a4a6bd05da16e26b11daa1b917184ece80f0400",
Network::Mainnet => "08ce3d9731b000c08338455c8a4a6bd05da16e26b11daa1b917184ece80f0400",
// zcash-cli -testnet getblockhash 0 | zebrad revhex
Testnet => "382c4a332661c7ed0671f32a34d724619f086c61873bce7c99859dd9920aa605",
Network::Testnet => "382c4a332661c7ed0671f32a34d724619f086c61873bce7c99859dd9920aa605",
}
.parse()
.expect("hard-coded hash parses")

View File

@ -1,7 +1,6 @@
//! The minimum difficulty block rule for Zcash.
use zebra_chain::block::BlockHeight;
use zebra_chain::{Network, Network::*};
use zebra_chain::{block::BlockHeight, parameters::Network};
/// The testnet block height when minimum difficulty blocks start being
/// accepted.
@ -29,6 +28,7 @@ impl MinimumDifficulty {
/// Returns the current minimum difficulty rule for `network` and `height`.
pub fn current(network: Network, height: BlockHeight) -> MinimumDifficulty {
use MinimumDifficulty::*;
use Network::*;
match network {
Mainnet => Rejected,

View File

@ -2,8 +2,10 @@
use super::*;
use zebra_chain::block::BlockHeight;
use zebra_chain::{Network, Network::*};
use zebra_chain::{
block::BlockHeight,
parameters::Network::{self, *},
};
#[test]
fn minimum_difficulty_mainnet() {

View File

@ -5,7 +5,7 @@ use std::{
time::Duration,
};
use zebra_chain::Network;
use zebra_chain::parameters::Network;
/// Configuration for networking code.
#[derive(Clone, Debug, Deserialize, Serialize)]

View File

@ -5,7 +5,7 @@ use std::time::Duration;
// XXX should these constants be split into protocol also?
use crate::protocol::external::types::*;
use zebra_chain::NetworkUpgrade::{self, *};
use zebra_chain::parameters::NetworkUpgrade;
/// The buffer size for the peer set.
pub const PEERSET_BUFFER_SIZE: usize = 10;
@ -63,7 +63,7 @@ pub const CURRENT_VERSION: Version = Version(170_012);
//
// TODO: replace with NetworkUpgrade::current(network, height).
// See the detailed comment in handshake.rs, where this constant is used.
pub const MIN_NETWORK_UPGRADE: NetworkUpgrade = Heartwood;
pub const MIN_NETWORK_UPGRADE: NetworkUpgrade = NetworkUpgrade::Heartwood;
/// The default RTT estimate for peer responses.
pub const EWMA_DEFAULT_RTT: Duration = Duration::from_secs(1);

View File

@ -29,7 +29,7 @@ use crate::{
Request, Response,
};
use zebra_chain::Network::*;
use zebra_chain::parameters::Network;
use super::CandidateSet;
use super::PeerSet;
@ -102,8 +102,8 @@ where
// TODO: use the right port if the port is unspecified
// split the address and port configs?
let (wrong_net, wrong_net_port) = match config.network {
Mainnet => (Testnet, 18233),
Testnet => (Mainnet, 8233),
Network::Mainnet => (Network::Testnet, 18233),
Network::Testnet => (Network::Mainnet, 8233),
};
if config.listen_addr.port() == wrong_net_port {
warn!(

View File

@ -11,12 +11,12 @@ use tokio_util::codec::{Decoder, Encoder};
use zebra_chain::{
block::BlockHeight,
block::{Block, BlockHeaderHash},
parameters::Network,
serialization::{
sha256d, ReadZcashExt, SerializationError as Error, WriteZcashExt, ZcashDeserialize,
ZcashSerialize,
},
transaction::Transaction,
Network,
};
use crate::constants;

View File

@ -4,9 +4,13 @@ use crate::constants::magics;
use std::fmt;
use zebra_chain::block::BlockHeight;
use zebra_chain::Network::{self, *};
use zebra_chain::NetworkUpgrade::{self, *};
use zebra_chain::{
block::BlockHeight,
parameters::{
Network::{self, *},
NetworkUpgrade::{self, *},
},
};
#[cfg(test)]
use proptest_derive::Arbitrary;

View File

@ -24,8 +24,7 @@ use tower::{Service, ServiceExt};
use zebra_chain::{
block::BlockHeight,
block::{Block, BlockHeaderHash},
Network,
Network::*,
parameters::Network,
};
pub mod in_memory;
@ -78,8 +77,8 @@ impl Config {
/// provided `zebra_state::Config`.
pub(crate) fn sled_config(&self, network: Network) -> sled::Config {
let net_dir = match network {
Mainnet => "mainnet",
Testnet => "testnet",
Network::Mainnet => "mainnet",
Network::Testnet => "testnet",
};
let path = self.cache_dir.join(net_dir).join("state");
@ -238,12 +237,12 @@ mod tests {
#[test]
fn test_path_mainnet() {
test_path(Mainnet);
test_path(Network::Mainnet);
}
#[test]
fn test_path_testnet() {
test_path(Testnet);
test_path(Network::Testnet);
}
/// Check the sled path for `network`.
@ -258,8 +257,8 @@ mod tests {
assert_eq!(path.file_name(), Some(OsStr::new("state")));
assert!(path.pop());
match network {
Mainnet => assert_eq!(path.file_name(), Some(OsStr::new("mainnet"))),
Testnet => assert_eq!(path.file_name(), Some(OsStr::new("testnet"))),
Network::Mainnet => assert_eq!(path.file_name(), Some(OsStr::new("mainnet"))),
Network::Testnet => assert_eq!(path.file_name(), Some(OsStr::new("testnet"))),
}
}

View File

@ -15,7 +15,7 @@ use zebra_chain::serialization::{SerializationError, ZcashDeserialize, ZcashSeri
use zebra_chain::{
block::BlockHeight,
block::{Block, BlockHeaderHash},
Network,
parameters::Network,
};
/// Type alias of our wrapped service

View File

@ -2,7 +2,7 @@ use color_eyre::eyre::Report;
use once_cell::sync::Lazy;
use std::sync::Arc;
use tempdir::TempDir;
use zebra_chain::{block::Block, serialization::ZcashDeserialize, Network, Network::*};
use zebra_chain::{block::Block, parameters::Network, serialization::ZcashDeserialize};
use zebra_test::transcript::{TransError, Transcript};
use zebra_state::*;
@ -97,12 +97,12 @@ static GET_TIP_TRANSCRIPT_TESTNET: Lazy<Vec<(Request, Result<Response, TransErro
#[tokio::test]
async fn check_transcripts_mainnet() -> Result<(), Report> {
check_transcripts(Mainnet).await
check_transcripts(Network::Mainnet).await
}
#[tokio::test]
async fn check_transcripts_testnet() -> Result<(), Report> {
check_transcripts(Testnet).await
check_transcripts(Network::Testnet).await
}
#[spandoc::spandoc]

View File

@ -9,7 +9,7 @@ use tracing_futures::Instrument;
use zebra_chain::{
block::{Block, BlockHeaderHash},
Network,
parameters::Network,
};
use zebra_consensus::checkpoint;
use zebra_consensus::parameters;