Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions zcash_client_sqlite/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! // At this point, the cache and scanned data are locally consistent (though not
//! // necessarily consistent with the latest chain tip - this would be discovered the
//! // next time this codepath is executed after new blocks are received).
//! scan_cached_blocks(&db_cache, &db_data);
//! scan_cached_blocks(&db_cache, &db_data, None);
//! ```

use protobuf::parse_from_bytes;
Expand Down Expand Up @@ -268,7 +268,7 @@ mod tests {
validate_combined_chain(db_cache, db_data).unwrap();

// Scan the cache
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Data-only chain should be valid
validate_combined_chain(db_cache, db_data).unwrap();
Expand All @@ -286,7 +286,7 @@ mod tests {
validate_combined_chain(db_cache, db_data).unwrap();

// Scan the cache again
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be default Parameter so it's not a breaking change?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the closest I could get to that. Rust doesn't do defaults in the same way swift or kotlin would. The alternative is a separate function but I'm leaning away from that since we're basically the only consumers of this. So it's safer to break things now than it will be next year :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and I think two functions just adds unnecessary complexity (one function for scanning is simpler)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, :( I totally agree on breaking things sooner than later


// Data-only chain should be valid
validate_combined_chain(db_cache, db_data).unwrap();
Expand Down Expand Up @@ -324,7 +324,7 @@ mod tests {
insert_into_cache(db_cache, &cb2);

// Scan the cache
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Data-only chain should be valid
validate_combined_chain(db_cache, db_data).unwrap();
Expand Down Expand Up @@ -389,7 +389,7 @@ mod tests {
insert_into_cache(db_cache, &cb2);

// Scan the cache
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Data-only chain should be valid
validate_combined_chain(db_cache, db_data).unwrap();
Expand Down Expand Up @@ -454,7 +454,7 @@ mod tests {
insert_into_cache(db_cache, &cb2);

// Scan the cache
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Account balance should reflect both received notes
assert_eq!(get_balance(db_data, 0).unwrap(), value + value2);
Expand Down
31 changes: 19 additions & 12 deletions zcash_client_sqlite/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ struct WitnessRow {
witness: IncrementalWitness<Node>,
}

/// Scans new blocks added to the cache for any transactions received by the tracked
/// accounts.
/// Scans at most `limit` new blocks added to the cache for any transactions received by
/// the tracked accounts.
///
/// This function will return without error after scanning at most `limit` new blocks, to
/// enable the caller to update their UI with scanning progress. Repeatedly calling this
/// function will process sequential ranges of blocks, and is equivalent to calling
/// `scan_cached_blocks` and passing `None` for the optional `limit` value.
///
/// This function pays attention only to cached blocks with heights greater than the
/// highest scanned block in `db_data`. Cached blocks with lower heights are not verified
Expand All @@ -57,6 +62,7 @@ struct WitnessRow {
pub fn scan_cached_blocks<P: AsRef<Path>, Q: AsRef<Path>>(
db_cache: P,
db_data: Q,
limit: Option<i32>,
) -> Result<(), Error> {
let cache = Connection::open(db_cache)?;
let data = Connection::open(db_data)?;
Expand All @@ -68,9 +74,10 @@ pub fn scan_cached_blocks<P: AsRef<Path>, Q: AsRef<Path>>(
})?;

Comment thread
gmale marked this conversation as resolved.
Outdated
// Fetch the CompactBlocks we need to scan
let mut stmt_blocks = cache
.prepare("SELECT height, data FROM compactblocks WHERE height > ? ORDER BY height ASC")?;
let rows = stmt_blocks.query_map(&[last_height], |row| {
let mut stmt_blocks = cache.prepare(
"SELECT height, data FROM compactblocks WHERE height > ? ORDER BY height ASC LIMIT ?",
)?;
let rows = stmt_blocks.query_map(&[last_height, limit.unwrap_or(i32::max_value())], |row| {
Ok(CompactBlockRow {
height: row.get(0)?,
data: row.get(1)?,
Expand Down Expand Up @@ -357,7 +364,7 @@ mod tests {
value,
);
insert_into_cache(db_cache, &cb1);
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();
assert_eq!(get_balance(db_data, 0).unwrap(), value);

// We cannot scan a block of height SAPLING_ACTIVATION_HEIGHT + 2 next
Expand All @@ -374,7 +381,7 @@ mod tests {
value,
);
insert_into_cache(db_cache, &cb3);
match scan_cached_blocks(db_cache, db_data) {
match scan_cached_blocks(db_cache, db_data, None) {
Ok(_) => panic!("Should have failed"),
Err(e) => assert_eq!(
e.to_string(),
Expand All @@ -388,7 +395,7 @@ mod tests {

// If we add a block of height SAPLING_ACTIVATION_HEIGHT + 1, we can now scan both
insert_into_cache(db_cache, &cb2);
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();
assert_eq!(
get_balance(db_data, 0).unwrap(),
Amount::from_u64(150_000).unwrap()
Expand Down Expand Up @@ -424,7 +431,7 @@ mod tests {
insert_into_cache(db_cache, &cb);

// Scan the cache
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Account balance should reflect the received note
assert_eq!(get_balance(db_data, 0).unwrap(), value);
Expand All @@ -435,7 +442,7 @@ mod tests {
insert_into_cache(db_cache, &cb2);

// Scan the cache again
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Account balance should reflect both received notes
assert_eq!(get_balance(db_data, 0).unwrap(), value + value2);
Expand Down Expand Up @@ -470,7 +477,7 @@ mod tests {
insert_into_cache(db_cache, &cb);

// Scan the cache
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Account balance should reflect the received note
assert_eq!(get_balance(db_data, 0).unwrap(), value);
Expand All @@ -492,7 +499,7 @@ mod tests {
);

// Scan the cache again
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Account balance should equal the change
assert_eq!(get_balance(db_data, 0).unwrap(), value - value2);
Expand Down
14 changes: 7 additions & 7 deletions zcash_client_sqlite/src/transact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ mod tests {
value,
);
insert_into_cache(db_cache, &cb);
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Verified balance matches total balance
assert_eq!(get_balance(db_data, 0).unwrap(), value);
Expand All @@ -476,7 +476,7 @@ mod tests {
value,
);
insert_into_cache(db_cache, &cb);
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Verified balance does not include the second note
assert_eq!(get_balance(db_data, 0).unwrap(), value + value);
Expand Down Expand Up @@ -512,7 +512,7 @@ mod tests {
);
insert_into_cache(db_cache, &cb);
}
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Second spend still fails
match create_to_address(
Expand All @@ -539,7 +539,7 @@ mod tests {
value,
);
insert_into_cache(db_cache, &cb);
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Second spend should now succeed
create_to_address(
Expand Down Expand Up @@ -578,7 +578,7 @@ mod tests {
value,
);
insert_into_cache(db_cache, &cb);
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();
assert_eq!(get_balance(db_data, 0).unwrap(), value);

// Send some of the funds to another address
Expand Down Expand Up @@ -623,7 +623,7 @@ mod tests {
);
insert_into_cache(db_cache, &cb);
}
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Second spend still fails
match create_to_address(
Expand All @@ -650,7 +650,7 @@ mod tests {
value,
);
insert_into_cache(db_cache, &cb);
scan_cached_blocks(db_cache, db_data).unwrap();
scan_cached_blocks(db_cache, db_data, None).unwrap();

// Second spend should now succeed
create_to_address(
Expand Down