Merge branch 'main' into impl-Params-from-zcash-protocol
This commit is contained in:
commit
8f962412bf
|
@ -25,6 +25,7 @@ mod header;
|
||||||
mod height;
|
mod height;
|
||||||
mod serialize;
|
mod serialize;
|
||||||
|
|
||||||
|
pub mod genesis;
|
||||||
pub mod merkle;
|
pub mod merkle;
|
||||||
|
|
||||||
#[cfg(any(test, feature = "proptest-impl"))]
|
#[cfg(any(test, feature = "proptest-impl"))]
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
//! Regtest genesis block
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use hex::FromHex;
|
||||||
|
|
||||||
|
use crate::{block::Block, serialization::ZcashDeserializeInto};
|
||||||
|
|
||||||
|
/// Genesis block for Regtest, copied from zcashd via `getblock 0 0` RPC method
|
||||||
|
pub fn regtest_genesis_block() -> Arc<Block> {
|
||||||
|
let regtest_genesis_block_bytes =
|
||||||
|
<Vec<u8>>::from_hex(include_str!("genesis/block-regtest-0-000-000.txt").trim())
|
||||||
|
.expect("Block bytes are in valid hex representation");
|
||||||
|
|
||||||
|
regtest_genesis_block_bytes
|
||||||
|
.zcash_deserialize_into()
|
||||||
|
.map(Arc::new)
|
||||||
|
.expect("hard-coded Regtest genesis block data must deserialize successfully")
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
040000000000000000000000000000000000000000000000000000000000000000000000db4d7a85b768123f1dff1d4c4cece70083b2d27e117b4ac2e31d087988a5eac40000000000000000000000000000000000000000000000000000000000000000dae5494d0f0f0f2009000000000000000000000000000000000000000000000000000000000000002401936b7db1eb4ac39f151b8704642d0a8bda13ec547d54cd5e43ba142fc6d8877cab07b30101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff071f0104455a6361736830623963346565663862376363343137656535303031653335303039383462366665613335363833613763616331343161303433633432303634383335643334ffffffff010000000000000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000
|
|
@ -455,9 +455,15 @@ impl HistoryTree {
|
||||||
sapling_root: &sapling::tree::Root,
|
sapling_root: &sapling::tree::Root,
|
||||||
orchard_root: &orchard::tree::Root,
|
orchard_root: &orchard::tree::Root,
|
||||||
) -> Result<(), HistoryTreeError> {
|
) -> Result<(), HistoryTreeError> {
|
||||||
let heartwood_height = NetworkUpgrade::Heartwood
|
let Some(heartwood_height) = NetworkUpgrade::Heartwood.activation_height(network) else {
|
||||||
.activation_height(network)
|
assert!(
|
||||||
.expect("Heartwood height is known");
|
self.0.is_none(),
|
||||||
|
"history tree must not exist pre-Heartwood"
|
||||||
|
);
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
match block
|
match block
|
||||||
.coinbase_height()
|
.coinbase_height()
|
||||||
.expect("must have height")
|
.expect("must have height")
|
||||||
|
|
|
@ -9,6 +9,8 @@ use crate::{
|
||||||
parameters::NetworkUpgrade,
|
parameters::NetworkUpgrade,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use self::testnet::ConfiguredActivationHeights;
|
||||||
|
|
||||||
pub mod testnet;
|
pub mod testnet;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -144,8 +146,6 @@ impl<'a> From<&'a Network> for &'a str {
|
||||||
fn from(network: &'a Network) -> &'a str {
|
fn from(network: &'a Network) -> &'a str {
|
||||||
match network {
|
match network {
|
||||||
Network::Mainnet => "Mainnet",
|
Network::Mainnet => "Mainnet",
|
||||||
// TODO:
|
|
||||||
// - zcashd calls the Regtest cache dir 'regtest' (#8327).
|
|
||||||
Network::Testnet(params) => params.network_name(),
|
Network::Testnet(params) => params.network_name(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,6 +168,11 @@ impl Network {
|
||||||
Self::Testnet(Arc::new(params))
|
Self::Testnet(Arc::new(params))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new [`Network::Testnet`] with `Regtest` parameters and the provided network upgrade activation heights.
|
||||||
|
pub fn new_regtest(activation_heights: ConfiguredActivationHeights) -> Self {
|
||||||
|
Self::new_configured_testnet(testnet::Parameters::new_regtest(activation_heights))
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns true if the network is the default Testnet, or false otherwise.
|
/// Returns true if the network is the default Testnet, or false otherwise.
|
||||||
pub fn is_default_testnet(&self) -> bool {
|
pub fn is_default_testnet(&self) -> bool {
|
||||||
if let Self::Testnet(params) = self {
|
if let Self::Testnet(params) = self {
|
||||||
|
@ -177,6 +182,23 @@ impl Network {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true if the network is Regtest, or false otherwise.
|
||||||
|
pub fn is_regtest(&self) -> bool {
|
||||||
|
if let Self::Testnet(params) = self {
|
||||||
|
params.is_regtest()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if proof-of-work validation should be disabled for this network
|
||||||
|
pub fn disable_pow(&self) -> bool {
|
||||||
|
if let Self::Testnet(params) = self {
|
||||||
|
params.disable_pow()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Returns the [`NetworkKind`] for this network.
|
/// Returns the [`NetworkKind`] for this network.
|
||||||
pub fn kind(&self) -> NetworkKind {
|
pub fn kind(&self) -> NetworkKind {
|
||||||
match self {
|
match self {
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::{collections::BTreeMap, fmt};
|
||||||
use zcash_primitives::constants as zp_constants;
|
use zcash_primitives::constants as zp_constants;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
block::Height,
|
block::{self, Height},
|
||||||
parameters::{
|
parameters::{
|
||||||
network_upgrade::TESTNET_ACTIVATION_HEIGHTS, Network, NetworkUpgrade,
|
network_upgrade::TESTNET_ACTIVATION_HEIGHTS, Network, NetworkUpgrade,
|
||||||
NETWORK_UPGRADES_IN_ORDER,
|
NETWORK_UPGRADES_IN_ORDER,
|
||||||
|
@ -24,6 +24,17 @@ pub const RESERVED_NETWORK_NAMES: [&str; 6] = [
|
||||||
/// Maximum length for a configured network name.
|
/// Maximum length for a configured network name.
|
||||||
pub const MAX_NETWORK_NAME_LENGTH: usize = 30;
|
pub const MAX_NETWORK_NAME_LENGTH: usize = 30;
|
||||||
|
|
||||||
|
/// Maximum length for a configured human-readable prefix.
|
||||||
|
pub const MAX_HRP_LENGTH: usize = 30;
|
||||||
|
|
||||||
|
/// The block hash of the Regtest genesis block, `zcash-cli -regtest getblockhash 0`
|
||||||
|
const REGTEST_GENESIS_HASH: &str =
|
||||||
|
"029f11d80ef9765602235e1bc9727e3eb6ba20839319f761fee920d63401e327";
|
||||||
|
|
||||||
|
/// The block hash of the Testnet genesis block, `zcash-cli -testnet getblockhash 0`
|
||||||
|
const TESTNET_GENESIS_HASH: &str =
|
||||||
|
"05a60a92d99d85997cce3b87616c089f6124d7342af37106edc76126334a2c38";
|
||||||
|
|
||||||
/// Configurable activation heights for Regtest and configured Testnets.
|
/// Configurable activation heights for Regtest and configured Testnets.
|
||||||
#[derive(Deserialize, Default)]
|
#[derive(Deserialize, Default)]
|
||||||
#[serde(rename_all = "PascalCase")]
|
#[serde(rename_all = "PascalCase")]
|
||||||
|
@ -50,6 +61,8 @@ pub struct ConfiguredActivationHeights {
|
||||||
pub struct ParametersBuilder {
|
pub struct ParametersBuilder {
|
||||||
/// The name of this network to be used by the `Display` trait impl.
|
/// The name of this network to be used by the `Display` trait impl.
|
||||||
network_name: String,
|
network_name: String,
|
||||||
|
/// The genesis block hash
|
||||||
|
genesis_hash: block::Hash,
|
||||||
/// The network upgrade activation heights for this network, see [`Parameters::activation_heights`] for more details.
|
/// The network upgrade activation heights for this network, see [`Parameters::activation_heights`] for more details.
|
||||||
activation_heights: BTreeMap<Height, NetworkUpgrade>,
|
activation_heights: BTreeMap<Height, NetworkUpgrade>,
|
||||||
/// Sapling extended spending key human-readable prefix for this network
|
/// Sapling extended spending key human-readable prefix for this network
|
||||||
|
@ -58,28 +71,29 @@ pub struct ParametersBuilder {
|
||||||
hrp_sapling_extended_full_viewing_key: String,
|
hrp_sapling_extended_full_viewing_key: String,
|
||||||
/// Sapling payment address human-readable prefix for this network
|
/// Sapling payment address human-readable prefix for this network
|
||||||
hrp_sapling_payment_address: String,
|
hrp_sapling_payment_address: String,
|
||||||
|
/// A flag for disabling proof-of-work checks when Zebra is validating blocks
|
||||||
|
disable_pow: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ParametersBuilder {
|
impl Default for ParametersBuilder {
|
||||||
|
/// Creates a [`ParametersBuilder`] with all of the default Testnet parameters except `network_name`.
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
network_name: "UnknownTestnet".to_string(),
|
network_name: "UnknownTestnet".to_string(),
|
||||||
// # Correctness
|
// # Correctness
|
||||||
//
|
//
|
||||||
// `Genesis` network upgrade activation height must always be 0
|
// `Genesis` network upgrade activation height must always be 0
|
||||||
activation_heights: [
|
activation_heights: TESTNET_ACTIVATION_HEIGHTS.iter().cloned().collect(),
|
||||||
(Height(0), NetworkUpgrade::Genesis),
|
|
||||||
// TODO: Find out if `BeforeOverwinter` must always be active at Height(1), remove it here if it's not required.
|
|
||||||
(Height(1), NetworkUpgrade::BeforeOverwinter),
|
|
||||||
]
|
|
||||||
.into_iter()
|
|
||||||
.collect(),
|
|
||||||
hrp_sapling_extended_spending_key:
|
hrp_sapling_extended_spending_key:
|
||||||
zp_constants::testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY.to_string(),
|
zp_constants::testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY.to_string(),
|
||||||
hrp_sapling_extended_full_viewing_key:
|
hrp_sapling_extended_full_viewing_key:
|
||||||
zp_constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY.to_string(),
|
zp_constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY.to_string(),
|
||||||
hrp_sapling_payment_address: zp_constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS
|
hrp_sapling_payment_address: zp_constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS
|
||||||
.to_string(),
|
.to_string(),
|
||||||
|
genesis_hash: TESTNET_GENESIS_HASH
|
||||||
|
.parse()
|
||||||
|
.expect("hard-coded hash parses"),
|
||||||
|
disable_pow: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,6 +123,53 @@ impl ParametersBuilder {
|
||||||
self
|
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(
|
||||||
|
mut self,
|
||||||
|
hrp_sapling_extended_spending_key: impl fmt::Display,
|
||||||
|
hrp_sapling_extended_full_viewing_key: impl fmt::Display,
|
||||||
|
hrp_sapling_payment_address: impl fmt::Display,
|
||||||
|
) -> Self {
|
||||||
|
self.hrp_sapling_extended_spending_key = hrp_sapling_extended_spending_key.to_string();
|
||||||
|
self.hrp_sapling_extended_full_viewing_key =
|
||||||
|
hrp_sapling_extended_full_viewing_key.to_string();
|
||||||
|
self.hrp_sapling_payment_address = hrp_sapling_payment_address.to_string();
|
||||||
|
|
||||||
|
let sapling_hrps = [
|
||||||
|
&self.hrp_sapling_extended_spending_key,
|
||||||
|
&self.hrp_sapling_extended_full_viewing_key,
|
||||||
|
&self.hrp_sapling_payment_address,
|
||||||
|
];
|
||||||
|
|
||||||
|
for sapling_hrp in sapling_hrps {
|
||||||
|
assert!(sapling_hrp.len() <= MAX_HRP_LENGTH, "Sapling human-readable prefix {sapling_hrp} is too long, must be {MAX_HRP_LENGTH} characters or less");
|
||||||
|
assert!(
|
||||||
|
sapling_hrp.chars().all(|c| c.is_ascii_lowercase() || c == '-'),
|
||||||
|
"human-readable prefixes should contain only lowercase ASCII characters and dashes, hrp: {sapling_hrp}"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
sapling_hrps
|
||||||
|
.iter()
|
||||||
|
.filter(|&&hrp| hrp == sapling_hrp)
|
||||||
|
.count(),
|
||||||
|
1,
|
||||||
|
"Sapling human-readable prefixes must be unique, repeated Sapling HRP: {sapling_hrp}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses the hex-encoded block hash and sets it as the genesis hash in the [`Parameters`] being built.
|
||||||
|
pub fn with_genesis_hash(mut self, genesis_hash: impl fmt::Display) -> Self {
|
||||||
|
self.genesis_hash = genesis_hash
|
||||||
|
.to_string()
|
||||||
|
.parse()
|
||||||
|
.expect("configured genesis hash must parse");
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Checks that the provided network upgrade activation heights are in the correct order, then
|
/// Checks that the provided network upgrade activation heights are in the correct order, then
|
||||||
/// sets them as the new network upgrade activation heights.
|
/// sets them as the new network upgrade activation heights.
|
||||||
pub fn with_activation_heights(
|
pub fn with_activation_heights(
|
||||||
|
@ -166,27 +227,38 @@ impl ParametersBuilder {
|
||||||
// # Correctness
|
// # Correctness
|
||||||
//
|
//
|
||||||
// Height(0) must be reserved for the `NetworkUpgrade::Genesis`.
|
// Height(0) must be reserved for the `NetworkUpgrade::Genesis`.
|
||||||
|
// TODO: Find out if `BeforeOverwinter` must always be active at Height(1), remove it here if it's not required.
|
||||||
self.activation_heights.split_off(&Height(2));
|
self.activation_heights.split_off(&Height(2));
|
||||||
self.activation_heights.extend(activation_heights);
|
self.activation_heights.extend(activation_heights);
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the `disable_pow` flag to be used in the [`Parameters`] being built.
|
||||||
|
pub fn with_disable_pow(mut self, disable_pow: bool) -> Self {
|
||||||
|
self.disable_pow = disable_pow;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Converts the builder to a [`Parameters`] struct
|
/// Converts the builder to a [`Parameters`] struct
|
||||||
pub fn finish(self) -> Parameters {
|
pub fn finish(self) -> Parameters {
|
||||||
let Self {
|
let Self {
|
||||||
network_name,
|
network_name,
|
||||||
|
genesis_hash,
|
||||||
activation_heights,
|
activation_heights,
|
||||||
hrp_sapling_extended_spending_key,
|
hrp_sapling_extended_spending_key,
|
||||||
hrp_sapling_extended_full_viewing_key,
|
hrp_sapling_extended_full_viewing_key,
|
||||||
hrp_sapling_payment_address,
|
hrp_sapling_payment_address,
|
||||||
|
disable_pow,
|
||||||
} = self;
|
} = self;
|
||||||
Parameters {
|
Parameters {
|
||||||
network_name,
|
network_name,
|
||||||
|
genesis_hash,
|
||||||
activation_heights,
|
activation_heights,
|
||||||
hrp_sapling_extended_spending_key,
|
hrp_sapling_extended_spending_key,
|
||||||
hrp_sapling_extended_full_viewing_key,
|
hrp_sapling_extended_full_viewing_key,
|
||||||
hrp_sapling_payment_address,
|
hrp_sapling_payment_address,
|
||||||
|
disable_pow,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,6 +273,8 @@ impl ParametersBuilder {
|
||||||
pub struct Parameters {
|
pub struct Parameters {
|
||||||
/// The name of this network to be used by the `Display` trait impl.
|
/// The name of this network to be used by the `Display` trait impl.
|
||||||
network_name: String,
|
network_name: String,
|
||||||
|
/// The genesis block hash
|
||||||
|
genesis_hash: block::Hash,
|
||||||
/// The network upgrade activation heights for this network.
|
/// The network upgrade activation heights for this network.
|
||||||
///
|
///
|
||||||
/// Note: This value is ignored by `Network::activation_list()` when `zebra-chain` is
|
/// Note: This value is ignored by `Network::activation_list()` when `zebra-chain` is
|
||||||
|
@ -213,6 +287,8 @@ pub struct Parameters {
|
||||||
hrp_sapling_extended_full_viewing_key: String,
|
hrp_sapling_extended_full_viewing_key: String,
|
||||||
/// Sapling payment address human-readable prefix for this network
|
/// Sapling payment address human-readable prefix for this network
|
||||||
hrp_sapling_payment_address: String,
|
hrp_sapling_payment_address: String,
|
||||||
|
/// A flag for disabling proof-of-work checks when Zebra is validating blocks
|
||||||
|
disable_pow: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Parameters {
|
impl Default for Parameters {
|
||||||
|
@ -220,7 +296,6 @@ impl Default for Parameters {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
network_name: "Testnet".to_string(),
|
network_name: "Testnet".to_string(),
|
||||||
activation_heights: TESTNET_ACTIVATION_HEIGHTS.iter().cloned().collect(),
|
|
||||||
..Self::build().finish()
|
..Self::build().finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -232,16 +307,63 @@ impl Parameters {
|
||||||
ParametersBuilder::default()
|
ParametersBuilder::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Accepts a [`ConfiguredActivationHeights`].
|
||||||
|
///
|
||||||
|
/// Creates an instance of [`Parameters`] with `Regtest` values.
|
||||||
|
pub fn new_regtest(activation_heights: ConfiguredActivationHeights) -> Self {
|
||||||
|
Self {
|
||||||
|
network_name: "Regtest".to_string(),
|
||||||
|
..Self::build()
|
||||||
|
.with_genesis_hash(REGTEST_GENESIS_HASH)
|
||||||
|
.with_disable_pow(true)
|
||||||
|
.with_sapling_hrps(
|
||||||
|
zp_constants::regtest::HRP_SAPLING_EXTENDED_SPENDING_KEY,
|
||||||
|
zp_constants::regtest::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
|
||||||
|
zp_constants::regtest::HRP_SAPLING_PAYMENT_ADDRESS,
|
||||||
|
)
|
||||||
|
// Removes default Testnet activation heights if not configured,
|
||||||
|
// most network upgrades are disabled by default for Regtest in zcashd
|
||||||
|
.with_activation_heights(activation_heights)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns true if the instance of [`Parameters`] represents the default public Testnet.
|
/// Returns true if the instance of [`Parameters`] represents the default public Testnet.
|
||||||
pub fn is_default_testnet(&self) -> bool {
|
pub fn is_default_testnet(&self) -> bool {
|
||||||
self == &Self::default()
|
self == &Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true if the instance of [`Parameters`] represents Regtest.
|
||||||
|
pub fn is_regtest(&self) -> bool {
|
||||||
|
let Self {
|
||||||
|
network_name,
|
||||||
|
genesis_hash,
|
||||||
|
// Activation heights are configurable on Regtest
|
||||||
|
activation_heights: _,
|
||||||
|
hrp_sapling_extended_spending_key,
|
||||||
|
hrp_sapling_extended_full_viewing_key,
|
||||||
|
hrp_sapling_payment_address,
|
||||||
|
disable_pow,
|
||||||
|
} = Self::new_regtest(ConfiguredActivationHeights::default());
|
||||||
|
|
||||||
|
self.network_name == network_name
|
||||||
|
&& 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
|
||||||
|
&& self.hrp_sapling_payment_address == hrp_sapling_payment_address
|
||||||
|
&& self.disable_pow == disable_pow
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the network name
|
/// Returns the network name
|
||||||
pub fn network_name(&self) -> &str {
|
pub fn network_name(&self) -> &str {
|
||||||
&self.network_name
|
&self.network_name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the genesis hash
|
||||||
|
pub fn genesis_hash(&self) -> block::Hash {
|
||||||
|
self.genesis_hash
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the network upgrade activation heights
|
/// Returns the network upgrade activation heights
|
||||||
pub fn activation_heights(&self) -> &BTreeMap<Height, NetworkUpgrade> {
|
pub fn activation_heights(&self) -> &BTreeMap<Height, NetworkUpgrade> {
|
||||||
&self.activation_heights
|
&self.activation_heights
|
||||||
|
@ -261,4 +383,9 @@ impl Parameters {
|
||||||
pub fn hrp_sapling_payment_address(&self) -> &str {
|
pub fn hrp_sapling_payment_address(&self) -> &str {
|
||||||
&self.hrp_sapling_payment_address
|
&self.hrp_sapling_payment_address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true if proof-of-work validation should be disabled for this network
|
||||||
|
pub fn disable_pow(&self) -> bool {
|
||||||
|
self.disable_pow
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
//! Fixed test vectors for the network consensus parameters.
|
//! Fixed test vectors for the network consensus parameters.
|
||||||
|
|
||||||
use zcash_primitives::consensus::{self as zp_consensus, Parameters};
|
use zcash_primitives::{
|
||||||
|
consensus::{self as zp_consensus, Parameters},
|
||||||
|
constants as zp_constants,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
block::Height,
|
block::Height,
|
||||||
parameters::{
|
parameters::{
|
||||||
testnet::{
|
testnet::{
|
||||||
self, ConfiguredActivationHeights, MAX_NETWORK_NAME_LENGTH, RESERVED_NETWORK_NAMES,
|
self, ConfiguredActivationHeights, MAX_HRP_LENGTH, MAX_NETWORK_NAME_LENGTH,
|
||||||
|
RESERVED_NETWORK_NAMES,
|
||||||
},
|
},
|
||||||
Network, NetworkUpgrade, NETWORK_UPGRADES_IN_ORDER,
|
Network, NetworkUpgrade, MAINNET_ACTIVATION_HEIGHTS, NETWORK_UPGRADES_IN_ORDER,
|
||||||
|
TESTNET_ACTIVATION_HEIGHTS,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -93,8 +98,9 @@ fn check_parameters_impl() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks that `NetworkUpgrade::activation_height()` returns the activation height of the next
|
/// Checks that `NetworkUpgrade::activation_height()` returns the activation height of the next
|
||||||
/// network upgrade if it doesn't find an activation height for a prior network upgrade, and that the
|
/// network upgrade if it doesn't find an activation height for a prior network upgrade, that the
|
||||||
/// `Genesis` upgrade is always at `Height(0)`.
|
/// `Genesis` upgrade is always at `Height(0)`, and that the default Mainnet/Testnet/Regtest activation
|
||||||
|
/// heights are what's expected.
|
||||||
#[test]
|
#[test]
|
||||||
fn activates_network_upgrades_correctly() {
|
fn activates_network_upgrades_correctly() {
|
||||||
let expected_activation_height = 1;
|
let expected_activation_height = 1;
|
||||||
|
@ -126,6 +132,164 @@ fn activates_network_upgrades_correctly() {
|
||||||
should match NU5 activation height, network_upgrade: {nu}, activation_height: {activation_height:?}"
|
should match NU5 activation height, network_upgrade: {nu}, activation_height: {activation_height:?}"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let expected_default_regtest_activation_heights = &[
|
||||||
|
(Height(0), NetworkUpgrade::Genesis),
|
||||||
|
(Height(1), NetworkUpgrade::BeforeOverwinter),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (network, expected_activation_heights) in [
|
||||||
|
(Network::Mainnet, MAINNET_ACTIVATION_HEIGHTS),
|
||||||
|
(Network::new_default_testnet(), TESTNET_ACTIVATION_HEIGHTS),
|
||||||
|
(
|
||||||
|
Network::new_regtest(Default::default()),
|
||||||
|
expected_default_regtest_activation_heights,
|
||||||
|
),
|
||||||
|
] {
|
||||||
|
assert_eq!(
|
||||||
|
network.activation_list(),
|
||||||
|
expected_activation_heights.iter().cloned().collect(),
|
||||||
|
"network activation list should match expected activation heights"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks that configured testnet names are validated and used correctly.
|
||||||
|
#[test]
|
||||||
|
fn check_configured_network_name() {
|
||||||
|
// Sets a no-op panic hook to avoid long output.
|
||||||
|
std::panic::set_hook(Box::new(|_| {}));
|
||||||
|
|
||||||
|
// Checks that reserved network names cannot be used for configured testnets.
|
||||||
|
for reserved_network_name in RESERVED_NETWORK_NAMES {
|
||||||
|
std::panic::catch_unwind(|| {
|
||||||
|
testnet::Parameters::build().with_network_name(reserved_network_name)
|
||||||
|
})
|
||||||
|
.expect_err("should panic when attempting to set network name as a reserved name");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that max length is enforced, and that network names may only contain alphanumeric characters and '_'.
|
||||||
|
for invalid_network_name in [
|
||||||
|
"a".repeat(MAX_NETWORK_NAME_LENGTH + 1),
|
||||||
|
"!!!!non-alphanumeric-name".to_string(),
|
||||||
|
] {
|
||||||
|
std::panic::catch_unwind(|| {
|
||||||
|
testnet::Parameters::build().with_network_name(invalid_network_name)
|
||||||
|
})
|
||||||
|
.expect_err("should panic when setting network name that's too long or contains non-alphanumeric characters (except '_')");
|
||||||
|
}
|
||||||
|
|
||||||
|
drop(std::panic::take_hook());
|
||||||
|
|
||||||
|
// Checks that network names are displayed correctly
|
||||||
|
assert_eq!(
|
||||||
|
Network::new_default_testnet().to_string(),
|
||||||
|
"Testnet",
|
||||||
|
"default testnet should be displayed as 'Testnet'"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Network::Mainnet.to_string(),
|
||||||
|
"Mainnet",
|
||||||
|
"Mainnet should be displayed as 'Mainnet'"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Network::new_regtest(Default::default()).to_string(),
|
||||||
|
"Regtest",
|
||||||
|
"Regtest should be displayed as 'Regtest'"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check that network name can contain alphanumeric characters and '_'.
|
||||||
|
let expected_name = "ConfiguredTestnet_1";
|
||||||
|
let network = testnet::Parameters::build()
|
||||||
|
// Check that network name can contain `MAX_NETWORK_NAME_LENGTH` characters
|
||||||
|
.with_network_name("a".repeat(MAX_NETWORK_NAME_LENGTH))
|
||||||
|
.with_network_name(expected_name)
|
||||||
|
.to_network();
|
||||||
|
|
||||||
|
// Check that configured network name is displayed
|
||||||
|
assert_eq!(
|
||||||
|
network.to_string(),
|
||||||
|
expected_name,
|
||||||
|
"network must be displayed as configured network name"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks that configured Sapling human-readable prefixes (HRPs) are validated and used correctly.
|
||||||
|
#[test]
|
||||||
|
fn check_configured_sapling_hrps() {
|
||||||
|
// Sets a no-op panic hook to avoid long output.
|
||||||
|
std::panic::set_hook(Box::new(|_| {}));
|
||||||
|
|
||||||
|
// Check that configured Sapling HRPs must be unique.
|
||||||
|
std::panic::catch_unwind(|| {
|
||||||
|
testnet::Parameters::build().with_sapling_hrps("", "", "");
|
||||||
|
})
|
||||||
|
.expect_err("should panic when setting non-unique Sapling HRPs");
|
||||||
|
|
||||||
|
// Check that max length is enforced, and that network names may only contain lowecase ASCII characters and dashes.
|
||||||
|
for invalid_hrp in [
|
||||||
|
"a".repeat(MAX_NETWORK_NAME_LENGTH + 1),
|
||||||
|
"!!!!non-alphabetical-name".to_string(),
|
||||||
|
"A".to_string(),
|
||||||
|
] {
|
||||||
|
std::panic::catch_unwind(|| {
|
||||||
|
testnet::Parameters::build().with_sapling_hrps(invalid_hrp, "dummy-hrp-a", "dummy-hrp-b");
|
||||||
|
})
|
||||||
|
.expect_err("should panic when setting Sapling HRPs that are too long or contain non-alphanumeric characters (except '-')");
|
||||||
|
}
|
||||||
|
|
||||||
|
drop(std::panic::take_hook());
|
||||||
|
|
||||||
|
// Check that Sapling HRPs can contain lowercase ascii characters and dashes.
|
||||||
|
let expected_hrp_sapling_extended_spending_key = "sapling-hrp-a";
|
||||||
|
let expected_hrp_sapling_extended_full_viewing_key = "sapling-hrp-b";
|
||||||
|
let expected_hrp_sapling_payment_address = "sapling-hrp-c";
|
||||||
|
|
||||||
|
let network = testnet::Parameters::build()
|
||||||
|
// Check that Sapling HRPs can contain `MAX_HRP_LENGTH` characters
|
||||||
|
.with_sapling_hrps("a".repeat(MAX_HRP_LENGTH), "dummy-hrp-a", "dummy-hrp-b")
|
||||||
|
.with_sapling_hrps(
|
||||||
|
expected_hrp_sapling_extended_spending_key,
|
||||||
|
expected_hrp_sapling_extended_full_viewing_key,
|
||||||
|
expected_hrp_sapling_payment_address,
|
||||||
|
)
|
||||||
|
.to_network();
|
||||||
|
|
||||||
|
// Check that configured Sapling HRPs are returned by `Parameters` trait methods
|
||||||
|
assert_eq!(
|
||||||
|
network.hrp_sapling_extended_spending_key(),
|
||||||
|
expected_hrp_sapling_extended_spending_key,
|
||||||
|
"should return expected Sapling extended spending key HRP"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
network.hrp_sapling_extended_full_viewing_key(),
|
||||||
|
expected_hrp_sapling_extended_full_viewing_key,
|
||||||
|
"should return expected Sapling EFVK HRP"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
network.hrp_sapling_payment_address(),
|
||||||
|
expected_hrp_sapling_payment_address,
|
||||||
|
"should return expected Sapling payment address HRP"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check that default Mainnet, Testnet, and Regtest HRPs are valid, these calls will panic
|
||||||
|
// if any of the values fail validation.
|
||||||
|
testnet::Parameters::build()
|
||||||
|
.with_sapling_hrps(
|
||||||
|
zp_constants::mainnet::HRP_SAPLING_EXTENDED_SPENDING_KEY,
|
||||||
|
zp_constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
|
||||||
|
zp_constants::mainnet::HRP_SAPLING_PAYMENT_ADDRESS,
|
||||||
|
)
|
||||||
|
.with_sapling_hrps(
|
||||||
|
zp_constants::testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY,
|
||||||
|
zp_constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
|
||||||
|
zp_constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS,
|
||||||
|
)
|
||||||
|
.with_sapling_hrps(
|
||||||
|
zp_constants::regtest::HRP_SAPLING_EXTENDED_SPENDING_KEY,
|
||||||
|
zp_constants::regtest::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
|
||||||
|
zp_constants::regtest::HRP_SAPLING_PAYMENT_ADDRESS,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks that configured testnet names are validated and used correctly.
|
/// Checks that configured testnet names are validated and used correctly.
|
||||||
|
@ -153,6 +317,8 @@ fn check_network_name() {
|
||||||
.expect_err("should panic when setting network name that's too long or contains non-alphanumeric characters (except '_')");
|
.expect_err("should panic when setting network name that's too long or contains non-alphanumeric characters (except '_')");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drop(std::panic::take_hook());
|
||||||
|
|
||||||
// Checks that network names are displayed correctly
|
// Checks that network names are displayed correctly
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Network::new_default_testnet().to_string(),
|
Network::new_default_testnet().to_string(),
|
||||||
|
|
|
@ -10,7 +10,7 @@ impl Arbitrary for equihash::Solution {
|
||||||
.prop_map(|v| {
|
.prop_map(|v| {
|
||||||
let mut bytes = [0; equihash::SOLUTION_SIZE];
|
let mut bytes = [0; equihash::SOLUTION_SIZE];
|
||||||
bytes.copy_from_slice(v.as_slice());
|
bytes.copy_from_slice(v.as_slice());
|
||||||
Self(bytes)
|
Self::Common(bytes)
|
||||||
})
|
})
|
||||||
.boxed()
|
.boxed()
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,16 +29,26 @@ pub struct SolverCancelled;
|
||||||
/// The size of an Equihash solution in bytes (always 1344).
|
/// The size of an Equihash solution in bytes (always 1344).
|
||||||
pub(crate) const SOLUTION_SIZE: usize = 1344;
|
pub(crate) const SOLUTION_SIZE: usize = 1344;
|
||||||
|
|
||||||
|
/// The size of an Equihash solution in bytes on Regtest (always 36).
|
||||||
|
pub(crate) const REGTEST_SOLUTION_SIZE: usize = 36;
|
||||||
|
|
||||||
/// Equihash Solution in compressed format.
|
/// Equihash Solution in compressed format.
|
||||||
///
|
///
|
||||||
/// A wrapper around [u8; 1344] because Rust doesn't implement common
|
/// A wrapper around `[u8; n]` where `n` is the solution size because
|
||||||
/// traits like `Debug`, `Clone`, etc for collections like array
|
/// Rust doesn't implement common traits like `Debug`, `Clone`, etc.
|
||||||
/// beyond lengths 0 to 32.
|
/// for collections like arrays beyond lengths 0 to 32.
|
||||||
///
|
///
|
||||||
/// The size of an Equihash solution in bytes is always 1344 so the
|
/// The size of an Equihash solution in bytes is always 1344 on Mainnet and Testnet, and
|
||||||
/// length of this type is fixed.
|
/// is always 36 on Regtest so the length of this type is fixed.
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct Solution(#[serde(with = "BigArray")] pub [u8; SOLUTION_SIZE]);
|
// It's okay to use the extra space on Regtest
|
||||||
|
#[allow(clippy::large_enum_variant)]
|
||||||
|
pub enum Solution {
|
||||||
|
/// Equihash solution on Mainnet or Testnet
|
||||||
|
Common(#[serde(with = "BigArray")] [u8; SOLUTION_SIZE]),
|
||||||
|
/// Equihash solution on Regtest
|
||||||
|
Regtest(#[serde(with = "BigArray")] [u8; REGTEST_SOLUTION_SIZE]),
|
||||||
|
}
|
||||||
|
|
||||||
impl Solution {
|
impl Solution {
|
||||||
/// The length of the portion of the header used as input when verifying
|
/// The length of the portion of the header used as input when verifying
|
||||||
|
@ -48,15 +58,24 @@ impl Solution {
|
||||||
/// to the verification function.
|
/// to the verification function.
|
||||||
pub const INPUT_LENGTH: usize = 4 + 32 * 3 + 4 * 2;
|
pub const INPUT_LENGTH: usize = 4 + 32 * 3 + 4 * 2;
|
||||||
|
|
||||||
|
/// Returns the inner value of the [`Solution`] as a byte slice.
|
||||||
|
fn value(&self) -> &[u8] {
|
||||||
|
match self {
|
||||||
|
Solution::Common(solution) => solution.as_slice(),
|
||||||
|
Solution::Regtest(solution) => solution.as_slice(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns `Ok(())` if `EquihashSolution` is valid for `header`
|
/// Returns `Ok(())` if `EquihashSolution` is valid for `header`
|
||||||
#[allow(clippy::unwrap_in_result)]
|
#[allow(clippy::unwrap_in_result)]
|
||||||
pub fn check(&self, header: &Header) -> Result<(), Error> {
|
pub fn check(&self, header: &Header) -> Result<(), Error> {
|
||||||
let n = 200;
|
|
||||||
let k = 9;
|
|
||||||
let nonce = &header.nonce;
|
let nonce = &header.nonce;
|
||||||
let solution = &self.0;
|
let (solution, n, k) = match self {
|
||||||
let mut input = Vec::new();
|
Solution::Common(solution) => (solution.as_slice(), 200, 9),
|
||||||
|
Solution::Regtest(solution) => (solution.as_slice(), 48, 5),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut input = Vec::new();
|
||||||
header
|
header
|
||||||
.zcash_serialize(&mut input)
|
.zcash_serialize(&mut input)
|
||||||
.expect("serialization into a vec can't fail");
|
.expect("serialization into a vec can't fail");
|
||||||
|
@ -73,23 +92,29 @@ impl Solution {
|
||||||
/// Returns a [`Solution`] containing the bytes from `solution`.
|
/// Returns a [`Solution`] containing the bytes from `solution`.
|
||||||
/// Returns an error if `solution` is the wrong length.
|
/// Returns an error if `solution` is the wrong length.
|
||||||
pub fn from_bytes(solution: &[u8]) -> Result<Self, SerializationError> {
|
pub fn from_bytes(solution: &[u8]) -> Result<Self, SerializationError> {
|
||||||
if solution.len() != SOLUTION_SIZE {
|
match solution.len() {
|
||||||
return Err(SerializationError::Parse(
|
// Won't panic, because we just checked the length.
|
||||||
|
SOLUTION_SIZE => {
|
||||||
|
let mut bytes = [0; SOLUTION_SIZE];
|
||||||
|
bytes.copy_from_slice(solution);
|
||||||
|
Ok(Self::Common(bytes))
|
||||||
|
}
|
||||||
|
REGTEST_SOLUTION_SIZE => {
|
||||||
|
let mut bytes = [0; REGTEST_SOLUTION_SIZE];
|
||||||
|
bytes.copy_from_slice(solution);
|
||||||
|
Ok(Self::Regtest(bytes))
|
||||||
|
}
|
||||||
|
_unexpected_len => Err(SerializationError::Parse(
|
||||||
"incorrect equihash solution size",
|
"incorrect equihash solution size",
|
||||||
));
|
)),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut bytes = [0; SOLUTION_SIZE];
|
|
||||||
// Won't panic, because we just checked the length.
|
|
||||||
bytes.copy_from_slice(solution);
|
|
||||||
|
|
||||||
Ok(Self(bytes))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a [`Solution`] of `[0; SOLUTION_SIZE]` to be used in block proposals.
|
/// Returns a [`Solution`] of `[0; SOLUTION_SIZE]` to be used in block proposals.
|
||||||
#[cfg(feature = "getblocktemplate-rpcs")]
|
#[cfg(feature = "getblocktemplate-rpcs")]
|
||||||
pub fn for_proposal() -> Self {
|
pub fn for_proposal() -> Self {
|
||||||
Self([0; SOLUTION_SIZE])
|
// TODO: Accept network as an argument, and if it's Regtest, return the shorter null solution.
|
||||||
|
Self::Common([0; SOLUTION_SIZE])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Mines and returns one or more [`Solution`]s based on a template `header`.
|
/// Mines and returns one or more [`Solution`]s based on a template `header`.
|
||||||
|
@ -126,14 +151,14 @@ impl Solution {
|
||||||
|
|
||||||
impl PartialEq<Solution> for Solution {
|
impl PartialEq<Solution> for Solution {
|
||||||
fn eq(&self, other: &Solution) -> bool {
|
fn eq(&self, other: &Solution) -> bool {
|
||||||
self.0.as_ref() == other.0.as_ref()
|
self.value() == other.value()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Solution {
|
impl fmt::Debug for Solution {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
f.debug_tuple("EquihashSolution")
|
f.debug_tuple("EquihashSolution")
|
||||||
.field(&hex::encode(&self.0[..]))
|
.field(&hex::encode(self.value()))
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,13 +178,13 @@ impl Eq for Solution {}
|
||||||
#[cfg(any(test, feature = "proptest-impl"))]
|
#[cfg(any(test, feature = "proptest-impl"))]
|
||||||
impl Default for Solution {
|
impl Default for Solution {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self([0; SOLUTION_SIZE])
|
Self::Common([0; SOLUTION_SIZE])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ZcashSerialize for Solution {
|
impl ZcashSerialize for Solution {
|
||||||
fn zcash_serialize<W: io::Write>(&self, writer: W) -> Result<(), io::Error> {
|
fn zcash_serialize<W: io::Write>(&self, writer: W) -> Result<(), io::Error> {
|
||||||
zcash_serialize_bytes(&self.0.to_vec(), writer)
|
zcash_serialize_bytes(&self.value().to_vec(), writer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -176,16 +176,18 @@ where
|
||||||
Err(BlockError::MaxHeight(height, hash, block::Height::MAX))?;
|
Err(BlockError::MaxHeight(height, hash, block::Height::MAX))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// > The block data MUST be validated and checked against the server's usual
|
if !network.disable_pow() {
|
||||||
// > acceptance rules (excluding the check for a valid proof-of-work).
|
// > The block data MUST be validated and checked against the server's usual
|
||||||
// <https://en.bitcoin.it/wiki/BIP_0023#Block_Proposal>
|
// > acceptance rules (excluding the check for a valid proof-of-work).
|
||||||
if request.is_proposal() {
|
// <https://en.bitcoin.it/wiki/BIP_0023#Block_Proposal>
|
||||||
check::difficulty_threshold_is_valid(&block.header, &network, &height, &hash)?;
|
if request.is_proposal() {
|
||||||
} else {
|
check::difficulty_threshold_is_valid(&block.header, &network, &height, &hash)?;
|
||||||
// Do the difficulty checks first, to raise the threshold for
|
} else {
|
||||||
// attacks that use any other fields.
|
// Do the difficulty checks first, to raise the threshold for
|
||||||
check::difficulty_is_valid(&block.header, &network, &height, &hash)?;
|
// attacks that use any other fields.
|
||||||
check::equihash_solution_is_valid(&block.header)?;
|
check::difficulty_is_valid(&block.header, &network, &height, &hash)?;
|
||||||
|
check::equihash_solution_is_valid(&block.header)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next, check the Merkle root validity, to ensure that
|
// Next, check the Merkle root validity, to ensure that
|
||||||
|
|
|
@ -595,8 +595,10 @@ where
|
||||||
.ok_or(VerifyCheckpointError::CoinbaseHeight { hash })?;
|
.ok_or(VerifyCheckpointError::CoinbaseHeight { hash })?;
|
||||||
self.check_height(height)?;
|
self.check_height(height)?;
|
||||||
|
|
||||||
crate::block::check::difficulty_is_valid(&block.header, &self.network, &height, &hash)?;
|
if !self.network.disable_pow() {
|
||||||
crate::block::check::equihash_solution_is_valid(&block.header)?;
|
crate::block::check::difficulty_is_valid(&block.header, &self.network, &height, &hash)?;
|
||||||
|
crate::block::check::equihash_solution_is_valid(&block.header)?;
|
||||||
|
}
|
||||||
|
|
||||||
// don't do precalculation until the block passes basic difficulty checks
|
// don't do precalculation until the block passes basic difficulty checks
|
||||||
let block = CheckpointVerifiedBlock::with_hash(block, hash);
|
let block = CheckpointVerifiedBlock::with_hash(block, hash);
|
||||||
|
|
|
@ -55,41 +55,60 @@ impl ParameterCheckpoint for Network {
|
||||||
fn genesis_hash(&self) -> zebra_chain::block::Hash {
|
fn genesis_hash(&self) -> zebra_chain::block::Hash {
|
||||||
match self {
|
match self {
|
||||||
// zcash-cli getblockhash 0
|
// zcash-cli getblockhash 0
|
||||||
Network::Mainnet => "00040fe8ec8471911baa1db1266ea15dd06b4a8a5c453883c000b031973dce08",
|
Network::Mainnet => "00040fe8ec8471911baa1db1266ea15dd06b4a8a5c453883c000b031973dce08"
|
||||||
// zcash-cli -testnet getblockhash 0
|
.parse()
|
||||||
// TODO: Add a `genesis_hash` field to `testnet::Parameters` and return it here (#8366)
|
.expect("hard-coded hash parses"),
|
||||||
Network::Testnet(_params) => {
|
// See `zebra_chain::parameters::network::testnet` for more details.
|
||||||
"05a60a92d99d85997cce3b87616c089f6124d7342af37106edc76126334a2c38"
|
Network::Testnet(params) => params.genesis_hash(),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.parse()
|
|
||||||
.expect("hard-coded hash parses")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn checkpoint_list(&self) -> CheckpointList {
|
fn checkpoint_list(&self) -> CheckpointList {
|
||||||
// parse calls CheckpointList::from_list
|
let (checkpoints_for_network, should_fallback_to_genesis_hash_as_checkpoint) = match self {
|
||||||
// TODO:
|
Network::Mainnet => (MAINNET_CHECKPOINTS, false),
|
||||||
// - Add a `genesis_hash` field to `testnet::Parameters` and return it here (#8366)
|
Network::Testnet(params) if params.is_default_testnet() => (TESTNET_CHECKPOINTS, false),
|
||||||
// - Try to disable checkpoints entirely for regtest and custom testnets
|
Network::Testnet(_params) => (TESTNET_CHECKPOINTS, true),
|
||||||
let checkpoint_list: CheckpointList = match self {
|
|
||||||
Network::Mainnet => MAINNET_CHECKPOINTS
|
|
||||||
.parse()
|
|
||||||
.expect("Hard-coded Mainnet checkpoint list parses and validates"),
|
|
||||||
Network::Testnet(_params) => TESTNET_CHECKPOINTS
|
|
||||||
.parse()
|
|
||||||
.expect("Hard-coded Testnet checkpoint list parses and validates"),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match checkpoint_list.hash(block::Height(0)) {
|
// Check that the list starts with the correct genesis block and parses checkpoint list.
|
||||||
Some(hash) if hash == self.genesis_hash() => checkpoint_list,
|
let first_checkpoint_height = checkpoints_for_network
|
||||||
Some(_) => {
|
.lines()
|
||||||
panic!("The hard-coded genesis checkpoint does not match the network genesis hash")
|
.next()
|
||||||
|
.map(checkpoint_height_and_hash);
|
||||||
|
|
||||||
|
match first_checkpoint_height {
|
||||||
|
// parse calls CheckpointList::from_list
|
||||||
|
Some(Ok((block::Height(0), hash))) if hash == self.genesis_hash() => {
|
||||||
|
checkpoints_for_network
|
||||||
|
.parse()
|
||||||
|
.expect("hard-coded checkpoint list parses and validates")
|
||||||
}
|
}
|
||||||
None => unreachable!("Parser should have checked for a missing genesis checkpoint"),
|
_ if should_fallback_to_genesis_hash_as_checkpoint => {
|
||||||
|
CheckpointList::from_list([(block::Height(0), self.genesis_hash())])
|
||||||
|
.expect("hard-coded checkpoint list parses and validates")
|
||||||
|
}
|
||||||
|
Some(Ok((block::Height(0), _))) => {
|
||||||
|
panic!("the genesis checkpoint does not match the {self} genesis hash")
|
||||||
|
}
|
||||||
|
Some(Ok(_)) => panic!("checkpoints must start at the genesis block height 0"),
|
||||||
|
Some(Err(err)) => panic!("{err}"),
|
||||||
|
None => panic!(
|
||||||
|
"there must be at least one checkpoint on default networks, for the genesis block"
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a checkpoint to a [`block::Height`] and [`block::Hash`].
|
||||||
|
fn checkpoint_height_and_hash(checkpoint: &str) -> Result<(block::Height, block::Hash), BoxError> {
|
||||||
|
let fields = checkpoint.split(' ').collect::<Vec<_>>();
|
||||||
|
if let [height, hash] = fields[..] {
|
||||||
|
Ok((height.parse()?, hash.parse()?))
|
||||||
|
} else {
|
||||||
|
Err(format!("Invalid checkpoint format: expected 2 space-separated fields but found {}: '{checkpoint}'", fields.len()).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A list of block height and hash checkpoints.
|
/// A list of block height and hash checkpoints.
|
||||||
///
|
///
|
||||||
/// Checkpoints should be chosen to avoid forks or chain reorganizations,
|
/// Checkpoints should be chosen to avoid forks or chain reorganizations,
|
||||||
|
@ -115,12 +134,7 @@ impl FromStr for CheckpointList {
|
||||||
let mut checkpoint_list: Vec<(block::Height, block::Hash)> = Vec::new();
|
let mut checkpoint_list: Vec<(block::Height, block::Hash)> = Vec::new();
|
||||||
|
|
||||||
for checkpoint in s.lines() {
|
for checkpoint in s.lines() {
|
||||||
let fields = checkpoint.split(' ').collect::<Vec<_>>();
|
checkpoint_list.push(checkpoint_height_and_hash(checkpoint)?);
|
||||||
if let [height, hash] = fields[..] {
|
|
||||||
checkpoint_list.push((height.parse()?, hash.parse()?));
|
|
||||||
} else {
|
|
||||||
Err(format!("Invalid checkpoint format: expected 2 space-separated fields but found {}: '{checkpoint}'", fields.len()))?;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckpointList::from_list(checkpoint_list)
|
CheckpointList::from_list(checkpoint_list)
|
||||||
|
@ -146,17 +160,9 @@ impl CheckpointList {
|
||||||
let checkpoints: BTreeMap<block::Height, block::Hash> =
|
let checkpoints: BTreeMap<block::Height, block::Hash> =
|
||||||
original_checkpoints.into_iter().collect();
|
original_checkpoints.into_iter().collect();
|
||||||
|
|
||||||
// Check that the list starts with the correct genesis block
|
// Check that the list starts with _some_ genesis block
|
||||||
match checkpoints.iter().next() {
|
match checkpoints.iter().next() {
|
||||||
// TODO: If required (we may not need checkpoints at all in Regtest or custom testnets):
|
Some((block::Height(0), _hash)) => {}
|
||||||
// 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()
|
|
||||||
|| hash == &Network::new_default_testnet().genesis_hash()) => {}
|
|
||||||
Some((block::Height(0), _)) => {
|
|
||||||
Err("the genesis checkpoint does not match the Mainnet or Testnet genesis hash")?
|
|
||||||
}
|
|
||||||
Some(_) => Err("checkpoints must start at the genesis block height 0")?,
|
Some(_) => Err("checkpoints must start at the genesis block height 0")?,
|
||||||
None => Err("there must be at least one checkpoint, for the genesis block")?,
|
None => Err("there must be at least one checkpoint, for the genesis block")?,
|
||||||
};
|
};
|
||||||
|
|
|
@ -237,6 +237,7 @@ fn checkpoint_list_load_hard_coded() -> Result<(), BoxError> {
|
||||||
|
|
||||||
let _ = Mainnet.checkpoint_list();
|
let _ = Mainnet.checkpoint_list();
|
||||||
let _ = Network::new_default_testnet().checkpoint_list();
|
let _ = Network::new_default_testnet().checkpoint_list();
|
||||||
|
let _ = Network::new_regtest(Default::default()).checkpoint_list();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -630,8 +630,7 @@ impl<'de> Deserialize<'de> for Config {
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct DTestnetParameters {
|
struct DTestnetParameters {
|
||||||
network_name: Option<String>,
|
network_name: Option<String>,
|
||||||
#[serde(default)]
|
activation_heights: Option<ConfiguredActivationHeights>,
|
||||||
activation_heights: ConfiguredActivationHeights,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -640,6 +639,7 @@ impl<'de> Deserialize<'de> for Config {
|
||||||
listen_addr: String,
|
listen_addr: String,
|
||||||
network: NetworkKind,
|
network: NetworkKind,
|
||||||
testnet_parameters: Option<DTestnetParameters>,
|
testnet_parameters: Option<DTestnetParameters>,
|
||||||
|
regtest_activation_heights: ConfiguredActivationHeights,
|
||||||
initial_mainnet_peers: IndexSet<String>,
|
initial_mainnet_peers: IndexSet<String>,
|
||||||
initial_testnet_peers: IndexSet<String>,
|
initial_testnet_peers: IndexSet<String>,
|
||||||
cache_dir: CacheDir,
|
cache_dir: CacheDir,
|
||||||
|
@ -656,6 +656,7 @@ impl<'de> Deserialize<'de> for Config {
|
||||||
listen_addr: "0.0.0.0".to_string(),
|
listen_addr: "0.0.0.0".to_string(),
|
||||||
network: Default::default(),
|
network: Default::default(),
|
||||||
testnet_parameters: None,
|
testnet_parameters: None,
|
||||||
|
regtest_activation_heights: ConfiguredActivationHeights::default(),
|
||||||
initial_mainnet_peers: config.initial_mainnet_peers,
|
initial_mainnet_peers: config.initial_mainnet_peers,
|
||||||
initial_testnet_peers: config.initial_testnet_peers,
|
initial_testnet_peers: config.initial_testnet_peers,
|
||||||
cache_dir: config.cache_dir,
|
cache_dir: config.cache_dir,
|
||||||
|
@ -670,6 +671,7 @@ impl<'de> Deserialize<'de> for Config {
|
||||||
listen_addr,
|
listen_addr,
|
||||||
network: network_kind,
|
network: network_kind,
|
||||||
testnet_parameters,
|
testnet_parameters,
|
||||||
|
regtest_activation_heights,
|
||||||
initial_mainnet_peers,
|
initial_mainnet_peers,
|
||||||
initial_testnet_peers,
|
initial_testnet_peers,
|
||||||
cache_dir,
|
cache_dir,
|
||||||
|
@ -678,33 +680,55 @@ impl<'de> Deserialize<'de> for Config {
|
||||||
max_connections_per_ip,
|
max_connections_per_ip,
|
||||||
} = DConfig::deserialize(deserializer)?;
|
} = DConfig::deserialize(deserializer)?;
|
||||||
|
|
||||||
// TODO: Panic here if the initial testnet peers are the default initial testnet peers.
|
/// Accepts an [`IndexSet`] of initial peers,
|
||||||
let network = if let Some(DTestnetParameters {
|
///
|
||||||
network_name,
|
/// Returns true if any of them are the default Testnet or Mainnet initial peers.
|
||||||
activation_heights,
|
fn contains_default_initial_peers(initial_peers: &IndexSet<String>) -> bool {
|
||||||
}) = testnet_parameters
|
let Config {
|
||||||
{
|
initial_mainnet_peers: mut default_initial_peers,
|
||||||
assert_eq!(
|
initial_testnet_peers: default_initial_testnet_peers,
|
||||||
network_kind,
|
..
|
||||||
|
} = Config::default();
|
||||||
|
default_initial_peers.extend(default_initial_testnet_peers);
|
||||||
|
|
||||||
|
initial_peers
|
||||||
|
.intersection(&default_initial_peers)
|
||||||
|
.next()
|
||||||
|
.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
let network = match (network_kind, testnet_parameters) {
|
||||||
|
(NetworkKind::Mainnet, _) => Network::Mainnet,
|
||||||
|
(NetworkKind::Testnet, None) => Network::new_default_testnet(),
|
||||||
|
(NetworkKind::Regtest, _) => Network::new_regtest(regtest_activation_heights),
|
||||||
|
(
|
||||||
NetworkKind::Testnet,
|
NetworkKind::Testnet,
|
||||||
"set network to 'Testnet' to use configured testnet parameters"
|
Some(DTestnetParameters {
|
||||||
);
|
network_name,
|
||||||
|
activation_heights,
|
||||||
|
}),
|
||||||
|
) => {
|
||||||
|
let mut params_builder = testnet::Parameters::build();
|
||||||
|
|
||||||
let mut params_builder = testnet::Parameters::build();
|
if let Some(network_name) = network_name {
|
||||||
|
params_builder = params_builder.with_network_name(network_name)
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(network_name) = network_name {
|
// Retain default Testnet activation heights unless there's an empty [testnet_parameters.activation_heights] section.
|
||||||
params_builder = params_builder.with_network_name(network_name)
|
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 = params_builder.with_activation_heights(activation_heights)
|
||||||
.with_activation_heights(activation_heights)
|
}
|
||||||
.to_network()
|
|
||||||
} else {
|
params_builder.to_network()
|
||||||
// Convert to default `Network` for a `NetworkKind` if there are no testnet parameters.
|
|
||||||
match network_kind {
|
|
||||||
NetworkKind::Mainnet => Network::Mainnet,
|
|
||||||
NetworkKind::Testnet => Network::new_default_testnet(),
|
|
||||||
NetworkKind::Regtest => unimplemented!("Regtest is not yet implemented in Zebra"),
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1098,6 +1098,11 @@ where
|
||||||
data: None,
|
data: None,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
// # Concurrency
|
||||||
|
//
|
||||||
|
// For consistency, this lookup must be performed first, then all the other
|
||||||
|
// lookups must be based on the hash.
|
||||||
|
|
||||||
// Fetch the block referenced by [`hash_or_height`] from the state.
|
// Fetch the block referenced by [`hash_or_height`] from the state.
|
||||||
// TODO: If this RPC is called a lot, just get the block header,
|
// TODO: If this RPC is called a lot, just get the block header,
|
||||||
// rather than the whole block.
|
// rather than the whole block.
|
||||||
|
@ -1128,6 +1133,9 @@ where
|
||||||
_ => unreachable!("unmatched response to a block request"),
|
_ => unreachable!("unmatched response to a block request"),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let hash = hash_or_height.hash().unwrap_or_else(|| block.hash());
|
||||||
|
let hash_or_height = hash.into();
|
||||||
|
|
||||||
// Fetch the Sapling & Orchard treestates referenced by
|
// Fetch the Sapling & Orchard treestates referenced by
|
||||||
// [`hash_or_height`] from the state.
|
// [`hash_or_height`] from the state.
|
||||||
|
|
||||||
|
@ -1156,8 +1164,6 @@ where
|
||||||
// We've got all the data we need for the RPC response, so we
|
// We've got all the data we need for the RPC response, so we
|
||||||
// assemble the response.
|
// assemble the response.
|
||||||
|
|
||||||
let hash = block.hash();
|
|
||||||
|
|
||||||
let height = block
|
let height = block
|
||||||
.coinbase_height()
|
.coinbase_height()
|
||||||
.expect("verified blocks have a valid height");
|
.expect("verified blocks have a valid height");
|
||||||
|
|
|
@ -451,6 +451,7 @@ fn finalized_equals_pushed_history_tree() -> Result<()> {
|
||||||
/// Check that rejected blocks do not change the internal state of a genesis chain
|
/// Check that rejected blocks do not change the internal state of a genesis chain
|
||||||
/// in a non-finalized state.
|
/// in a non-finalized state.
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(not(target_os = "windows"))]
|
||||||
fn rejection_restores_internal_state_genesis() -> Result<()> {
|
fn rejection_restores_internal_state_genesis() -> Result<()> {
|
||||||
let _init_guard = zebra_test::init();
|
let _init_guard = zebra_test::init();
|
||||||
|
|
||||||
|
|
|
@ -161,10 +161,12 @@ use color_eyre::{
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use tower::ServiceExt;
|
||||||
use zebra_chain::{
|
use zebra_chain::{
|
||||||
block::{self, Height},
|
block::{self, genesis::regtest_genesis_block, Height},
|
||||||
parameters::Network::{self, *},
|
parameters::Network::{self, *},
|
||||||
};
|
};
|
||||||
|
use zebra_consensus::ParameterCheckpoint;
|
||||||
use zebra_network::constants::PORT_IN_USE_ERROR;
|
use zebra_network::constants::PORT_IN_USE_ERROR;
|
||||||
use zebra_node_services::rpc_client::RpcRequestClient;
|
use zebra_node_services::rpc_client::RpcRequestClient;
|
||||||
use zebra_state::{constants::LOCK_FILE_ERROR, state_database_format_version_in_code};
|
use zebra_state::{constants::LOCK_FILE_ERROR, state_database_format_version_in_code};
|
||||||
|
@ -1757,7 +1759,7 @@ fn non_blocking_logger() -> Result<()> {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait until the spawned task finishes up to 45 seconds before shutting down tokio runtime
|
// Wait until the spawned task finishes up to 45 seconds before shutting down tokio runtime
|
||||||
if done_rx.recv_timeout(Duration::from_secs(45)).is_ok() {
|
if done_rx.recv_timeout(Duration::from_secs(90)).is_ok() {
|
||||||
rt.shutdown_timeout(Duration::from_secs(3));
|
rt.shutdown_timeout(Duration::from_secs(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3099,3 +3101,29 @@ fn scan_start_where_left() -> Result<()> {
|
||||||
async fn scan_task_commands() -> Result<()> {
|
async fn scan_task_commands() -> Result<()> {
|
||||||
common::shielded_scan::scan_task_commands::run().await
|
common::shielded_scan::scan_task_commands::run().await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks that the Regtest genesis block can be validated.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn validate_regtest_genesis_block() {
|
||||||
|
let _init_guard = zebra_test::init();
|
||||||
|
|
||||||
|
let network = Network::new_regtest(Default::default());
|
||||||
|
let state = zebra_state::init_test(&network);
|
||||||
|
let (
|
||||||
|
block_verifier_router,
|
||||||
|
_transaction_verifier,
|
||||||
|
_parameter_download_task_handle,
|
||||||
|
_max_checkpoint_height,
|
||||||
|
) = zebra_consensus::router::init(zebra_consensus::Config::default(), &network, state).await;
|
||||||
|
|
||||||
|
let genesis_hash = block_verifier_router
|
||||||
|
.oneshot(zebra_consensus::Request::Commit(regtest_genesis_block()))
|
||||||
|
.await
|
||||||
|
.expect("should validate Regtest genesis block");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
genesis_hash,
|
||||||
|
network.genesis_hash(),
|
||||||
|
"validated block hash should match network genesis hash"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ initial_testnet_peers = [
|
||||||
]
|
]
|
||||||
listen_addr = "0.0.0.0:8233"
|
listen_addr = "0.0.0.0:8233"
|
||||||
max_connections_per_ip = 1
|
max_connections_per_ip = 1
|
||||||
network = "Testnet"
|
network = "Regtest"
|
||||||
peerset_initial_target_size = 25
|
peerset_initial_target_size = 25
|
||||||
|
|
||||||
[network.testnet_parameters]
|
[network.testnet_parameters]
|
||||||
|
@ -72,6 +72,15 @@ Heartwood = 903_800
|
||||||
Canopy = 1_028_500
|
Canopy = 1_028_500
|
||||||
NU5 = 1_842_420
|
NU5 = 1_842_420
|
||||||
|
|
||||||
|
[network.regtest_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]
|
[rpc]
|
||||||
debug_force_finished_sync = false
|
debug_force_finished_sync = false
|
||||||
parallel_cpu_threads = 0
|
parallel_cpu_threads = 0
|
||||||
|
|
Loading…
Reference in New Issue