zcash_client_sqlite: Make note selection queries consistent between Sapling and Orchard.

This commit is contained in:
Kris Nuttycombe 2024-03-10 15:07:54 -06:00 committed by Jack Grigg
parent 6086774b9b
commit cad174c1d7
2 changed files with 23 additions and 8 deletions

View File

@ -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![
@ -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

View File

@ -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(),
@ -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
@ -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)",
)?;