Skip to content

Commit

Permalink
zcash_client_sqlite: Track Orchard commitment tree sizes in TestState
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Mar 9, 2024
1 parent dd8808f commit ef5dba4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 13 deletions.
1 change: 1 addition & 0 deletions zcash_client_sqlite/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ mod tests {
AddressType::DefaultExternal,
NonNegativeAmount::const_from_u64(8),
2,
2,
);
st.generate_next_block(
&dfvk,
Expand Down
49 changes: 42 additions & 7 deletions zcash_client_sqlite/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub(crate) struct CachedBlock {
height: BlockHeight,
hash: BlockHash,
sapling_end_size: u32,
orchard_end_size: u32,
}

impl CachedBlock {
Expand All @@ -178,14 +179,21 @@ impl CachedBlock {
height: sapling_activation_height,
hash: BlockHash([0; 32]),
sapling_end_size: 0,
orchard_end_size: 0,
}
}

fn at(height: BlockHeight, hash: BlockHash, sapling_tree_size: u32) -> Self {
fn at(
height: BlockHeight,
hash: BlockHash,
sapling_tree_size: u32,
orchard_tree_size: u32,
) -> Self {
Self {
height,
hash,
sapling_end_size: sapling_tree_size,
orchard_end_size: orchard_tree_size,
}
}

Expand All @@ -196,6 +204,8 @@ impl CachedBlock {
hash: cb.hash(),
sapling_end_size: self.sapling_end_size
+ cb.vtx.iter().map(|tx| tx.outputs.len() as u32).sum::<u32>(),
orchard_end_size: self.orchard_end_size
+ cb.vtx.iter().map(|tx| tx.actions.len() as u32).sum::<u32>(),
}
}
}
Expand Down Expand Up @@ -249,6 +259,7 @@ where
req,
value,
cached_block.sapling_end_size,
cached_block.orchard_end_size,
);
assert!(self.latest_cached_block.is_some());

Expand All @@ -268,6 +279,7 @@ where
req: AddressType,
value: NonNegativeAmount,
initial_sapling_tree_size: u32,
initial_orchard_tree_size: u32,
) -> (Cache::InsertResult, Fvk::Nullifier) {

Check failure on line 283 in zcash_client_sqlite/src/testing.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_sqlite/src/testing.rs:274:5 | 274 | / pub(crate) fn generate_block_at<Fvk: TestFvk>( 275 | | &mut self, 276 | | height: BlockHeight, 277 | | prev_hash: BlockHash, ... | 282 | | initial_orchard_tree_size: u32, 283 | | ) -> (Cache::InsertResult, Fvk::Nullifier) { | |______________________________________________^ | = 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
let (cb, nf) = fake_compact_block(
&self.network(),
Expand All @@ -277,11 +289,18 @@ where
req,
value,
initial_sapling_tree_size,
initial_orchard_tree_size,
);
let res = self.cache.insert(&cb);

self.latest_cached_block = Some(
CachedBlock::at(height - 1, cb.hash(), initial_sapling_tree_size).roll_forward(&cb),
CachedBlock::at(
height - 1,
cb.hash(),
initial_sapling_tree_size,
initial_orchard_tree_size,
)
.roll_forward(&cb),
);

(res, nf)
Expand Down Expand Up @@ -311,6 +330,7 @@ where
to.into(),
value,
cached_block.sapling_end_size,
cached_block.orchard_end_size,
);
let res = self.cache.insert(&cb);

Expand Down Expand Up @@ -361,7 +381,7 @@ where
tx_index,
tx,
cached_block.sapling_end_size,
0,
cached_block.orchard_end_size,
);
let res = self.cache.insert(&cb);

Expand Down Expand Up @@ -424,10 +444,12 @@ where
self.cache
.block_source()
.with_blocks::<_, Infallible>(None, None, |block: CompactBlock| {
let chain_metadata = block.chain_metadata.unwrap();
self.latest_cached_block = Some(CachedBlock::at(

Check warning on line 448 in zcash_client_sqlite/src/testing.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/testing.rs#L447-L448

Added lines #L447 - L448 were not covered by tests
BlockHeight::from_u32(block.height.try_into().unwrap()),
BlockHash::from_slice(block.hash.as_slice()),
block.chain_metadata.unwrap().sapling_commitment_tree_size,
chain_metadata.sapling_commitment_tree_size,
chain_metadata.orchard_commitment_tree_size,

Check warning on line 452 in zcash_client_sqlite/src/testing.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/testing.rs#L451-L452

Added lines #L451 - L452 were not covered by tests
));
Ok(())
})
Expand Down Expand Up @@ -1103,6 +1125,7 @@ fn fake_compact_block<P: consensus::Parameters, Fvk: TestFvk>(
req: AddressType,
value: NonNegativeAmount,
initial_sapling_tree_size: u32,
initial_orchard_tree_size: u32,
) -> (CompactBlock, Fvk::Nullifier) {

Check failure on line 1129 in zcash_client_sqlite/src/testing.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_sqlite/src/testing.rs:1120:1 | 1120 | / fn fake_compact_block<P: consensus::Parameters, Fvk: TestFvk>( 1121 | | params: &P, 1122 | | height: BlockHeight, 1123 | | prev_hash: BlockHash, ... | 1128 | | initial_orchard_tree_size: u32, 1129 | | ) -> (CompactBlock, Fvk::Nullifier) { | |___________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
// Create a fake Note for the account
let mut rng = OsRng;
Expand All @@ -1119,8 +1142,13 @@ fn fake_compact_block<P: consensus::Parameters, Fvk: TestFvk>(
&mut rng,
);

let cb =
fake_compact_block_from_compact_tx(ctx, height, prev_hash, initial_sapling_tree_size, 0);
let cb = fake_compact_block_from_compact_tx(
ctx,
height,
prev_hash,
initial_sapling_tree_size,
initial_orchard_tree_size,
);
(cb, nf)
}

Expand Down Expand Up @@ -1177,6 +1205,7 @@ fn fake_compact_block_spending<P: consensus::Parameters, Fvk: TestFvk>(
to: Address,
value: NonNegativeAmount,
initial_sapling_tree_size: u32,
initial_orchard_tree_size: u32,
) -> CompactBlock {
let mut rng = OsRng;
let mut ctx = fake_compact_tx(&mut rng);
Expand Down Expand Up @@ -1254,7 +1283,13 @@ fn fake_compact_block_spending<P: consensus::Parameters, Fvk: TestFvk>(
}
}

fake_compact_block_from_compact_tx(ctx, height, prev_hash, initial_sapling_tree_size, 0)
fake_compact_block_from_compact_tx(
ctx,
height,
prev_hash,
initial_sapling_tree_size,
initial_orchard_tree_size,
)
}

fn fake_compact_block_from_compact_tx(
Expand Down
9 changes: 9 additions & 0 deletions zcash_client_sqlite/src/testing/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,13 @@ pub(crate) fn birthday_in_anchor_shard<T: ShieldedPoolTester>() {
u64::from(birthday.sapling_frontier().value().unwrap().position() + 1)
.try_into()
.unwrap();
#[cfg(feature = "orchard")]
let initial_orchard_tree_size =
u64::from(birthday.orchard_frontier().value().unwrap().position() + 1)
.try_into()
.unwrap();
#[cfg(not(feature = "orchard"))]
let initial_orchard_tree_size = 0;

// Generate 9 blocks that have no value for us, starting at the birthday height.
let not_our_key = T::sk_to_fvk(&T::sk(&[]));
Expand All @@ -1263,6 +1270,7 @@ pub(crate) fn birthday_in_anchor_shard<T: ShieldedPoolTester>() {
AddressType::DefaultExternal,
not_our_value,
initial_sapling_tree_size,
initial_orchard_tree_size,
);
for _ in 1..9 {
st.generate_next_block(&not_our_key, AddressType::DefaultExternal, not_our_value);
Expand Down Expand Up @@ -1341,6 +1349,7 @@ pub(crate) fn checkpoint_gaps<T: ShieldedPoolTester>() {
AddressType::DefaultExternal,
not_our_value,
st.latest_cached_block().unwrap().sapling_end_size,
st.latest_cached_block().unwrap().orchard_end_size,
);

// Scan the block
Expand Down
2 changes: 2 additions & 0 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +2645,7 @@ mod tests {
AddressType::DefaultExternal,
not_our_value,
17,
17,
);
st.scan_cached_blocks(end_height, 1);

Expand All @@ -2661,6 +2662,7 @@ mod tests {
AddressType::DefaultExternal,
not_our_value,
0,
0,
);
st.scan_cached_blocks(start_height, 1);

Expand Down
35 changes: 29 additions & 6 deletions zcash_client_sqlite/src/wallet/scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,9 @@ pub(crate) mod tests {
// We'll start inserting leaf notes 5 notes after the end of the third subtree, with a gap
// of 10 blocks. After `scan_cached_blocks`, the scan queue should have a requested scan
// range of 300..310 with `FoundNote` priority, 310..320 with `Scanned` priority.
// We set both Sapling and Orchard to the same initial tree size for simplicity.
let initial_sapling_tree_size = (0x1 << 16) * 3 + 5;
let initial_orchard_tree_size = (0x1 << 16) * 3 + 5;
let initial_height = sapling_activation_height + 310;

let value = NonNegativeAmount::const_from_u64(50000);
Expand All @@ -619,6 +621,7 @@ pub(crate) mod tests {
AddressType::DefaultExternal,
value,
initial_sapling_tree_size,
initial_orchard_tree_size,
);

for _ in 1..=10 {
Expand Down Expand Up @@ -921,16 +924,26 @@ pub(crate) mod tests {
assert_eq!(actual, expected);

// Now, scan the max scanned block.
let initial_sapling_tree_size =
u64::from(birthday.sapling_frontier().value().unwrap().position() + 1)
.try_into()
.unwrap();
#[cfg(feature = "orchard")]
let initial_orchard_tree_size =
u64::from(birthday.orchard_frontier().value().unwrap().position() + 1)
.try_into()
.unwrap();
#[cfg(not(feature = "orchard"))]
let initial_orchard_tree_size = 0;
st.generate_block_at(
max_scanned,
BlockHash([0u8; 32]),
&dfvk,
AddressType::DefaultExternal,
// 1235 notes into into the second shard
NonNegativeAmount::const_from_u64(10000),
u64::from(birthday.sapling_frontier().value().unwrap().position() + 1)
.try_into()
.unwrap(),
initial_sapling_tree_size,
initial_orchard_tree_size,
);
st.scan_cached_blocks(max_scanned, 1);

Expand Down Expand Up @@ -1041,15 +1054,25 @@ pub(crate) mod tests {
assert_eq!(actual, expected);

// Now, scan the max scanned block.
let initial_sapling_tree_size =
u64::from(birthday.sapling_frontier().value().unwrap().position() + 1)
.try_into()
.unwrap();
#[cfg(feature = "orchard")]
let initial_orchard_tree_size =
u64::from(birthday.orchard_frontier().value().unwrap().position() + 1)
.try_into()
.unwrap();
#[cfg(not(feature = "orchard"))]
let initial_orchard_tree_size = 0;
st.generate_block_at(
max_scanned,
BlockHash([0u8; 32]),
&dfvk,
AddressType::DefaultExternal,
NonNegativeAmount::const_from_u64(10000),
u64::from(birthday.sapling_frontier().value().unwrap().position() + 1)
.try_into()
.unwrap(),
initial_sapling_tree_size,
initial_orchard_tree_size,
);
st.scan_cached_blocks(max_scanned, 1);

Expand Down

0 comments on commit ef5dba4

Please sign in to comment.