From 8dfa836a03ed6fb491cf4857de61a22de587cc3a Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Sat, 9 Mar 2024 15:38:49 -0700 Subject: [PATCH] zcash_client_sqlite: Use correct offsets for Orchard commitment positions. --- zcash_client_backend/src/data_api.rs | 7 +++--- zcash_client_sqlite/src/lib.rs | 36 ++++++++++++++++++---------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/zcash_client_backend/src/data_api.rs b/zcash_client_backend/src/data_api.rs index b16bd7b96..5a72ea930 100644 --- a/zcash_client_backend/src/data_api.rs +++ b/zcash_client_backend/src/data_api.rs @@ -427,7 +427,7 @@ impl WalletSummary { /// belonging to a wallet. pub trait InputSource { /// The type of errors produced by a wallet backend. - type Error; + type Error: Debug; /// Backend-specific account identifier. /// @@ -498,7 +498,7 @@ pub trait InputSource { /// be abstracted away from any particular data storage substrate. pub trait WalletRead { /// The type of errors that may be generated when querying a wallet data store. - type Error; + type Error: Debug; /// The type of the account identifier. /// @@ -1312,7 +1312,8 @@ pub trait WalletWrite: WalletRead { /// At present, this only serves the Sapling protocol, but it will be modified to /// also provide operations related to Orchard note commitment trees in the future. pub trait WalletCommitmentTrees { - type Error; + type Error: Debug; + /// The type of the backing [`ShardStore`] for the Sapling note commitment tree. type SaplingShardStore<'a>: ShardStore< H = sapling::Node, diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index 65083ba3c..904115916 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -512,15 +512,25 @@ impl WalletWrite for WalletDb &mut self, blocks: Vec>, ) -> Result<(), Self::Error> { + struct BlockPositions { + height: BlockHeight, + sapling_start_position: Position, + #[cfg(feature = "orchard")] + orchard_start_position: Position, + } + self.transactionally(|wdb| { - let start_positions = blocks.first().map(|block| { - ( - block.height(), - Position::from( - u64::from(block.sapling().final_tree_size()) - - u64::try_from(block.sapling().commitments().len()).unwrap(), - ), - ) + let start_positions = blocks.first().map(|block| BlockPositions { + height: block.height(), + sapling_start_position: Position::from( + u64::from(block.sapling().final_tree_size()) + - u64::try_from(block.sapling().commitments().len()).unwrap(), + ), + #[cfg(feature = "orchard")] + orchard_start_position: Position::from( + u64::from(block.orchard().final_tree_size()) + - u64::try_from(block.orchard().commitments().len()).unwrap(), + ), }); let mut sapling_commitments = vec![]; #[cfg(feature = "orchard")] @@ -655,7 +665,7 @@ impl WalletWrite for WalletDb // We will have a start position and a last scanned height in all cases where // `blocks` is non-empty. - if let Some(((start_height, start_position), last_scanned_height)) = + if let Some((start_positions, last_scanned_height)) = start_positions.zip(last_scanned_height) { // Create subtrees from the note commitments in parallel. @@ -665,7 +675,8 @@ impl WalletWrite for WalletDb .par_chunks_mut(CHUNK_SIZE) .enumerate() .filter_map(|(i, chunk)| { - let start = start_position + (i * CHUNK_SIZE) as u64; + let start = + start_positions.sapling_start_position + (i * CHUNK_SIZE) as u64; let end = start + chunk.len() as u64; shardtree::LocatedTree::from_iter( @@ -695,7 +706,8 @@ impl WalletWrite for WalletDb .par_chunks_mut(CHUNK_SIZE) .enumerate() .filter_map(|(i, chunk)| { - let start = start_position + (i * CHUNK_SIZE) as u64; + let start = + start_positions.orchard_start_position + (i * CHUNK_SIZE) as u64; let end = start + chunk.len() as u64; shardtree::LocatedTree::from_iter( @@ -725,7 +737,7 @@ impl WalletWrite for WalletDb wdb.conn.0, &wdb.params, Range { - start: start_height, + start: start_positions.height, end: last_scanned_height + 1, }, ¬e_positions,