zcash_client_sqlite: Make transaction retrieval depend upon presence of raw tx data.

This commit is contained in:
Kris Nuttycombe 2024-08-09 16:55:35 -06:00
parent 0bfa3d35bc
commit 5a1645ac76
3 changed files with 29 additions and 27 deletions

View File

@ -1405,6 +1405,16 @@ pub struct SentTransaction<'a, AccountId> {
impl<'a, AccountId> SentTransaction<'a, AccountId> {
/// Constructs a new [`SentTransaction`] from its constituent parts.
///
/// ### Parameters
/// - `tx`: the raw transaction data
/// - `created`: the system time at which the transaction was created
/// - `target_height`: the target height that was used in the construction of the transaction
/// - `account`: the account that spent funds in creation of the transaction
/// - `outputs`: the outputs created by the transaction, including those sent to external
/// recipients which may not otherwise be recoverable
/// - `fee_amount`: the fee value paid by the transaction
/// - `utxos_spent`: the UTXOs controlled by the wallet that were spent in this transaction
pub fn new(
tx: &'a Transaction,
created: time::OffsetDateTime,

View File

@ -119,7 +119,7 @@ pub mod error;
pub mod wallet;
use wallet::{
commitment_tree::{self, put_shard_roots},
notify_tx_retrieved, SubtreeScanProgress, TxQueryType,
notify_tx_retrieved, SubtreeScanProgress,
};
#[cfg(feature = "transparent-inputs")]
@ -800,12 +800,7 @@ impl<P: consensus::Parameters> WalletWrite for WalletDb<rusqlite::Connection, P>
for tx in block.transactions() {
let tx_row = wallet::put_tx_meta(wdb.conn.0, tx, block.height())?;
wallet::queue_tx_retrieval(
wdb.conn.0,
std::iter::once(tx.txid()),
TxQueryType::Enhancement,
None,
)?;
wallet::queue_tx_retrieval(wdb.conn.0, std::iter::once(tx.txid()), None)?;
// Mark notes as spent and remove them from the scanning cache
for spend in tx.sapling_spends() {
@ -1518,7 +1513,6 @@ impl<P: consensus::Parameters> WalletWrite for WalletDb<rusqlite::Connection, P>
wallet::queue_tx_retrieval(
wdb.conn.0,
b.vin.iter().map(|txin| *txin.prevout.txid()),
TxQueryType::Enhancement,
Some(tx_ref)
)?;
}
@ -1531,7 +1525,6 @@ impl<P: consensus::Parameters> WalletWrite for WalletDb<rusqlite::Connection, P>
wallet::queue_tx_retrieval(
wdb.conn.0,
std::iter::once(d_tx.tx().txid()),
TxQueryType::Status,
None
)?;
}

View File

@ -1978,12 +1978,7 @@ pub(crate) fn store_transaction_to_be_sent<P: consensus::Parameters>(
// at present for fully transparent transactions, because any transaction with a shielded
// component will be detected via ordinary chain scanning and/or nullifier checking.
if !detectable_via_scanning {
queue_tx_retrieval(
wdb.conn.0,
std::iter::once(sent_tx.tx().txid()),
TxQueryType::Status,
None,
)?;
queue_tx_retrieval(wdb.conn.0, std::iter::once(sent_tx.tx().txid()), None)?;
}
Ok(())
@ -2409,31 +2404,35 @@ impl TxQueryType {
pub(crate) fn queue_tx_retrieval(
conn: &rusqlite::Transaction<'_>,
txids: impl Iterator<Item = TxId>,
query_type: TxQueryType,
dependent_tx_ref: Option<TxRef>,
) -> Result<(), SqliteClientError> {
// Add an entry to the transaction retrieval queue if it would not be redundant.
let mut stmt_insert_tx = conn.prepare_cached(
"INSERT INTO tx_retrieval_queue (txid, query_type, dependent_transaction_id)
SELECT :txid, :query_type, :dependent_transaction_id
-- do not queue enhancement requests if we already have the raw transaction
WHERE :query_type <> :enhancement_type OR NOT EXISTS (
SELECT 1 FROM transactions
WHERE txid = :txid
AND raw IS NOT NULL
)
-- if there is already a status request, we can upgrade it to an enhancement request
SELECT
:txid,
IIF(
EXISTS (SELECT 1 FROM transactions WHERE txid = :txid AND raw IS NOT NULL),
:status_type,
:enhancement_type
),
:dependent_transaction_id
ON CONFLICT (txid) DO UPDATE
SET query_type = MAX(:query_type, query_type),
SET query_type =
IIF(
EXISTS (SELECT 1 FROM transactions WHERE txid = :txid AND raw IS NOT NULL),
:status_type,
:enhancement_type
),
dependent_transaction_id = IFNULL(:dependent_transaction_id, dependent_transaction_id)",
)?;
for txid in txids {
stmt_insert_tx.execute(named_params! {
":txid": txid.as_ref(),
":query_type": query_type.code(),
":dependent_transaction_id": dependent_tx_ref.map(|r| r.0),
":status_type": TxQueryType::Status.code(),
":enhancement_type": TxQueryType::Enhancement.code(),
":dependent_transaction_id": dependent_tx_ref.map(|r| r.0),
})?;
}