Skip to content

Commit

Permalink
Post-rebase fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariopil committed Nov 5, 2024
1 parent 8500594 commit bb23624
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 116 deletions.
92 changes: 36 additions & 56 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,15 @@ dependencies = [
"byteorder",
]

[[package]]
name = "equihash"
version = "0.2.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"blake2b_simd",
"byteorder",
]

[[package]]
name = "equivalent"
version = "1.0.1"
Expand Down Expand Up @@ -1372,7 +1381,7 @@ dependencies = [
[[package]]
name = "f4jumble"
version = "0.1.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"blake2b_simd",
]
Expand Down Expand Up @@ -5548,14 +5557,14 @@ dependencies = [

[[package]]
name = "which"
version = "4.4.2"
version = "6.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7"
checksum = "b4ee928febd44d98f2f459a4a79bd4d928591333a494a10a868418ac1b39cf1f"
dependencies = [
"either",
"home",
"once_cell",
"rustix",
"winsafe",
]

[[package]]
Expand Down Expand Up @@ -5805,6 +5814,12 @@ dependencies = [
"windows-sys 0.48.0",
]

[[package]]
name = "winsafe"
version = "0.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"

[[package]]
name = "wyz"
version = "0.5.1"
Expand Down Expand Up @@ -5834,8 +5849,8 @@ checksum = "213b7324336b53d2414b2db8537e56544d981803139155afa84f76eeebb7a546"

[[package]]
name = "zcash_address"
version = "0.5.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
version = "0.6.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"bech32",
"bs58",
Expand All @@ -5846,8 +5861,8 @@ dependencies = [

[[package]]
name = "zcash_client_backend"
version = "0.13.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
version = "0.14.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"base64 0.22.1",
"bech32",
Expand Down Expand Up @@ -5886,7 +5901,7 @@ dependencies = [
[[package]]
name = "zcash_encoding"
version = "0.2.1"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"byteorder",
"nonempty",
Expand All @@ -5895,7 +5910,7 @@ dependencies = [
[[package]]
name = "zcash_history"
version = "0.4.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"blake2b_simd",
"byteorder",
Expand All @@ -5904,8 +5919,8 @@ dependencies = [

[[package]]
name = "zcash_keys"
version = "0.3.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
version = "0.4.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"bech32",
"blake2b_simd",
Expand Down Expand Up @@ -5942,8 +5957,8 @@ dependencies = [

[[package]]
name = "zcash_primitives"
version = "0.17.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
version = "0.19.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"aes",
"bip32",
Expand Down Expand Up @@ -5980,8 +5995,8 @@ dependencies = [

[[package]]
name = "zcash_proofs"
version = "0.17.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
version = "0.19.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"bellman",
"blake2b_simd",
Expand All @@ -6002,8 +6017,8 @@ dependencies = [

[[package]]
name = "zcash_protocol"
version = "0.3.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
version = "0.4.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"document-features",
"memuse",
Expand Down Expand Up @@ -6045,7 +6060,7 @@ dependencies = [
"criterion",
"dirs",
"ed25519-zebra",
"equihash",
"equihash 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures",
"group",
"halo2_proofs",
Expand Down Expand Up @@ -6543,47 +6558,12 @@ dependencies = [

[[package]]
name = "zip321"
version = "0.1.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#1b4f984a61cc88029c0b78e9d13225aad76c6903"
version = "0.2.0"
source = "git+https://github.com/ShieldedLabs/librustzcash/?branch=nsm-zebra#5fcd3034e47d425df4846192abd4d84fb3752207"
dependencies = [
"base64 0.22.1",
"nom",
"percent-encoding",
"zcash_address",
"zcash_protocol",
]

[[patch.unused]]
name = "zcash_address"
version = "0.5.0"
source = "git+https://github.com/zcash/librustzcash.git?rev=1410f1449100a417bfbc4f6c7167aa9808e38792#1410f1449100a417bfbc4f6c7167aa9808e38792"

[[patch.unused]]
name = "zcash_client_backend"
version = "0.13.0"
source = "git+https://github.com/zcash/librustzcash.git?rev=1410f1449100a417bfbc4f6c7167aa9808e38792#1410f1449100a417bfbc4f6c7167aa9808e38792"

[[patch.unused]]
name = "zcash_encoding"
version = "0.2.1"
source = "git+https://github.com/zcash/librustzcash.git?rev=1410f1449100a417bfbc4f6c7167aa9808e38792#1410f1449100a417bfbc4f6c7167aa9808e38792"

[[patch.unused]]
name = "zcash_history"
version = "0.4.0"
source = "git+https://github.com/zcash/librustzcash.git?rev=1410f1449100a417bfbc4f6c7167aa9808e38792#1410f1449100a417bfbc4f6c7167aa9808e38792"

[[patch.unused]]
name = "zcash_primitives"
version = "0.17.0"
source = "git+https://github.com/zcash/librustzcash.git?rev=1410f1449100a417bfbc4f6c7167aa9808e38792#1410f1449100a417bfbc4f6c7167aa9808e38792"

[[patch.unused]]
name = "zcash_proofs"
version = "0.17.0"
source = "git+https://github.com/zcash/librustzcash.git?rev=1410f1449100a417bfbc4f6c7167aa9808e38792#1410f1449100a417bfbc4f6c7167aa9808e38792"

[[patch.unused]]
name = "zcash_protocol"
version = "0.3.0"
source = "git+https://github.com/zcash/librustzcash.git?rev=1410f1449100a417bfbc4f6c7167aa9808e38792#1410f1449100a417bfbc4f6c7167aa9808e38792"
12 changes: 3 additions & 9 deletions zebra-chain/src/block/subsidy/funding_streams/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ use color_eyre::Report;

use crate::{
block::subsidy::general::block_subsidy_pre_nsm,
parameters::{
subsidy::FundingStreamReceiver,
testnet::{
self, ConfiguredActivationHeights, ConfiguredFundingStreamRecipient,
ConfiguredFundingStreams,
},
NetworkKind,
},
parameters::{subsidy::FundingStreamReceiver, NetworkKind},
};

use super::*;
Expand Down Expand Up @@ -79,7 +72,8 @@ fn test_funding_stream_values() -> Result<(), Report> {
nu6_fund_height_range.end,
nu6_fund_height_range.end.next().unwrap(),
] {
let fsv = funding_stream_values(height, network, block_subsidy(height, network)?).unwrap();
let fsv = funding_stream_values(height, network, block_subsidy_pre_nsm(height, network)?)
.unwrap();

if height < canopy_activation_height {
assert!(fsv.is_empty());
Expand Down
49 changes: 23 additions & 26 deletions zebra-chain/src/block/subsidy/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,39 @@ use crate::{
///
/// Returns `None` if the divisor would overflow a `u64`.
pub fn halving_divisor(height: Height, network: &Network) -> Option<u64> {
// Some far-future shifts can be more than 63 bits
1u64.checked_shl(num_halvings(height, network))
}

/// The halving index for a block height and network.
///
/// `Halving(height)`, as described in [protocol specification §7.8][7.8]
///
/// [7.8]: https://zips.z.cash/protocol/protocol.pdf#subsidies
pub fn num_halvings(height: Height, network: &Network) -> u32 {
let slow_start_shift = network.slow_start_shift();
let blossom_height = Blossom
.activation_height(network)
.expect("blossom activation height should be available");

if height < blossom_height {
let pre_blossom_height = height - network.slow_start_shift();
let halving_shift = pre_blossom_height / PRE_BLOSSOM_HALVING_INTERVAL;

let halving_div = 1u64
.checked_shl(
halving_shift
.try_into()
.expect("already checked for negatives"),
)
.expect("pre-blossom heights produce small shifts");

Some(halving_div)
let halving_index = if height < slow_start_shift {
0
} else if height < blossom_height {
let pre_blossom_height = height - slow_start_shift;
pre_blossom_height / network.pre_blossom_halving_interval()
} else {
let pre_blossom_height = blossom_height - network.slow_start_shift();
let pre_blossom_height = blossom_height - slow_start_shift;
let scaled_pre_blossom_height =
pre_blossom_height * HeightDiff::from(BLOSSOM_POW_TARGET_SPACING_RATIO);

let post_blossom_height = height - blossom_height;

let halving_shift =
(scaled_pre_blossom_height + post_blossom_height) / POST_BLOSSOM_HALVING_INTERVAL;
(scaled_pre_blossom_height + post_blossom_height) / network.post_blossom_halving_interval()
};

// Some far-future shifts can be more than 63 bits
1u64.checked_shl(
halving_shift
.try_into()
.expect("already checked for negatives"),
)
}
halving_index
.try_into()
.expect("already checked for negatives")
}

#[cfg(zcash_unstable = "nsm")]
Expand Down Expand Up @@ -429,9 +428,7 @@ mod test {
fn check_height_for_num_halvings() {
for network in Network::iter() {
for halving in 1..1000 {
let Some(height_for_halving) =
zebra_chain::parameters::subsidy::height_for_halving(halving, &network)
else {
let Some(height_for_halving) = height_for_halving(halving, &network) else {
panic!("could not find height for halving {halving}");
};

Expand Down
3 changes: 3 additions & 0 deletions zebra-chain/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ pub enum SubsidyError {

#[error("invalid burn amount")]
InvalidBurnAmount,

#[error("unexpected error occurred: {0}")]
Other(String),
}

impl From<amount::Error> for SubsidyError {
Expand Down
2 changes: 1 addition & 1 deletion zebra-consensus/src/block/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ fn miner_fees_validation_failure() -> Result<(), Report> {
let block = Block::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_347499_BYTES[..])
.expect("block should deserialize");
let height = block.coinbase_height().expect("valid coinbase height");
let expected_block_subsidy = block_subsidy(height, &network)?;
let expected_block_subsidy = block_subsidy_pre_nsm(height, &network)?;
// See [ZIP-1015](https://zips.z.cash/zip-1015).
let expected_deferred_amount: Amount<zebra_chain::amount::NonNegative> =
subsidy::funding_streams::funding_stream_values(height, &network, expected_block_subsidy)
Expand Down
2 changes: 0 additions & 2 deletions zebra-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ jubjub = "0.10.0"

tokio = { version = "1.41.0", features = ["full", "tracing", "test-util"] }

tokio = { version = "1.40.0", features = ["full", "tracing", "test-util"] }

zebra-chain = { path = "../zebra-chain", version = "1.0.0-beta.42", features = ["proptest-impl"] }
zebra-test = { path = "../zebra-test/", version = "1.0.0-beta.42" }

Expand Down
39 changes: 17 additions & 22 deletions zebra-state/src/service/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ pub fn subsidy_is_valid(
let coinbase = block.transactions.first().ok_or(SubsidyError::NoCoinbase)?;

// Validate funding streams
let Some(halving_div) = subsidy::general::halving_divisor(height, network) else {
let Some(halving_div) = general::halving_divisor(height, network) else {
// Far future halving, with no founders reward or funding streams
return Ok(());
};
Expand All @@ -560,13 +560,10 @@ pub fn subsidy_is_valid(
// Note: Canopy activation is at the first halving on mainnet, but not on testnet
// ZIP-1014 only applies to mainnet, ZIP-214 contains the specific rules for testnet
// funding stream amount values
let funding_streams = subsidy::funding_streams::funding_stream_values(
height,
network,
expected_block_subsidy,
)
// we always expect a funding stream hashmap response even if empty
.map_err(|err| BlockError::Other(err.to_string()))?;
let funding_streams =
funding_streams::funding_stream_values(height, network, expected_block_subsidy)
// we always expect a funding stream hashmap response even if empty
.map_err(|err| SubsidyError::Other(err.to_string()))?;

// # Consensus
//
Expand All @@ -583,20 +580,18 @@ pub fn subsidy_is_valid(
continue;
}

let address =
subsidy::funding_streams::funding_stream_address(height, network, receiver)
// funding stream receivers other than the deferred pool must have an address
.ok_or_else(|| {
BlockError::Other(format!(
"missing funding stream address at height {height:?}"
))
})?;

let has_expected_output =
subsidy::funding_streams::filter_outputs_by_address(coinbase, address)
.iter()
.map(zebra_chain::transparent::Output::value)
.any(|value| value == expected_amount);
let address = funding_streams::funding_stream_address(height, network, receiver)
// funding stream receivers other than the deferred pool must have an address
.ok_or_else(|| {
SubsidyError::Other(format!(
"missing funding stream address at height {height:?}"
))
})?;

let has_expected_output = funding_streams::filter_outputs_by_address(coinbase, address)
.iter()
.map(zebra_chain::transparent::Output::value)
.any(|value| value == expected_amount);

if !has_expected_output {
Err(SubsidyError::FundingStreamNotFound)?;
Expand Down

0 comments on commit bb23624

Please sign in to comment.