Skip to content

Commit

Permalink
zcash_client_sqlite: Make note selection queries consistent between S…
Browse files Browse the repository at this point in the history
…apling and Orchard.
  • Loading branch information
nuttycom committed Mar 10, 2024
1 parent a017717 commit 6f0ccb1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
8 changes: 7 additions & 1 deletion zcash_client_sqlite/src/wallet/orchard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ pub(crate) fn get_spendable_orchard_note<P: consensus::Parameters>(
INNER JOIN transactions ON transactions.id_tx = orchard_received_notes.tx
WHERE txid = :txid
AND action_index = :action_index
AND accounts.ufvk IS NOT NULL
AND recipient_key_scope IS NOT NULL
AND nf IS NOT NULL
AND spent IS NULL",
named_params![
Expand Down Expand Up @@ -289,10 +291,14 @@ pub(crate) fn select_spendable_orchard_notes<P: consensus::Parameters>(
OVER (PARTITION BY orchard_received_notes.account_id, spent ORDER BY orchard_received_notes.id) AS so_far,
accounts.ufvk as ufvk, recipient_key_scope
FROM orchard_received_notes
INNER JOIN accounts on accounts.id = orchard_received_notes.account_id
INNER JOIN accounts
ON accounts.id = orchard_received_notes.account_id
INNER JOIN transactions
ON transactions.id_tx = orchard_received_notes.tx
WHERE orchard_received_notes.account_id = :account
AND accounts.ufvk IS NOT NULL
AND recipient_key_scope IS NOT NULL
AND nf IS NOT NULL
AND commitment_tree_position IS NOT NULL
AND spent IS NULL
AND transactions.block <= :anchor_height
Expand Down
23 changes: 16 additions & 7 deletions zcash_client_sqlite/src/wallet/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ pub(crate) fn get_spendable_sapling_note<P: consensus::Parameters>(
diversifier, value, rcm, commitment_tree_position,
accounts.ufvk, recipient_key_scope
FROM sapling_received_notes
INNER JOIN accounts on accounts.id = sapling_received_notes.account_id
INNER JOIN accounts ON accounts.id = sapling_received_notes.account_id
INNER JOIN transactions ON transactions.id_tx = sapling_received_notes.tx
WHERE txid = :txid
AND output_index = :output_index
AND accounts.ufvk IS NOT NULL
AND recipient_key_scope IS NOT NULL
AND output_index = :output_index
AND nf IS NOT NULL
AND commitment_tree_position IS NOT NULL
AND spent IS NULL",
named_params![
":txid": txid.as_ref(),
Expand Down Expand Up @@ -284,17 +286,20 @@ pub(crate) fn select_spendable_sapling_notes<P: consensus::Parameters>(
let mut stmt_select_notes = conn.prepare_cached(
"WITH eligible AS (
SELECT
sapling_received_notes.id AS id, txid, output_index, diversifier, value, rcm, commitment_tree_position,
sapling_received_notes.id AS id, txid, output_index,
diversifier, value, rcm, commitment_tree_position,
SUM(value)
OVER (PARTITION BY sapling_received_notes.account_id, spent ORDER BY sapling_received_notes.id) AS so_far,
accounts.ufvk as ufvk, recipient_key_scope
FROM sapling_received_notes
INNER JOIN accounts on accounts.id = sapling_received_notes.account_id
INNER JOIN accounts
ON accounts.id = sapling_received_notes.account_id
INNER JOIN transactions
ON transactions.id_tx = sapling_received_notes.tx
WHERE sapling_received_notes.account_id = :account
AND ufvk IS NOT NULL
AND accounts.ufvk IS NOT NULL
AND recipient_key_scope IS NOT NULL
AND nf IS NOT NULL
AND commitment_tree_position IS NOT NULL
AND spent IS NULL
AND transactions.block <= :anchor_height
Expand All @@ -310,10 +315,14 @@ pub(crate) fn select_spendable_sapling_notes<P: consensus::Parameters>(
AND unscanned.block_range_end > :wallet_birthday
)
)
SELECT id, txid, output_index, diversifier, value, rcm, commitment_tree_position, ufvk, recipient_key_scope
SELECT id, txid, output_index,
diversifier, value, rcm, commitment_tree_position,
ufvk, recipient_key_scope
FROM eligible WHERE so_far < :target_value
UNION
SELECT id, txid, output_index, diversifier, value, rcm, commitment_tree_position, ufvk, recipient_key_scope
SELECT id, txid, output_index,
diversifier, value, rcm, commitment_tree_position,
ufvk, recipient_key_scope
FROM (SELECT * from eligible WHERE so_far >= :target_value LIMIT 1)",
)?;

Expand Down

0 comments on commit 6f0ccb1

Please sign in to comment.