Skip to content

Commit

Permalink
Merge branch 'main' into regtest-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
upbqdn committed May 21, 2024
2 parents 4baf595 + 0cceb6a commit 86e11b4
Show file tree
Hide file tree
Showing 26 changed files with 402 additions and 201 deletions.
136 changes: 68 additions & 68 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions zebra-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ humantime = "2.1.0"
# Error Handling & Formatting
displaydoc = "0.2.4"
static_assertions = "1.1.0"
thiserror = "1.0.60"
thiserror = "1.0.61"
tracing = "0.1.39"

# Serialization
hex = { version = "0.4.3", features = ["serde"] }
serde = { version = "1.0.201", features = ["serde_derive", "rc"] }
serde = { version = "1.0.202", features = ["serde_derive", "rc"] }
serde_with = "3.7.0"
serde-big-array = "0.5.1"

# Processing
futures = "0.3.30"
itertools = "0.12.1"
itertools = "0.13.0"
rayon = "1.10.0"

# ZF deps
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod transaction;
pub mod arbitrary;

pub use genesis::*;
pub use network::{testnet, Network, NetworkKind};
pub use network::{magic::Magic, testnet, Network, NetworkKind};
pub use network_upgrade::*;
pub use transaction::*;

Expand Down
12 changes: 12 additions & 0 deletions zebra-chain/src/parameters/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ pub const SLOW_START_INTERVAL: Height = Height(20_000);
///
/// This calculation is exact, because `SLOW_START_INTERVAL` is divisible by 2.
pub const SLOW_START_SHIFT: Height = Height(SLOW_START_INTERVAL.0 / 2);

/// Magic numbers used to identify different Zcash networks.
pub mod magics {
use crate::parameters::network::magic::Magic;

/// The production mainnet.
pub const MAINNET: Magic = Magic([0x24, 0xe9, 0x27, 0x64]);
/// The testnet.
pub const TESTNET: Magic = Magic([0xfa, 0x1a, 0xf9, 0xbf]);
/// The regtest, see <https://github.com/zcash/zcash/blob/master/src/chainparams.cpp#L716-L719>
pub const REGTEST: Magic = Magic([0xaa, 0xe8, 0x3f, 0x5f]);
}
3 changes: 2 additions & 1 deletion zebra-chain/src/parameters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
parameters::NetworkUpgrade,
};

pub mod magic;
pub mod testnet;

#[cfg(test)]
Expand Down Expand Up @@ -75,7 +76,7 @@ impl From<Network> for NetworkKind {
}

/// An enum describing the possible network choices.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize)]
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
#[serde(into = "NetworkKind")]
pub enum Network {
/// The production mainnet.
Expand Down
56 changes: 56 additions & 0 deletions zebra-chain/src/parameters/network/magic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Network `Magic` type and implementation.

use std::fmt;

use crate::parameters::{constants::magics, Network};

#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;

/// A magic number identifying the network.
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub struct Magic(pub [u8; 4]);

impl fmt::Debug for Magic {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Magic").field(&hex::encode(self.0)).finish()
}
}

impl Network {
/// Get the magic value associated to this `Network`.
pub fn magic(&self) -> Magic {
match self {
Network::Mainnet => magics::MAINNET,
Network::Testnet(params) => params.network_magic(),
}
}
}

#[cfg(test)]
mod proptest {

use proptest::prelude::*;

use super::{magics, Magic};

#[test]
fn magic_debug() {
let _init_guard = zebra_test::init();

assert_eq!(format!("{:?}", magics::MAINNET), "Magic(\"24e92764\")");
assert_eq!(format!("{:?}", magics::TESTNET), "Magic(\"fa1af9bf\")");
assert_eq!(format!("{:?}", magics::REGTEST), "Magic(\"aae83f5f\")");
}

proptest! {

#[test]
fn proptest_magic_from_array(data in any::<[u8; 4]>()) {
let _init_guard = zebra_test::init();

assert_eq!(format!("{:?}", Magic(data)), format!("Magic({:x?})", hex::encode(data)));
}
}
}
45 changes: 40 additions & 5 deletions zebra-chain/src/parameters/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use zcash_primitives::constants as zp_constants;
use crate::{
block::{self, Height},
parameters::{
constants::{SLOW_START_INTERVAL, SLOW_START_SHIFT},
constants::{magics, SLOW_START_INTERVAL, SLOW_START_SHIFT},
network_upgrade::TESTNET_ACTIVATION_HEIGHTS,
Network, NetworkUpgrade, NETWORK_UPGRADES_IN_ORDER,
},
work::difficulty::{ExpandedDifficulty, U256},
};

use super::magic::Magic;

/// The Regtest NU5 activation height in tests
// TODO: Serialize testnet parameters in Config then remove this and use a configured NU5 activation height.
#[cfg(any(test, feature = "proptest-impl"))]
Expand Down Expand Up @@ -64,10 +66,12 @@ pub struct ConfiguredActivationHeights {
}

/// Builder for the [`Parameters`] struct.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParametersBuilder {
/// The name of this network to be used by the `Display` trait impl.
network_name: String,
/// The network magic, acts as an identifier for the network.
network_magic: Magic,
/// The genesis block hash
genesis_hash: block::Hash,
/// The network upgrade activation heights for this network, see [`Parameters::activation_heights`] for more details.
Expand All @@ -91,6 +95,7 @@ impl Default for ParametersBuilder {
fn default() -> Self {
Self {
network_name: "UnknownTestnet".to_string(),
network_magic: magics::TESTNET,
// # Correctness
//
// `Genesis` network upgrade activation height must always be 0
Expand Down Expand Up @@ -148,6 +153,20 @@ impl ParametersBuilder {
self
}

/// Sets the network name to be used in the [`Parameters`] being built.
pub fn with_network_magic(mut self, network_magic: Magic) -> Self {
assert!(
[magics::MAINNET, magics::REGTEST]
.into_iter()
.all(|reserved_magic| network_magic != reserved_magic),
"network magic should be distinct from reserved network magics"
);

self.network_magic = network_magic;

self
}

/// Checks that the provided Sapling human-readable prefixes (HRPs) are valid and unique, then
/// sets the Sapling HRPs to be used in the [`Parameters`] being built.
pub fn with_sapling_hrps(
Expand Down Expand Up @@ -265,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");
Expand All @@ -283,6 +306,7 @@ impl ParametersBuilder {
pub fn finish(self) -> Parameters {
let Self {
network_name,
network_magic,
genesis_hash,
activation_heights,
hrp_sapling_extended_spending_key,
Expand All @@ -294,6 +318,7 @@ impl ParametersBuilder {
} = self;
Parameters {
network_name,
network_magic,
genesis_hash,
activation_heights,
hrp_sapling_extended_spending_key,
Expand All @@ -313,10 +338,12 @@ impl ParametersBuilder {
}

/// Network consensus parameters for test networks such as Regtest and the default Testnet.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Parameters {
/// The name of this network to be used by the `Display` trait impl.
network_name: String,
/// The network magic, acts as an identifier for the network.
network_magic: Magic,
/// The genesis block hash
genesis_hash: block::Hash,
/// The network upgrade activation heights for this network.
Expand Down Expand Up @@ -366,6 +393,7 @@ impl Parameters {

Self {
network_name: "Regtest".to_string(),
network_magic: magics::REGTEST,
..Self::build()
.with_genesis_hash(REGTEST_GENESIS_HASH)
// This value is chosen to match zcashd, see: <https://github.com/zcash/zcash/blob/master/src/chainparams.cpp#L654>
Expand Down Expand Up @@ -397,6 +425,7 @@ impl Parameters {
pub fn is_regtest(&self) -> bool {
let Self {
network_name,
network_magic,
genesis_hash,
// Activation heights are configurable on Regtest
activation_heights: _,
Expand All @@ -410,6 +439,7 @@ impl Parameters {
} = Self::new_regtest(None);

self.network_name == network_name
&& self.network_magic == network_magic
&& self.genesis_hash == genesis_hash
&& self.hrp_sapling_extended_spending_key == hrp_sapling_extended_spending_key
&& self.hrp_sapling_extended_full_viewing_key == hrp_sapling_extended_full_viewing_key
Expand All @@ -425,6 +455,11 @@ impl Parameters {
&self.network_name
}

/// Returns the network magic
pub fn network_magic(&self) -> Magic {
self.network_magic
}

/// Returns the genesis hash
pub fn genesis_hash(&self) -> block::Hash {
self.genesis_hash
Expand Down
6 changes: 3 additions & 3 deletions zebra-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ chrono = { version = "0.4.38", default-features = false, features = ["clock", "s
displaydoc = "0.2.4"
lazy_static = "1.4.0"
once_cell = "1.18.0"
serde = { version = "1.0.201", features = ["serde_derive"] }
serde = { version = "1.0.202", features = ["serde_derive"] }

futures = "0.3.30"
futures-util = "0.3.28"
metrics = "0.22.3"
thiserror = "1.0.60"
thiserror = "1.0.61"
tokio = { version = "1.37.0", features = ["time", "sync", "tracing", "rt-multi-thread"] }
tower = { version = "0.4.13", features = ["timeout", "util", "buffer"] }
tracing = "0.1.39"
tracing-futures = "0.2.5"

orchard = "0.6.0"
orchard = "0.7.0"

zcash_proofs = { version = "0.13.0-rc.1", features = ["multicore" ] }
wagyu-zcash-parameters = "0.2.0"
Expand Down
13 changes: 7 additions & 6 deletions zebra-consensus/src/primitives/halo2/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tower::ServiceExt;

use halo2::pasta::{group::ff::PrimeField, pallas};
use orchard::{
builder::Builder,
builder::{Builder, BundleType},
bundle::Flags,
circuit::ProvingKey,
keys::{FullViewingKey, Scope, SpendingKey},
Expand All @@ -32,8 +32,6 @@ fn generate_test_vectors() {
let sk = SpendingKey::from_bytes([7; 32]).unwrap();
let recipient = FullViewingKey::from(&sk).address_at(0u32, Scope::External);

let enable_spends = true;
let enable_outputs = true;
let flags =
zebra_chain::orchard::Flags::ENABLE_SPENDS | zebra_chain::orchard::Flags::ENABLE_OUTPUTS;

Expand All @@ -43,17 +41,20 @@ fn generate_test_vectors() {
let shielded_data: Vec<zebra_chain::orchard::ShieldedData> = (1..=4)
.map(|num_recipients| {
let mut builder = Builder::new(
Flags::from_parts(enable_spends, enable_outputs),
BundleType::Transactional {
flags: Flags::from_byte(flags.bits()).unwrap(),
bundle_required: true,
},
Anchor::from_bytes(anchor_bytes).unwrap(),
);

for _ in 0..num_recipients {
builder
.add_recipient(None, recipient, NoteValue::from_raw(note_value), None)
.add_output(None, recipient, NoteValue::from_raw(note_value), None)
.unwrap();
}

let bundle: Bundle<_, i64> = builder.build(rng).unwrap();
let bundle: Bundle<_, i64> = builder.build(rng).unwrap().unwrap().0;

let bundle = bundle
.create_proof(&proving_key, rng)
Expand Down
6 changes: 3 additions & 3 deletions zebra-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ categories = ["cryptography::cryptocurrencies"]
futures-util = "0.3.28"
tonic = "0.11.0"
tonic-reflection = "0.11.0"
prost = "0.12.4"
serde = { version = "1.0.201", features = ["serde_derive"] }
prost = "0.12.6"
serde = { version = "1.0.202", features = ["serde_derive"] }
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
tokio-stream = "0.1.15"
tower = { version = "0.4.13", features = ["util", "buffer"] }
Expand All @@ -35,7 +35,7 @@ zebra-chain = { path = "../zebra-chain" , version = "1.0.0-beta.37" }
tonic-build = "0.11.0"

[dev-dependencies]
insta = { version = "1.38.0", features = ["redactions", "json", "ron"] }
insta = { version = "1.39.0", features = ["redactions", "json", "ron"] }

zebra-chain = { path = "../zebra-chain", features = ["proptest-impl"] }
zebra-state = { path = "../zebra-state" }
Expand Down
8 changes: 4 additions & 4 deletions zebra-network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ dirs = "5.0.1"
hex = "0.4.3"
humantime-serde = "1.1.1"
indexmap = { version = "2.2.6", features = ["serde"] }
itertools = "0.12.1"
itertools = "0.13.0"
lazy_static = "1.4.0"
num-integer = "0.1.46"
ordered-map = "0.4.2"
pin-project = "1.1.5"
rand = "0.8.5"
rayon = "1.10.0"
regex = "1.10.4"
serde = { version = "1.0.201", features = ["serde_derive"] }
serde = { version = "1.0.202", features = ["serde_derive"] }
tempfile = "3.10.1"
thiserror = "1.0.60"
thiserror = "1.0.61"

futures = "0.3.30"
tokio = { version = "1.37.0", features = ["fs", "io-util", "net", "time", "tracing", "macros", "rt-multi-thread"] }
Expand Down Expand Up @@ -91,7 +91,7 @@ proptest-derive = "0.4.0"

static_assertions = "1.1.0"
tokio = { version = "1.37.0", features = ["full", "tracing", "test-util"] }
toml = "0.8.11"
toml = "0.8.13"

zebra-chain = { path = "../zebra-chain", features = ["proptest-impl"] }
zebra-test = { path = "../zebra-test/" }
Loading

0 comments on commit 86e11b4

Please sign in to comment.