Skip to content

Commit

Permalink
Updates coinbase_spend_restriction() method to always return `OnlyS…
Browse files Browse the repository at this point in the history
…hieldedOutputs` on Regtest.
  • Loading branch information
arya2 committed Dec 13, 2024
1 parent 568b25e commit 71c1f63
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion zebra-chain/src/block/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ where
+ Copy
+ 'static,
{
let mut spend_restriction = transaction.coinbase_spend_restriction(height);
let mut spend_restriction = transaction.coinbase_spend_restriction(&Network::Mainnet, height);
let mut new_inputs = Vec::new();
let mut spent_outputs = HashMap::new();

Expand Down
6 changes: 4 additions & 2 deletions zebra-chain/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use unmined::{
use crate::{
amount::{Amount, Error as AmountError, NegativeAllowed, NonNegative},
block, orchard,
parameters::{ConsensusBranchId, NetworkUpgrade},
parameters::{ConsensusBranchId, Network, NetworkUpgrade},
primitives::{ed25519, Bctv14Proof, Groth16Proof},
sapling,
serialization::ZcashSerialize,
Expand Down Expand Up @@ -303,9 +303,11 @@ impl Transaction {
/// assuming it is mined at `spend_height`.
pub fn coinbase_spend_restriction(
&self,
network: &Network,
spend_height: block::Height,
) -> CoinbaseSpendRestriction {
if self.outputs().is_empty() {
// TODO: Replace `is_regtest()` with a check for a field.
if self.outputs().is_empty() || network.is_regtest() {
// we know this transaction must have shielded outputs,
// because of other consensus rules
OnlyShieldedOutputs { spend_height }
Expand Down
4 changes: 3 additions & 1 deletion zebra-consensus/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ where
// WONTFIX: Return an error for Request::Block as well to replace this check in
// the state once #2336 has been implemented?
if req.is_mempool() {
Self::check_maturity_height(&req, &spent_utxos)?;
Self::check_maturity_height(&network, &req, &spent_utxos)?;
}

let cached_ffi_transaction =
Expand Down Expand Up @@ -728,10 +728,12 @@ where
/// mature and valid for the request height, or a [`TransactionError`] if the transaction
/// spends transparent coinbase outputs that are immature and invalid for the request height.
pub fn check_maturity_height(
network: &Network,
request: &Request,
spent_utxos: &HashMap<transparent::OutPoint, transparent::Utxo>,
) -> Result<(), TransactionError> {
check::tx_transparent_coinbase_spends_maturity(
network,
request.transaction(),
request.height(),
request.known_utxos(),
Expand Down
3 changes: 2 additions & 1 deletion zebra-consensus/src/transaction/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ fn validate_expiry_height_mined(
/// Returns `Ok(())` if spent transparent coinbase outputs are
/// valid for the block height, or a [`Err(TransactionError)`](TransactionError)
pub fn tx_transparent_coinbase_spends_maturity(
network: &Network,
tx: Arc<Transaction>,
height: Height,
block_new_outputs: Arc<HashMap<transparent::OutPoint, transparent::OrderedUtxo>>,
Expand All @@ -488,7 +489,7 @@ pub fn tx_transparent_coinbase_spends_maturity(
.or_else(|| spent_utxos.get(&spend).cloned())
.expect("load_spent_utxos_fut.await should return an error if a utxo is missing");

let spend_restriction = tx.coinbase_spend_restriction(height);
let spend_restriction = tx.coinbase_spend_restriction(network, height);

zebra_state::check::transparent_coinbase_spend(spend, spend_restriction, &utxo)?;
}
Expand Down
2 changes: 1 addition & 1 deletion zebra-consensus/src/transaction/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ async fn mempool_request_with_immature_spend_is_rejected() {
transparent::Input::Coinbase { .. } => panic!("requires a non-coinbase transaction"),
};

let spend_restriction = tx.coinbase_spend_restriction(height);
let spend_restriction = tx.coinbase_spend_restriction(&Network::Mainnet, height);

let coinbase_spend_height = Height(5);

Expand Down
6 changes: 4 additions & 2 deletions zebra-state/src/service/check/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ pub fn transparent_spend(
// We don't want to use UTXOs from invalid pending blocks,
// so we check transparent coinbase maturity and shielding
// using known valid UTXOs during non-finalized chain validation.
let spend_restriction =
transaction.coinbase_spend_restriction(semantically_verified.height);
let spend_restriction = transaction.coinbase_spend_restriction(
&finalized_state.network(),
semantically_verified.height,
);
transparent_coinbase_spend(spend, spend_restriction, utxo.as_ref())?;

// We don't delete the UTXOs until the block is committed,
Expand Down

0 comments on commit 71c1f63

Please sign in to comment.