Skip to content

Commit

Permalink
zcash_client_sqlite: Ensure "any_spendable" check is done per-pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Mar 10, 2024
1 parent e1785cc commit 1f72ea9
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,28 +945,6 @@ pub(crate) fn get_wallet_summary<P: consensus::Parameters>(
.or(sapling_scan_progress)
.or(orchard_scan_progress);

// If the shard containing the summary height contains any unscanned ranges that start below or
// including that height, none of our balance is currently spendable.
#[tracing::instrument(skip_all)]
fn is_any_spendable(
conn: &rusqlite::Connection,
summary_height: BlockHeight,
) -> Result<bool, SqliteClientError> {
conn.query_row(
"SELECT NOT EXISTS(
SELECT 1 FROM v_sapling_shard_unscanned_ranges
WHERE :summary_height
BETWEEN subtree_start_height
AND IFNULL(subtree_end_height, :summary_height)
AND block_range_start <= :summary_height
)",
named_params![":summary_height": u32::from(summary_height)],
|row| row.get::<_, bool>(0),
)
.map_err(|e| e.into())
}
let any_spendable = is_any_spendable(tx, summary_height)?;

let mut stmt_accounts = tx.prepare_cached("SELECT id FROM accounts")?;
let mut account_balances = stmt_accounts
.query([])?
Expand All @@ -978,7 +956,6 @@ pub(crate) fn get_wallet_summary<P: consensus::Parameters>(
fn count_notes<F>(
tx: &rusqlite::Transaction,
summary_height: BlockHeight,
any_spendable: bool,
account_balances: &mut HashMap<AccountId, AccountBalance>,
table_prefix: &'static str,
with_pool_balance: F,
Expand All @@ -991,6 +968,31 @@ pub(crate) fn get_wallet_summary<P: consensus::Parameters>(
NonNegativeAmount,
) -> Result<(), SqliteClientError>,
{
// If the shard containing the summary height contains any unscanned ranges that start below or
// including that height, none of our balance is currently spendable.
#[tracing::instrument(skip_all)]
fn is_any_spendable(
conn: &rusqlite::Connection,
summary_height: BlockHeight,
table_prefix: &'static str,
) -> Result<bool, SqliteClientError> {
conn.query_row(
&format!(
"SELECT NOT EXISTS(
SELECT 1 FROM v_{table_prefix}_shard_unscanned_ranges
WHERE :summary_height
BETWEEN subtree_start_height
AND IFNULL(subtree_end_height, :summary_height)
AND block_range_start <= :summary_height
)"
),
named_params![":summary_height": u32::from(summary_height)],
|row| row.get::<_, bool>(0),
)
.map_err(|e| e.into())
}

let any_spendable = is_any_spendable(tx, summary_height, table_prefix)?;
let mut stmt_select_notes = tx.prepare_cached(&format!(
"SELECT n.account_id, n.value, n.is_change, scan_state.max_priority, t.block
FROM {table_prefix}_received_notes n
Expand Down Expand Up @@ -1073,7 +1075,6 @@ pub(crate) fn get_wallet_summary<P: consensus::Parameters>(
count_notes(
tx,
summary_height,
any_spendable,
&mut account_balances,
ORCHARD_TABLES_PREFIX,
|balances, spendable_value, change_pending_confirmation, value_pending_spendability| {
Expand All @@ -1091,7 +1092,6 @@ pub(crate) fn get_wallet_summary<P: consensus::Parameters>(
count_notes(
tx,
summary_height,
any_spendable,
&mut account_balances,
SAPLING_TABLES_PREFIX,
|balances, spendable_value, change_pending_confirmation, value_pending_spendability| {
Expand Down

0 comments on commit 1f72ea9

Please sign in to comment.