From 9b17bbce3d731da24049af138d39f028e4a06b1a Mon Sep 17 00:00:00 2001 From: Conrado Gouvea Date: Thu, 18 Nov 2021 21:36:50 -0300 Subject: [PATCH] Add missing nSpendsSapling limit check (#3076) --- zebra-chain/src/sapling/spend.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/zebra-chain/src/sapling/spend.rs b/zebra-chain/src/sapling/spend.rs index 6dac34bdf..4c067a87d 100644 --- a/zebra-chain/src/sapling/spend.rs +++ b/zebra-chain/src/sapling/spend.rs @@ -262,7 +262,15 @@ pub(crate) const SHARED_ANCHOR_SPEND_SIZE: u64 = SHARED_ANCHOR_SPEND_PREFIX_SIZE /// The maximum number of sapling spends in a valid Zcash on-chain transaction V4. impl TrustedPreallocate for Spend { fn max_allocation() -> u64 { - (MAX_BLOCK_BYTES - 1) / ANCHOR_PER_SPEND_SIZE + const MAX: u64 = (MAX_BLOCK_BYTES - 1) / ANCHOR_PER_SPEND_SIZE; + // > [NU5 onward] nSpendsSapling, nOutputsSapling, and nActionsOrchard MUST all be less than 2^16. + // https://zips.z.cash/protocol/protocol.pdf#txnencodingandconsensus + // This acts as nSpendsSapling and is therefore subject to the rule. + // The maximum value is actually smaller due to the block size limit, + // but we ensure the 2^16 limit with a static assertion. + // (The check is not required pre-NU5, but it doesn't cause problems.) + static_assertions::const_assert!(MAX < (1 << 16)); + MAX } }