Skip to content

Commit

Permalink
Merge branch 'wallet/sqlite_cached_statements'
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Jun 19, 2023
2 parents d2f105e + 48434bb commit d5166b5
Show file tree
Hide file tree
Showing 8 changed files with 968 additions and 1,645 deletions.
2 changes: 2 additions & 0 deletions zcash_client_sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ uuid = "1.1"

[dev-dependencies]
assert_matches = "1.5"
incrementalmerkletree = { version = "0.4", features = ["legacy-api", "test-dependencies"] }
proptest = "1.0.0"
rand_core = "0.6"
regex = "1.4"
Expand All @@ -59,6 +60,7 @@ zcash_address = { version = "0.3", path = "../components/zcash_address", feature
[features]
mainnet = []
test-dependencies = [
"incrementalmerkletree/test-dependencies",
"zcash_primitives/test-dependencies",
"zcash_client_backend/test-dependencies",
]
Expand Down
97 changes: 55 additions & 42 deletions zcash_client_sqlite/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ mod tests {
init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
let (dfvk, _taddr) = init_test_accounts_table(&mut db_data);

// Empty chain should return None
assert_matches!(db_data.get_max_height_hash(), Ok(None));
Expand Down Expand Up @@ -328,8 +328,7 @@ mod tests {
assert_matches!(validate_chain_result, Ok(()));

// Scan the cache
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Data-only chain should be valid
validate_chain(&db_cache, db_data.get_max_height_hash().unwrap(), None).unwrap();
Expand All @@ -348,7 +347,7 @@ mod tests {
validate_chain(&db_cache, db_data.get_max_height_hash().unwrap(), None).unwrap();

// Scan the cache again
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Data-only chain should be valid
validate_chain(&db_cache, db_data.get_max_height_hash().unwrap(), None).unwrap();
Expand All @@ -365,7 +364,7 @@ mod tests {
init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
let (dfvk, _taddr) = init_test_accounts_table(&mut db_data);

// Create some fake CompactBlocks
let (cb, _) = fake_compact_block(
Expand All @@ -386,8 +385,7 @@ mod tests {
insert_into_cache(&db_cache, &cb2);

// Scan the cache
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Data-only chain should be valid
validate_chain(&db_cache, db_data.get_max_height_hash().unwrap(), None).unwrap();
Expand Down Expand Up @@ -427,7 +425,7 @@ mod tests {
init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
let (dfvk, _taddr) = init_test_accounts_table(&mut db_data);

// Create some fake CompactBlocks
let (cb, _) = fake_compact_block(
Expand All @@ -448,8 +446,7 @@ mod tests {
insert_into_cache(&db_cache, &cb2);

// Scan the cache
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Data-only chain should be valid
validate_chain(&db_cache, db_data.get_max_height_hash().unwrap(), None).unwrap();
Expand Down Expand Up @@ -489,11 +486,11 @@ mod tests {
init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
let (dfvk, _taddr) = init_test_accounts_table(&mut db_data);

// Account balance should be zero
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
Amount::zero()
);

Expand All @@ -519,36 +516,46 @@ mod tests {
insert_into_cache(&db_cache, &cb2);

// Scan the cache
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Account balance should reflect both received notes
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
(value + value2).unwrap()
);

// "Rewind" to height of last scanned block
truncate_to_height(&db_data, sapling_activation_height() + 1).unwrap();
db_data
.transactionally(|wdb| {
truncate_to_height(&wdb.conn.0, &wdb.params, sapling_activation_height() + 1)
})
.unwrap();

// Account balance should be unaltered
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
(value + value2).unwrap()
);

// Rewind so that one block is dropped
truncate_to_height(&db_data, sapling_activation_height()).unwrap();
db_data
.transactionally(|wdb| {
truncate_to_height(&wdb.conn.0, &wdb.params, sapling_activation_height())
})
.unwrap();

// Account balance should only contain the first received note
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
assert_eq!(
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
value
);

// Scan the cache again
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Account balance should again reflect both received notes
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
(value + value2).unwrap()
);
}
Expand All @@ -564,7 +571,7 @@ mod tests {
init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
let (dfvk, _taddr) = init_test_accounts_table(&mut db_data);

// Create a block with height SAPLING_ACTIVATION_HEIGHT
let value = Amount::from_u64(50000).unwrap();
Expand All @@ -576,9 +583,11 @@ mod tests {
value,
);
insert_into_cache(&db_cache, &cb1);
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();
assert_eq!(
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
value
);

// We cannot scan a block of height SAPLING_ACTIVATION_HEIGHT + 2 next
let (cb2, _) = fake_compact_block(
Expand All @@ -596,7 +605,7 @@ mod tests {
value,
);
insert_into_cache(&db_cache, &cb3);
match scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None) {
match scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None) {
Err(Error::Chain(e)) => {
assert_matches!(
e.cause(),
Expand All @@ -609,9 +618,9 @@ 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(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
Amount::from_u64(150_000).unwrap()
);
}
Expand All @@ -627,11 +636,11 @@ mod tests {
init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
let (dfvk, _taddr) = init_test_accounts_table(&mut db_data);

// Account balance should be zero
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
Amount::zero()
);

Expand All @@ -647,11 +656,13 @@ mod tests {
insert_into_cache(&db_cache, &cb);

// Scan the cache
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Account balance should reflect the received note
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
assert_eq!(
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
value
);

// Create a second fake CompactBlock sending more value to the address
let value2 = Amount::from_u64(7).unwrap();
Expand All @@ -665,11 +676,11 @@ mod tests {
insert_into_cache(&db_cache, &cb2);

// Scan the cache again
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Account balance should reflect both received notes
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
(value + value2).unwrap()
);
}
Expand All @@ -685,11 +696,11 @@ mod tests {
init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
let (dfvk, _taddr) = init_test_accounts_table(&mut db_data);

// Account balance should be zero
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
Amount::zero()
);

Expand All @@ -705,11 +716,13 @@ mod tests {
insert_into_cache(&db_cache, &cb);

// Scan the cache
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Account balance should reflect the received note
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
assert_eq!(
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
value
);

// Create a second fake CompactBlock spending value from the address
let extsk2 = ExtendedSpendingKey::master(&[0]);
Expand All @@ -728,11 +741,11 @@ mod tests {
);

// Scan the cache again
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_data, None).unwrap();

// Account balance should equal the change
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
get_balance(&db_data.conn, AccountId::from(0)).unwrap(),
(value - value2).unwrap()
);
}
Expand Down
Loading

0 comments on commit d5166b5

Please sign in to comment.