|
| 1 | +use rusqlite::named_params; |
| 2 | +use transparent::bundle::TxOut; |
| 3 | +use zaino_state::{FetchServiceSubscriber, MempoolKey}; |
| 4 | +use zcash_client_backend::data_api::WalletRead; |
| 5 | +use zcash_client_sqlite::error::SqliteClientError; |
| 6 | +use zcash_primitives::transaction::Transaction; |
| 7 | +use zcash_protocol::{consensus::BlockHeight, value::Zatoshis}; |
| 8 | + |
| 9 | +use crate::components::database::DbConnection; |
| 10 | + |
| 11 | +/// Equivalent to `CTransaction::GetValueOut` in `zcashd`. |
| 12 | +pub(super) fn wtx_get_value_out(tx: &Transaction) -> Option<Zatoshis> { |
| 13 | + tx.transparent_bundle() |
| 14 | + .map(|bundle| bundle.vout.iter().map(|txout| txout.value).sum()) |
| 15 | + .unwrap_or(Some(Zatoshis::ZERO)) |
| 16 | +} |
| 17 | + |
| 18 | +/// Equivalent to [`CWalletTx::GetDebit`] in `zcashd`. |
| 19 | +/// |
| 20 | +/// [`CWalletTx::GetDebit`]: https://github.com/zcash/zcash/blob/2352fbc1ed650ac4369006bea11f7f20ee046b84/src/wallet/wallet.cpp#L4822 |
| 21 | +pub(super) fn wtx_get_debit( |
| 22 | + wallet: &DbConnection, |
| 23 | + tx: &Transaction, |
| 24 | + is_mine: impl Fn(&DbConnection, &TxOut) -> bool, |
| 25 | +) -> Option<Zatoshis> { |
| 26 | + match tx.transparent_bundle() { |
| 27 | + None => Some(Zatoshis::ZERO), |
| 28 | + Some(bundle) if bundle.vin.is_empty() => Some(Zatoshis::ZERO), |
| 29 | + // Equivalent to `CWallet::GetDebit(CTransaction)` in `zcashd`. |
| 30 | + Some(bundle) => bundle |
| 31 | + .vin |
| 32 | + .iter() |
| 33 | + .map(|txin| { |
| 34 | + // Equivalent to `CWallet::GetDebit(CTxIn)` in `zcashd`. |
| 35 | + wallet |
| 36 | + .get_transaction(*txin.prevout.txid()) |
| 37 | + .ok() |
| 38 | + .flatten() |
| 39 | + .as_ref() |
| 40 | + .and_then(|prev_tx| prev_tx.transparent_bundle()) |
| 41 | + .and_then(|bundle| bundle.vout.get(txin.prevout.n() as usize)) |
| 42 | + .filter(|txout| is_mine(wallet, txout)) |
| 43 | + .map(|txout| txout.value) |
| 44 | + .unwrap_or(Zatoshis::ZERO) |
| 45 | + }) |
| 46 | + .sum::<Option<Zatoshis>>(), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Equivalent to [`CWalletTx::GetCredit`] in `zcashd`. |
| 51 | +/// |
| 52 | +/// [`CWalletTx::GetCredit`]: https://github.com/zcash/zcash/blob/2352fbc1ed650ac4369006bea11f7f20ee046b84/src/wallet/wallet.cpp#L4853 |
| 53 | +pub(super) async fn wtx_get_credit( |
| 54 | + wallet: &DbConnection, |
| 55 | + chain: &FetchServiceSubscriber, |
| 56 | + tx: &Transaction, |
| 57 | + as_of_height: Option<BlockHeight>, |
| 58 | + is_mine: impl Fn(&DbConnection, &TxOut) -> bool, |
| 59 | +) -> Result<Option<Zatoshis>, SqliteClientError> { |
| 60 | + Ok(match tx.transparent_bundle() { |
| 61 | + None => Some(Zatoshis::ZERO), |
| 62 | + // Must wait until coinbase is safely deep enough in the chain before valuing it. |
| 63 | + Some(bundle) |
| 64 | + if bundle.is_coinbase() |
| 65 | + && wtx_get_blocks_to_maturity(wallet, chain, tx, as_of_height).await? > 0 => |
| 66 | + { |
| 67 | + Some(Zatoshis::ZERO) |
| 68 | + } |
| 69 | + // Equivalent to `CWallet::GetCredit(CTransaction)` in `zcashd`. |
| 70 | + Some(bundle) => bundle |
| 71 | + .vout |
| 72 | + .iter() |
| 73 | + .map(|txout| { |
| 74 | + // Equivalent to `CWallet::GetCredit(CTxOut)` in `zcashd`. |
| 75 | + if is_mine(wallet, txout) { |
| 76 | + txout.value |
| 77 | + } else { |
| 78 | + Zatoshis::ZERO |
| 79 | + } |
| 80 | + }) |
| 81 | + .sum::<Option<Zatoshis>>(), |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +/// Equivalent to [`CWalletTx::IsFromMe`] in `zcashd`. |
| 86 | +/// |
| 87 | +/// [`CWalletTx::IsFromMe`]: https://github.com/zcash/zcash/blob/2352fbc1ed650ac4369006bea11f7f20ee046b84/src/wallet/wallet.cpp#L4967 |
| 88 | +pub(super) fn wtx_is_from_me( |
| 89 | + wallet: &DbConnection, |
| 90 | + tx: &Transaction, |
| 91 | + is_mine: impl Fn(&DbConnection, &TxOut) -> bool, |
| 92 | +) -> Result<bool, SqliteClientError> { |
| 93 | + if wtx_get_debit(wallet, tx, is_mine).ok_or_else(|| { |
| 94 | + SqliteClientError::BalanceError(zcash_protocol::value::BalanceError::Overflow) |
| 95 | + })? > Zatoshis::ZERO |
| 96 | + { |
| 97 | + return Ok(true); |
| 98 | + } |
| 99 | + |
| 100 | + wallet.with_raw(|conn| { |
| 101 | + if let Some(bundle) = tx.sapling_bundle() { |
| 102 | + let mut stmt_note_exists = conn.prepare( |
| 103 | + "SELECT EXISTS( |
| 104 | + SELECT 1 |
| 105 | + FROM sapling_received_notes |
| 106 | + WHERE nf = :nf |
| 107 | + )", |
| 108 | + )?; |
| 109 | + |
| 110 | + for spend in bundle.shielded_spends() { |
| 111 | + if stmt_note_exists |
| 112 | + .query_row(named_params! {":nf": spend.nullifier().0}, |row| row.get(0))? |
| 113 | + { |
| 114 | + return Ok(true); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // TODO: Fix bug in `zcashd` where we forgot to add Orchard here. |
| 120 | + if let Some(bundle) = tx.orchard_bundle() { |
| 121 | + let mut stmt_note_exists = conn.prepare( |
| 122 | + "SELECT EXISTS( |
| 123 | + SELECT 1 |
| 124 | + FROM orchard_received_notes |
| 125 | + WHERE nf = :nf |
| 126 | + )", |
| 127 | + )?; |
| 128 | + |
| 129 | + for action in bundle.actions() { |
| 130 | + if stmt_note_exists.query_row( |
| 131 | + named_params! {":nf": action.nullifier().to_bytes()}, |
| 132 | + |row| row.get(0), |
| 133 | + )? { |
| 134 | + return Ok(true); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + Ok(false) |
| 140 | + }) |
| 141 | +} |
| 142 | + |
| 143 | +/// Equivalent to [`CMerkleTx::GetBlocksToMaturity`] in `zcashd`. |
| 144 | +/// |
| 145 | +/// [`CMerkleTx::GetBlocksToMaturity`]: https://github.com/zcash/zcash/blob/2352fbc1ed650ac4369006bea11f7f20ee046b84/src/wallet/wallet.cpp#L6915 |
| 146 | +async fn wtx_get_blocks_to_maturity( |
| 147 | + wallet: &DbConnection, |
| 148 | + chain: &FetchServiceSubscriber, |
| 149 | + tx: &Transaction, |
| 150 | + as_of_height: Option<BlockHeight>, |
| 151 | +) -> Result<u32, SqliteClientError> { |
| 152 | + Ok( |
| 153 | + if tx.transparent_bundle().map_or(false, |b| b.is_coinbase()) { |
| 154 | + if let Some(depth) = |
| 155 | + wtx_get_depth_in_main_chain(wallet, chain, tx, as_of_height).await? |
| 156 | + { |
| 157 | + (COINBASE_MATURITY + 1).saturating_sub(depth) |
| 158 | + } else { |
| 159 | + // TODO: Confirm this is what `zcashd` computes for an orphaned coinbase. |
| 160 | + COINBASE_MATURITY + 2 |
| 161 | + } |
| 162 | + } else { |
| 163 | + 0 |
| 164 | + }, |
| 165 | + ) |
| 166 | +} |
| 167 | + |
| 168 | +/// Returns depth of transaction in blockchain. |
| 169 | +/// |
| 170 | +/// - `None` : not in blockchain, and not in memory pool (conflicted transaction) |
| 171 | +/// - `Some(0)` : in memory pool, waiting to be included in a block (never returned if `as_of_height` is set) |
| 172 | +/// - `Some(1..)` : this many blocks deep in the main chain |
| 173 | +async fn wtx_get_depth_in_main_chain( |
| 174 | + wallet: &DbConnection, |
| 175 | + chain: &FetchServiceSubscriber, |
| 176 | + tx: &Transaction, |
| 177 | + as_of_height: Option<BlockHeight>, |
| 178 | +) -> Result<Option<u32>, SqliteClientError> { |
| 179 | + let chain_height = wallet |
| 180 | + .chain_height()? |
| 181 | + .ok_or_else(|| SqliteClientError::ChainHeightUnknown)?; |
| 182 | + |
| 183 | + let effective_chain_height = chain_height.min(as_of_height.unwrap_or(chain_height)); |
| 184 | + |
| 185 | + let depth = if let Some(mined_height) = wallet.get_tx_height(tx.txid())? { |
| 186 | + Some(effective_chain_height + 1 - mined_height) |
| 187 | + } else if as_of_height.is_none() |
| 188 | + && chain |
| 189 | + .mempool |
| 190 | + .contains_txid(&MempoolKey(tx.txid().to_string())) |
| 191 | + .await |
| 192 | + { |
| 193 | + Some(0) |
| 194 | + } else { |
| 195 | + None |
| 196 | + }; |
| 197 | + |
| 198 | + Ok(depth) |
| 199 | +} |
| 200 | + |
| 201 | +const COINBASE_MATURITY: u32 = 100; |
0 commit comments