Documentation fixes, minor cleanup, renames a test, adds TODOs, and fixes test logic

This commit is contained in:
Arya 2024-07-23 22:20:56 -04:00
parent 1de09560f6
commit 4fa07659f1
6 changed files with 65 additions and 16 deletions

View File

@ -1,4 +1,16 @@
//! Constants and calculations for Block Subsidy and Funding Streams
//!
//! This module contains the consensus parameters which are required for
//! verification.
//!
//! Some consensus parameters change based on network upgrades. Each network
//! upgrade happens at a particular block height. Some parameters have a value
//! (or function) before the upgrade height, at the upgrade height, and after
//! the upgrade height. (For example, the value of the reserved field in the
//! block header during the Heartwood upgrade.)
//!
//! Typically, consensus parameters are accessed via a function that takes a
//! `Network` and `block::Height`.
use std::collections::HashMap;
@ -403,7 +415,7 @@ pub fn funding_stream_address_period(height: Height, network: impl ParameterSubs
//
// In Rust, "integer division rounds towards zero":
// <https://doc.rust-lang.org/stable/reference/expressions/operator-expr.html#arithmetic-and-logical-binary-operators>
// This is the same as `floor()`, because these numbers are all positive.
// This is the same as `floor()`, because these numbers are all positive.
let height_after_first_halving = height - network.height_for_first_halving();

View File

@ -7,7 +7,7 @@ use crate::{
constants::{magics, SLOW_START_INTERVAL, SLOW_START_SHIFT},
network_upgrade::TESTNET_ACTIVATION_HEIGHTS,
subsidy::{funding_stream_address_period, FUNDING_STREAM_RECEIVER_DENOMINATOR},
Network, NetworkUpgrade, NETWORK_UPGRADES_IN_ORDER,
Network, NetworkKind, NetworkUpgrade, NETWORK_UPGRADES_IN_ORDER,
},
work::difficulty::{ExpandedDifficulty, U256},
};
@ -50,6 +50,7 @@ const REGTEST_GENESIS_HASH: &str =
const TESTNET_GENESIS_HASH: &str =
"05a60a92d99d85997cce3b87616c089f6124d7342af37106edc76126334a2c38";
/// Used to validate number of funding stream recipient addresses on configured Testnets.
struct TestnetParameterSubsidyImpl;
impl ParameterSubsidy for TestnetParameterSubsidyImpl {
@ -58,7 +59,7 @@ impl ParameterSubsidy for TestnetParameterSubsidyImpl {
}
}
/// Configurable funding streams for Regtest and configured Testnets.
/// Configurable funding stream recipient for configured Testnets.
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct ConfiguredFundingStreamRecipient {
@ -80,7 +81,7 @@ impl ConfiguredFundingStreamRecipient {
}
}
/// Configurable funding streams for Regtest and configured Testnets.
/// Configurable funding streams for configured Testnets.
#[derive(Deserialize, Clone, Default, Debug)]
#[serde(deny_unknown_fields)]
pub struct ConfiguredFundingStreams {
@ -91,6 +92,8 @@ pub struct ConfiguredFundingStreams {
}
impl ConfiguredFundingStreams {
/// Converts a [`ConfiguredFundingStreams`] to a [`FundingStreams`], using the provided default values
/// if `height_range` or `recipients` are None.
fn convert_with_default(self, default_funding_streams: FundingStreams) -> FundingStreams {
let height_range = self
.height_range
@ -137,6 +140,14 @@ impl ConfiguredFundingStreams {
"recipients must have a sufficient number of addresses for height range, \
minimum num addresses required: {expected_min_num_addresses}"
);
for address in recipient.addresses() {
assert_eq!(
address.network_kind(),
NetworkKind::Testnet,
"configured funding stream addresses must be for Testnet"
);
}
}
// check that sum of receiver numerators is valid.
@ -662,7 +673,7 @@ impl Network {
}
}
/// Returns funding streams for this network at the provided height
/// Returns post-Canopy funding streams for this network at the provided height
pub fn funding_streams(&self, height: Height) -> &FundingStreams {
if NetworkUpgrade::current(self, height) < NetworkUpgrade::Nu6 {
self.pre_nu6_funding_streams()

View File

@ -7,8 +7,9 @@ use crate::{
block::Height,
parameters::{
subsidy::{
FundingStreamReceiver, FUNDING_STREAM_ECC_ADDRESSES_TESTNET,
POST_NU6_FUNDING_STREAMS_TESTNET, PRE_NU6_FUNDING_STREAMS_TESTNET,
FundingStreamReceiver, FUNDING_STREAM_ECC_ADDRESSES_MAINNET,
FUNDING_STREAM_ECC_ADDRESSES_TESTNET, POST_NU6_FUNDING_STREAMS_TESTNET,
PRE_NU6_FUNDING_STREAMS_TESTNET,
},
testnet::{
self, ConfiguredActivationHeights, ConfiguredFundingStreamRecipient,
@ -301,8 +302,10 @@ fn check_full_activation_list() {
}
}
/// Tests that a set of constraints are enforced when building Testnet parameters,
/// and that funding stream configurations that should be valid can be built.
#[test]
fn check_funding_streams() {
fn check_configured_funding_stream_constraints() {
let configured_funding_streams = [
Default::default(),
ConfiguredFundingStreams {
@ -415,6 +418,20 @@ fn check_funding_streams() {
});
});
// should panic when recipient addresses are for Mainnet.
let expected_panic_wrong_addr_network = std::panic::catch_unwind(|| {
testnet::Parameters::build().with_pre_nu6_funding_streams(ConfiguredFundingStreams {
recipients: Some(vec![ConfiguredFundingStreamRecipient {
receiver: FundingStreamReceiver::Ecc,
numerator: 10,
addresses: FUNDING_STREAM_ECC_ADDRESSES_MAINNET
.map(Into::into)
.to_vec(),
}]),
..Default::default()
});
});
// drop panic hook before expecting errors.
let _ = std::panic::take_hook();
@ -422,4 +439,6 @@ fn check_funding_streams() {
expected_panic_numerator.expect_err(
"should panic when sum of numerators is greater than funding stream denominator",
);
expected_panic_wrong_addr_network
.expect_err("should panic when recipient addresses are for Mainnet");
}

View File

@ -69,10 +69,12 @@ fn funding_stream_address_index(height: Height, network: &Network) -> usize {
// when configured Testnet parameters are built.
let num_addresses = funding_streams
.recipients()
.iter()
.values()
.next()
.map(|(_, recipient)| recipient.addresses().len())
.unwrap_or_default();
// TODO: Return an Option from this function and replace `.unwrap()` with `?`
.unwrap()
.addresses()
.len();
assert!(index > 0 && index <= num_addresses);
// spec formula will output an index starting at 1 but

View File

@ -1,7 +1,7 @@
//! Tests for funding streams.
use color_eyre::Report;
use zebra_chain::parameters::subsidy::FundingStreamReceiver;
use zebra_chain::parameters::{subsidy::FundingStreamReceiver, NetworkKind};
use super::*;
@ -45,8 +45,8 @@ fn test_funding_stream_values() -> Result<(), Report> {
);
// funding stream period is ending
let funding_streams = network.pre_nu6_funding_streams();
let range = funding_streams.height_range();
// TODO: Check post-NU6 funding streams here as well.
let range = network.pre_nu6_funding_streams().height_range();
let end = range.end;
let last = end - 1;
@ -66,9 +66,15 @@ fn test_funding_stream_addresses() -> Result<(), Report> {
for network in Network::iter() {
for (receiver, recipient) in network.pre_nu6_funding_streams().recipients() {
for address in recipient.addresses() {
let expected_network_kind = match network.kind() {
NetworkKind::Mainnet => NetworkKind::Mainnet,
// `Regtest` uses `Testnet` transparent addresses.
NetworkKind::Testnet | NetworkKind::Regtest => NetworkKind::Testnet,
};
assert_eq!(
address.network_kind(),
network.kind(),
expected_network_kind,
"incorrect network for {receiver:?} funding stream address constant: {address}",
);

View File

@ -193,7 +193,6 @@ addresses = [
"t26ovBdKAJLtrvBsE2QGF4nqBkEuptuPFZz",
]
[rpc]
debug_force_finished_sync = false
parallel_cpu_threads = 0