Skip to content

Commit

Permalink
Rpc coinbase outputs calculation fix & tests updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mariopil committed Nov 4, 2024
1 parent 4029650 commit 1ba587c
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 17 deletions.
2 changes: 1 addition & 1 deletion zebra-chain/src/parameters/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ impl Parameters {
nu5: nu5_activation_height,
nu6: nu6_activation_height,
#[cfg(zcash_unstable = "nsm")]
zfuture: nu5_activation_height.map(|height| height + 101),
zfuture: nu5_activation_height.map(|height| height + 1),
..Default::default()
})
.with_halving_interval(PRE_BLOSSOM_REGTEST_HALVING_INTERVAL);
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/parameters/network/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn activates_network_upgrades_correctly() {
// TODO: Remove this once the testnet parameters are being serialized (#8920).
(Height(100), NetworkUpgrade::Nu5),
#[cfg(zcash_unstable = "nsm")]
(Height(201), NetworkUpgrade::ZFuture),
(Height(101), NetworkUpgrade::ZFuture),
];

for (network, expected_activation_heights) in [
Expand Down
36 changes: 35 additions & 1 deletion zebra-rpc/src/methods/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ where
let mempool = self.mempool.clone();
let mut latest_chain_tip = self.latest_chain_tip.clone();
let sync_status = self.sync_status.clone();
let state = self.state.clone();
let mut state = self.state.clone();

if let Some(HexData(block_proposal_bytes)) = parameters
.as_ref()
Expand Down Expand Up @@ -901,6 +901,38 @@ where
None
};

#[cfg(zcash_unstable = "nsm")]
let expected_block_subsidy = {
let money_reserve = match state
.ready()
.await
.map_err(|_| Error {
code: ErrorCode::InternalError,
message: "".into(),
data: None,
})?
.call(ReadRequest::TipPoolValues)
.await
.map_err(|_| Error {
code: ErrorCode::InternalError,
message: "".into(),
data: None,
})? {
ReadResponse::TipPoolValues {
tip_hash: _,
tip_height: _,
value_balance,
} => value_balance.money_reserve(),
_ => unreachable!("wrong response to ReadRequest::TipPoolValues"),
};
general::block_subsidy(next_block_height, &network, money_reserve)
.map_server_error()?
};

#[cfg(not(zcash_unstable = "nsm"))]
let expected_block_subsidy =
general::block_subsidy_pre_nsm(next_block_height, &network).map_server_error()?;

// Randomly select some mempool transactions.
let mempool_txs = zip317::select_mempool_transactions(
&network,
Expand All @@ -909,6 +941,7 @@ where
mempool_txs,
debug_like_zcashd,
extra_coinbase_data.clone(),
expected_block_subsidy,
#[cfg(zcash_unstable = "nsm")]
burn_amount,
)
Expand All @@ -932,6 +965,7 @@ where
submit_old,
debug_like_zcashd,
extra_coinbase_data,
expected_block_subsidy,
#[cfg(zcash_unstable = "nsm")]
burn_amount,
);
Expand Down
26 changes: 14 additions & 12 deletions zebra-rpc/src/methods/get_block_template_rpcs/get_block_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use jsonrpc_core::{Error, ErrorCode, Result};
use tower::{Service, ServiceExt};

use zebra_chain::{
amount::{self, Amount, NegativeOrZero, NonNegative, MAX_MONEY},
amount::{self, Amount, NegativeOrZero, NonNegative},
block::{
self,
merkle::{self, AuthDataRoot},
Expand Down Expand Up @@ -297,6 +297,7 @@ pub fn generate_coinbase_and_roots(
history_tree: Arc<zebra_chain::history_tree::HistoryTree>,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
expected_block_subsidy: Amount<NonNegative>,
#[cfg(zcash_unstable = "nsm")] burn_amount: Option<Amount<NonNegative>>,
) -> (TransactionTemplate<NegativeOrZero>, DefaultRoots) {
// Generate the coinbase transaction
Expand All @@ -308,6 +309,7 @@ pub fn generate_coinbase_and_roots(
miner_fee,
like_zcashd,
extra_coinbase_data,
expected_block_subsidy,
#[cfg(zcash_unstable = "nsm")]
burn_amount,
);
Expand Down Expand Up @@ -336,16 +338,25 @@ pub fn generate_coinbase_and_roots(
///
/// If `like_zcashd` is true, try to match the coinbase transactions generated by `zcashd`
/// in the `getblocktemplate` RPC.
#[allow(clippy::too_many_arguments)]
pub fn generate_coinbase_transaction(
network: &Network,
height: Height,
miner_address: &transparent::Address,
miner_fee: Amount<NonNegative>,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
expected_block_subsidy: Amount<NonNegative>,
#[cfg(zcash_unstable = "nsm")] burn_amount: Option<Amount<NonNegative>>,
) -> UnminedTx {
let outputs = standard_coinbase_outputs(network, height, miner_address, miner_fee, like_zcashd);
let outputs = standard_coinbase_outputs(
network,
height,
miner_address,
miner_fee,
like_zcashd,
expected_block_subsidy,
);

if like_zcashd {
#[cfg(zcash_unstable = "nsm")]
Expand Down Expand Up @@ -424,17 +435,8 @@ pub fn standard_coinbase_outputs(
miner_address: &transparent::Address,
miner_fee: Amount<NonNegative>,
like_zcashd: bool,
expected_block_subsidy: Amount<NonNegative>,
) -> Vec<(Amount<NonNegative>, transparent::Script)> {
#[cfg(zcash_unstable = "nsm")]
let expected_block_subsidy = general::block_subsidy(
height,
network,
MAX_MONEY.try_into().expect("MAX_MONEY is a valid amount"),
)
.expect("valid block subsidy");
#[cfg(not(zcash_unstable = "nsm"))]
let expected_block_subsidy =
general::block_subsidy_pre_nsm(height, network).expect("valid block subsidy");
let funding_streams =
funding_streams::funding_stream_values(height, network, expected_block_subsidy)
.expect("funding stream value calculations are valid for reasonable chain heights");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ impl GetBlockTemplate {
submit_old: Option<bool>,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
expected_block_subsidy: Amount<NonNegative>,
#[cfg(zcash_unstable = "nsm")] burn_amount: Option<Amount<NonNegative>>,
) -> Self {
// Calculate the next block height.
Expand Down Expand Up @@ -272,6 +273,7 @@ impl GetBlockTemplate {
chain_tip_and_local_time.history_tree.clone(),
like_zcashd,
extra_coinbase_data,
expected_block_subsidy,
#[cfg(zcash_unstable = "nsm")]
burn_amount,
);
Expand Down
5 changes: 5 additions & 0 deletions zebra-rpc/src/methods/get_block_template_rpcs/zip317.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ use crate::methods::get_block_template_rpcs::{
/// Returns selected transactions from `mempool_txs`.
///
/// [ZIP-317]: https://zips.z.cash/zip-0317#block-production
#[allow(clippy::too_many_arguments)]
pub async fn select_mempool_transactions(
network: &Network,
next_block_height: Height,
miner_address: &transparent::Address,
mempool_txs: Vec<VerifiedUnminedTx>,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
expected_block_subsidy: Amount<NonNegative>,
#[cfg(zcash_unstable = "nsm")] burn_amount: Option<Amount<NonNegative>>,
) -> Vec<VerifiedUnminedTx> {
// Use a fake coinbase transaction to break the dependency between transaction
Expand All @@ -53,6 +55,7 @@ pub async fn select_mempool_transactions(
miner_address,
like_zcashd,
extra_coinbase_data,
expected_block_subsidy,
#[cfg(zcash_unstable = "nsm")]
burn_amount,
);
Expand Down Expand Up @@ -123,6 +126,7 @@ pub fn fake_coinbase_transaction(
miner_address: &transparent::Address,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
expected_block_subsidy: Amount<NonNegative>,
#[cfg(zcash_unstable = "nsm")] burn_amount: Option<Amount<NonNegative>>,
) -> TransactionTemplate<NegativeOrZero> {
// Block heights are encoded as variable-length (script) and `u32` (lock time, expiry height).
Expand All @@ -143,6 +147,7 @@ pub fn fake_coinbase_transaction(
miner_fee,
like_zcashd,
extra_coinbase_data,
expected_block_subsidy,
#[cfg(zcash_unstable = "nsm")]
burn_amount,
);
Expand Down
11 changes: 11 additions & 0 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,7 @@ async fn rpc_getblocktemplate_mining_address(use_p2pkh: bool) {
parameters::NetworkKind,
serialization::DateTime32,
transaction::{zip317, VerifiedUnminedTx},
value_balance::ValueBalance,
work::difficulty::{CompactDifficulty, ExpandedDifficulty, U256},
};
use zebra_consensus::MAX_BLOCK_SIGOPS;
Expand Down Expand Up @@ -1337,6 +1338,16 @@ async fn rpc_getblocktemplate_mining_address(use_p2pkh: bool) {
max_time: fake_max_time,
history_tree: fake_history_tree(&Mainnet),
}));

#[cfg(zcash_unstable = "nsm")]
read_state
.expect_request_that(|req| matches!(req, ReadRequest::TipPoolValues))
.await
.respond(ReadResponse::TipPoolValues {
tip_height: fake_tip_height,
tip_hash: fake_tip_hash,
value_balance: ValueBalance::zero(),
});
}
};

Expand Down
18 changes: 16 additions & 2 deletions zebrad/tests/acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3247,6 +3247,8 @@ async fn trusted_chain_sync_handles_forks_correctly() -> Result<()> {
#[cfg(feature = "getblocktemplate-rpcs")]
async fn nu6_funding_streams_and_coinbase_balance() -> Result<()> {
use zebra_chain::{
amount::MAX_MONEY,
block::subsidy::general,
chain_sync_status::MockSyncStatus,
parameters::{
subsidy::{FundingStreamReceiver, FUNDING_STREAM_MG_ADDRESSES_TESTNET},
Expand Down Expand Up @@ -3287,7 +3289,7 @@ async fn nu6_funding_streams_and_coinbase_balance() -> Result<()> {
.with_activation_heights(ConfiguredActivationHeights {
nu6: Some(1),
#[cfg(zcash_unstable = "nsm")]
zfuture: Some(10),
zfuture: Some(2),
..Default::default()
});

Expand Down Expand Up @@ -3444,14 +3446,25 @@ async fn nu6_funding_streams_and_coinbase_balance() -> Result<()> {
})
.to_network();

let block_height = Height(block_template.height);
#[cfg(zcash_unstable = "nsm")]
let expected_block_subsidy = general::block_subsidy(
block_height,
&network,
MAX_MONEY.try_into().expect("MAX_MONEY is a valid amount"),
)?;
#[cfg(not(zcash_unstable = "nsm"))]
let expected_block_subsidy = general::block_subsidy_pre_nsm(block_height, &network)?;

let (coinbase_txn, default_roots) = generate_coinbase_and_roots(
&network,
Height(block_template.height),
block_height,
&miner_address,
&[],
history_tree.clone(),
true,
vec![],
expected_block_subsidy,
#[cfg(zcash_unstable = "nsm")]
None,
);
Expand Down Expand Up @@ -3497,6 +3510,7 @@ async fn nu6_funding_streams_and_coinbase_balance() -> Result<()> {
history_tree.clone(),
true,
vec![],
expected_block_subsidy,
#[cfg(zcash_unstable = "nsm")]
None,
);
Expand Down

0 comments on commit 1ba587c

Please sign in to comment.