Skip to content

Commit

Permalink
zcash_client_backend: Add Orchard note commitment tree size to block …
Browse files Browse the repository at this point in the history
…metadata.
  • Loading branch information
nuttycom committed Nov 8, 2023
1 parent e4b9d73 commit 7e1c1c2
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 91 deletions.
5 changes: 5 additions & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ and this library adheres to Rust's notion of

### Added
- `zcash_client_backend::data_api`:
- `BlockMetadata::orchard_tree_size`.
- `TransparentInputSource`
- `SaplingInputSource`
- `ScannedBlock::sapling_tree_size`.
- `ScannedBlock::orchard_tree_size`.
- `wallet::propose_standard_transfer_to_address`
- `wallet::input_selection::SaplingInputs`
- `wallet::input_selection::ShieldingSelector` has been
Expand All @@ -33,6 +36,8 @@ and this library adheres to Rust's notion of
backend-specific note identifier. The related `NoteRef` type parameter has
been removed from `error::Error`.
- A new variant `UnsupportedPoolType` has been added.
- `ScannedBlock::metadata` has been renamed to `to_block_metadata` and now
returns an owned value rather than a reference.
- `wallet::shield_transparent_funds` no longer
takes a `memo` argument; instead, memos to be associated with the shielded
outputs should be specified in the construction of the value of the
Expand Down
62 changes: 45 additions & 17 deletions zcash_client_backend/src/data_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,23 @@ pub trait WalletRead {
pub struct BlockMetadata {
block_height: BlockHeight,
block_hash: BlockHash,
sapling_tree_size: u32,
//TODO: orchard_tree_size: u32
sapling_tree_size: Option<u32>,
orchard_tree_size: Option<u32>,
}

impl BlockMetadata {
/// Constructs a new [`BlockMetadata`] value from its constituent parts.
pub fn from_parts(
block_height: BlockHeight,
block_hash: BlockHash,
sapling_tree_size: u32,
sapling_tree_size: Option<u32>,
orchard_tree_size: Option<u32>,
) -> Self {
Self {
block_height,
block_hash,
sapling_tree_size,
orchard_tree_size,
}
}

Expand All @@ -437,20 +439,29 @@ impl BlockMetadata {
self.block_hash
}

/// Returns the size of the Sapling note commitment tree as of the block that this
/// Returns the size of the Sapling note commitment tree as of the end of the block that this
/// [`BlockMetadata`] describes.
pub fn sapling_tree_size(&self) -> u32 {
pub fn sapling_tree_size(&self) -> Option<u32> {
self.sapling_tree_size
}

/// Returns the size of the Orchard note commitment tree as of the end of the block that this
/// [`BlockMetadata`] describes.
pub fn orchard_tree_size(&self) -> Option<u32> {
self.orchard_tree_size
}
}

/// The subset of information that is relevant to this wallet that has been
/// decrypted and extracted from a [`CompactBlock`].
///
/// [`CompactBlock`]: crate::proto::compact_formats::CompactBlock
pub struct ScannedBlock<Nf> {
metadata: BlockMetadata,
block_height: BlockHeight,
block_hash: BlockHash,
block_time: u32,
sapling_tree_size: u32,
orchard_tree_size: u32,
transactions: Vec<WalletTx<Nf>>,
sapling_nullifier_map: Vec<(TxId, u16, Vec<sapling::Nullifier>)>,
sapling_commitments: Vec<(sapling::Node, Retention<BlockHeight>)>,
Expand All @@ -459,15 +470,21 @@ pub struct ScannedBlock<Nf> {
impl<Nf> ScannedBlock<Nf> {
/// Constructs a new `ScannedBlock`
pub fn from_parts(
metadata: BlockMetadata,
block_height: BlockHeight,
block_hash: BlockHash,
block_time: u32,
sapling_tree_size: u32,
orchard_tree_size: u32,
transactions: Vec<WalletTx<Nf>>,
sapling_nullifier_map: Vec<(TxId, u16, Vec<sapling::Nullifier>)>,
sapling_commitments: Vec<(sapling::Node, Retention<BlockHeight>)>,
) -> Self {

Check failure on line 481 in zcash_client_backend/src/data_api.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

this function has too many arguments (8/7)

error: this function has too many arguments (8/7) --> zcash_client_backend/src/data_api.rs:472:5 | 472 | / pub fn from_parts( 473 | | block_height: BlockHeight, 474 | | block_hash: BlockHash, 475 | | block_time: u32, ... | 480 | | sapling_commitments: Vec<(sapling::Node, Retention<BlockHeight>)>, 481 | | ) -> Self { | |_____________^ | = note: `-D clippy::too-many-arguments` implied by `-D warnings` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments

Check failure on line 481 in zcash_client_backend/src/data_api.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

this function has too many arguments (8/7)

error: this function has too many arguments (8/7) --> zcash_client_backend/src/data_api.rs:472:5 | 472 | / pub fn from_parts( 473 | | block_height: BlockHeight, 474 | | block_hash: BlockHash, 475 | | block_time: u32, ... | 480 | | sapling_commitments: Vec<(sapling::Node, Retention<BlockHeight>)>, 481 | | ) -> Self { | |_____________^ | = note: `-D clippy::too-many-arguments` implied by `-D warnings` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
Self {
metadata,
block_height,
block_hash,
block_time,
sapling_tree_size,
orchard_tree_size,
transactions,
sapling_nullifier_map,
sapling_commitments,
Expand All @@ -476,26 +493,27 @@ impl<Nf> ScannedBlock<Nf> {

/// Returns the height of the block that was scanned.
pub fn height(&self) -> BlockHeight {
self.metadata.block_height
self.block_height
}

/// Returns the block hash of the block that was scanned.
pub fn block_hash(&self) -> BlockHash {
self.metadata.block_hash
self.block_hash
}

/// Returns the block time of the block that was scanned, as a Unix timestamp in seconds.
pub fn block_time(&self) -> u32 {
self.block_time
}

/// Returns the metadata describing the state of the note commitment trees as of the end of the
/// scanned block.
///
/// The metadata returned from this method is guaranteed to be consistent with what is returned
/// by [`Self::height`] and [`Self::block_hash`].
pub fn metadata(&self) -> &BlockMetadata {
&self.metadata
/// Returns the size of the Sapling note commitment tree as of the end of the scanned block.
pub fn sapling_tree_size(&self) -> u32 {
self.sapling_tree_size
}

/// Returns the size of the Orchard note commitment tree as of the end of the scanned block.
pub fn orchard_tree_size(&self) -> u32 {
self.orchard_tree_size
}

/// Returns the list of transactions from the block that are relevant to the wallet.
Expand Down Expand Up @@ -524,6 +542,16 @@ impl<Nf> ScannedBlock<Nf> {
pub fn into_sapling_commitments(self) -> Vec<(sapling::Node, Retention<BlockHeight>)> {
self.sapling_commitments
}

/// Returns the [`BlockMetadata`] corresponding to the scanned block.
pub fn to_block_metadata(&self) -> BlockMetadata {
BlockMetadata {
block_height: self.block_height,
block_hash: self.block_hash,
sapling_tree_size: Some(self.sapling_tree_size),
orchard_tree_size: Some(self.orchard_tree_size),
}
}
}

/// A transaction that was detected during scanning of the blockchain,
Expand Down
28 changes: 21 additions & 7 deletions zcash_client_backend/src/data_api/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,23 +345,37 @@ where
continuity_check_metadata = Some(BlockMetadata::from_parts(
BlockHeight::from(0),
BlockHash([0; 32]),
0,
Some(0),
Some(0),
));
}
continuity_check_metadata = continuity_check_metadata.as_ref().map(|m| {
continuity_check_metadata = continuity_check_metadata.as_ref().map(|bm| {
BlockMetadata::from_parts(
block.height(),
block.hash(),
block
.chain_metadata
.as_ref()
.map(|m| m.sapling_commitment_tree_size)
.unwrap_or_else(|| {
m.sapling_tree_size()
+ u32::try_from(
.map(|bcm| bcm.sapling_commitment_tree_size)
.or_else(|| {
bm.sapling_tree_size().map(|s| {
s + u32::try_from(
block.vtx.iter().map(|tx| tx.outputs.len()).sum::<usize>(),
)
.unwrap()
})
}),
block
.chain_metadata
.as_ref()
.map(|bcm| bcm.orchard_commitment_tree_size)
.or_else(|| {
bm.orchard_tree_size().map(|s| {
s + u32::try_from(
block.vtx.iter().map(|tx| tx.actions.len()).sum::<usize>(),
)
.unwrap()
})
}),
)
});
Expand Down Expand Up @@ -415,7 +429,7 @@ where
.map(|out| (out.account(), *out.nf()))
}));

prior_block_metadata = Some(*scanned_block.metadata());
prior_block_metadata = Some(scanned_block.to_block_metadata());
scanned_blocks.push(scanned_block);

Ok(())
Expand Down
Loading

0 comments on commit 7e1c1c2

Please sign in to comment.