zcash_client_sqlite: Deduplicate `WalletDb::get_sapling_nullifiers`

This commit is contained in:
Kris Nuttycombe 2024-03-07 12:39:27 -07:00
parent 7aacfc3bb0
commit d55edd8dd8
2 changed files with 18 additions and 31 deletions

View File

@ -408,10 +408,7 @@ impl<C: Borrow<rusqlite::Connection>, P: consensus::Parameters> WalletRead for W
&self,
query: NullifierQuery,
) -> Result<Vec<(AccountId, sapling::Nullifier)>, Self::Error> {
match query {
NullifierQuery::Unspent => wallet::sapling::get_sapling_nullifiers(self.conn.borrow()),
NullifierQuery::All => wallet::sapling::get_all_sapling_nullifiers(self.conn.borrow()),
}
wallet::sapling::get_sapling_nullifiers(self.conn.borrow(), query)
}
#[cfg(feature = "orchard")]

View File

@ -7,6 +7,7 @@ use std::rc::Rc;
use sapling::{self, Diversifier, Nullifier, Rseed};
use zcash_client_backend::{
data_api::NullifierQuery,
keys::UnifiedFullViewingKey,
wallet::{Note, ReceivedNote, WalletSaplingOutput},
DecryptedOutput, TransferType,
@ -323,36 +324,25 @@ pub(crate) fn select_spendable_sapling_notes<P: consensus::Parameters>(
/// - No transaction in which the note's nullifier appears has been observed as mined.
pub(crate) fn get_sapling_nullifiers(
conn: &Connection,
query: NullifierQuery,
) -> Result<Vec<(AccountId, Nullifier)>, SqliteClientError> {
// Get the nullifiers for the notes we are tracking
let mut stmt_fetch_nullifiers = conn.prepare(
"SELECT rn.id, rn.account_id, rn.nf, tx.block as block
let mut stmt_fetch_nullifiers = match query {
NullifierQuery::Unspent => conn.prepare(
"SELECT rn.id, rn.account_id, rn.nf
FROM sapling_received_notes rn
LEFT OUTER JOIN transactions tx
ON tx.id_tx = rn.spent
WHERE block IS NULL
WHERE tx.block IS NULL
AND nf IS NOT NULL",
)?;
let nullifiers = stmt_fetch_nullifiers.query_and_then([], |row| {
let account = AccountId(row.get(1)?);
let nf_bytes: Vec<u8> = row.get(2)?;
Ok::<_, rusqlite::Error>((account, sapling::Nullifier::from_slice(&nf_bytes).unwrap()))
})?;
let res: Vec<_> = nullifiers.collect::<Result<_, _>>()?;
Ok(res)
}
/// Returns the nullifiers for the notes that this wallet is tracking.
pub(crate) fn get_all_sapling_nullifiers(
conn: &Connection,
) -> Result<Vec<(AccountId, Nullifier)>, SqliteClientError> {
// Get the nullifiers for the notes we are tracking
let mut stmt_fetch_nullifiers = conn.prepare(
),
NullifierQuery::All => conn.prepare(
"SELECT rn.id, rn.account_id, rn.nf
FROM sapling_received_notes rn
WHERE nf IS NOT NULL",
)?;
),
}?;
let nullifiers = stmt_fetch_nullifiers.query_and_then([], |row| {
let account = AccountId(row.get(1)?);
let nf_bytes: Vec<u8> = row.get(2)?;