use zcash_primitives::transaction::builder::DEFAULT_TX_EXPIRY_DELTA;
use zcash_protocol::consensus::{BlockHeight, NetworkUpgrade, Parameters};
const NU7_POW_TARGET_SPACING_RATIO: u32 = 3;
pub const NU7_TX_EXPIRY_DELTA: u32 = DEFAULT_TX_EXPIRY_DELTA * NU7_POW_TARGET_SPACING_RATIO;
pub fn tx_expiry_delta(chain: &impl Parameters, target_height: BlockHeight) -> u32 {
let nu7_active = chain
.activation_height(NetworkUpgrade::Nu7)
.is_some_and(|activation| target_height >= activation);
if nu7_active {
NU7_TX_EXPIRY_DELTA
} else {
DEFAULT_TX_EXPIRY_DELTA
}
}
pub fn tx_expiry_height(chain: &impl Parameters, target_height: BlockHeight) -> BlockHeight {
target_height + tx_expiry_delta(chain, target_height)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::ChainType;
use crate::testutils::mock_activation_heights_with;
const ACTIVATION: u32 = 10;
fn chain_activating_nu7_at(height: Option<u32>) -> ChainType {
ChainType::Regtest(mock_activation_heights_with(|era| era.set_nu7(height)))
}
#[test]
fn the_delta_triples_from_the_activation_height() {
let chain = chain_activating_nu7_at(Some(ACTIVATION));
assert_eq!(
tx_expiry_delta(&chain, BlockHeight::from_u32(ACTIVATION - 1)),
DEFAULT_TX_EXPIRY_DELTA
);
assert_eq!(
tx_expiry_delta(&chain, BlockHeight::from_u32(ACTIVATION)),
NU7_TX_EXPIRY_DELTA
);
assert_eq!(
tx_expiry_height(&chain, BlockHeight::from_u32(ACTIVATION)),
BlockHeight::from_u32(ACTIVATION + NU7_TX_EXPIRY_DELTA)
);
}
#[test]
fn a_chain_without_nu7_keeps_the_default_delta() {
let chain = chain_activating_nu7_at(None);
assert_eq!(
tx_expiry_delta(&chain, BlockHeight::from_u32(u32::MAX / 2)),
DEFAULT_TX_EXPIRY_DELTA
);
}
}