use std::{
collections::{HashMap, HashSet},
io::{Cursor, Write},
sync::Arc,
};
use chrono::{DateTime, Duration, LocalResult, TimeZone, Utc};
use crate::{
amount::{Amount, DeferredPoolBalanceChange, NonNegative, MAX_MONEY},
block::{
serialize::MAX_BLOCK_BYTES, Block, BlockTimeError, Commitment::*, Hash, Header, Height,
},
parameters::{Network, NetworkUpgrade::*},
sapling,
serialization::{
sha256d, SerializationError, ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize,
},
transaction::{LockTime, Transaction},
transparent,
value_balance::ValueBalance,
};
use super::generate;
fn serial_transaction_merkle_root(leaves: &[[u8; 32]]) -> Option<[u8; 32]> {
let mut level = leaves.to_vec();
while level.len() > 1 {
if !level.len().is_multiple_of(2) {
level.push(*level.last().unwrap());
}
let mut parents = Vec::new();
for pair in level.as_chunks::<2>().0 {
let mut writer = sha256d::Writer::default();
writer.write_all(&pair[0]).unwrap();
writer.write_all(&pair[1]).unwrap();
parents.push(writer.finish());
}
level = parents;
}
level.first().copied()
}
fn serial_auth_data_root(leaves: &[[u8; 32]]) -> [u8; 32] {
let mut level = leaves.to_vec();
level.resize(level.len().next_power_of_two(), [0; 32]);
while level.len() > 1 {
let mut parents = Vec::new();
for pair in level.as_chunks::<2>().0 {
let digest = blake2b_simd::Params::new()
.hash_length(32)
.personal(b"ZcashAuthDatHash")
.to_state()
.update(&pair[0])
.update(&pair[1])
.finalize();
parents.push(digest.as_bytes().try_into().unwrap());
}
level = parents;
}
level[0]
}
pub(super) fn assert_parallel_decode_matches_serial(bytes: &[u8]) {
let mut serial_bytes = bytes;
let mut parallel_bytes = bytes;
let serial = Block::zcash_deserialize(&mut serial_bytes);
let parallel = Block::zcash_deserialize_parallel(&mut parallel_bytes);
assert_eq!(serial_bytes, parallel_bytes, "consumed bytes differ");
match (serial, parallel) {
(Ok(serial), Ok(parallel)) => {
assert_eq!(serial, parallel);
assert_eq!(
serial.zcash_serialize_to_vec().unwrap(),
parallel.zcash_serialize_to_vec().unwrap()
);
for (serial, parallel) in serial.transactions.iter().zip(¶llel.transactions) {
assert_eq!(serial.hash(), parallel.hash());
assert_eq!(serial.auth_digest(), parallel.auth_digest());
}
}
(Err(serial), Err(parallel)) => {
assert_eq!(format!("{serial:?}"), format!("{parallel:?}"));
}
(serial, parallel) => panic!("decode results differ: {serial:?} / {parallel:?}"),
}
}
#[test]
fn parallel_decode_applies_the_transaction_size_check() {
let _init_guard = zebra_test::init();
let large = v1_transaction_with_output_script(200);
let below_minimum = v1_transaction_with_output_script(34);
let header_len = block_with_repeated_transaction(&large, 0).len() - 1;
for large_count in [0, 1, 10] {
let rejected = 165 * large_count + 1;
let small_count = rejected + 100;
let mut block = block_with_repeated_transaction(&large, 0);
block.truncate(header_len);
crate::serialization::CompactSizeMessage::try_from(large_count + small_count)
.expect("count fits")
.zcash_serialize(&mut block)
.expect("count serializes");
let transactions_start = block.len();
for _ in 0..large_count {
block.extend_from_slice(&large);
}
for _ in 0..small_count {
block.extend_from_slice(&below_minimum);
}
assert_too_small(&block);
assert_parallel_decode_matches_serial(&block);
let mut unread = &block[..];
assert!(Block::zcash_deserialize_parallel(&mut unread).is_err());
assert_eq!(
block.len() - unread.len(),
transactions_start + large_count * large.len() + rejected * below_minimum.len(),
"{large_count} large transactions",
);
}
}
#[test]
fn parallel_decode_mark_is_exclusive() {
use std::sync::atomic::{AtomicBool, Ordering};
use crate::block::serialize::ParallelDecode;
let decoding = AtomicBool::new(false);
let mark = ParallelDecode::try_start(&decoding).expect("no block is being decoded");
assert!(decoding.load(Ordering::Acquire));
assert!(ParallelDecode::try_start(&decoding).is_none());
drop(mark);
assert!(!decoding.load(Ordering::Acquire));
assert!(ParallelDecode::try_start(&decoding).is_some());
}
#[test]
fn large_blocks_decode_during_a_parallel_decode() {
use crate::block::serialize::{parallel_block, ParallelDecode, PARALLEL_DECODE};
let _init_guard = zebra_test::init();
let large = generate::many_transaction_v4_block(100);
assert!(parallel_block(&large).is_some(), "a large block uses Rayon");
let _decoding = loop {
if let Some(mark) = ParallelDecode::try_start(&PARALLEL_DECODE) {
break mark;
}
std::thread::yield_now();
};
assert_parallel_decode_matches_serial(&large);
}
#[test]
fn parallel_decode_vectors() {
use crate::block::serialize::{parallel_block, scan_transaction};
let _init_guard = zebra_test::init();
for bytes in zebra_test::vectors::BLOCKS.iter() {
assert_parallel_decode_matches_serial(bytes);
let block = Block::zcash_deserialize(*bytes).unwrap();
if block.transactions.len() >= 64 {
assert!(parallel_block(bytes).is_some(), "large vector uses Rayon");
}
for transaction in &block.transactions {
let mut encoded = transaction.zcash_serialize_to_vec().unwrap();
let end = encoded.len();
encoded.extend_from_slice(&[0xaa, 0xbb]);
let mut remaining = encoded.as_slice();
assert_eq!(scan_transaction(&mut remaining), Some(()));
assert_eq!(remaining, &encoded[end..], "scanner transaction boundary");
}
let mut trailing = bytes.to_vec();
trailing.extend_from_slice(&[0xaa, 0xbb]);
assert_parallel_decode_matches_serial(&trailing);
for end in [0, 1, 3, 4, 100, bytes.len() / 2, bytes.len() - 1] {
assert_parallel_decode_matches_serial(&bytes[..end.min(bytes.len())]);
}
}
}
#[test]
fn parallel_decode_zip244_and_v6_pools() {
use crate::{
block::serialize::parallel_block, transaction::arbitrary::shielded::fake_bundle_for_branch,
};
use ::orchard::ValuePool;
use zcash_protocol::consensus::BranchId;
let _init_guard = zebra_test::init();
let mut transactions: Vec<_> = zebra_test::zip0244::TEST_VECTORS
.iter()
.map(|vector| Transaction::zcash_deserialize(vector.tx.as_slice()).unwrap())
.collect();
let sapling = transactions
.iter()
.find_map(|tx| tx.sapling_bundle().cloned())
.unwrap();
for pools in 0..8 {
let transaction = zcash_primitives::transaction::TransactionData::from_parts_v6(
BranchId::Nu6_3,
0,
0.into(),
None,
(pools & 1 != 0).then(|| sapling.clone()),
(pools & 2 != 0).then(|| {
fake_bundle_for_branch(BranchId::Nu6_3, ValuePool::Orchard, 2, 1).unwrap()
}),
(pools & 4 != 0).then(|| {
fake_bundle_for_branch(BranchId::Nu6_3, ValuePool::Ironwood, 2, 2).unwrap()
}),
)
.freeze()
.unwrap();
let mut encoded = Vec::new();
transaction.write(&mut encoded).unwrap();
transactions.push(Transaction::zcash_deserialize(encoded.as_slice()).unwrap());
}
for transaction in transactions {
let transaction = Arc::new(transaction);
let block = Block {
header: Arc::new(generate::block_header().0),
transactions: vec![transaction; 64],
};
let bytes = block.zcash_serialize_to_vec().unwrap();
assert!(
parallel_block(&bytes).is_some(),
"shielded block uses Rayon"
);
assert_parallel_decode_matches_serial(&bytes);
for end in [bytes.len() / 2, bytes.len() - 1] {
assert_parallel_decode_matches_serial(&bytes[..end]);
}
}
}
#[test]
fn parallel_decode_large_transactions_keep_byte_prefix_bounds() {
use crate::{
block::serialize::{
scan_transaction, serial_decode_prefix_is_complete, PARALLEL_DECODE_BATCH_BYTES,
},
transaction::arbitrary::shielded::fake_bundle_for_branch,
};
use ::orchard::ValuePool;
use zcash_protocol::consensus::BranchId;
let _init_guard = zebra_test::init();
let limit = PARALLEL_DECODE_BATCH_BYTES;
assert!(!serial_decode_prefix_is_complete(63, limit - 1));
assert!(serial_decode_prefix_is_complete(64, 0));
assert!(serial_decode_prefix_is_complete(1, limit));
let bundle = fake_bundle_for_branch(BranchId::Nu6_3, ValuePool::Orchard, 2, 1).unwrap();
let cv_bytes = bundle.actions().iter().next().unwrap().cv_net().to_bytes();
let transaction = zcash_primitives::transaction::TransactionData::from_parts_v6(
BranchId::Nu6_3,
0,
0.into(),
None,
None,
Some(bundle),
None,
)
.freeze()
.unwrap();
let mut transaction_bytes = Vec::new();
transaction.write(&mut transaction_bytes).unwrap();
let cv_offset = transaction_bytes
.windows(cv_bytes.len())
.position(|bytes| bytes == cv_bytes.as_slice())
.expect("the serialized action contains its value commitment");
let transaction =
Arc::new(Transaction::zcash_deserialize(transaction_bytes.as_slice()).unwrap());
let block = Block {
header: Arc::new(generate::block_header().0),
transactions: vec![transaction; 208],
};
let bytes = block.zcash_serialize_to_vec().unwrap();
assert!(u64::try_from(bytes.len()).unwrap() <= MAX_BLOCK_BYTES);
let transaction_start = block.header.zcash_serialize_to_vec().unwrap().len() + 1;
let prefix = limit.div_ceil(transaction_bytes.len());
assert!(prefix < 64, "large transactions reach the byte bound first");
assert!(!serial_decode_prefix_is_complete(
prefix - 1,
(prefix - 1) * transaction_bytes.len()
));
assert!(serial_decode_prefix_is_complete(
prefix,
prefix * transaction_bytes.len()
));
assert_parallel_decode_matches_serial(&bytes);
for index in [prefix - 1, prefix, prefix + 1, 63, 207] {
let start = transaction_start + index * transaction_bytes.len();
let mut malformed = bytes.clone();
malformed[start] ^= 0xff;
assert_parallel_decode_matches_serial(&malformed);
let mut malformed = bytes.clone();
malformed[start + cv_offset..start + cv_offset + cv_bytes.len()].fill(0xff);
let mut scanned = &malformed[start..];
assert_eq!(scan_transaction(&mut scanned), Some(()));
assert!(Block::zcash_deserialize(malformed.as_slice()).is_err());
assert_parallel_decode_matches_serial(&malformed);
for offset in [4, transaction_bytes.len() / 2, transaction_bytes.len() - 1] {
assert_parallel_decode_matches_serial(&bytes[..start + offset]);
}
}
}
#[test]
fn parallel_decode_threshold_and_malformed() {
use crate::block::serialize::parallel_block;
let _init_guard = zebra_test::init();
for count in [0, 1, 63, 64, 65, 9_000, 9_500] {
let bytes = generate::many_transaction_v4_block(count);
assert_parallel_decode_matches_serial(&bytes);
assert_eq!(
parallel_block(&bytes).is_some(),
count >= 64,
"parallel entry threshold for {count} transactions"
);
}
let bytes = generate::many_transaction_v4_block(65);
let mut regtest = Block::zcash_deserialize(bytes.as_slice()).unwrap();
Arc::make_mut(&mut regtest.header).solution = crate::work::equihash::Solution::from_bytes(
&[0; crate::work::equihash::REGTEST_SOLUTION_SIZE],
)
.unwrap();
let regtest = regtest.zcash_serialize_to_vec().unwrap();
assert!(
parallel_block(®test).is_some(),
"Regtest header boundary"
);
assert_parallel_decode_matches_serial(®test);
let (_, header) = generate::block_header();
let header_end = header.len();
let transaction_start = header_end + 1;
for count in [
vec![0xfd, 1, 0],
vec![0xfe, 0, 0, 1, 0],
vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
] {
let mut malformed = header.clone();
malformed.extend_from_slice(&count);
malformed.extend_from_slice(&bytes[transaction_start..]);
assert_parallel_decode_matches_serial(&malformed);
}
for end in (0..bytes.len()).step_by(53).chain([bytes.len() - 1]) {
assert_parallel_decode_matches_serial(&bytes[..end]);
}
for transaction in [0, 64] {
for offset in 0..211 {
let mut malformed = bytes.clone();
malformed[transaction_start + transaction * 211 + offset] ^= 0xff;
assert_parallel_decode_matches_serial(&malformed);
}
}
for offset in [8, 45, 157, 166] {
let mut malformed = bytes[..transaction_start + offset].to_vec();
malformed.extend_from_slice(&[0xff; 9]);
malformed.extend_from_slice(&bytes[transaction_start + offset + 1..]);
assert_parallel_decode_matches_serial(&malformed);
}
}
#[test]
fn parallel_decode_bounds_speculation_and_keeps_first_error() {
use crate::block::serialize::{parallel_block, scan_batch, PARALLEL_DECODE_TASK_BYTES};
let _init_guard = zebra_test::init();
let bytes = generate::many_transaction_v4_block(9_000);
let start = generate::block_header().1.len() + 3;
let body = &bytes[start..];
let mut ranges = Vec::new();
for (budget, count, expected) in [
(body.len(), 64, 64),
(body.len(), 9_000, 9_000),
(64 * 211, 9_000, 64),
(64 * 211 - 1, 9_000, 63),
(210, 9_000, 0),
] {
scan_batch(&body[..budget], count, &mut ranges);
assert_eq!(ranges.len(), expected, "budget {budget}, count {count}");
assert!(ranges.last().is_none_or(|range| range.end <= budget));
}
scan_batch(&body[..422 - 1], 2, &mut ranges);
assert_eq!(ranges, vec![0..211]);
let task_len = PARALLEL_DECODE_TASK_BYTES.div_ceil(211);
for index in [
0,
63,
64,
65,
127,
128,
255,
256,
64 + task_len - 1,
64 + task_len,
2 * task_len,
5_000,
8_191,
8_192,
8_999,
] {
let mut malformed = bytes.clone();
malformed[start + index * 211 + 200] = 1;
let (result, _) = parallel_block(&malformed).unwrap();
assert!(matches!(
result,
Err(SerializationError::BadTransactionBalance)
));
assert_parallel_decode_matches_serial(&malformed);
if index + 40 < 9_000 {
malformed[start + (index + 40) * 211 + 200] = 1;
assert_parallel_decode_matches_serial(&malformed);
}
let mut truncated = bytes.clone();
truncated.truncate(start + index * 211 + 100);
assert_parallel_decode_matches_serial(&truncated);
}
let mut malformed = bytes;
malformed[start + 64 * 211 + 200] = 1;
malformed.truncate(start + 66 * 211 - 1);
assert_parallel_decode_matches_serial(&malformed);
}
#[test]
fn parallel_decode_huge_transaction_after_prefix() {
use crate::block::serialize::{parallel_block, scan_batch};
let _init_guard = zebra_test::init();
let small = generate::many_transaction_v4_block(9_000);
let header_len = generate::block_header().1.len();
let txs = &small[header_len + 3..];
let mut huge = Vec::new();
huge.extend_from_slice(&0x8000_0004_u32.to_le_bytes());
huge.extend_from_slice(&0x892f_2085_u32.to_le_bytes());
huge.push(1);
huge.extend_from_slice(&[2; 36]);
huge.push(0xfe);
huge.extend_from_slice(&600_000_u32.to_le_bytes());
huge.extend(std::iter::repeat_n(0, 600_000));
huge.extend_from_slice(&u32::MAX.to_le_bytes());
huge.push(0); huge.extend_from_slice(&[0; 4 + 4 + 8 + 3]);
let mut ranges = Vec::new();
scan_batch(&huge[..64 * 211], 64, &mut ranges);
assert!(ranges.is_empty());
for (before, after) in [(64, 2_000), (500, 2_000), (64, 0)] {
let mut bytes = generate::block_header().1;
crate::serialization::CompactSizeMessage::try_from(before + 1 + after)
.unwrap()
.zcash_serialize(&mut bytes)
.unwrap();
bytes.extend_from_slice(&txs[..before * 211]);
bytes.extend_from_slice(&huge);
bytes.extend_from_slice(&txs[before * 211..(before + after) * 211]);
assert!(parallel_block(&bytes).is_some());
assert_parallel_decode_matches_serial(&bytes);
let mut malformed = bytes.clone();
malformed[bytes.len() - after * 211 - 3 - 8] = 1;
assert_parallel_decode_matches_serial(&malformed);
malformed.truncate(bytes.len() - after * 211 - 1_000);
assert_parallel_decode_matches_serial(&malformed);
}
}
#[test]
fn parallel_merkle_roots_match_serial_reference() {
use crate::{
block::merkle::{AuthDataRoot, Root, PARALLEL_MERKLE_THRESHOLD},
transaction::{AuthDigest, Hash as TransactionHash},
};
let _init_guard = zebra_test::init();
let cutoff = PARALLEL_MERKLE_THRESHOLD;
let mut state = 0x4d59_5df4_d0f3_3173_u64;
let leaves: Vec<[u8; 32]> = (0..20_000)
.map(|_| {
let mut leaf = [0; 32];
for bytes in leaf.as_chunks_mut::<8>().0 {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
*bytes = state.to_le_bytes();
}
leaf
})
.collect();
for leaf_count in [
0,
1,
2,
3,
127,
128,
129,
255,
256,
257,
511,
512,
513,
cutoff - 1,
cutoff,
cutoff + 1,
2_047,
2_048,
2_049,
4_095,
4_096,
4_097,
8_191,
8_192,
8_193,
9_194,
16_383,
16_384,
16_385,
19_999,
20_000,
] {
let leaves = &leaves[..leaf_count];
let actual_auth = leaves
.iter()
.copied()
.map(AuthDigest)
.collect::<AuthDataRoot>();
assert_eq!(
actual_auth,
AuthDataRoot::from(serial_auth_data_root(leaves)),
"auth-data root for {leaf_count} leaves"
);
let transaction_root = || {
leaves
.iter()
.copied()
.map(TransactionHash)
.collect::<Root>()
};
match serial_transaction_merkle_root(leaves) {
Some(expected) => assert_eq!(
transaction_root(),
Root::from(expected),
"transaction root for {leaf_count} leaves"
),
None => assert!(
std::panic::catch_unwind(transaction_root).is_err(),
"an empty transaction tree must still panic"
),
}
}
}
#[test]
fn parallel_merkle_roots_match_serial_vectors() {
use crate::{
block::merkle::{AuthDataRoot, Root},
transaction::AuthDigest,
};
let _init_guard = zebra_test::init();
for block_bytes in zebra_test::vectors::BLOCKS.iter() {
let block = Block::zcash_deserialize(*block_bytes).unwrap();
let auth_digests: Vec<_> = block
.transactions
.iter()
.map(|tx| tx.auth_digest().unwrap_or(AuthDigest([0xFF; 32])).0)
.collect();
let transaction_hashes: Vec<_> = block.transactions.iter().map(|tx| tx.hash().0).collect();
assert_eq!(
block.auth_data_root(),
AuthDataRoot::from(serial_auth_data_root(&auth_digests)),
"auth data root at {:?}",
block.coinbase_height()
);
assert_eq!(
block.header.merkle_root,
Root::from(serial_transaction_merkle_root(&transaction_hashes).unwrap()),
"header transaction root at {:?}",
block.coinbase_height()
);
assert_eq!(
block.transactions.iter().collect::<Root>(),
block.header.merkle_root,
"parallel transaction root at {:?}",
block.coinbase_height()
);
}
}
#[test]
fn parallel_merkle_roots_match_serial_edge_cases() {
use crate::{block::merkle::AuthDataRoot, transaction::AuthDigest};
let _init_guard = zebra_test::init();
let legacy =
Block::zcash_deserialize(&**zebra_test::vectors::BLOCK_MAINNET_1046400_BYTES).unwrap();
let mut mixed =
Block::zcash_deserialize(&**zebra_test::vectors::BLOCK_MAINNET_1687108_BYTES).unwrap();
assert!(legacy
.transactions
.iter()
.all(|tx| tx.auth_digest().is_none()));
assert!(mixed
.transactions
.iter()
.any(|tx| tx.auth_digest().is_some()));
mixed.transactions.insert(1, legacy.transactions[0].clone());
for source in [legacy, mixed] {
for tx_count in [0, 1, 2, 3, 7, 15, 16, 17, 32, 257] {
let block = Block {
header: source.header.clone(),
transactions: source
.transactions
.iter()
.cycle()
.take(tx_count)
.cloned()
.collect(),
};
let digests: Vec<_> = block
.transactions
.iter()
.map(|tx| tx.auth_digest().unwrap_or(AuthDigest([0xFF; 32])).0)
.collect();
let serial = AuthDataRoot::from(serial_auth_data_root(&digests));
let actual = block.auth_data_root();
assert_eq!(actual, serial, "auth data root for {tx_count} transactions");
if tx_count == 0 {
assert_eq!(actual, AuthDataRoot::from([0; 32]));
}
}
}
}
#[test]
fn blockheaderhash_debug() {
let _init_guard = zebra_test::init();
let preimage = b"foo bar baz";
let mut sha_writer = sha256d::Writer::default();
let _ = sha_writer.write_all(preimage);
let hash = Hash(sha_writer.finish());
assert_eq!(
format!("{hash:?}"),
"block::Hash(\"3166411bd5343e0b284a108f39a929fbbb62619784f8c6dafe520703b5b446bf\")"
);
}
#[test]
fn blockheaderhash_from_blockheader() {
let _init_guard = zebra_test::init();
let (blockheader, _blockheader_bytes) = generate::block_header();
let hash = Hash::from(&blockheader);
assert_eq!(
format!("{hash:?}"),
"block::Hash(\"d1d6974bbe1d4d127c889119b2fc05724c67588dc72708839727586b8c2bc939\")"
);
let mut bytes = Cursor::new(Vec::new());
blockheader
.zcash_serialize(&mut bytes)
.expect("these bytes to serialize from a blockheader without issue");
bytes.set_position(0);
let other_header = bytes
.zcash_deserialize_into()
.expect("these bytes to deserialize into a blockheader without issue");
assert_eq!(blockheader, other_header);
}
#[test]
fn chain_value_pool_change_propagates_transaction_value_balance_errors() {
let _init_guard = zebra_test::init();
let max_money: Amount<NonNegative> = MAX_MONEY.try_into().expect("MAX_MONEY is a valid amount");
let coinbase = Transaction::test_v1(
vec![transparent::Input::Coinbase {
height: Height(1),
data: vec![0],
sequence: 0xFFFF_FFFF,
}],
vec![
transparent::Output::new(max_money, transparent::Script::new(&[])),
transparent::Output::new(max_money, transparent::Script::new(&[])),
],
LockTime::unlocked(),
);
let utxos = HashMap::<transparent::OutPoint, transparent::Utxo>::new();
assert!(
coinbase.value_balance(&utxos).is_err(),
"transaction-level value balance should reject an output sum above MAX_MONEY"
);
let header: Header = zebra_test::vectors::DUMMY_HEADER
.zcash_deserialize_into()
.expect("dummy header should deserialize");
let block = Block {
header: Arc::new(header),
transactions: vec![Arc::new(coinbase)],
};
assert!(
block
.chain_value_pool_change(
&utxos,
DeferredPoolBalanceChange::zero(),
&Network::Mainnet,
ValueBalance::zero()
)
.is_err(),
"block-level aggregation should propagate transaction value-balance errors"
);
}
#[test]
fn genesis_transparent_outputs_are_not_issued_supply() {
use crate::parameters::testnet::{ConfiguredActivationHeights, Parameters};
let _init_guard = zebra_test::init();
for bytes in [
&*zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES,
&*zebra_test::vectors::BLOCK_TESTNET_GENESIS_BYTES,
] {
let genesis: Block = bytes.zcash_deserialize_into().unwrap();
assert!(genesis.transactions[0]
.outputs()
.iter()
.all(|output| output.value.is_zero()));
}
let network = Parameters::build()
.with_slow_start_interval(Height::MIN)
.with_activation_heights(ConfiguredActivationHeights {
nu7: Some(1),
..Default::default()
})
.unwrap()
.clear_funding_streams()
.with_lockbox_disbursements(vec![])
.to_network()
.unwrap();
let genesis: Block = zebra_test::vectors::BLOCK_TESTNET_GENESIS_BYTES
.zcash_deserialize_into()
.unwrap();
for value in [0, 1, MAX_MONEY] {
let mut genesis = genesis.clone();
let transaction = Arc::make_mut(&mut genesis.transactions[0]);
let mut outputs = transaction.outputs();
outputs[0].value = value.try_into().unwrap();
transaction.set_outputs(outputs);
for network in [&network, &Network::Mainnet] {
let change = genesis
.chain_value_pool_change(
&HashMap::<transparent::OutPoint, transparent::Utxo>::new(),
DeferredPoolBalanceChange::zero(),
network,
ValueBalance::zero(),
)
.unwrap();
let pools = ValueBalance::zero()
.add_chain_value_pool_change(change)
.unwrap();
assert!(pools.transparent_amount().is_zero());
assert!(pools.nsm_amount().is_zero());
assert!(pools.total().unwrap().is_zero());
}
}
}
#[test]
fn chain_value_pool_change_accrues_the_nsm_reserve() {
use crate::{
parameters::testnet::ConfiguredActivationHeights, transaction::Hash as TransactionHash,
transparent::OutPoint,
};
let _init_guard = zebra_test::init();
let height = Height(1_000);
let spent_value: Amount<NonNegative> = 100_000.try_into().expect("valid amount");
let change: Amount<NonNegative> = 90_000.try_into().expect("valid amount");
let coinbase = Transaction::test_v1(
vec![transparent::Input::Coinbase {
height,
data: vec![0],
sequence: 0xFFFF_FFFF,
}],
vec![transparent::Output::new(
change,
transparent::Script::new(&[]),
)],
LockTime::unlocked(),
);
let outpoint = OutPoint::from_usize(TransactionHash([0; 32]), 0);
let spend = Transaction::test_v1(
vec![transparent::Input::PrevOut {
outpoint,
unlock_script: transparent::Script::new(&[]),
sequence: 0,
}],
vec![transparent::Output::new(
change,
transparent::Script::new(&[]),
)],
LockTime::unlocked(),
);
let utxos = HashMap::from([(
outpoint,
transparent::Utxo::new(
transparent::Output::new(spent_value, transparent::Script::new(&[])),
Height(999),
false,
),
)]);
let header: Header = zebra_test::vectors::DUMMY_HEADER
.zcash_deserialize_into()
.expect("dummy header should deserialize");
let block = Block {
header: Arc::new(header),
transactions: vec![Arc::new(coinbase), Arc::new(spend)],
};
for (nu7, expected_reserve) in [(None, 0), (Some(height.0), 6_000), (Some(999), 6_000)] {
let network = Network::new_regtest(
ConfiguredActivationHeights {
canopy: Some(1),
nu5: Some(2),
nu6: Some(3),
nu6_1: Some(4),
nu6_2: Some(5),
nu6_3: Some(6),
nu7,
..Default::default()
}
.into(),
);
let (change, fees) = block
.chain_value_pool_change_and_fees(
&utxos,
DeferredPoolBalanceChange::zero(),
&network,
ValueBalance::zero(),
)
.expect("chain value pool change should be calculable");
assert_eq!(change.nsm_amount().zatoshis(), expected_reserve);
assert_eq!(fees.map(|fees| fees.zatoshis()), nu7.map(|_| 10_000));
}
}
#[test]
fn nsm_seed_reissuance_and_funding_follow_the_parent() -> Result<(), Box<dyn std::error::Error>> {
use crate::{
amount::NegativeAllowed,
parameters::{
subsidy::{
block_subsidy_with_parent_pools, funding_stream_address, funding_stream_values,
scheduled_block_subsidy, subsidy_is_valid, CoinbaseTransactionError,
FundingStreamReceiver, SubsidyError,
},
testnet::{
ConfiguredActivationHeights, ConfiguredFundingStreamRecipient,
ConfiguredFundingStreams, Parameters,
},
},
};
use std::ops::Neg;
let height = Height(1_000);
let reserve = Amount::<NonNegative>::try_from(10_000_000_000u64)?;
let network = Parameters::build()
.with_slow_start_interval(Height(0))
.with_activation_heights(ConfiguredActivationHeights {
canopy: Some(1),
nu5: Some(2),
nu6: Some(3),
nu6_1: Some(4),
nu6_2: Some(5),
nu6_3: Some(6),
nu7: Some(height.0),
..Default::default()
})?
.with_initial_nsm_value_balance(reserve)
.with_nsm_reissuance_height(height)
.with_funding_streams(vec![ConfiguredFundingStreams {
height_range: Some(height..Height(1_010)),
recipients: Some(vec![
ConfiguredFundingStreamRecipient::new_for(FundingStreamReceiver::MajorGrants),
ConfiguredFundingStreamRecipient {
receiver: FundingStreamReceiver::Deferred,
numerator: 12,
addresses: None,
},
]),
}])
.to_network()?;
let before_seed = ValueBalance::zero();
let header: Header = zebra_test::vectors::DUMMY_HEADER.zcash_deserialize_into()?;
let make_block = |height, outputs| Block {
header: Arc::new(header),
transactions: vec![Arc::new(Transaction::test_v1(
vec![transparent::Input::Coinbase {
height,
data: vec![0],
sequence: u32::MAX,
}],
outputs,
LockTime::unlocked(),
))],
};
let before_activation = make_block(
Height(999),
vec![transparent::Output::new(
scheduled_block_subsidy(Height(999), &network)?,
transparent::Script::new(&[]),
)],
);
let pre_activation_change = before_activation.chain_value_pool_change(
&HashMap::<transparent::OutPoint, transparent::Utxo>::new(),
DeferredPoolBalanceChange::zero(),
&network,
before_seed,
)?;
let parent = before_seed.add_chain_value_pool_change(pre_activation_change)?;
assert!(parent.nsm_amount().is_zero());
let total = block_subsidy_with_parent_pools(height, &network, parent)?;
assert_eq!(
(total - scheduled_block_subsidy(height, &network)?)?.zatoshis(),
1_375
);
let funding = funding_stream_values(height, &network, total)?;
let grant = funding[&FundingStreamReceiver::MajorGrants];
let deferred = funding[&FundingStreamReceiver::Deferred];
assert_eq!(grant.zatoshis(), total.zatoshis() * 8 / 100);
assert_eq!(deferred.zatoshis(), total.zatoshis() * 12 / 100);
let grant_script = funding_stream_address(height, &network, FundingStreamReceiver::MajorGrants)
.expect("configured funding address")
.script();
let miner = (total - grant - deferred)?;
let block = make_block(
height,
vec![
transparent::Output::new(miner, transparent::Script::new(&[])),
transparent::Output::new(grant, grant_script.clone()),
],
);
let deferred_change = DeferredPoolBalanceChange::new(deferred.constrain::<NegativeAllowed>()?);
let (change, fees) = block.chain_value_pool_change_and_fees(
&HashMap::<transparent::OutPoint, transparent::Utxo>::new(),
deferred_change,
&network,
parent,
)?;
assert_eq!(fees, Some(Amount::zero()));
assert_eq!(change.nsm_amount().zatoshis(), 9_999_998_625);
let after = parent.add_chain_value_pool_change(change)?;
assert_eq!(after.nsm_amount().zatoshis(), 9_999_998_625);
assert_eq!(after.total()?, (parent.total()? + total)?);
let next = make_block(height.next()?, Vec::new());
let next_change = next.chain_value_pool_change(
&HashMap::<transparent::OutPoint, transparent::Utxo>::new(),
DeferredPoolBalanceChange::zero(),
&network,
after,
)?;
assert_eq!(next_change.nsm_amount().zatoshis(), -1_375);
assert_eq!(ValueBalance::from_bytes(&after.to_bytes())?, after);
let scheduled_grant =
funding_stream_values(height, &network, scheduled_block_subsidy(height, &network)?)?
[&FundingStreamReceiver::MajorGrants];
let underfunded = make_block(
height,
vec![
transparent::Output::new(
(total - scheduled_grant - deferred)?,
transparent::Script::new(&[]),
),
transparent::Output::new(scheduled_grant, grant_script),
],
);
assert_eq!(
underfunded.chain_value_pool_change(
&HashMap::<transparent::OutPoint, transparent::Utxo>::new(),
deferred_change,
&network,
parent
)?,
change,
"ledger accounting does not validate funding payouts",
);
assert!(matches!(
subsidy_is_valid(&underfunded, &network, total),
Err(CoinbaseTransactionError::Subsidy(
SubsidyError::FundingStreamNotFound
)),
));
assert_eq!(subsidy_is_valid(&block, &network, total)?, deferred_change);
let mut other_parent = after;
other_parent.set_nsm_amount((reserve * 2)?);
let other_total = block_subsidy_with_parent_pools(height.next()?, &network, other_parent)?;
assert!(matches!(
subsidy_is_valid(&block, &network, other_total),
Err(CoinbaseTransactionError::Subsidy(
SubsidyError::FundingStreamNotFound
)),
));
assert_eq!(after.add_chain_value_pool_change(change.neg())?, parent);
assert_eq!(
parent.add_chain_value_pool_change(pre_activation_change.neg())?,
before_seed
);
Ok(())
}
#[test]
fn blockheader_serialization() {
let _init_guard = zebra_test::init();
const BLOCK_HEADER_LENGTH: usize = crate::work::equihash::Solution::INPUT_LENGTH
+ 32
+ 3
+ crate::work::equihash::SOLUTION_SIZE;
for block in zebra_test::vectors::BLOCKS.iter() {
let header_bytes = &block[..BLOCK_HEADER_LENGTH];
let mut header = header_bytes
.zcash_deserialize_into::<Header>()
.expect("blockheader test vector should deserialize");
let _serialized_header = header
.zcash_serialize_to_vec()
.expect("blockheader test vector should serialize");
let header_bytes = [&[255; 4], &header_bytes[4..]].concat();
let deserialization_err = header_bytes
.zcash_deserialize_into::<Header>()
.expect_err("blockheader test vector should fail to deserialize");
let SerializationError::Parse(err_msg) = deserialization_err else {
panic!("SerializationError variant should be Parse")
};
assert_eq!(err_msg, "high bit was set in version field");
let header_bytes = [&[0; 4], &header_bytes[4..]].concat();
let deserialization_err = header_bytes
.zcash_deserialize_into::<Header>()
.expect_err("blockheader test vector should fail to deserialize");
let SerializationError::Parse(err_msg) = deserialization_err else {
panic!("SerializationError variant should be Parse")
};
assert_eq!(err_msg, "version must be at least 4");
header.version = u32::MAX;
let serialization_err = header
.zcash_serialize_to_vec()
.expect_err("blockheader test vector with modified version should fail to serialize");
assert_eq!(
serialization_err.kind(),
std::io::ErrorKind::Other,
"error kind should be Other"
);
let err_msg = serialization_err
.into_inner()
.expect("there should be an inner error");
assert_eq!(err_msg.to_string(), "high bit was set in version field");
}
}
#[test]
fn blockheader_serialized_size() {
let _init_guard = zebra_test::init();
for block in zebra_test::vectors::BLOCKS.iter() {
let mut header = block[..Header::serialized_size(&Network::Mainnet)]
.zcash_deserialize_into::<Header>()
.expect("blockheader test vector should deserialize");
let serialized_header = header
.zcash_serialize_to_vec()
.expect("blockheader test vector should serialize");
assert_eq!(
serialized_header.len(),
Header::serialized_size(&Network::Mainnet),
"serialized header size should match Header::serialized_size on Mainnet"
);
header.solution = crate::work::equihash::Solution::from_bytes(
&[0; crate::work::equihash::REGTEST_SOLUTION_SIZE],
)
.expect("Regtest solution size should be valid");
let serialized_header = header
.zcash_serialize_to_vec()
.expect("Regtest blockheader should serialize");
assert_eq!(
serialized_header.len(),
Header::serialized_size(&Network::new_regtest(Default::default())),
"serialized header size should match Header::serialized_size on Regtest"
);
}
}
#[test]
fn round_trip_blocks() {
let _init_guard = zebra_test::init();
zebra_test::vectors::BLOCK_MAINNET_434873_BYTES
.zcash_deserialize_into::<Block>()
.expect("bad version block test vector should deserialize");
for block_bytes in zebra_test::vectors::BLOCKS.iter() {
let block = block_bytes
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid");
let round_trip_bytes = block
.zcash_serialize_to_vec()
.expect("vec serialization is infallible");
assert_eq!(&round_trip_bytes[..], *block_bytes);
}
}
#[test]
fn coinbase_parsing_rejects_above_0x80() {
let _init_guard = zebra_test::init();
zebra_test::vectors::BAD_BLOCK_MAINNET_202_BYTES
.zcash_deserialize_into::<Block>()
.expect_err("parsing fails");
}
#[test]
fn block_test_vectors_unique() {
let _init_guard = zebra_test::init();
let block_count = zebra_test::vectors::BLOCKS.len();
let block_hashes: HashSet<_> = zebra_test::vectors::BLOCKS
.iter()
.map(|b| {
b.zcash_deserialize_into::<Block>()
.expect("block is structurally valid")
.hash()
})
.collect();
assert_eq!(
block_count,
block_hashes.len(),
"block test vectors must be unique"
);
}
#[test]
fn block_test_vectors() {
let _init_guard = zebra_test::init();
for net in Network::iter() {
let sapling_anchors = net.sapling_anchors();
let orchard_anchors = net.orchard_anchors();
for (&height, block) in net.block_iter() {
let block = block
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid");
assert_eq!(
block.coinbase_height().expect("block height is valid").0,
height,
"deserialized height must match BTreeMap key height"
);
if height
>= Sapling
.activation_height(&net)
.expect("activation height")
.0
{
assert!(
sapling_anchors.contains_key(&height),
"post-sapling block test vectors must have matching sapling root test vectors: \
missing {net} {height}"
);
}
if height >= Nu5.activation_height(&net).expect("activation height").0 {
assert!(
orchard_anchors.contains_key(&height),
"post-nu5 block test vectors must have matching orchard root test vectors: \
missing {net} {height}"
);
}
}
}
}
#[test]
fn block_commitment() {
let _init_guard = zebra_test::init();
for net in Network::iter() {
let sapling_anchors = net.sapling_anchors();
for (height, block) in net.block_iter() {
if let FinalSaplingRoot(anchor) = block
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid")
.commitment(&net)
.expect("unexpected structurally invalid block commitment at {net} {height}")
{
let expected_anchor = *sapling_anchors
.get(height)
.expect("unexpected missing final sapling root test vector");
assert_eq!(
anchor,
sapling::tree::Root::try_from(*expected_anchor).unwrap(),
"unexpected invalid final sapling root commitment at {net} {height}"
);
}
}
}
}
#[test]
fn block_limits_multi_tx() {
let _init_guard = zebra_test::init();
let mut block = generate::large_multi_transaction_block();
let mut data = Vec::new();
block
.zcash_serialize(&mut data)
.expect("block should serialize as we are not limiting generation yet");
assert!(data.len() <= MAX_BLOCK_BYTES as usize);
let block2 = Block::zcash_deserialize(&data[..])
.expect("block should deserialize as we are just below limit");
assert_eq!(block, block2);
block = generate::oversized_multi_transaction_block();
let mut data = Vec::new();
block
.zcash_serialize(&mut data)
.expect("block should serialize as we are not limiting generation yet");
assert!(data.len() > MAX_BLOCK_BYTES as usize);
Block::zcash_deserialize(&data[..]).expect_err("block should not deserialize");
}
#[test]
fn block_limits_single_tx() {
let _init_guard = zebra_test::init();
let mut block = generate::large_single_transaction_block_many_inputs();
let mut data = Vec::new();
block
.zcash_serialize(&mut data)
.expect("block should serialize as we are not limiting generation yet");
assert!(data.len() <= MAX_BLOCK_BYTES as usize);
Block::zcash_deserialize(&data[..])
.expect("block should deserialize as we are just below limit");
block = generate::oversized_single_transaction_block_many_inputs();
let mut data = Vec::new();
block
.zcash_serialize(&mut data)
.expect("block should serialize as we are not limiting generation yet");
assert!(data.len() > MAX_BLOCK_BYTES as usize);
Block::zcash_deserialize(&data[..]).expect_err("block should not deserialize");
}
fn node_time_check(
block_header_time: DateTime<Utc>,
now: DateTime<Utc>,
) -> Result<(), BlockTimeError> {
let (mut header, _header_bytes) = generate::block_header();
header.time = block_header_time;
header.time_is_valid_at(now, &Height(0), &Hash([0; 32]))
}
#[test]
fn time_check_now() {
let _init_guard = zebra_test::init();
let now = Utc::now();
let three_hours_in_the_past = now - Duration::hours(3);
let two_hours_in_the_future = now + Duration::hours(2);
let two_hours_and_one_second_in_the_future = now + Duration::hours(2) + Duration::seconds(1);
node_time_check(now, now).expect("the current time should be valid as a block header time");
node_time_check(three_hours_in_the_past, now)
.expect("a past time should be valid as a block header time");
node_time_check(two_hours_in_the_future, now)
.expect("2 hours in the future should be valid as a block header time");
node_time_check(two_hours_and_one_second_in_the_future, now)
.expect_err("2 hours and 1 second in the future should be invalid as a block header time");
node_time_check(now, three_hours_in_the_past)
.expect_err("3 hours in the future should be invalid as a block header time");
node_time_check(now, two_hours_in_the_future)
.expect("2 hours in the past should be valid as a block header time");
node_time_check(now, two_hours_and_one_second_in_the_future)
.expect("2 hours and 1 second in the past should be valid as a block header time");
}
static BLOCK_HEADER_VALID_TIMESTAMPS: &[i64] = &[
i64::MIN,
i64::MIN + 1,
(i32::MIN as i64) - 1,
(i32::MIN as i64),
(i32::MIN as i64) + 1,
-1,
0,
1,
LockTime::MIN_TIMESTAMP - 1,
LockTime::MIN_TIMESTAMP,
LockTime::MIN_TIMESTAMP + 1,
];
static BLOCK_HEADER_INVALID_TIMESTAMPS: &[i64] = &[
(i32::MAX as i64) - 1,
(i32::MAX as i64),
(i32::MAX as i64) + 1,
LockTime::MAX_TIMESTAMP - 1,
LockTime::MAX_TIMESTAMP,
LockTime::MAX_TIMESTAMP + 1,
i64::MAX - 1,
i64::MAX,
];
#[test]
fn time_check_fixed() {
let _init_guard = zebra_test::init();
let now = Utc::now();
for valid_timestamp in BLOCK_HEADER_VALID_TIMESTAMPS {
let block_header_time = match Utc.timestamp_opt(*valid_timestamp, 0) {
LocalResult::Single(time) => time,
LocalResult::None => {
continue;
}
LocalResult::Ambiguous(_, _) => {
unreachable!();
}
};
node_time_check(block_header_time, now)
.expect("the time should be valid as a block header time");
node_time_check(now, block_header_time)
.expect_err("the inverse comparison should be invalid");
}
for invalid_timestamp in BLOCK_HEADER_INVALID_TIMESTAMPS {
let block_header_time = match Utc.timestamp_opt(*invalid_timestamp, 0) {
LocalResult::Single(time) => time,
LocalResult::None => {
continue;
}
LocalResult::Ambiguous(_, _) => {
unreachable!();
}
};
node_time_check(block_header_time, now)
.expect_err("the time should be invalid as a block header time");
node_time_check(now, block_header_time).expect("the inverse comparison should be valid");
}
}
#[test]
fn transaction_fees_sum_the_non_coinbase_transactions() {
let _init_guard = zebra_test::init();
let height = Height(347_499);
let amount = |zatoshis: i64| Amount::<NonNegative>::try_from(zatoshis).expect("valid amount");
let output = |zatoshis| transparent::Output {
value: amount(zatoshis),
lock_script: transparent::Script::new(&[]),
};
let coinbase = Transaction::test_v4(
vec![transparent::Input::Coinbase {
height,
data: vec![0],
sequence: u32::MAX,
}],
vec![output(1_000_000)],
LockTime::unlocked(),
height,
);
let mut utxos = HashMap::new();
let mut transactions = vec![Arc::new(coinbase)];
for (index, fee) in [1, 1, 1_000].into_iter().enumerate() {
let outpoint = transparent::OutPoint {
hash: crate::transaction::Hash([index as u8 + 1; 32]),
index: 0,
};
utxos.insert(
outpoint,
transparent::Utxo::new(output(10_000), Height(1), false),
);
transactions.push(Arc::new(Transaction::test_v4(
vec![transparent::Input::PrevOut {
outpoint,
unlock_script: transparent::Script::new(&[]),
sequence: u32::MAX,
}],
vec![output(10_000 - fee)],
LockTime::unlocked(),
height,
)));
}
let mut block = zebra_test::vectors::BLOCK_MAINNET_347499_BYTES
.zcash_deserialize_into::<Block>()
.expect("block should deserialize");
let network = Network::new_regtest(
crate::parameters::testnet::ConfiguredActivationHeights {
nu7: Some(height.0),
..Default::default()
}
.into(),
);
for (count, fees, contribution) in [(1, 0, 0), (3, 2, 1), (4, 1_002, 601)] {
block.transactions = transactions[..count].to_vec();
assert_eq!(block.transaction_fees(&utxos), Ok(amount(fees)));
let (change, gross_fees) = block
.chain_value_pool_change_and_fees(
&utxos,
DeferredPoolBalanceChange::zero(),
&network,
ValueBalance::zero(),
)
.expect("valid transaction balances");
assert_eq!(gross_fees, Some(amount(fees)));
assert_eq!(change.nsm_amount().zatoshis(), contribution);
assert_eq!(change.transparent_amount().zatoshis(), 1_000_000 - fees);
}
Arc::make_mut(&mut block.transactions[3]).set_outputs(vec![output(10_001)]);
assert!(matches!(
block.chain_value_pool_change_and_fees(
&utxos,
DeferredPoolBalanceChange::zero(),
&network,
ValueBalance::zero(),
),
Err(crate::value_balance::ValueBalanceError::Total(_)),
));
let (change, fees) = block
.chain_value_pool_change_and_fees(
&utxos,
DeferredPoolBalanceChange::zero(),
&Network::Mainnet,
ValueBalance::zero(),
)
.expect("pre-NU7 accounting leaves fee validation to the verifier");
assert_eq!(fees, None);
assert_eq!(change.transparent_amount().zatoshis(), 999_999);
assert_eq!(change.nsm_amount().zatoshis(), 0);
}
fn block_with_repeated_transaction(transaction: &[u8], transactions: usize) -> Vec<u8> {
let genesis: Block = zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES
.zcash_deserialize_into()
.expect("genesis block is valid");
let mut bytes = genesis
.header
.zcash_serialize_to_vec()
.expect("header serializes");
crate::serialization::CompactSizeMessage::try_from(transactions)
.expect("count fits")
.zcash_serialize(&mut bytes)
.expect("count serializes");
for _ in 0..transactions {
bytes.extend_from_slice(transaction);
}
bytes
}
#[test]
fn block_with_tiny_transactions_is_rejected() {
use crate::serialization::TrustedPreallocate;
let _init_guard = zebra_test::init();
let empty_v1 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
assert!(Transaction::zcash_deserialize(&empty_v1[..]).is_ok());
let count = usize::try_from(Transaction::max_allocation()).expect("fits in usize");
let tiny = block_with_repeated_transaction(&empty_v1, count);
assert_too_small(&tiny);
let first_only = &tiny[..tiny.len() - (count - 1) * empty_v1.len()];
assert_too_small(first_only);
let mut smallest = vec![1, 0, 0, 0, 1];
smallest.extend_from_slice(&[0x11; 32]);
smallest.extend_from_slice(&[0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 1]);
smallest.extend_from_slice(&[0; 9]);
smallest.extend_from_slice(&[0; 4]);
assert_eq!(smallest.len(), 60);
assert!(Transaction::zcash_deserialize(&smallest[..]).is_ok());
let header_and_count = block_with_repeated_transaction(&smallest, 0).len() + 4;
let count =
(usize::try_from(MAX_BLOCK_BYTES).expect("fits") - header_and_count) / smallest.len();
let full = block_with_repeated_transaction(&smallest, count);
assert!(full.len() as u64 <= MAX_BLOCK_BYTES);
let block =
Block::zcash_deserialize(&full[..]).expect("block of valid-sized transactions parses");
assert_eq!(block.transactions.len(), count);
}
fn v1_transaction_with_output_script(script_len: u8) -> Vec<u8> {
let mut transaction = vec![1, 0, 0, 0, 0, 1];
transaction.extend_from_slice(&[0; 8]);
transaction.push(script_len);
transaction.extend(std::iter::repeat_n(0x51, script_len.into()));
transaction.extend_from_slice(&[0; 4]);
transaction
}
fn assert_too_small(block: &[u8]) {
match Block::zcash_deserialize(block) {
Err(SerializationError::Parse(message)) => assert_eq!(
message,
"block transactions are smaller than the smallest valid transaction"
),
other => panic!("unexpected result: {other:?}"),
}
}
#[test]
fn block_transaction_size_check_boundary() {
use crate::{serialization::TrustedPreallocate, transaction::MIN_TRANSPARENT_TX_SIZE};
let _init_guard = zebra_test::init();
let at_minimum = v1_transaction_with_output_script(35);
let below_minimum = v1_transaction_with_output_script(34);
assert_eq!(at_minimum.len() as u64, MIN_TRANSPARENT_TX_SIZE);
assert_eq!(below_minimum.len() as u64, MIN_TRANSPARENT_TX_SIZE - 1);
let block = block_with_repeated_transaction(&at_minimum, 1000);
let block = Block::zcash_deserialize(&block[..]).expect("minimum-sized transactions parse");
assert_eq!(block.transactions.len(), 1000);
assert_too_small(&block_with_repeated_transaction(&below_minimum, 1000));
let mut mixed = block_with_repeated_transaction(&at_minimum, 0);
mixed.pop();
mixed.push(2);
mixed.extend_from_slice(&v1_transaction_with_output_script(41));
mixed.extend_from_slice(&v1_transaction_with_output_script(29));
let block = Block::zcash_deserialize(&mixed[..]).expect("average at the minimum parses");
assert_eq!(block.transactions.len(), 2);
let empty = block_with_repeated_transaction(&at_minimum, 0);
let block = Block::zcash_deserialize(&empty[..]).expect("an empty transaction list parses");
assert!(block.transactions.is_empty());
let count = usize::try_from(Transaction::max_allocation()).expect("fits in usize") + 1;
let mut over = block_with_repeated_transaction(&at_minimum, 0);
over.pop();
crate::serialization::CompactSizeMessage::try_from(count)
.expect("count fits")
.zcash_serialize(&mut over)
.expect("count serializes");
over.extend_from_slice(&at_minimum);
assert!(matches!(
Block::zcash_deserialize(&over[..]),
Err(SerializationError::Parse(
"Vector longer than max_allocation"
))
));
}
#[test]
fn blocks_of_smallest_transactions_of_each_version_parse() {
use crate::transaction::MIN_TRANSPARENT_TX_SIZE;
let _init_guard = zebra_test::init();
let input = || transparent::Input::PrevOut {
outpoint: transparent::OutPoint::from_usize(crate::transaction::Hash([0x11; 32]), 0),
unlock_script: transparent::Script::new(&[]),
sequence: u32::MAX,
};
let output = || transparent::Output::new(Amount::zero(), transparent::Script::new(&[]));
let lock_time = LockTime::unlocked;
let expiry = Height(0);
let transactions = [
Transaction::test_v1(vec![input()], vec![output()], lock_time()),
Transaction::test_v2(vec![input()], vec![output()], lock_time()),
Transaction::test_v3(vec![input()], vec![output()], lock_time(), expiry),
Transaction::test_v4(vec![input()], vec![output()], lock_time(), expiry),
Transaction::test_v5(Nu5, vec![input()], vec![output()], lock_time(), expiry),
];
for transaction in transactions {
let bytes = transaction
.zcash_serialize_to_vec()
.expect("transaction serializes");
assert!(bytes.len() as u64 >= MIN_TRANSPARENT_TX_SIZE);
let header_and_count = block_with_repeated_transaction(&bytes, 0).len() + 4;
let count =
(usize::try_from(MAX_BLOCK_BYTES).expect("fits") - header_and_count) / bytes.len();
let block = block_with_repeated_transaction(&bytes, count);
assert!(block.len() as u64 <= MAX_BLOCK_BYTES);
let block = Block::zcash_deserialize(&block[..]).unwrap_or_else(|error| {
panic!(
"block of {}-byte transactions parses: {error:?}",
bytes.len()
)
});
assert_eq!(block.transactions.len(), count);
}
}