Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change: Track the balance of the deferred chain value pool #8729

Merged
merged 64 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
bf1278a
Addresses clippy lints
arya2 Jul 25, 2024
aa88979
checks network magic and returns early from `is_regtest()`
arya2 Jul 22, 2024
d9a53e6
Moves `subsidy.rs` to `zebra-chain`, refactors funding streams into …
arya2 Jul 23, 2024
124e249
Replaces Vec with HashMap, adds `ConfiguredFundingStreams` type and c…
arya2 Jul 24, 2024
cbf7ef3
Empties recipients list
arya2 Jul 24, 2024
54af589
Adds a comment on num_addresses calculation being invalid for configu…
arya2 Jul 24, 2024
db434c3
Documentation fixes, minor cleanup, renames a test, adds TODOs, and f…
arya2 Jul 24, 2024
7d7f606
Removes unnecessary `ParameterSubsidy` impl for &Network, adds docs a…
arya2 Jul 24, 2024
77a6f2c
Adds a "deferred" FundingStreamReceiver, adds a post-NU6 funding stre…
arya2 Jul 24, 2024
dbc7ca9
adds `lockbox_input_value()` fn
arya2 Jul 24, 2024
0f962c5
Adds TODOs for linking to relevant ZIPs and updating height ranges
arya2 Jul 24, 2024
9bca31f
Adds `nu6_lockbox_funding_stream` acceptance test
arya2 Jul 24, 2024
0d22b68
updates funding stream values test to check post-NU6 funding streams …
arya2 Jul 24, 2024
039c9a1
Reverts Mainnet/Testnet NU6 activation height definitions, updates `t…
arya2 Jul 24, 2024
2f14517
reverts unnecessary refactor
arya2 Jul 24, 2024
33a5545
appease clippy
arya2 Jul 24, 2024
1183d49
Adds a test for `lockbox_input_value()`
arya2 Jul 24, 2024
b523bf5
Applies suggestions from code review
arya2 Jul 25, 2024
7df2e28
Fixes potential panic
arya2 Jul 26, 2024
95378fd
Merge branch 'main' into deferred-pool
arya2 Jul 29, 2024
7173af7
Fixes bad merge
arya2 Jul 29, 2024
c6244e6
Update zebra-chain/src/parameters/network_upgrade.rs
arya2 Jul 29, 2024
166bc23
Updates acceptance test to check that invalid blocks are rejected
arya2 Jul 29, 2024
c113601
Checks that the original valid block template at height 2 is accepted…
arya2 Jul 29, 2024
4627c08
Reverts changes for coinbase should balance exactly ZIP
arya2 Jul 29, 2024
4d74245
Add `Deferred` to `ValueBalance`
upbqdn Jul 23, 2024
19f63b2
Update snapshots
upbqdn Jul 24, 2024
c54ef0c
Unrelated: Revise docs
upbqdn Jul 24, 2024
3a3f2ed
Add TODOs
upbqdn Jul 25, 2024
8f83b14
Stop recalculating the block subsidy
upbqdn Jul 28, 2024
5c470ef
Track deferred balances
upbqdn Jul 29, 2024
27177f2
Support heights below slow start shift in halvings
upbqdn Jul 29, 2024
99b761e
Fix `CheckpointVerifiedBlock` conversion in tests
upbqdn Jul 29, 2024
fbbd8d1
Allow deserialization of legacy `ValueBalance`s
upbqdn Jul 29, 2024
6aa2858
Merge branch 'main' into deferred-pool
mpguerra Jul 30, 2024
cbafe35
Simplify docs
upbqdn Jul 30, 2024
8074a13
Merge branch 'deferred-pool' into update-value-balance
upbqdn Jul 30, 2024
a0fd18a
Fix warnings raised by Clippy
upbqdn Jul 30, 2024
9c0c7ff
Fix warnings raised by `cargo fmt`
upbqdn Jul 30, 2024
0bdda3a
Update zebra-chain/src/block.rs
upbqdn Jul 30, 2024
a1b3ca6
Refactor docs around chain value pool changes
upbqdn Jul 31, 2024
c5e7dbe
Merge branch 'update-value-balance' of github.com:ZcashFoundation/zeb…
upbqdn Jul 31, 2024
b82f2be
Merge branch 'main' into deferred-pool
arya2 Jul 31, 2024
126fdab
updates test name
arya2 Aug 1, 2024
bf49432
Updates deferred pool funding stream name to "Lockbox", moves post-NU…
arya2 Aug 1, 2024
14fbfb4
Updates `get_block_subsidy()` RPC method to exclude lockbox funding s…
arya2 Aug 1, 2024
7987c76
Adds a TODO for updating `FundingStreamReceiver::name()` method docs
arya2 Aug 1, 2024
e5eaf38
Updates `FundingStreamRecipient::new()` to accept an iterator of item…
arya2 Aug 1, 2024
a813af8
Update zebra-consensus/src/checkpoint.rs
upbqdn Aug 1, 2024
16eeee4
Update docs for value balances
upbqdn Aug 1, 2024
f826113
Cleanup: Simplify getting info for FS receivers
upbqdn Aug 1, 2024
d7f3df9
Avoid a panic when deserializing value balances
upbqdn Aug 1, 2024
bcdb7f3
Merge branch 'deferred-pool' into update-value-balance
upbqdn Aug 1, 2024
8949301
Uses FPF Testnet address for post-NU6 testnet funding streams
arya2 Aug 1, 2024
cdb1684
Updates the NU6 consensus branch id
arya2 Aug 1, 2024
40da48d
Update zebra-consensus/src/checkpoint.rs
arya2 Aug 1, 2024
93160c5
Merge branch 'deferred-pool' into update-value-balance
upbqdn Aug 1, 2024
9620344
Merge branch 'update-value-balance' of github.com:ZcashFoundation/zeb…
upbqdn Aug 1, 2024
9cb40e4
Bump the major database format version
upbqdn Aug 1, 2024
b37f6b4
Add a database upgrade mark
upbqdn Aug 3, 2024
c378a25
Merge branch 'main' into update-value-balance
upbqdn Aug 3, 2024
ba5ecec
Fix tests after merge
upbqdn Aug 3, 2024
fd41ebe
Merge branch 'main' into update-value-balance
upbqdn Aug 3, 2024
64279cb
trigger GitHub actions
upbqdn Aug 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions zebra-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,7 @@ required-features = ["bench"]
[[bench]]
name = "redpallas"
harness = false

[lints.rust]
# TODO: Remove this once it's no longer needed for NU6.
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(zcash_unstable, values("nu6"))'] }
37 changes: 21 additions & 16 deletions zebra-chain/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{collections::HashMap, fmt, ops::Neg, sync::Arc};
use halo2::pasta::pallas;

use crate::{
amount::NegativeAllowed,
amount::{Amount, NegativeAllowed, NonNegative},
block::merkle::AuthDataRoot,
fmt::DisplayToDebug,
orchard,
Expand Down Expand Up @@ -205,34 +205,39 @@ impl Block {
.expect("number of transactions must fit u64")
}

/// Get the overall chain value pool change in this block,
/// the negative sum of the transaction value balances in this block.
/// Returns the overall chain value pool change in this block---the negative sum of the
/// transaction value balances in this block.
///
/// These are the changes in the transparent, sprout, sapling, and orchard
/// chain value pools, as a result of this block.
/// These are the changes in the transparent, Sprout, Sapling, Orchard, and
/// Deferred chain value pools, as a result of this block.
///
/// Positive values are added to the corresponding chain value pool.
/// Negative values are removed from the corresponding pool.
/// Positive values are added to the corresponding chain value pool and negative values are
/// removed from the corresponding pool.
///
/// <https://zebra.zfnd.org/dev/rfcs/0012-value-pools.html#definitions>
///
/// `utxos` must contain the [`transparent::Utxo`]s of every input in this block,
/// including UTXOs created by earlier transactions in this block.
/// (It can also contain unrelated UTXOs, which are ignored.)
/// The given `utxos` must contain the [`transparent::Utxo`]s of every input in this block,
/// including UTXOs created by earlier transactions in this block. It can also contain unrelated
/// UTXOs, which are ignored.
///
/// Note: the chain value pool has the opposite sign to the transaction
/// value pool.
/// Note that the chain value pool has the opposite sign to the transaction value pool.
pub fn chain_value_pool_change(
&self,
utxos: &HashMap<transparent::OutPoint, transparent::Utxo>,
deferred_balance: Option<Amount<NonNegative>>,
) -> Result<ValueBalance<NegativeAllowed>, ValueBalanceError> {
let transaction_value_balance_total = self
Ok(*self
.transactions
.iter()
.flat_map(|t| t.value_balance(utxos))
.sum::<Result<ValueBalance<NegativeAllowed>, _>>()?;

Ok(transaction_value_balance_total.neg())
.sum::<Result<ValueBalance<NegativeAllowed>, _>>()?
.neg()
.set_deferred_amount(
deferred_balance
.unwrap_or(Amount::zero())
.constrain::<NegativeAllowed>()
.map_err(ValueBalanceError::Deferred)?,
))
}

/// Compute the root of the authorizing data Merkle tree,
Expand Down
95 changes: 83 additions & 12 deletions zebra-chain/src/parameters/network/subsidy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,30 @@ pub enum FundingStreamReceiver {

/// The Major Grants (Zcash Community Grants) funding stream.
MajorGrants,
/// The deferred pool contribution.
// TODO: Add link to lockbox stream ZIP
Deferred,
}

impl FundingStreamReceiver {
/// The name for each funding stream receiver, as described in [ZIP-1014] and [`zcashd`].
/// Returns a human-readable name and a specification URL for the receiver, as described in
/// [ZIP-1014] and [`zcashd`].
///
/// [ZIP-1014]: https://zips.z.cash/zip-1014#abstract
/// [`zcashd`]: https://github.com/zcash/zcash/blob/3f09cfa00a3c90336580a127e0096d99e25a38d6/src/consensus/funding.cpp#L13-L32
pub fn name(self) -> &'static str {
match self {
FundingStreamReceiver::Ecc => "Electric Coin Company",
FundingStreamReceiver::ZcashFoundation => "Zcash Foundation",
FundingStreamReceiver::MajorGrants => "Major Grants",
}
// TODO: Update method documentation with a reference to https://zips.z.cash/draft-nuttycom-funding-allocation once its
// status is updated to 'Proposed'.
pub fn info(&self) -> (&'static str, &'static str) {
(
match self {
FundingStreamReceiver::Ecc => "Electric Coin Company",
FundingStreamReceiver::ZcashFoundation => "Zcash Foundation",
FundingStreamReceiver::MajorGrants => "Major Grants",
// TODO: Find out what this should be called and update the funding stream name
FundingStreamReceiver::Deferred => "Lockbox",
},
FUNDING_STREAM_SPECIFICATION,
)
}
}

Expand All @@ -83,6 +94,7 @@ impl FundingStreamReceiver {
/// [7.10.1]: https://zips.z.cash/protocol/protocol.pdf#zip214fundingstreams
pub const FUNDING_STREAM_RECEIVER_DENOMINATOR: u64 = 100;

// TODO: Update the link for post-NU6 funding streams.
/// The specification for all current funding stream receivers, a URL that links to [ZIP-214].
///
/// [ZIP-214]: https://zips.z.cash/zip-0214
Expand Down Expand Up @@ -181,7 +193,7 @@ lazy_static! {
recipients: [
(
FundingStreamReceiver::Ecc,
FundingStreamRecipient::new(7, FUNDING_STREAM_ECC_ADDRESSES_MAINNET.iter()),
FundingStreamRecipient::new(7, FUNDING_STREAM_ECC_ADDRESSES_MAINNET),
),
(
FundingStreamReceiver::ZcashFoundation,
Expand All @@ -199,8 +211,21 @@ lazy_static! {
/// The post-NU6 funding streams for Mainnet
// TODO: Add a reference to lockbox stream ZIP, this is currently based on https://zips.z.cash/draft-nuttycom-funding-allocation
pub static ref POST_NU6_FUNDING_STREAMS_MAINNET: FundingStreams = FundingStreams {
height_range: Height(2_726_400)..Height(3_146_400),
recipients: HashMap::new()
// TODO: Adjust this height range and recipient list once a proposal is selected
height_range: POST_NU6_FUNDING_STREAM_START_RANGE_MAINNET,
recipients: [
(
FundingStreamReceiver::Deferred,
FundingStreamRecipient::new::<[&str; 0], &str>(12, []),
),
(
FundingStreamReceiver::MajorGrants,
// TODO: Update these addresses
FundingStreamRecipient::new(8, FUNDING_STREAM_MG_ADDRESSES_MAINNET),
),
]
.into_iter()
.collect()
};

/// The pre-NU6 funding streams for Testnet as described in [protocol specification §7.10.1][7.10.1]
Expand Down Expand Up @@ -229,11 +254,45 @@ lazy_static! {
// TODO: Add a reference to lockbox stream ZIP, this is currently based on the number of blocks between the
// start and end heights for Mainnet in https://zips.z.cash/draft-nuttycom-funding-allocation
pub static ref POST_NU6_FUNDING_STREAMS_TESTNET: FundingStreams = FundingStreams {
height_range: Height(2_942_000)..Height(3_362_000),
recipients: HashMap::new()
// TODO: Adjust this height range and recipient list once a proposal is selected
height_range: POST_NU6_FUNDING_STREAM_START_RANGE_TESTNET,
recipients: [
(
FundingStreamReceiver::Deferred,
FundingStreamRecipient::new::<[&str; 0], &str>(12, []),
),
(
FundingStreamReceiver::MajorGrants,
// TODO: Update these addresses
FundingStreamRecipient::new(8, POST_NU6_FUNDING_STREAM_FPF_ADDRESSES_TESTNET),
),
]
.into_iter()
.collect()
};
}

/// The start height of post-NU6 funding streams on Mainnet
// TODO: Add a reference to lockbox stream ZIP, this is currently based on https://zips.z.cash/draft-nuttycom-funding-allocation
const POST_NU6_FUNDING_STREAM_START_HEIGHT_MAINNET: u32 = 2_726_400;

/// The start height of post-NU6 funding streams on Testnet
// TODO: Add a reference to lockbox stream ZIP, this is currently based on https://zips.z.cash/draft-nuttycom-funding-allocation
const POST_NU6_FUNDING_STREAM_START_HEIGHT_TESTNET: u32 = 2_942_000;

/// The number of blocks contained in the post-NU6 funding streams height ranges on Mainnet or Testnet.
const POST_NU6_FUNDING_STREAM_NUM_BLOCKS: u32 = 420_000;

/// The post-NU6 funding stream height range on Mainnet
const POST_NU6_FUNDING_STREAM_START_RANGE_MAINNET: std::ops::Range<Height> =
Height(POST_NU6_FUNDING_STREAM_START_HEIGHT_MAINNET)
..Height(POST_NU6_FUNDING_STREAM_START_HEIGHT_MAINNET + POST_NU6_FUNDING_STREAM_NUM_BLOCKS);

/// The post-NU6 funding stream height range on Testnet
const POST_NU6_FUNDING_STREAM_START_RANGE_TESTNET: std::ops::Range<Height> =
Height(POST_NU6_FUNDING_STREAM_START_HEIGHT_TESTNET)
..Height(POST_NU6_FUNDING_STREAM_START_HEIGHT_TESTNET + POST_NU6_FUNDING_STREAM_NUM_BLOCKS);

/// Address change interval function here as a constant
/// as described in [protocol specification §7.10.1][7.10.1].
///
Expand Down Expand Up @@ -402,6 +461,18 @@ pub const FUNDING_STREAM_ZF_ADDRESSES_TESTNET: [&str; FUNDING_STREAMS_NUM_ADDRES
pub const FUNDING_STREAM_MG_ADDRESSES_TESTNET: [&str; FUNDING_STREAMS_NUM_ADDRESSES_TESTNET] =
["t2Gvxv2uNM7hbbACjNox4H6DjByoKZ2Fa3P"; FUNDING_STREAMS_NUM_ADDRESSES_TESTNET];

/// Number of addresses for each post-NU6 funding stream in the Testnet.
/// In the spec ([protocol specification §7.10][7.10]) this is defined as: `fs.addressindex(fs.endheight - 1)`
/// however we know this value beforehand so we prefer to make it a constant instead.
///
/// [7.10]: https://zips.z.cash/protocol/protocol.pdf#fundingstreams
pub const POST_NU6_FUNDING_STREAMS_NUM_ADDRESSES_TESTNET: usize = 13;

/// List of addresses for the Major Grants post-NU6 funding stream in the Testnet administered by the Financial Privacy Fund (FPF).
pub const POST_NU6_FUNDING_STREAM_FPF_ADDRESSES_TESTNET: [&str;
POST_NU6_FUNDING_STREAMS_NUM_ADDRESSES_TESTNET] =
["t2HifwjUj9uyxr9bknR8LFuQbc98c3vkXtu"; POST_NU6_FUNDING_STREAMS_NUM_ADDRESSES_TESTNET];

/// Returns the address change period
/// as described in [protocol specification §7.10][7.10]
///
Expand Down
12 changes: 8 additions & 4 deletions zebra-chain/src/parameters/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ pub struct ConfiguredFundingStreamRecipient {
/// The numerator for each funding stream receiver category, see [`FundingStreamRecipient::numerator`] for more details.
pub numerator: u64,
/// Addresses for the funding stream recipient, see [`FundingStreamRecipient::addresses`] for more details.
pub addresses: Vec<String>,
pub addresses: Option<Vec<String>>,
}

impl ConfiguredFundingStreamRecipient {
/// Converts a [`ConfiguredFundingStreamRecipient`] to a [`FundingStreamReceiver`] and [`FundingStreamRecipient`].
pub fn into_recipient(self) -> (FundingStreamReceiver, FundingStreamRecipient) {
(
self.receiver,
FundingStreamRecipient::new(self.numerator, self.addresses),
FundingStreamRecipient::new(self.numerator, self.addresses.unwrap_or_default()),
)
}
}
Expand Down Expand Up @@ -133,8 +133,12 @@ impl ConfiguredFundingStreams {
))
.expect("no overflow should happen in this sub") as usize;

for recipient in funding_streams.recipients().values() {
// TODO: Make an exception for the `Deferred` receiver.
for (&receiver, recipient) in funding_streams.recipients() {
if receiver == FundingStreamReceiver::Deferred {
// The `Deferred` receiver doesn't need any addresses.
continue;
}

assert!(
recipient.addresses().len() >= expected_min_num_addresses,
"recipients must have a sufficient number of addresses for height range, \
Expand Down
34 changes: 21 additions & 13 deletions zebra-chain/src/parameters/network/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,23 @@ fn check_configured_funding_stream_constraints() {
recipients: Some(vec![ConfiguredFundingStreamRecipient {
receiver: FundingStreamReceiver::Ecc,
numerator: 20,
addresses: FUNDING_STREAM_ECC_ADDRESSES_TESTNET
.map(Into::into)
.to_vec(),
addresses: Some(
FUNDING_STREAM_ECC_ADDRESSES_TESTNET
.map(Into::into)
.to_vec(),
),
}]),
..Default::default()
},
ConfiguredFundingStreams {
recipients: Some(vec![ConfiguredFundingStreamRecipient {
receiver: FundingStreamReceiver::Ecc,
numerator: 100,
addresses: FUNDING_STREAM_ECC_ADDRESSES_TESTNET
.map(Into::into)
.to_vec(),
addresses: Some(
FUNDING_STREAM_ECC_ADDRESSES_TESTNET
.map(Into::into)
.to_vec(),
),
}]),
..Default::default()
},
Expand Down Expand Up @@ -398,7 +402,7 @@ fn check_configured_funding_stream_constraints() {
recipients: Some(vec![ConfiguredFundingStreamRecipient {
receiver: FundingStreamReceiver::Ecc,
numerator: 10,
addresses: vec![],
addresses: Some(vec![]),
}]),
..Default::default()
});
Expand All @@ -410,9 +414,11 @@ fn check_configured_funding_stream_constraints() {
recipients: Some(vec![ConfiguredFundingStreamRecipient {
receiver: FundingStreamReceiver::Ecc,
numerator: 101,
addresses: FUNDING_STREAM_ECC_ADDRESSES_TESTNET
.map(Into::into)
.to_vec(),
addresses: Some(
FUNDING_STREAM_ECC_ADDRESSES_TESTNET
.map(Into::into)
.to_vec(),
),
}]),
..Default::default()
});
Expand All @@ -424,9 +430,11 @@ fn check_configured_funding_stream_constraints() {
recipients: Some(vec![ConfiguredFundingStreamRecipient {
receiver: FundingStreamReceiver::Ecc,
numerator: 10,
addresses: FUNDING_STREAM_ECC_ADDRESSES_MAINNET
.map(Into::into)
.to_vec(),
addresses: Some(
FUNDING_STREAM_ECC_ADDRESSES_MAINNET
.map(Into::into)
.to_vec(),
),
}]),
..Default::default()
});
Expand Down
11 changes: 7 additions & 4 deletions zebra-chain/src/parameters/network_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ pub(super) const MAINNET_ACTIVATION_HEIGHTS: &[(block::Height, NetworkUpgrade)]
(block::Height(903_000), Heartwood),
(block::Height(1_046_400), Canopy),
(block::Height(1_687_104), Nu5),
// TODO: Add NU6.
// TODO: Add NU6
// (block::Height(2_726_400), Nu6),
];

/// Fake mainnet network upgrade activation heights, used in tests.
Expand Down Expand Up @@ -124,7 +125,8 @@ pub(super) const TESTNET_ACTIVATION_HEIGHTS: &[(block::Height, NetworkUpgrade)]
(block::Height(903_800), Heartwood),
(block::Height(1_028_500), Canopy),
(block::Height(1_842_420), Nu5),
// TODO: Add NU6.
// TODO: Add NU6
// (block::Height(2_942_000), Nu6),
];

/// Fake testnet network upgrade activation heights, used in tests.
Expand Down Expand Up @@ -214,8 +216,7 @@ pub(crate) const CONSENSUS_BRANCH_IDS: &[(NetworkUpgrade, ConsensusBranchId)] =
(Heartwood, ConsensusBranchId(0xf5b9230b)),
(Canopy, ConsensusBranchId(0xe9ff75a6)),
(Nu5, ConsensusBranchId(0xc2d6d0b4)),
// TODO: Use the real consensus branch ID once it's specified.
(Nu6, ConsensusBranchId(0xdeadc0de)),
(Nu6, ConsensusBranchId(0xc8e71055)),
];

/// The target block spacing before Blossom.
Expand Down Expand Up @@ -530,6 +531,8 @@ impl From<zcash_protocol::consensus::NetworkUpgrade> for NetworkUpgrade {
zcash_protocol::consensus::NetworkUpgrade::Heartwood => Self::Heartwood,
zcash_protocol::consensus::NetworkUpgrade::Canopy => Self::Canopy,
zcash_protocol::consensus::NetworkUpgrade::Nu5 => Self::Nu5,
#[cfg(zcash_unstable = "nu6")]
zcash_protocol::consensus::NetworkUpgrade::Nu6 => Self::Nu6,
}
}
}
Expand Down
Loading
Loading