From 7720a4fc589375a9eafd1b488ebf9c609c203b8c Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Thu, 9 Nov 2023 15:09:29 -0700 Subject: [PATCH] zcash_client_backend::scanning: Use `checked_sub` instead of manual check. Co-authored-by: str4d --- zcash_client_backend/src/scanning.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/zcash_client_backend/src/scanning.rs b/zcash_client_backend/src/scanning.rs index 884da23c4..4203faa92 100644 --- a/zcash_client_backend/src/scanning.rs +++ b/zcash_client_backend/src/scanning.rs @@ -381,14 +381,12 @@ pub(crate) fn scan_block_with_runner< // The default for m.sapling_commitment_tree_size is zero, so we need to check // that the subtraction will not underflow; if it would do so, we were given // invalid chain metadata for a block with Sapling outputs. - if m.sapling_commitment_tree_size >= sapling_output_count { - Ok(m.sapling_commitment_tree_size - sapling_output_count) - } else { - Err(ScanError::TreeSizeInvalid { + m.sapling_commitment_tree_size + .checked_sub(sapling_output_count) + .ok_or(ScanError::TreeSizeInvalid { protocol: ShieldedProtocol::Sapling, at_height: cur_height, }) - } }, ) }, @@ -427,14 +425,12 @@ pub(crate) fn scan_block_with_runner< // The default for m.orchard_commitment_tree_size is zero, so we need to check // that the subtraction will not underflow; if it would do so, we were given // invalid chain metadata for a block with Orchard actions. - if m.orchard_commitment_tree_size >= orchard_action_count { - Ok(m.orchard_commitment_tree_size - orchard_action_count) - } else { - Err(ScanError::TreeSizeInvalid { + m.orchard_commitment_tree_size + .checked_sub(orchard_action_count) + .ok_or(ScanError::TreeSizeInvalid { protocol: ShieldedProtocol::Orchard, at_height: cur_height, }) - } }, ) },