2023-06-09 10:02:00 -07:00
|
|
|
//! Functions for querying information in the wallet database.
|
2021-03-25 21:22:45 -07:00
|
|
|
//!
|
|
|
|
//! These functions should generally not be used directly; instead,
|
|
|
|
//! their functionality is available via the [`WalletRead`] and
|
|
|
|
//! [`WalletWrite`] traits.
|
|
|
|
//!
|
|
|
|
//! [`WalletRead`]: zcash_client_backend::data_api::WalletRead
|
|
|
|
//! [`WalletWrite`]: zcash_client_backend::data_api::WalletWrite
|
2023-04-14 10:59:45 -07:00
|
|
|
//!
|
|
|
|
//! # Views
|
|
|
|
//!
|
|
|
|
//! The wallet database exposes the following views as part of its public API:
|
|
|
|
//!
|
|
|
|
//! ## `v_transactions`
|
|
|
|
//!
|
|
|
|
//! This view exposes the history of transactions that affect the balance of each account in the
|
|
|
|
//! wallet. A transaction may be represented by multiple rows in this view, one for each account in
|
|
|
|
//! the wallet that contributes funds to or receives funds from the transaction in question. Each
|
|
|
|
//! row of the view contains:
|
|
|
|
//! - `account_balance_delta`: the net effect of the transaction on the associated account's
|
|
|
|
//! balance. This value is positive when funds are received by the account, and negative when the
|
|
|
|
//! balance of the account decreases due to a spend.
|
|
|
|
//! - `fee_paid`: the total fee paid to send the transaction, as a positive value. This fee is
|
|
|
|
//! associated with the transaction (similar to e.g. `txid` or `mined_height`), and not with any
|
|
|
|
//! specific account involved with that transaction. ` If multiple rows exist for a single
|
|
|
|
//! transaction, this fee amount will be repeated for each such row. Therefore, if more than one
|
|
|
|
//! of the wallet's accounts is involved with the transaction, this fee should be considered only
|
|
|
|
//! once in determining the total value sent from the wallet as a whole.
|
|
|
|
//!
|
|
|
|
//! ### Seed Phrase with Single Account
|
|
|
|
//!
|
|
|
|
//! In the case that the seed phrase for in this wallet has only been used to create a single
|
|
|
|
//! account, this view will contain one row per transaction, in the case that
|
|
|
|
//! `account_balance_delta` is negative, it is usually safe to add `fee_paid` back to the
|
|
|
|
//! `account_balance_delta` value to determine the amount sent to addresses outside the wallet.
|
|
|
|
//!
|
|
|
|
//! ### Seed Phrase with Multiple Accounts
|
|
|
|
//!
|
|
|
|
//! In the case that the seed phrase for in this wallet has been used to create multiple accounts,
|
|
|
|
//! this view may contain multiple rows per transaction, one for each account involved. In this
|
|
|
|
//! case, the total amount sent to addresses outside the wallet can usually be calculated by
|
|
|
|
//! grouping rows by `id_tx` and then using `SUM(account_balance_delta) + MAX(fee_paid)`.
|
|
|
|
//!
|
|
|
|
//! ### Imported Seed Phrases
|
|
|
|
//!
|
|
|
|
//! If a seed phrase is imported, and not every account associated with it is loaded into the
|
|
|
|
//! wallet, this view may show partial information about some transactions. In particular, any
|
|
|
|
//! computation that involves both `account_balance_delta` and `fee_paid` is likely to be
|
|
|
|
//! inaccurate.
|
|
|
|
//!
|
|
|
|
//! ## `v_tx_outputs`
|
|
|
|
//!
|
|
|
|
//! This view exposes the history of transaction outputs received by and sent from the wallet,
|
|
|
|
//! keyed by transaction ID, pool type, and output index. The contents of this view are useful for
|
|
|
|
//! producing a detailed report of the effects of a transaction. Each row of this view contains:
|
|
|
|
//! - `from_account` for sent outputs, the account from which the value was sent.
|
|
|
|
//! - `to_account` in the case that the output was received by an account in the wallet, the
|
|
|
|
//! identifier for the account receiving the funds.
|
|
|
|
//! - `to_address` the address to which an output was sent, or the address at which value was
|
|
|
|
//! received in the case of received transparent funds.
|
|
|
|
//! - `value` the value of the output. This is always a positive number, for both sent and received
|
|
|
|
//! outputs.
|
|
|
|
//! - `is_change` a boolean flag indicating whether this is a change output belonging to the
|
|
|
|
//! wallet.
|
|
|
|
//! - `memo` the shielded memo associated with the output, if any.
|
2019-03-08 18:23:31 -08:00
|
|
|
|
2023-08-16 10:15:10 -07:00
|
|
|
use incrementalmerkletree::Retention;
|
2023-06-14 15:49:16 -07:00
|
|
|
use rusqlite::{self, named_params, OptionalExtension, ToSql};
|
2023-08-16 10:15:10 -07:00
|
|
|
use shardtree::ShardTree;
|
2023-08-11 15:41:38 -07:00
|
|
|
use std::cmp;
|
2023-06-26 09:12:59 -07:00
|
|
|
use std::collections::HashMap;
|
2020-10-29 09:48:26 -07:00
|
|
|
use std::convert::TryFrom;
|
2023-06-26 09:12:59 -07:00
|
|
|
use std::io::{self, Cursor};
|
2023-08-11 15:41:38 -07:00
|
|
|
use std::num::NonZeroU32;
|
2023-08-16 10:15:10 -07:00
|
|
|
use tracing::debug;
|
2023-07-06 07:37:28 -07:00
|
|
|
|
2023-08-16 10:15:10 -07:00
|
|
|
use zcash_client_backend::data_api::{
|
|
|
|
scanning::{ScanPriority, ScanRange},
|
|
|
|
AccountBirthday, NoteId, ShieldedProtocol, SAPLING_SHARD_HEIGHT,
|
|
|
|
};
|
2023-07-06 14:54:48 -07:00
|
|
|
use zcash_primitives::transaction::TransactionData;
|
2020-08-06 13:11:25 -07:00
|
|
|
|
2020-08-05 16:01:22 -07:00
|
|
|
use zcash_primitives::{
|
2020-08-25 14:20:12 -07:00
|
|
|
block::BlockHash,
|
2021-08-12 10:21:30 -07:00
|
|
|
consensus::{self, BlockHeight, BranchId, NetworkUpgrade, Parameters},
|
2020-10-29 09:48:26 -07:00
|
|
|
memo::{Memo, MemoBytes},
|
2023-04-03 12:53:43 -07:00
|
|
|
merkle_tree::read_commitment_tree,
|
2022-01-20 13:33:29 -08:00
|
|
|
transaction::{components::Amount, Transaction, TxId},
|
2022-10-03 13:12:13 -07:00
|
|
|
zip32::{
|
|
|
|
sapling::{DiversifiableFullViewingKey, ExtendedFullViewingKey},
|
|
|
|
AccountId, DiversifierIndex,
|
|
|
|
},
|
2020-08-05 16:01:22 -07:00
|
|
|
};
|
2019-03-08 18:23:31 -08:00
|
|
|
|
2020-08-05 16:01:22 -07:00
|
|
|
use zcash_client_backend::{
|
2022-08-31 08:09:25 -07:00
|
|
|
address::{RecipientAddress, UnifiedAddress},
|
2023-07-01 17:16:23 -07:00
|
|
|
data_api::{BlockMetadata, PoolType, Recipient, SentTransactionOutput},
|
2023-06-07 15:00:27 -07:00
|
|
|
encoding::AddressCodec,
|
2022-06-13 10:54:32 -07:00
|
|
|
keys::UnifiedFullViewingKey,
|
2023-06-02 07:33:19 -07:00
|
|
|
wallet::WalletTx,
|
2020-08-05 16:01:22 -07:00
|
|
|
};
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2023-08-16 10:15:10 -07:00
|
|
|
use crate::wallet::commitment_tree::SqliteShardStore;
|
2023-06-13 10:20:18 -07:00
|
|
|
use crate::{
|
2023-07-03 16:06:43 -07:00
|
|
|
error::SqliteClientError, SqlTransaction, WalletCommitmentTrees, WalletDb, PRUNING_DEPTH,
|
2023-06-13 10:20:18 -07:00
|
|
|
};
|
2023-08-16 10:15:10 -07:00
|
|
|
use crate::{SAPLING_TABLES_PREFIX, VERIFY_LOOKAHEAD};
|
2021-02-12 13:08:31 -08:00
|
|
|
|
2023-07-06 07:37:28 -07:00
|
|
|
use self::scanning::replace_queue_entries;
|
|
|
|
|
2021-10-04 13:09:02 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2021-02-12 13:08:31 -08:00
|
|
|
use {
|
|
|
|
crate::UtxoId,
|
2022-11-09 07:13:34 -08:00
|
|
|
std::collections::BTreeSet,
|
2023-06-07 15:00:27 -07:00
|
|
|
zcash_client_backend::{address::AddressMetadata, wallet::WalletTransparentOutput},
|
2022-01-20 13:33:29 -08:00
|
|
|
zcash_primitives::{
|
2022-10-10 11:38:43 -07:00
|
|
|
legacy::{keys::IncomingViewingKey, Script, TransparentAddress},
|
2022-01-20 13:33:29 -08:00
|
|
|
transaction::components::{OutPoint, TxOut},
|
|
|
|
},
|
2021-02-12 13:08:31 -08:00
|
|
|
};
|
2019-03-08 18:23:31 -08:00
|
|
|
|
2023-08-03 10:19:40 -07:00
|
|
|
pub mod commitment_tree;
|
2020-08-25 14:29:01 -07:00
|
|
|
pub mod init;
|
2023-06-02 07:33:19 -07:00
|
|
|
pub(crate) mod sapling;
|
2023-07-06 07:37:28 -07:00
|
|
|
pub(crate) mod scanning;
|
2020-08-25 14:29:01 -07:00
|
|
|
|
2023-06-30 11:05:15 -07:00
|
|
|
pub(crate) const BLOCK_SAPLING_FRONTIER_ABSENT: &[u8] = &[0x0];
|
|
|
|
|
2022-10-10 11:38:43 -07:00
|
|
|
pub(crate) fn pool_code(pool_type: PoolType) -> i64 {
|
|
|
|
// These constants are *incidentally* shared with the typecodes
|
|
|
|
// for unified addresses, but this is exclusively an internal
|
|
|
|
// implementation detail.
|
|
|
|
match pool_type {
|
|
|
|
PoolType::Transparent => 0i64,
|
2023-06-26 09:12:59 -07:00
|
|
|
PoolType::Shielded(ShieldedProtocol::Sapling) => 2i64,
|
2022-02-02 10:57:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 14:07:29 -07:00
|
|
|
pub(crate) fn memo_repr(memo: Option<&MemoBytes>) -> Option<&[u8]> {
|
2023-08-01 11:00:10 -07:00
|
|
|
memo.map(|m| {
|
|
|
|
if m == &MemoBytes::empty() {
|
|
|
|
// we store the empty memo as a single 0xf6 byte
|
|
|
|
&[0xf6]
|
|
|
|
} else {
|
|
|
|
m.as_slice()
|
|
|
|
}
|
|
|
|
})
|
2023-06-16 14:07:29 -07:00
|
|
|
}
|
|
|
|
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn get_max_account_id(
|
|
|
|
conn: &rusqlite::Connection,
|
2022-09-13 15:43:04 -07:00
|
|
|
) -> Result<Option<AccountId>, SqliteClientError> {
|
|
|
|
// This returns the most recently generated address.
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.query_row("SELECT MAX(account) FROM accounts", [], |row| {
|
|
|
|
let account_id: Option<u32> = row.get(0)?;
|
|
|
|
Ok(account_id.map(AccountId::from))
|
|
|
|
})
|
|
|
|
.map_err(SqliteClientError::from)
|
2022-09-13 15:43:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn add_account<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Transaction,
|
|
|
|
params: &P,
|
2022-09-13 15:43:04 -07:00
|
|
|
account: AccountId,
|
|
|
|
key: &UnifiedFullViewingKey,
|
2023-08-16 10:15:10 -07:00
|
|
|
birthday: AccountBirthday,
|
2022-09-13 15:43:04 -07:00
|
|
|
) -> Result<(), SqliteClientError> {
|
2023-08-16 10:15:10 -07:00
|
|
|
// Set the wallet birthday, falling back to the chain tip if not specified
|
|
|
|
let chain_tip = scan_queue_extrema(conn)?.map(|(_, max)| max);
|
2022-09-13 15:43:04 -07:00
|
|
|
|
2022-10-02 08:09:52 -07:00
|
|
|
conn.execute(
|
2023-08-16 10:15:10 -07:00
|
|
|
"INSERT INTO accounts (account, ufvk, birthday_height, recover_until_height)
|
|
|
|
VALUES (:account, :ufvk, :birthday_height, :recover_until_height)",
|
|
|
|
named_params![
|
|
|
|
":account": u32::from(account),
|
|
|
|
":ufvk": &key.encode(params),
|
|
|
|
":birthday_height": u32::from(birthday.height()),
|
|
|
|
":recover_until_height": birthday.recover_until().map(u32::from)
|
|
|
|
],
|
2022-09-13 15:43:04 -07:00
|
|
|
)?;
|
|
|
|
|
2023-08-16 10:15:10 -07:00
|
|
|
// If a birthday frontier is available, insert it into the note commitment tree. If the
|
|
|
|
// birthday frontier is the empty frontier, we don't need to do anything.
|
|
|
|
if let Some(frontier) = birthday.sapling_frontier().value() {
|
|
|
|
debug!("Inserting frontier into ShardTree: {:?}", frontier);
|
|
|
|
let shard_store = SqliteShardStore::<
|
|
|
|
_,
|
|
|
|
zcash_primitives::sapling::Node,
|
|
|
|
SAPLING_SHARD_HEIGHT,
|
|
|
|
>::from_connection(conn, SAPLING_TABLES_PREFIX)?;
|
|
|
|
let mut shard_tree: ShardTree<
|
|
|
|
_,
|
|
|
|
{ zcash_primitives::sapling::NOTE_COMMITMENT_TREE_DEPTH },
|
|
|
|
SAPLING_SHARD_HEIGHT,
|
|
|
|
> = ShardTree::new(shard_store, PRUNING_DEPTH.try_into().unwrap());
|
|
|
|
shard_tree.insert_frontier_nodes(
|
|
|
|
frontier.clone(),
|
|
|
|
Retention::Checkpoint {
|
|
|
|
// This subtraction is safe, because the non-empty frontier cannot exist at genesis
|
|
|
|
id: birthday.height() - 1,
|
|
|
|
is_marked: false,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let sapling_activation_height = params
|
|
|
|
.activation_height(NetworkUpgrade::Sapling)
|
|
|
|
.expect("Sapling activation height must be available.");
|
|
|
|
|
|
|
|
// Add the ignored range up to and including the birthday height.
|
|
|
|
if sapling_activation_height < birthday.height() {
|
|
|
|
let ignored_range = sapling_activation_height..birthday.height();
|
|
|
|
|
|
|
|
replace_queue_entries::<SqliteClientError>(
|
|
|
|
conn,
|
|
|
|
&ignored_range,
|
|
|
|
Some(ScanRange::from_parts(
|
|
|
|
ignored_range.clone(),
|
|
|
|
ScanPriority::Ignored,
|
|
|
|
))
|
|
|
|
.into_iter(),
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Rewrite the scan ranges above the birthday height so that we'll ensure we re-scan to find
|
|
|
|
// any notes that might belong to the newly added account.
|
|
|
|
if let Some(t) = chain_tip {
|
|
|
|
let rescan_range = birthday.height()..(t + 1);
|
|
|
|
|
|
|
|
replace_queue_entries::<SqliteClientError>(
|
|
|
|
conn,
|
|
|
|
&rescan_range,
|
|
|
|
Some(ScanRange::from_parts(
|
|
|
|
rescan_range.clone(),
|
|
|
|
ScanPriority::Historic,
|
|
|
|
))
|
|
|
|
.into_iter(),
|
|
|
|
true,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2022-09-13 15:43:04 -07:00
|
|
|
// Always derive the default Unified Address for the account.
|
2022-10-12 21:40:51 -07:00
|
|
|
let (address, d_idx) = key.default_address();
|
2023-08-16 10:15:10 -07:00
|
|
|
insert_address(conn, params, account, d_idx, &address)?;
|
2022-09-13 15:43:04 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-12 11:42:12 -07:00
|
|
|
pub(crate) fn get_current_address<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
params: &P,
|
2022-08-31 08:09:25 -07:00
|
|
|
account: AccountId,
|
2022-09-12 11:42:12 -07:00
|
|
|
) -> Result<Option<(UnifiedAddress, DiversifierIndex)>, SqliteClientError> {
|
|
|
|
// This returns the most recently generated address.
|
2023-06-09 10:02:00 -07:00
|
|
|
let addr: Option<(String, Vec<u8>)> = conn
|
2022-10-02 08:09:52 -07:00
|
|
|
.query_row(
|
2022-09-12 11:42:12 -07:00
|
|
|
"SELECT address, diversifier_index_be
|
|
|
|
FROM addresses WHERE account = :account
|
|
|
|
ORDER BY diversifier_index_be DESC
|
|
|
|
LIMIT 1",
|
2022-10-02 08:09:52 -07:00
|
|
|
named_params![":account": &u32::from(account)],
|
2022-09-12 11:42:12 -07:00
|
|
|
|row| Ok((row.get(0)?, row.get(1)?)),
|
2022-08-31 08:14:12 -07:00
|
|
|
)
|
|
|
|
.optional()?;
|
2022-08-31 08:09:25 -07:00
|
|
|
|
2022-09-12 11:42:12 -07:00
|
|
|
addr.map(|(addr_str, di_vec)| {
|
|
|
|
let mut di_be: [u8; 11] = di_vec.try_into().map_err(|_| {
|
2022-10-25 11:26:02 -07:00
|
|
|
SqliteClientError::CorruptedData("Diversifier index is not an 11-byte value".to_owned())
|
2022-09-12 11:42:12 -07:00
|
|
|
})?;
|
|
|
|
di_be.reverse();
|
|
|
|
|
2023-06-09 10:02:00 -07:00
|
|
|
RecipientAddress::decode(params, &addr_str)
|
2022-08-31 08:09:25 -07:00
|
|
|
.ok_or_else(|| {
|
|
|
|
SqliteClientError::CorruptedData("Not a valid Zcash recipient address".to_owned())
|
|
|
|
})
|
|
|
|
.and_then(|addr| match addr {
|
|
|
|
RecipientAddress::Unified(ua) => Ok(ua),
|
|
|
|
_ => Err(SqliteClientError::CorruptedData(format!(
|
|
|
|
"Addresses table contains {} which is not a unified address",
|
|
|
|
addr_str,
|
|
|
|
))),
|
|
|
|
})
|
2022-09-12 11:42:12 -07:00
|
|
|
.map(|addr| (addr, DiversifierIndex(di_be)))
|
2022-08-31 08:09:25 -07:00
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
}
|
|
|
|
|
2023-06-07 15:00:27 -07:00
|
|
|
/// Adds the given address and diversifier index to the addresses table.
|
|
|
|
///
|
|
|
|
/// Returns the database row for the newly-inserted address.
|
|
|
|
pub(crate) fn insert_address<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
2023-06-07 15:00:27 -07:00
|
|
|
params: &P,
|
|
|
|
account: AccountId,
|
|
|
|
mut diversifier_index: DiversifierIndex,
|
|
|
|
address: &UnifiedAddress,
|
|
|
|
) -> Result<(), rusqlite::Error> {
|
|
|
|
let mut stmt = conn.prepare_cached(
|
|
|
|
"INSERT INTO addresses (
|
2023-08-16 10:15:10 -07:00
|
|
|
account,
|
|
|
|
diversifier_index_be,
|
|
|
|
address,
|
|
|
|
cached_transparent_receiver_address
|
|
|
|
)
|
|
|
|
VALUES (
|
|
|
|
:account,
|
|
|
|
:diversifier_index_be,
|
|
|
|
:address,
|
|
|
|
:cached_transparent_receiver_address
|
|
|
|
)",
|
2023-06-07 15:00:27 -07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
// the diversifier index is stored in big-endian order to allow sorting
|
|
|
|
diversifier_index.0.reverse();
|
|
|
|
stmt.execute(named_params![
|
|
|
|
":account": &u32::from(account),
|
|
|
|
":diversifier_index_be": &&diversifier_index.0[..],
|
|
|
|
":address": &address.encode(params),
|
|
|
|
":cached_transparent_receiver_address": &address.transparent().map(|r| r.encode(params)),
|
|
|
|
])?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:48:06 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
pub(crate) fn get_transparent_receivers<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
2022-09-30 18:03:17 -07:00
|
|
|
params: &P,
|
2022-09-08 11:48:06 -07:00
|
|
|
account: AccountId,
|
2022-10-25 09:54:12 -07:00
|
|
|
) -> Result<HashMap<TransparentAddress, AddressMetadata>, SqliteClientError> {
|
|
|
|
let mut ret = HashMap::new();
|
2022-09-08 11:48:06 -07:00
|
|
|
|
|
|
|
// Get all UAs derived
|
2022-10-25 11:04:02 -07:00
|
|
|
let mut ua_query = conn
|
|
|
|
.prepare("SELECT address, diversifier_index_be FROM addresses WHERE account = :account")?;
|
2022-10-02 08:09:52 -07:00
|
|
|
let mut rows = ua_query.query(named_params![":account": &u32::from(account)])?;
|
2022-09-08 11:48:06 -07:00
|
|
|
|
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let ua_str: String = row.get(0)?;
|
2022-10-25 09:54:12 -07:00
|
|
|
let di_vec: Vec<u8> = row.get(1)?;
|
|
|
|
let mut di_be: [u8; 11] = di_vec.try_into().map_err(|_| {
|
|
|
|
SqliteClientError::CorruptedData(
|
|
|
|
"Diverisifier index is not an 11-byte value".to_owned(),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
di_be.reverse();
|
|
|
|
|
2022-09-30 18:03:17 -07:00
|
|
|
let ua = RecipientAddress::decode(params, &ua_str)
|
2022-09-08 11:48:06 -07:00
|
|
|
.ok_or_else(|| {
|
|
|
|
SqliteClientError::CorruptedData("Not a valid Zcash recipient address".to_owned())
|
|
|
|
})
|
|
|
|
.and_then(|addr| match addr {
|
|
|
|
RecipientAddress::Unified(ua) => Ok(ua),
|
|
|
|
_ => Err(SqliteClientError::CorruptedData(format!(
|
|
|
|
"Addresses table contains {} which is not a unified address",
|
|
|
|
ua_str,
|
|
|
|
))),
|
|
|
|
})?;
|
2022-10-25 09:54:12 -07:00
|
|
|
|
2022-09-08 11:48:06 -07:00
|
|
|
if let Some(taddr) = ua.transparent() {
|
2022-10-25 09:54:12 -07:00
|
|
|
ret.insert(
|
|
|
|
*taddr,
|
|
|
|
AddressMetadata::new(account, DiversifierIndex(di_be)),
|
|
|
|
);
|
2022-09-08 11:48:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 09:54:12 -07:00
|
|
|
if let Some((taddr, diversifier_index)) = get_legacy_transparent_address(params, conn, account)?
|
|
|
|
{
|
|
|
|
ret.insert(taddr, AddressMetadata::new(account, diversifier_index));
|
2022-09-08 11:48:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:32:30 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
pub(crate) fn get_legacy_transparent_address<P: consensus::Parameters>(
|
|
|
|
params: &P,
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
2022-10-24 16:32:30 -07:00
|
|
|
account: AccountId,
|
2022-10-25 09:54:12 -07:00
|
|
|
) -> Result<Option<(TransparentAddress, DiversifierIndex)>, SqliteClientError> {
|
2022-10-24 16:32:30 -07:00
|
|
|
// Get the UFVK for the account.
|
|
|
|
let ufvk_str: Option<String> = conn
|
|
|
|
.query_row(
|
|
|
|
"SELECT ufvk FROM accounts WHERE account = :account",
|
|
|
|
[u32::from(account)],
|
|
|
|
|row| row.get(0),
|
|
|
|
)
|
|
|
|
.optional()?;
|
|
|
|
|
|
|
|
if let Some(ufvk_str) = ufvk_str {
|
|
|
|
let ufvk = UnifiedFullViewingKey::decode(params, &ufvk_str)
|
|
|
|
.map_err(SqliteClientError::CorruptedData)?;
|
|
|
|
|
|
|
|
// Derive the default transparent address (if it wasn't already part of a derived UA).
|
|
|
|
ufvk.transparent()
|
|
|
|
.map(|tfvk| {
|
|
|
|
tfvk.derive_external_ivk()
|
2022-10-25 09:54:12 -07:00
|
|
|
.map(|tivk| {
|
|
|
|
let (taddr, child_index) = tivk.default_address();
|
|
|
|
(taddr, DiversifierIndex::from(child_index))
|
|
|
|
})
|
2022-10-24 16:32:30 -07:00
|
|
|
.map_err(SqliteClientError::HdwalletError)
|
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 17:57:20 -07:00
|
|
|
/// Returns the [`UnifiedFullViewingKey`]s for the wallet.
|
|
|
|
pub(crate) fn get_unified_full_viewing_keys<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
params: &P,
|
2022-06-13 17:57:20 -07:00
|
|
|
) -> Result<HashMap<AccountId, UnifiedFullViewingKey>, SqliteClientError> {
|
|
|
|
// Fetch the UnifiedFullViewingKeys we are tracking
|
2023-06-09 10:02:00 -07:00
|
|
|
let mut stmt_fetch_accounts =
|
|
|
|
conn.prepare("SELECT account, ufvk FROM accounts ORDER BY account ASC")?;
|
2020-08-26 14:47:47 -07:00
|
|
|
|
2022-10-02 08:09:52 -07:00
|
|
|
let rows = stmt_fetch_accounts.query_map([], |row| {
|
2022-08-02 15:10:28 -07:00
|
|
|
let acct: u32 = row.get(0)?;
|
|
|
|
let account = AccountId::from(acct);
|
|
|
|
let ufvk_str: String = row.get(1)?;
|
2023-06-09 10:02:00 -07:00
|
|
|
let ufvk = UnifiedFullViewingKey::decode(params, &ufvk_str)
|
2022-08-02 15:10:28 -07:00
|
|
|
.map_err(SqliteClientError::CorruptedData);
|
|
|
|
|
|
|
|
Ok((account, ufvk))
|
|
|
|
})?;
|
2020-08-26 14:47:47 -07:00
|
|
|
|
2022-06-13 17:57:20 -07:00
|
|
|
let mut res: HashMap<AccountId, UnifiedFullViewingKey> = HashMap::new();
|
2021-01-11 17:13:40 -08:00
|
|
|
for row in rows {
|
2022-06-13 17:57:20 -07:00
|
|
|
let (account_id, ufvkr) = row?;
|
|
|
|
res.insert(account_id, ufvkr?);
|
2021-01-11 17:13:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
2020-08-26 14:47:47 -07:00
|
|
|
}
|
|
|
|
|
2022-10-03 13:44:04 -07:00
|
|
|
/// Returns the account id corresponding to a given [`UnifiedFullViewingKey`],
|
|
|
|
/// if any.
|
|
|
|
pub(crate) fn get_account_for_ufvk<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
params: &P,
|
2022-10-03 13:44:04 -07:00
|
|
|
ufvk: &UnifiedFullViewingKey,
|
|
|
|
) -> Result<Option<AccountId>, SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.query_row(
|
|
|
|
"SELECT account FROM accounts WHERE ufvk = ?",
|
|
|
|
[&ufvk.encode(params)],
|
|
|
|
|row| {
|
|
|
|
let acct: u32 = row.get(0)?;
|
|
|
|
Ok(AccountId::from(acct))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.optional()
|
|
|
|
.map_err(SqliteClientError::from)
|
2022-10-03 13:44:04 -07:00
|
|
|
}
|
|
|
|
|
2021-03-25 21:22:45 -07:00
|
|
|
/// Checks whether the specified [`ExtendedFullViewingKey`] is valid and corresponds to the
|
|
|
|
/// specified account.
|
|
|
|
///
|
|
|
|
/// [`ExtendedFullViewingKey`]: zcash_primitives::zip32::ExtendedFullViewingKey
|
2023-04-03 12:31:45 -07:00
|
|
|
pub(crate) fn is_valid_account_extfvk<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
params: &P,
|
2020-08-26 14:47:47 -07:00
|
|
|
account: AccountId,
|
|
|
|
extfvk: &ExtendedFullViewingKey,
|
|
|
|
) -> Result<bool, SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.prepare("SELECT ufvk FROM accounts WHERE account = ?")?
|
2022-10-02 08:09:52 -07:00
|
|
|
.query_row([u32::from(account).to_sql()?], |row| {
|
2022-06-13 10:54:32 -07:00
|
|
|
row.get(0).map(|ufvk_str: String| {
|
2023-06-09 10:02:00 -07:00
|
|
|
UnifiedFullViewingKey::decode(params, &ufvk_str)
|
2022-06-13 10:54:32 -07:00
|
|
|
.map_err(SqliteClientError::CorruptedData)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.optional()
|
2020-08-26 14:47:47 -07:00
|
|
|
.map_err(SqliteClientError::from)
|
2022-06-13 10:54:32 -07:00
|
|
|
.and_then(|row| {
|
|
|
|
if let Some(ufvk) = row {
|
2022-06-13 19:27:55 -07:00
|
|
|
ufvk.map(|ufvk| {
|
|
|
|
ufvk.sapling().map(|dfvk| dfvk.to_bytes())
|
|
|
|
== Some(DiversifiableFullViewingKey::from(extfvk.clone()).to_bytes())
|
|
|
|
})
|
2022-06-13 10:54:32 -07:00
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
})
|
2020-08-26 14:47:47 -07:00
|
|
|
}
|
|
|
|
|
2019-03-08 18:53:38 -08:00
|
|
|
/// Returns the balance for the account, including all mined unspent notes that we know
|
|
|
|
/// about.
|
|
|
|
///
|
2020-07-09 14:49:30 -07:00
|
|
|
/// WARNING: This balance is potentially unreliable, as mined notes may become unmined due
|
|
|
|
/// to chain reorgs. You should generally not show this balance to users without some
|
2021-01-15 11:00:14 -08:00
|
|
|
/// caveat. Use [`get_balance_at`] where you need a more reliable indication of the
|
2020-07-09 14:49:30 -07:00
|
|
|
/// wallet balance.
|
2023-04-03 12:31:45 -07:00
|
|
|
#[cfg(test)]
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn get_balance(
|
|
|
|
conn: &rusqlite::Connection,
|
2023-04-03 12:31:45 -07:00
|
|
|
account: AccountId,
|
|
|
|
) -> Result<Amount, SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
let balance = conn.query_row(
|
2023-04-26 12:13:45 -07:00
|
|
|
"SELECT SUM(value) FROM sapling_received_notes
|
|
|
|
INNER JOIN transactions ON transactions.id_tx = sapling_received_notes.tx
|
2019-03-08 18:53:38 -08:00
|
|
|
WHERE account = ? AND spent IS NULL AND transactions.block IS NOT NULL",
|
2022-10-02 08:09:52 -07:00
|
|
|
[u32::from(account)],
|
2019-03-08 18:53:38 -08:00
|
|
|
|row| row.get(0).or(Ok(0)),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
match Amount::from_i64(balance) {
|
|
|
|
Ok(amount) if !amount.is_negative() => Ok(amount),
|
2021-01-12 17:24:18 -08:00
|
|
|
_ => Err(SqliteClientError::CorruptedData(
|
2023-04-26 12:13:45 -07:00
|
|
|
"Sum of values in sapling_received_notes is out of range".to_string(),
|
2021-01-12 17:24:18 -08:00
|
|
|
)),
|
2019-03-08 18:53:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 17:13:40 -08:00
|
|
|
/// Returns the verified balance for the account at the specified height,
|
2021-01-08 11:49:10 -08:00
|
|
|
/// This may be used to obtain a balance that ignores notes that have been
|
|
|
|
/// received so recently that they are not yet deemed spendable.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn get_balance_at(
|
|
|
|
conn: &rusqlite::Connection,
|
2020-08-06 13:11:25 -07:00
|
|
|
account: AccountId,
|
2020-08-26 14:47:47 -07:00
|
|
|
anchor_height: BlockHeight,
|
2020-08-05 18:14:45 -07:00
|
|
|
) -> Result<Amount, SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
let balance = conn.query_row(
|
2023-04-26 12:13:45 -07:00
|
|
|
"SELECT SUM(value) FROM sapling_received_notes
|
|
|
|
INNER JOIN transactions ON transactions.id_tx = sapling_received_notes.tx
|
2019-03-08 18:53:38 -08:00
|
|
|
WHERE account = ? AND spent IS NULL AND transactions.block <= ?",
|
2022-10-02 08:09:52 -07:00
|
|
|
[u32::from(account), u32::from(anchor_height)],
|
2019-03-08 18:53:38 -08:00
|
|
|
|row| row.get(0).or(Ok(0)),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
match Amount::from_i64(balance) {
|
|
|
|
Ok(amount) if !amount.is_negative() => Ok(amount),
|
2021-01-12 17:24:18 -08:00
|
|
|
_ => Err(SqliteClientError::CorruptedData(
|
2023-04-26 12:13:45 -07:00
|
|
|
"Sum of values in sapling_received_notes is out of range".to_string(),
|
2021-01-12 17:24:18 -08:00
|
|
|
)),
|
2019-03-08 18:53:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 12:45:49 -07:00
|
|
|
/// Returns the memo for a received note, if the note is known to the wallet.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn get_received_memo(
|
|
|
|
conn: &rusqlite::Connection,
|
2023-08-02 12:45:49 -07:00
|
|
|
note_id: NoteId,
|
2023-05-16 09:27:40 -07:00
|
|
|
) -> Result<Option<Memo>, SqliteClientError> {
|
2023-08-02 12:45:49 -07:00
|
|
|
let memo_bytes: Option<Vec<_>> = match note_id.protocol() {
|
|
|
|
ShieldedProtocol::Sapling => conn
|
|
|
|
.query_row(
|
|
|
|
"SELECT memo FROM sapling_received_notes
|
|
|
|
JOIN transactions ON sapling_received_notes.tx = transactions.id_tx
|
|
|
|
WHERE transactions.txid = :txid
|
|
|
|
AND sapling_received_notes.output_index = :output_index",
|
|
|
|
named_params![
|
|
|
|
":txid": note_id.txid().as_ref(),
|
|
|
|
":output_index": note_id.output_index()
|
|
|
|
],
|
|
|
|
|row| row.get(0),
|
|
|
|
)
|
|
|
|
.optional()?
|
|
|
|
.flatten(),
|
|
|
|
};
|
2019-03-08 19:22:35 -08:00
|
|
|
|
2023-05-16 09:27:40 -07:00
|
|
|
memo_bytes
|
|
|
|
.map(|b| {
|
|
|
|
MemoBytes::from_bytes(&b)
|
|
|
|
.and_then(Memo::try_from)
|
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
})
|
|
|
|
.transpose()
|
2019-03-08 19:22:35 -08:00
|
|
|
}
|
|
|
|
|
2023-08-07 10:25:27 -07:00
|
|
|
/// Looks up a transaction by its [`TxId`].
|
2023-08-07 06:45:15 -07:00
|
|
|
///
|
|
|
|
/// Returns the decoded transaction, along with the block height that was used in its decoding.
|
|
|
|
/// This is either the block height at which the transaction was mined, or the expiry height if the
|
|
|
|
/// wallet created the transaction but the transaction has not yet been mined from the perspective
|
|
|
|
/// of the wallet.
|
2022-02-02 09:29:19 -08:00
|
|
|
pub(crate) fn get_transaction<P: Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
params: &P,
|
2023-08-02 12:45:49 -07:00
|
|
|
txid: TxId,
|
2023-08-01 11:00:58 -07:00
|
|
|
) -> Result<(BlockHeight, Transaction), SqliteClientError> {
|
2023-07-06 14:54:48 -07:00
|
|
|
let (tx_bytes, block_height, expiry_height): (
|
|
|
|
Vec<_>,
|
|
|
|
Option<BlockHeight>,
|
|
|
|
Option<BlockHeight>,
|
|
|
|
) = conn.query_row(
|
|
|
|
"SELECT raw, block, expiry_height FROM transactions
|
2023-08-02 12:45:49 -07:00
|
|
|
WHERE txid = ?",
|
|
|
|
[txid.as_ref()],
|
2021-08-12 10:21:30 -07:00
|
|
|
|row| {
|
2023-07-06 14:54:48 -07:00
|
|
|
let h: Option<u32> = row.get(1)?;
|
|
|
|
let expiry: Option<u32> = row.get(2)?;
|
|
|
|
Ok((
|
|
|
|
row.get(0)?,
|
|
|
|
h.map(BlockHeight::from),
|
|
|
|
expiry.map(BlockHeight::from),
|
|
|
|
))
|
2021-08-12 10:21:30 -07:00
|
|
|
},
|
2021-04-13 10:02:35 -07:00
|
|
|
)?;
|
|
|
|
|
2023-07-06 14:54:48 -07:00
|
|
|
// We need to provide a consensus branch ID so that pre-v5 `Transaction` structs
|
|
|
|
// (which don't commit directly to one) can store it internally.
|
|
|
|
// - If the transaction is mined, we use the block height to get the correct one.
|
|
|
|
// - If the transaction is unmined and has a cached non-zero expiry height, we use
|
|
|
|
// that (relying on the invariant that a transaction can't be mined across a network
|
|
|
|
// upgrade boundary, so the expiry height must be in the same epoch).
|
|
|
|
// - Otherwise, we use a placeholder for the initial transaction parse (as the
|
|
|
|
// consensus branch ID is not used there), and then either use its non-zero expiry
|
|
|
|
// height or return an error.
|
|
|
|
if let Some(height) =
|
|
|
|
block_height.or_else(|| expiry_height.filter(|h| h > &BlockHeight::from(0)))
|
|
|
|
{
|
|
|
|
Transaction::read(&tx_bytes[..], BranchId::for_height(params, height))
|
2023-08-01 11:00:58 -07:00
|
|
|
.map(|t| (height, t))
|
2023-07-06 14:54:48 -07:00
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
} else {
|
|
|
|
let tx_data = Transaction::read(&tx_bytes[..], BranchId::Sprout)
|
|
|
|
.map_err(SqliteClientError::from)?
|
|
|
|
.into_data();
|
|
|
|
|
|
|
|
let expiry_height = tx_data.expiry_height();
|
|
|
|
if expiry_height > BlockHeight::from(0) {
|
|
|
|
TransactionData::from_parts(
|
|
|
|
tx_data.version(),
|
|
|
|
BranchId::for_height(params, expiry_height),
|
|
|
|
tx_data.lock_time(),
|
|
|
|
expiry_height,
|
|
|
|
tx_data.transparent_bundle().cloned(),
|
|
|
|
tx_data.sprout_bundle().cloned(),
|
|
|
|
tx_data.sapling_bundle().cloned(),
|
|
|
|
tx_data.orchard_bundle().cloned(),
|
|
|
|
)
|
|
|
|
.freeze()
|
2023-08-01 11:00:58 -07:00
|
|
|
.map(|t| (expiry_height, t))
|
2023-07-06 14:54:48 -07:00
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
} else {
|
|
|
|
Err(SqliteClientError::CorruptedData(
|
|
|
|
"Consensus branch ID not known, cannot parse this transaction until it is mined"
|
|
|
|
.to_string(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 10:02:35 -07:00
|
|
|
}
|
|
|
|
|
2023-08-02 12:45:49 -07:00
|
|
|
/// Returns the memo for a sent note, if the sent note is known to the wallet.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn get_sent_memo(
|
|
|
|
conn: &rusqlite::Connection,
|
2023-08-02 12:45:49 -07:00
|
|
|
note_id: NoteId,
|
2023-05-16 09:27:40 -07:00
|
|
|
) -> Result<Option<Memo>, SqliteClientError> {
|
2023-08-02 12:45:49 -07:00
|
|
|
let memo_bytes: Option<Vec<_>> = conn
|
|
|
|
.query_row(
|
|
|
|
"SELECT memo FROM sent_notes
|
|
|
|
JOIN transactions ON sent_notes.tx = transactions.id_tx
|
|
|
|
WHERE transactions.txid = :txid
|
|
|
|
AND sent_notes.output_pool = :pool_code
|
|
|
|
AND sent_notes.output_index = :output_index",
|
|
|
|
named_params![
|
|
|
|
":txid": note_id.txid().as_ref(),
|
|
|
|
":pool_code": pool_code(PoolType::Shielded(note_id.protocol())),
|
|
|
|
":output_index": note_id.output_index()
|
|
|
|
],
|
|
|
|
|row| row.get(0),
|
|
|
|
)
|
|
|
|
.optional()?
|
|
|
|
.flatten();
|
2019-03-08 19:22:35 -08:00
|
|
|
|
2023-05-16 09:27:40 -07:00
|
|
|
memo_bytes
|
|
|
|
.map(|b| {
|
|
|
|
MemoBytes::from_bytes(&b)
|
|
|
|
.and_then(Memo::try_from)
|
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
})
|
|
|
|
.transpose()
|
2019-03-08 19:22:35 -08:00
|
|
|
}
|
|
|
|
|
2023-08-16 10:15:10 -07:00
|
|
|
/// Returns the minimum birthday height for accounts in the wallet.
|
|
|
|
///
|
|
|
|
/// TODO ORCHARD: we should consider whether we want to permit protocol-restricted accounts; if so,
|
|
|
|
/// we would then want this method to take a protocol identifier to be able to learn the wallet's
|
|
|
|
/// "Orchard birthday" which might be different from the overall wallet birthday.
|
|
|
|
pub(crate) fn wallet_birthday(
|
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
) -> Result<Option<BlockHeight>, rusqlite::Error> {
|
|
|
|
conn.query_row(
|
|
|
|
"SELECT MIN(birthday_height) AS wallet_birthday FROM accounts",
|
|
|
|
[],
|
|
|
|
|row| {
|
|
|
|
row.get::<_, Option<u32>>(0)
|
|
|
|
.map(|opt| opt.map(BlockHeight::from))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn account_birthday(
|
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
account: AccountId,
|
|
|
|
) -> Result<BlockHeight, SqliteClientError> {
|
|
|
|
conn.query_row(
|
|
|
|
"SELECT birthday_height
|
|
|
|
FROM accounts
|
|
|
|
WHERE account = :account_id",
|
|
|
|
named_params![":account_id": u32::from(account)],
|
|
|
|
|row| row.get::<_, u32>(0).map(BlockHeight::from),
|
|
|
|
)
|
|
|
|
.optional()
|
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
.and_then(|opt| opt.ok_or(SqliteClientError::AccountUnknown(account)))
|
|
|
|
}
|
|
|
|
|
2021-03-25 21:22:45 -07:00
|
|
|
/// Returns the minimum and maximum heights for blocks stored in the wallet database.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn block_height_extrema(
|
|
|
|
conn: &rusqlite::Connection,
|
2020-08-25 14:20:12 -07:00
|
|
|
) -> Result<Option<(BlockHeight, BlockHeight)>, rusqlite::Error> {
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.query_row("SELECT MIN(height), MAX(height) FROM blocks", [], |row| {
|
2023-06-16 14:06:32 -07:00
|
|
|
let min_height: Option<u32> = row.get(0)?;
|
|
|
|
let max_height: Option<u32> = row.get(1)?;
|
|
|
|
Ok(min_height
|
|
|
|
.map(BlockHeight::from)
|
|
|
|
.zip(max_height.map(BlockHeight::from)))
|
2023-06-09 10:02:00 -07:00
|
|
|
})
|
2020-08-25 14:20:12 -07:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:41:38 -07:00
|
|
|
/// Returns the minimum and maximum heights of blocks in the chain which may be scanned.
|
|
|
|
pub(crate) fn scan_queue_extrema(
|
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
) -> Result<Option<(BlockHeight, BlockHeight)>, rusqlite::Error> {
|
|
|
|
conn.query_row(
|
|
|
|
"SELECT MIN(block_range_start), MAX(block_range_end) FROM scan_queue",
|
|
|
|
[],
|
|
|
|
|row| {
|
|
|
|
let min_height: Option<u32> = row.get(0)?;
|
|
|
|
let max_height: Option<u32> = row.get(1)?;
|
|
|
|
|
|
|
|
// Scan ranges are end-exclusive, so we subtract 1 from `max_height` to obtain the
|
|
|
|
// height of the last known chain tip;
|
|
|
|
Ok(min_height
|
|
|
|
.map(BlockHeight::from)
|
|
|
|
.zip(max_height.map(|h| BlockHeight::from(h.saturating_sub(1)))))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get_target_and_anchor_heights(
|
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
min_confirmations: NonZeroU32,
|
|
|
|
) -> Result<Option<(BlockHeight, BlockHeight)>, rusqlite::Error> {
|
|
|
|
scan_queue_extrema(conn).map(|heights| {
|
|
|
|
heights.map(|(min_height, max_height)| {
|
|
|
|
let target_height = max_height + 1;
|
|
|
|
// Select an anchor min_confirmations back from the target block,
|
|
|
|
// unless that would be before the earliest block we have.
|
|
|
|
let anchor_height = BlockHeight::from(cmp::max(
|
|
|
|
u32::from(target_height).saturating_sub(min_confirmations.into()),
|
|
|
|
u32::from(min_height),
|
|
|
|
));
|
|
|
|
|
|
|
|
(target_height, anchor_height)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-01 17:16:23 -07:00
|
|
|
fn parse_block_metadata(
|
|
|
|
row: (BlockHeight, Vec<u8>, Option<u32>, Vec<u8>),
|
2023-07-03 16:06:43 -07:00
|
|
|
) -> Result<BlockMetadata, SqliteClientError> {
|
2023-07-01 17:16:23 -07:00
|
|
|
let (block_height, hash_data, sapling_tree_size_opt, sapling_tree) = row;
|
2023-07-03 16:06:43 -07:00
|
|
|
let sapling_tree_size = sapling_tree_size_opt.map_or_else(|| {
|
2023-07-01 17:16:23 -07:00
|
|
|
if sapling_tree == BLOCK_SAPLING_FRONTIER_ABSENT {
|
2023-07-03 16:06:43 -07:00
|
|
|
Err(SqliteClientError::CorruptedData("One of either the Sapling tree size or the legacy Sapling commitment tree must be present.".to_owned()))
|
2023-07-01 17:16:23 -07:00
|
|
|
} else {
|
|
|
|
// parse the legacy commitment tree data
|
|
|
|
read_commitment_tree::<
|
|
|
|
zcash_primitives::sapling::Node,
|
|
|
|
_,
|
|
|
|
{ zcash_primitives::sapling::NOTE_COMMITMENT_TREE_DEPTH },
|
|
|
|
>(Cursor::new(sapling_tree))
|
2023-07-03 16:06:43 -07:00
|
|
|
.map(|tree| tree.size().try_into().unwrap())
|
2023-07-01 17:16:23 -07:00
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
}
|
2023-07-03 16:06:43 -07:00
|
|
|
}, Ok)?;
|
2023-07-01 17:16:23 -07:00
|
|
|
|
|
|
|
let block_hash = BlockHash::try_from_slice(&hash_data).ok_or_else(|| {
|
|
|
|
SqliteClientError::from(io::Error::new(
|
|
|
|
io::ErrorKind::InvalidData,
|
|
|
|
format!("Invalid block hash length: {}", hash_data.len()),
|
|
|
|
))
|
2023-07-03 16:06:43 -07:00
|
|
|
})?;
|
2023-07-01 17:16:23 -07:00
|
|
|
|
2023-07-03 16:06:43 -07:00
|
|
|
Ok(BlockMetadata::from_parts(
|
|
|
|
block_height,
|
|
|
|
block_hash,
|
|
|
|
sapling_tree_size,
|
|
|
|
))
|
2023-07-01 17:16:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn block_metadata(
|
2023-04-03 12:53:43 -07:00
|
|
|
conn: &rusqlite::Connection,
|
2023-07-01 17:16:23 -07:00
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<Option<BlockMetadata>, SqliteClientError> {
|
2023-07-03 16:06:43 -07:00
|
|
|
conn.query_row(
|
|
|
|
"SELECT height, hash, sapling_commitment_tree_size, sapling_tree
|
2023-07-06 07:37:28 -07:00
|
|
|
FROM blocks
|
|
|
|
WHERE height = :block_height",
|
2023-07-03 16:06:43 -07:00
|
|
|
named_params![":block_height": u32::from(block_height)],
|
|
|
|
|row| {
|
|
|
|
let height: u32 = row.get(0)?;
|
|
|
|
let block_hash: Vec<u8> = row.get(1)?;
|
|
|
|
let sapling_tree_size: Option<u32> = row.get(2)?;
|
|
|
|
let sapling_tree: Vec<u8> = row.get(3)?;
|
|
|
|
Ok((
|
|
|
|
BlockHeight::from(height),
|
|
|
|
block_hash,
|
|
|
|
sapling_tree_size,
|
|
|
|
sapling_tree,
|
|
|
|
))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.optional()
|
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
.and_then(|meta_row| meta_row.map(parse_block_metadata).transpose())
|
2023-07-01 17:16:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn block_fully_scanned(
|
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
) -> Result<Option<BlockMetadata>, SqliteClientError> {
|
2023-07-21 08:23:49 -07:00
|
|
|
// We assume here that the wallet was either initialized via `init_blocks_table`, or
|
|
|
|
// its birthday is Sapling activation, so the earliest block in the `blocks` table is
|
|
|
|
// the first fully-scanned block (because it occurs before any wallet activity).
|
|
|
|
//
|
|
|
|
// We further assume that the only way we get a contiguous range of block heights in
|
|
|
|
// the `blocks` table starting with this earliest block, is if all scanning operations
|
|
|
|
// have been performed on those blocks. This holds because the `blocks` table is only
|
|
|
|
// altered by `WalletDb::put_blocks` via `put_block`, and the effective combination of
|
|
|
|
// intra-range linear scanning and the nullifier map ensures that we discover all
|
|
|
|
// wallet-related information within the contiguous range.
|
|
|
|
//
|
|
|
|
// The fully-scanned height is therefore the greatest height in the first contiguous
|
|
|
|
// range of block rows, which is a combined case of the "gaps and islands" and
|
|
|
|
// "greatest N per group" SQL query problems.
|
2023-07-03 16:06:43 -07:00
|
|
|
conn.query_row(
|
|
|
|
"SELECT height, hash, sapling_commitment_tree_size, sapling_tree
|
2023-07-21 08:23:49 -07:00
|
|
|
FROM blocks
|
|
|
|
INNER JOIN (
|
|
|
|
WITH contiguous AS (
|
|
|
|
SELECT height, ROW_NUMBER() OVER (ORDER BY height) - height AS grp
|
|
|
|
FROM blocks
|
|
|
|
)
|
|
|
|
SELECT MAX(height) AS [fully_scanned_height]
|
|
|
|
FROM contiguous
|
|
|
|
GROUP BY grp
|
|
|
|
ORDER BY height
|
|
|
|
LIMIT 1
|
|
|
|
)
|
|
|
|
ON height = fully_scanned_height",
|
2023-07-03 16:06:43 -07:00
|
|
|
[],
|
|
|
|
|row| {
|
|
|
|
let height: u32 = row.get(0)?;
|
|
|
|
let block_hash: Vec<u8> = row.get(1)?;
|
|
|
|
let sapling_tree_size: Option<u32> = row.get(2)?;
|
|
|
|
let sapling_tree: Vec<u8> = row.get(3)?;
|
|
|
|
Ok((
|
|
|
|
BlockHeight::from(height),
|
|
|
|
block_hash,
|
|
|
|
sapling_tree_size,
|
|
|
|
sapling_tree,
|
|
|
|
))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.optional()
|
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
.and_then(|meta_row| meta_row.map(parse_block_metadata).transpose())
|
2023-04-03 12:53:43 -07:00
|
|
|
}
|
|
|
|
|
2021-03-25 21:22:45 -07:00
|
|
|
/// Returns the block height at which the specified transaction was mined,
|
|
|
|
/// if any.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn get_tx_height(
|
|
|
|
conn: &rusqlite::Connection,
|
2021-01-12 17:24:18 -08:00
|
|
|
txid: TxId,
|
|
|
|
) -> Result<Option<BlockHeight>, rusqlite::Error> {
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.query_row(
|
|
|
|
"SELECT block FROM transactions WHERE txid = ?",
|
|
|
|
[txid.as_ref().to_vec()],
|
|
|
|
|row| row.get(0).map(u32::into),
|
|
|
|
)
|
|
|
|
.optional()
|
2020-08-25 14:20:12 -07:00
|
|
|
}
|
|
|
|
|
2021-03-25 21:22:45 -07:00
|
|
|
/// Returns the block hash for the block at the specified height,
|
|
|
|
/// if any.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn get_block_hash(
|
|
|
|
conn: &rusqlite::Connection,
|
2020-08-25 14:20:12 -07:00
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<Option<BlockHash>, rusqlite::Error> {
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.query_row(
|
|
|
|
"SELECT hash FROM blocks WHERE height = ?",
|
|
|
|
[u32::from(block_height)],
|
|
|
|
|row| {
|
|
|
|
let row_data = row.get::<_, Vec<_>>(0)?;
|
|
|
|
Ok(BlockHash::from_slice(&row_data))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.optional()
|
2020-08-25 14:20:12 -07:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:41:38 -07:00
|
|
|
pub(crate) fn get_max_height_hash(
|
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
) -> Result<Option<(BlockHeight, BlockHash)>, rusqlite::Error> {
|
|
|
|
conn.query_row(
|
|
|
|
"SELECT height, hash FROM blocks ORDER BY height DESC LIMIT 1",
|
|
|
|
[],
|
|
|
|
|row| {
|
|
|
|
let height = row.get::<_, u32>(0).map(BlockHeight::from)?;
|
|
|
|
let row_data = row.get::<_, Vec<_>>(1)?;
|
|
|
|
Ok((height, BlockHash::from_slice(&row_data)))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.optional()
|
|
|
|
}
|
|
|
|
|
2023-04-26 15:27:56 -07:00
|
|
|
/// Gets the height to which the database must be truncated if any truncation that would remove a
|
|
|
|
/// number of blocks greater than the pruning height is attempted.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn get_min_unspent_height(
|
|
|
|
conn: &rusqlite::Connection,
|
2023-04-03 12:31:45 -07:00
|
|
|
) -> Result<Option<BlockHeight>, SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.query_row(
|
|
|
|
"SELECT MIN(tx.block)
|
2023-08-11 15:41:38 -07:00
|
|
|
FROM sapling_received_notes n
|
|
|
|
JOIN transactions tx ON tx.id_tx = n.tx
|
|
|
|
WHERE n.spent IS NULL",
|
2023-06-09 10:02:00 -07:00
|
|
|
[],
|
|
|
|
|row| {
|
|
|
|
row.get(0)
|
|
|
|
.map(|maybe_height: Option<u32>| maybe_height.map(|height| height.into()))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.map_err(SqliteClientError::from)
|
2021-04-13 10:02:35 -07:00
|
|
|
}
|
|
|
|
|
2023-04-26 15:27:56 -07:00
|
|
|
/// Truncates the database to the given height.
|
2020-08-25 14:20:12 -07:00
|
|
|
///
|
|
|
|
/// If the requested height is greater than or equal to the height of the last scanned
|
|
|
|
/// block, this function does nothing.
|
2020-12-02 13:36:56 -08:00
|
|
|
///
|
|
|
|
/// This should only be executed inside a transactional context.
|
2023-06-29 15:26:22 -07:00
|
|
|
pub(crate) fn truncate_to_height<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Transaction,
|
|
|
|
params: &P,
|
2020-08-25 14:20:12 -07:00
|
|
|
block_height: BlockHeight,
|
2021-01-13 14:20:11 -08:00
|
|
|
) -> Result<(), SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
let sapling_activation_height = params
|
2020-08-25 14:20:12 -07:00
|
|
|
.activation_height(NetworkUpgrade::Sapling)
|
2023-07-13 10:25:56 -07:00
|
|
|
.expect("Sapling activation height must be available.");
|
2020-08-25 14:20:12 -07:00
|
|
|
|
|
|
|
// Recall where we synced up to previously.
|
2023-06-09 10:02:00 -07:00
|
|
|
let last_scanned_height = conn.query_row("SELECT MAX(height) FROM blocks", [], |row| {
|
2023-06-16 14:06:32 -07:00
|
|
|
row.get::<_, Option<u32>>(0)
|
|
|
|
.map(|opt| opt.map_or_else(|| sapling_activation_height - 1, BlockHeight::from))
|
2023-06-09 10:02:00 -07:00
|
|
|
})?;
|
2020-08-25 14:20:12 -07:00
|
|
|
|
2023-07-03 16:06:43 -07:00
|
|
|
if block_height < last_scanned_height - PRUNING_DEPTH {
|
2023-06-09 10:02:00 -07:00
|
|
|
if let Some(h) = get_min_unspent_height(conn)? {
|
2022-01-31 15:36:09 -08:00
|
|
|
if block_height > h {
|
|
|
|
return Err(SqliteClientError::RequestedRewindInvalid(h, block_height));
|
2021-04-13 10:02:35 -07:00
|
|
|
}
|
|
|
|
}
|
2022-01-31 15:36:09 -08:00
|
|
|
}
|
2020-08-25 14:20:12 -07:00
|
|
|
|
2021-04-13 10:02:35 -07:00
|
|
|
// nothing to do if we're deleting back down to the max height
|
2022-01-31 15:36:09 -08:00
|
|
|
if block_height < last_scanned_height {
|
2023-06-13 10:20:18 -07:00
|
|
|
// Truncate the note commitment trees
|
|
|
|
let mut wdb = WalletDb {
|
|
|
|
conn: SqlTransaction(conn),
|
|
|
|
params: params.clone(),
|
|
|
|
};
|
|
|
|
wdb.with_sapling_tree_mut(|tree| {
|
|
|
|
tree.truncate_removing_checkpoint(&block_height).map(|_| ())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// Remove any legacy Sapling witnesses
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.execute(
|
2022-01-31 15:36:09 -08:00
|
|
|
"DELETE FROM sapling_witnesses WHERE block > ?",
|
2022-10-02 08:09:52 -07:00
|
|
|
[u32::from(block_height)],
|
2022-01-31 15:36:09 -08:00
|
|
|
)?;
|
|
|
|
|
|
|
|
// Rewind received notes
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.execute(
|
2023-04-26 12:13:45 -07:00
|
|
|
"DELETE FROM sapling_received_notes
|
2023-06-16 14:07:53 -07:00
|
|
|
WHERE id_note IN (
|
|
|
|
SELECT rn.id_note
|
|
|
|
FROM sapling_received_notes rn
|
|
|
|
LEFT OUTER JOIN transactions tx
|
|
|
|
ON tx.id_tx = rn.tx
|
|
|
|
WHERE tx.block IS NOT NULL AND tx.block > ?
|
|
|
|
);",
|
2022-10-02 08:09:52 -07:00
|
|
|
[u32::from(block_height)],
|
2022-01-31 15:36:09 -08:00
|
|
|
)?;
|
|
|
|
|
2022-02-02 10:57:34 -08:00
|
|
|
// Do not delete sent notes; this can contain data that is not recoverable
|
|
|
|
// from the chain. Wallets must continue to operate correctly in the
|
|
|
|
// presence of stale sent notes that link to unmined transactions.
|
2020-08-25 14:20:12 -07:00
|
|
|
|
2022-01-31 15:36:09 -08:00
|
|
|
// Rewind utxos
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.execute(
|
2022-01-31 15:36:09 -08:00
|
|
|
"DELETE FROM utxos WHERE height > ?",
|
2022-10-02 08:09:52 -07:00
|
|
|
[u32::from(block_height)],
|
2022-01-31 15:36:09 -08:00
|
|
|
)?;
|
|
|
|
|
|
|
|
// Un-mine transactions.
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.execute(
|
2023-07-06 07:37:28 -07:00
|
|
|
"UPDATE transactions SET block = NULL, tx_index = NULL
|
|
|
|
WHERE block IS NOT NULL AND block > ?",
|
2022-10-02 08:09:52 -07:00
|
|
|
[u32::from(block_height)],
|
2022-01-31 15:36:09 -08:00
|
|
|
)?;
|
|
|
|
|
|
|
|
// Now that they aren't depended on, delete scanned blocks.
|
2023-06-09 10:02:00 -07:00
|
|
|
conn.execute(
|
2022-01-31 15:36:09 -08:00
|
|
|
"DELETE FROM blocks WHERE height > ?",
|
2022-10-02 08:09:52 -07:00
|
|
|
[u32::from(block_height)],
|
2022-01-31 15:36:09 -08:00
|
|
|
)?;
|
2023-07-06 07:37:28 -07:00
|
|
|
|
2023-07-21 11:01:52 -07:00
|
|
|
// Delete from the nullifier map any entries with a locator referencing a block
|
|
|
|
// height greater than the truncation height.
|
|
|
|
conn.execute(
|
|
|
|
"DELETE FROM tx_locator_map
|
|
|
|
WHERE block_height > :block_height",
|
|
|
|
named_params![":block_height": u32::from(block_height)],
|
|
|
|
)?;
|
|
|
|
|
2023-07-11 19:42:32 -07:00
|
|
|
// Delete from the scanning queue any range with a start height greater than the
|
|
|
|
// truncation height, and then truncate any remaining range by setting the end
|
|
|
|
// equal to the truncation height + 1.
|
2023-07-06 07:37:28 -07:00
|
|
|
conn.execute(
|
|
|
|
"DELETE FROM scan_queue
|
2023-07-11 19:42:32 -07:00
|
|
|
WHERE block_range_start > :block_height",
|
2023-07-06 07:37:28 -07:00
|
|
|
named_params![":block_height": u32::from(block_height)],
|
|
|
|
)?;
|
|
|
|
|
|
|
|
conn.execute(
|
|
|
|
"UPDATE scan_queue
|
2023-07-11 19:42:32 -07:00
|
|
|
SET block_range_end = :end_height
|
|
|
|
WHERE block_range_end > :end_height",
|
|
|
|
named_params![":end_height": u32::from(block_height + 1)],
|
2023-07-06 07:37:28 -07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
// Prioritize the range starting at the height we just rewound to for verification
|
2023-07-19 09:02:28 -07:00
|
|
|
let query_range = block_height..(block_height + VERIFY_LOOKAHEAD);
|
2023-07-06 07:37:28 -07:00
|
|
|
let scan_range = ScanRange::from_parts(query_range.clone(), ScanPriority::Verify);
|
2023-08-16 10:15:10 -07:00
|
|
|
replace_queue_entries::<SqliteClientError>(
|
|
|
|
conn,
|
|
|
|
&query_range,
|
|
|
|
Some(scan_range).into_iter(),
|
|
|
|
false,
|
|
|
|
)?;
|
2021-01-12 17:24:18 -08:00
|
|
|
}
|
2021-04-13 10:02:35 -07:00
|
|
|
|
|
|
|
Ok(())
|
2020-08-25 14:20:12 -07:00
|
|
|
}
|
2020-08-06 13:11:25 -07:00
|
|
|
|
2022-01-20 13:33:29 -08:00
|
|
|
/// Returns unspent transparent outputs that have been received by this wallet at the given
|
|
|
|
/// transparent address, such that the block that included the transaction was mined at a
|
|
|
|
/// height less than or equal to the provided `max_height`.
|
2021-10-04 13:09:02 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-02-02 09:29:19 -08:00
|
|
|
pub(crate) fn get_unspent_transparent_outputs<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
params: &P,
|
2020-12-22 06:10:13 -08:00
|
|
|
address: &TransparentAddress,
|
2021-09-03 16:32:40 -07:00
|
|
|
max_height: BlockHeight,
|
2022-11-09 07:13:34 -08:00
|
|
|
exclude: &[OutPoint],
|
2020-12-22 06:10:13 -08:00
|
|
|
) -> Result<Vec<WalletTransparentOutput>, SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
let mut stmt_blocks = conn.prepare(
|
2022-10-12 21:40:51 -07:00
|
|
|
"SELECT u.prevout_txid, u.prevout_idx, u.script,
|
2022-10-01 11:58:01 -07:00
|
|
|
u.value_zat, u.height, tx.block as block
|
2021-03-26 17:39:43 -07:00
|
|
|
FROM utxos u
|
|
|
|
LEFT OUTER JOIN transactions tx
|
|
|
|
ON tx.id_tx = u.spent_in_tx
|
2023-06-14 15:49:16 -07:00
|
|
|
WHERE u.address = :address
|
|
|
|
AND u.height <= :max_height
|
2022-10-25 11:04:02 -07:00
|
|
|
AND tx.block IS NULL",
|
2020-12-22 06:10:13 -08:00
|
|
|
)?;
|
|
|
|
|
2023-06-09 10:02:00 -07:00
|
|
|
let addr_str = address.encode(params);
|
2020-12-22 06:10:13 -08:00
|
|
|
|
2022-10-14 10:08:32 -07:00
|
|
|
let mut utxos = Vec::<WalletTransparentOutput>::new();
|
2023-06-14 15:49:16 -07:00
|
|
|
let mut rows = stmt_blocks.query(named_params![
|
|
|
|
":address": addr_str,
|
|
|
|
":max_height": u32::from(max_height)
|
|
|
|
])?;
|
2022-11-09 07:13:34 -08:00
|
|
|
let excluded: BTreeSet<OutPoint> = exclude.iter().cloned().collect();
|
2022-10-14 10:08:32 -07:00
|
|
|
while let Some(row) = rows.next()? {
|
2022-10-12 21:40:51 -07:00
|
|
|
let txid: Vec<u8> = row.get(0)?;
|
2020-12-22 06:10:13 -08:00
|
|
|
let mut txid_bytes = [0u8; 32];
|
2022-10-01 11:58:01 -07:00
|
|
|
txid_bytes.copy_from_slice(&txid);
|
|
|
|
|
2022-10-12 21:40:51 -07:00
|
|
|
let index: u32 = row.get(1)?;
|
|
|
|
let script_pubkey = Script(row.get(2)?);
|
|
|
|
let value = Amount::from_i64(row.get(3)?).unwrap();
|
|
|
|
let height: u32 = row.get(4)?;
|
2020-12-22 06:10:13 -08:00
|
|
|
|
2022-11-09 07:13:34 -08:00
|
|
|
let outpoint = OutPoint::new(txid_bytes, index);
|
|
|
|
if excluded.contains(&outpoint) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-10-14 10:08:32 -07:00
|
|
|
let output = WalletTransparentOutput::from_parts(
|
2022-11-09 07:13:34 -08:00
|
|
|
outpoint,
|
2022-10-14 10:08:32 -07:00
|
|
|
TxOut {
|
2021-10-01 10:42:04 -07:00
|
|
|
value,
|
|
|
|
script_pubkey,
|
|
|
|
},
|
2022-10-14 10:08:32 -07:00
|
|
|
BlockHeight::from(height),
|
|
|
|
)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
SqliteClientError::CorruptedData(
|
|
|
|
"Txout script_pubkey value did not correspond to a P2PKH or P2SH address"
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
})?;
|
2020-12-22 06:10:13 -08:00
|
|
|
|
2022-10-14 10:08:32 -07:00
|
|
|
utxos.push(output);
|
2020-12-22 06:10:13 -08:00
|
|
|
}
|
2022-10-14 10:08:32 -07:00
|
|
|
|
2020-12-22 06:10:13 -08:00
|
|
|
Ok(utxos)
|
|
|
|
}
|
|
|
|
|
2022-10-25 11:04:02 -07:00
|
|
|
/// Returns the unspent balance for each transparent address associated with the specified account,
|
|
|
|
/// such that the block that included the transaction was mined at a height less than or equal to
|
|
|
|
/// the provided `max_height`.
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
pub(crate) fn get_transparent_balances<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
|
|
|
params: &P,
|
2022-10-25 11:04:02 -07:00
|
|
|
account: AccountId,
|
|
|
|
max_height: BlockHeight,
|
|
|
|
) -> Result<HashMap<TransparentAddress, Amount>, SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
let mut stmt_blocks = conn.prepare(
|
2022-10-25 11:04:02 -07:00
|
|
|
"SELECT u.address, SUM(u.value_zat)
|
|
|
|
FROM utxos u
|
|
|
|
LEFT OUTER JOIN transactions tx
|
|
|
|
ON tx.id_tx = u.spent_in_tx
|
2023-06-14 15:49:16 -07:00
|
|
|
WHERE u.received_by_account = :account_id
|
|
|
|
AND u.height <= :max_height
|
2022-12-08 10:52:11 -08:00
|
|
|
AND tx.block IS NULL
|
|
|
|
GROUP BY u.address",
|
2022-10-25 11:04:02 -07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut res = HashMap::new();
|
2023-06-14 15:49:16 -07:00
|
|
|
let mut rows = stmt_blocks.query(named_params![
|
|
|
|
":account_id": u32::from(account),
|
|
|
|
":max_height": u32::from(max_height)
|
|
|
|
])?;
|
2022-10-25 11:04:02 -07:00
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let taddr_str: String = row.get(0)?;
|
2023-06-09 10:02:00 -07:00
|
|
|
let taddr = TransparentAddress::decode(params, &taddr_str)?;
|
2022-10-25 11:04:02 -07:00
|
|
|
let value = Amount::from_i64(row.get(1)?).unwrap();
|
|
|
|
|
|
|
|
res.insert(taddr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2021-03-25 22:47:59 -07:00
|
|
|
/// Inserts information about a scanned block into the database.
|
2023-06-14 15:49:16 -07:00
|
|
|
pub(crate) fn put_block(
|
2023-07-01 17:16:23 -07:00
|
|
|
conn: &rusqlite::Transaction<'_>,
|
2021-01-12 17:24:18 -08:00
|
|
|
block_height: BlockHeight,
|
|
|
|
block_hash: BlockHash,
|
|
|
|
block_time: u32,
|
2023-07-01 17:16:23 -07:00
|
|
|
sapling_commitment_tree_size: u32,
|
2021-01-12 17:24:18 -08:00
|
|
|
) -> Result<(), SqliteClientError> {
|
2023-07-01 17:16:23 -07:00
|
|
|
let block_hash_data = conn
|
|
|
|
.query_row(
|
|
|
|
"SELECT hash FROM blocks WHERE height = ?",
|
|
|
|
[u32::from(block_height)],
|
|
|
|
|row| row.get::<_, Vec<u8>>(0),
|
|
|
|
)
|
|
|
|
.optional()?;
|
|
|
|
|
|
|
|
// Ensure that in the case of an upsert, we don't overwrite block data
|
|
|
|
// with information for a block with a different hash.
|
|
|
|
if let Some(bytes) = block_hash_data {
|
|
|
|
let expected_hash = BlockHash::try_from_slice(&bytes).ok_or_else(|| {
|
|
|
|
SqliteClientError::CorruptedData(format!(
|
|
|
|
"Invalid block hash at height {}",
|
|
|
|
u32::from(block_height)
|
|
|
|
))
|
|
|
|
})?;
|
|
|
|
if expected_hash != block_hash {
|
|
|
|
return Err(SqliteClientError::BlockConflict(block_height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 15:49:16 -07:00
|
|
|
let mut stmt_upsert_block = conn.prepare_cached(
|
2023-04-03 12:53:43 -07:00
|
|
|
"INSERT INTO blocks (
|
|
|
|
height,
|
|
|
|
hash,
|
|
|
|
time,
|
|
|
|
sapling_commitment_tree_size,
|
|
|
|
sapling_tree
|
|
|
|
)
|
2023-06-14 15:49:16 -07:00
|
|
|
VALUES (
|
|
|
|
:height,
|
|
|
|
:hash,
|
|
|
|
:block_time,
|
|
|
|
:sapling_commitment_tree_size,
|
|
|
|
x'00'
|
|
|
|
)
|
|
|
|
ON CONFLICT (height) DO UPDATE
|
|
|
|
SET hash = :hash,
|
|
|
|
time = :block_time,
|
|
|
|
sapling_commitment_tree_size = :sapling_commitment_tree_size",
|
2023-06-06 11:35:39 -07:00
|
|
|
)?;
|
|
|
|
|
2023-06-14 15:49:16 -07:00
|
|
|
stmt_upsert_block.execute(named_params![
|
|
|
|
":height": u32::from(block_height),
|
|
|
|
":hash": &block_hash.0[..],
|
|
|
|
":block_time": block_time,
|
|
|
|
":sapling_commitment_tree_size": sapling_commitment_tree_size
|
2023-06-06 11:35:39 -07:00
|
|
|
])?;
|
|
|
|
|
|
|
|
Ok(())
|
2021-01-12 17:24:18 -08:00
|
|
|
}
|
|
|
|
|
2021-03-25 22:47:59 -07:00
|
|
|
/// Inserts information about a mined transaction that was observed to
|
2021-03-25 21:22:45 -07:00
|
|
|
/// contain a note related to this wallet into the database.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn put_tx_meta<N>(
|
|
|
|
conn: &rusqlite::Connection,
|
2021-03-09 19:55:44 -08:00
|
|
|
tx: &WalletTx<N>,
|
2021-01-12 17:24:18 -08:00
|
|
|
height: BlockHeight,
|
|
|
|
) -> Result<i64, SqliteClientError> {
|
2023-06-06 11:35:39 -07:00
|
|
|
// It isn't there, so insert our transaction into the database.
|
2023-06-09 10:02:00 -07:00
|
|
|
let mut stmt_upsert_tx_meta = conn.prepare_cached(
|
2023-06-06 11:35:39 -07:00
|
|
|
"INSERT INTO transactions (txid, block, tx_index)
|
2023-06-07 14:03:20 -07:00
|
|
|
VALUES (:txid, :block, :tx_index)
|
2023-06-06 11:35:39 -07:00
|
|
|
ON CONFLICT (txid) DO UPDATE
|
|
|
|
SET block = :block,
|
|
|
|
tx_index = :tx_index
|
|
|
|
RETURNING id_tx",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let tx_params = named_params![
|
|
|
|
":txid": &tx.txid.as_ref()[..],
|
|
|
|
":block": u32::from(height),
|
|
|
|
":tx_index": i64::try_from(tx.index).expect("transaction indices are representable as i64"),
|
|
|
|
];
|
|
|
|
|
|
|
|
stmt_upsert_tx_meta
|
|
|
|
.query_row(tx_params, |row| row.get::<_, i64>(0))
|
|
|
|
.map_err(SqliteClientError::from)
|
2021-01-12 17:24:18 -08:00
|
|
|
}
|
|
|
|
|
2021-03-25 22:47:59 -07:00
|
|
|
/// Inserts full transaction data into the database.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn put_tx_data(
|
|
|
|
conn: &rusqlite::Connection,
|
2021-01-12 17:24:18 -08:00
|
|
|
tx: &Transaction,
|
2022-09-01 20:03:39 -07:00
|
|
|
fee: Option<Amount>,
|
2021-01-12 17:24:18 -08:00
|
|
|
created_at: Option<time::OffsetDateTime>,
|
|
|
|
) -> Result<i64, SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
let mut stmt_upsert_tx_data = conn.prepare_cached(
|
2023-06-06 11:35:39 -07:00
|
|
|
"INSERT INTO transactions (txid, created, expiry_height, raw, fee)
|
|
|
|
VALUES (:txid, :created_at, :expiry_height, :raw, :fee)
|
|
|
|
ON CONFLICT (txid) DO UPDATE
|
|
|
|
SET expiry_height = :expiry_height,
|
|
|
|
raw = :raw,
|
|
|
|
fee = IFNULL(:fee, fee)
|
|
|
|
RETURNING id_tx",
|
|
|
|
)?;
|
2021-01-12 17:24:18 -08:00
|
|
|
|
2023-06-06 11:35:39 -07:00
|
|
|
let txid = tx.txid();
|
2021-01-12 17:24:18 -08:00
|
|
|
let mut raw_tx = vec![];
|
|
|
|
tx.write(&mut raw_tx)?;
|
|
|
|
|
2023-06-06 11:35:39 -07:00
|
|
|
let tx_params = named_params![
|
|
|
|
":txid": &txid.as_ref()[..],
|
|
|
|
":created_at": created_at,
|
|
|
|
":expiry_height": u32::from(tx.expiry_height()),
|
|
|
|
":raw": raw_tx,
|
|
|
|
":fee": fee.map(i64::from),
|
|
|
|
];
|
|
|
|
|
|
|
|
stmt_upsert_tx_data
|
|
|
|
.query_row(tx_params, |row| row.get::<_, i64>(0))
|
|
|
|
.map_err(SqliteClientError::from)
|
2021-01-12 17:24:18 -08:00
|
|
|
}
|
|
|
|
|
2022-01-20 13:33:29 -08:00
|
|
|
/// Marks the given UTXO as having been spent.
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2023-06-08 07:53:26 -07:00
|
|
|
pub(crate) fn mark_transparent_utxo_spent(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
2020-12-22 06:10:13 -08:00
|
|
|
tx_ref: i64,
|
|
|
|
outpoint: &OutPoint,
|
|
|
|
) -> Result<(), SqliteClientError> {
|
2023-06-08 07:53:26 -07:00
|
|
|
let mut stmt_mark_transparent_utxo_spent = conn.prepare_cached(
|
|
|
|
"UPDATE utxos SET spent_in_tx = :spent_in_tx
|
|
|
|
WHERE prevout_txid = :prevout_txid
|
|
|
|
AND prevout_idx = :prevout_idx",
|
|
|
|
)?;
|
2020-12-22 06:10:13 -08:00
|
|
|
|
2023-06-08 07:53:26 -07:00
|
|
|
let sql_args = named_params![
|
|
|
|
":spent_in_tx": &tx_ref,
|
|
|
|
":prevout_txid": &outpoint.hash().to_vec(),
|
|
|
|
":prevout_idx": &outpoint.n(),
|
|
|
|
];
|
|
|
|
|
|
|
|
stmt_mark_transparent_utxo_spent.execute(sql_args)?;
|
2020-12-22 06:10:13 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-20 13:33:29 -08:00
|
|
|
/// Adds the given received UTXO to the datastore.
|
2021-10-04 13:09:02 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2023-06-08 07:53:26 -07:00
|
|
|
pub(crate) fn put_received_transparent_utxo<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
2023-06-08 07:53:26 -07:00
|
|
|
params: &P,
|
2020-12-22 06:10:13 -08:00
|
|
|
output: &WalletTransparentOutput,
|
|
|
|
) -> Result<UtxoId, SqliteClientError> {
|
2023-06-08 07:53:26 -07:00
|
|
|
let address_str = output.recipient_address().encode(params);
|
|
|
|
let account_id = conn
|
|
|
|
.query_row(
|
|
|
|
"SELECT account FROM addresses WHERE cached_transparent_receiver_address = :address",
|
|
|
|
named_params![":address": &address_str],
|
|
|
|
|row| row.get::<_, u32>(0).map(AccountId::from),
|
|
|
|
)
|
|
|
|
.optional()?;
|
|
|
|
|
|
|
|
let utxoid = if let Some(account) = account_id {
|
|
|
|
put_legacy_transparent_utxo(conn, params, output, account)?
|
|
|
|
} else {
|
|
|
|
// If the UTXO is received at the legacy transparent address, there may be no entry in the
|
|
|
|
// addresses table that can be used to tie the address to a particular account. In this
|
|
|
|
// case, we should look up the legacy address for account 0 and check whether it matches
|
|
|
|
// the address for the received UTXO, and if so then insert/update it directly.
|
|
|
|
let account = AccountId::from(0u32);
|
|
|
|
get_legacy_transparent_address(params, conn, account).and_then(|legacy_taddr| {
|
|
|
|
if legacy_taddr
|
|
|
|
.iter()
|
|
|
|
.any(|(taddr, _)| taddr == output.recipient_address())
|
|
|
|
{
|
|
|
|
put_legacy_transparent_utxo(conn, params, output, account)
|
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
} else {
|
|
|
|
Err(SqliteClientError::AddressNotRecognized(
|
|
|
|
*output.recipient_address(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
})?
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(utxoid)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
pub(crate) fn put_legacy_transparent_utxo<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
2023-06-08 07:53:26 -07:00
|
|
|
params: &P,
|
|
|
|
output: &WalletTransparentOutput,
|
|
|
|
received_by_account: AccountId,
|
|
|
|
) -> Result<UtxoId, rusqlite::Error> {
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
let mut stmt_upsert_legacy_transparent_utxo = conn.prepare_cached(
|
|
|
|
"INSERT INTO utxos (
|
2023-06-14 15:49:16 -07:00
|
|
|
prevout_txid, prevout_idx,
|
2023-06-08 07:53:26 -07:00
|
|
|
received_by_account, address, script,
|
|
|
|
value_zat, height)
|
|
|
|
VALUES
|
|
|
|
(:prevout_txid, :prevout_idx,
|
|
|
|
:received_by_account, :address, :script,
|
|
|
|
:value_zat, :height)
|
|
|
|
ON CONFLICT (prevout_txid, prevout_idx) DO UPDATE
|
|
|
|
SET received_by_account = :received_by_account,
|
|
|
|
height = :height,
|
|
|
|
address = :address,
|
|
|
|
script = :script,
|
|
|
|
value_zat = :value_zat
|
|
|
|
RETURNING id_utxo",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let sql_args = named_params![
|
|
|
|
":prevout_txid": &output.outpoint().hash().to_vec(),
|
|
|
|
":prevout_idx": &output.outpoint().n(),
|
|
|
|
":received_by_account": &u32::from(received_by_account),
|
|
|
|
":address": &output.recipient_address().encode(params),
|
|
|
|
":script": &output.txout().script_pubkey.0,
|
|
|
|
":value_zat": &i64::from(output.txout().value),
|
|
|
|
":height": &u32::from(output.height()),
|
|
|
|
];
|
|
|
|
|
|
|
|
stmt_upsert_legacy_transparent_utxo.query_row(sql_args, |row| row.get::<_, i64>(0).map(UtxoId))
|
2021-03-26 17:39:43 -07:00
|
|
|
}
|
|
|
|
|
2021-03-25 22:47:59 -07:00
|
|
|
/// Marks notes that have not been mined in transactions
|
2021-03-25 21:22:45 -07:00
|
|
|
/// as expired, up to the given block height.
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn update_expired_notes(
|
|
|
|
conn: &rusqlite::Connection,
|
2023-07-05 22:08:00 -07:00
|
|
|
expiry_height: BlockHeight,
|
2021-01-12 17:24:18 -08:00
|
|
|
) -> Result<(), SqliteClientError> {
|
2023-06-09 10:02:00 -07:00
|
|
|
let mut stmt_update_expired = conn.prepare_cached(
|
2023-06-16 14:07:53 -07:00
|
|
|
"UPDATE sapling_received_notes SET spent = NULL WHERE EXISTS (
|
|
|
|
SELECT id_tx FROM transactions
|
|
|
|
WHERE id_tx = sapling_received_notes.spent AND block IS NULL AND expiry_height < ?
|
|
|
|
)",
|
|
|
|
)?;
|
2023-07-05 22:08:00 -07:00
|
|
|
stmt_update_expired.execute([u32::from(expiry_height)])?;
|
2023-06-09 10:02:00 -07:00
|
|
|
Ok(())
|
2021-01-12 17:24:18 -08:00
|
|
|
}
|
|
|
|
|
2023-06-07 14:41:52 -07:00
|
|
|
// A utility function for creation of parameters for use in `insert_sent_output`
|
|
|
|
// and `put_sent_output`
|
|
|
|
fn recipient_params<P: consensus::Parameters>(
|
|
|
|
params: &P,
|
|
|
|
to: &Recipient,
|
|
|
|
) -> (Option<String>, Option<u32>, PoolType) {
|
|
|
|
match to {
|
|
|
|
Recipient::Transparent(addr) => (Some(addr.encode(params)), None, PoolType::Transparent),
|
2023-06-26 09:12:59 -07:00
|
|
|
Recipient::Sapling(addr) => (
|
|
|
|
Some(addr.encode(params)),
|
|
|
|
None,
|
|
|
|
PoolType::Shielded(ShieldedProtocol::Sapling),
|
|
|
|
),
|
2023-06-07 14:41:52 -07:00
|
|
|
Recipient::Unified(addr, pool) => (Some(addr.encode(params)), None, *pool),
|
|
|
|
Recipient::InternalAccount(id, pool) => (None, Some(u32::from(*id)), *pool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:38:43 -07:00
|
|
|
/// Records information about a transaction output that your wallet created.
|
2023-06-07 14:41:52 -07:00
|
|
|
pub(crate) fn insert_sent_output<P: consensus::Parameters>(
|
2023-06-09 10:02:00 -07:00
|
|
|
conn: &rusqlite::Connection,
|
2023-06-07 14:41:52 -07:00
|
|
|
params: &P,
|
2021-04-13 10:02:35 -07:00
|
|
|
tx_ref: i64,
|
2022-10-10 11:38:43 -07:00
|
|
|
from_account: AccountId,
|
|
|
|
output: &SentTransactionOutput,
|
2021-04-13 10:02:35 -07:00
|
|
|
) -> Result<(), SqliteClientError> {
|
2023-06-07 14:41:52 -07:00
|
|
|
let mut stmt_insert_sent_output = conn.prepare_cached(
|
|
|
|
"INSERT INTO sent_notes (
|
|
|
|
tx, output_pool, output_index, from_account,
|
|
|
|
to_address, to_account, value, memo)
|
|
|
|
VALUES (
|
|
|
|
:tx, :output_pool, :output_index, :from_account,
|
|
|
|
:to_address, :to_account, :value, :memo)",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let (to_address, to_account, pool_type) = recipient_params(params, output.recipient());
|
|
|
|
let sql_args = named_params![
|
|
|
|
":tx": &tx_ref,
|
|
|
|
":output_pool": &pool_code(pool_type),
|
|
|
|
":output_index": &i64::try_from(output.output_index()).unwrap(),
|
|
|
|
":from_account": &u32::from(from_account),
|
|
|
|
":to_address": &to_address,
|
|
|
|
":to_account": &to_account,
|
|
|
|
":value": &i64::from(output.value()),
|
2023-08-01 11:00:10 -07:00
|
|
|
":memo": memo_repr(output.memo())
|
2023-06-07 14:41:52 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
stmt_insert_sent_output.execute(sql_args)?;
|
|
|
|
|
|
|
|
Ok(())
|
2021-04-13 10:02:35 -07:00
|
|
|
}
|
|
|
|
|
2023-06-12 11:17:20 -07:00
|
|
|
/// Records information about a transaction output that your wallet created, from the constituent
|
|
|
|
/// properties of that output.
|
2021-03-23 22:54:15 -07:00
|
|
|
///
|
2023-06-12 11:17:20 -07:00
|
|
|
/// - If `recipient` is a Unified address, `output_index` is an index into the outputs of the
|
|
|
|
/// transaction within the bundle associated with the recipient's output pool.
|
|
|
|
/// - If `recipient` is a Sapling address, `output_index` is an index into the Sapling outputs of
|
|
|
|
/// the transaction.
|
|
|
|
/// - If `recipient` is a transparent address, `output_index` is an index into the transparent
|
|
|
|
/// outputs of the transaction.
|
|
|
|
/// - If `recipient` is an internal account, `output_index` is an index into the Sapling outputs of
|
|
|
|
/// the transaction.
|
2022-10-10 11:38:43 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-06-09 10:02:00 -07:00
|
|
|
pub(crate) fn put_sent_output<P: consensus::Parameters>(
|
|
|
|
conn: &rusqlite::Connection,
|
2023-06-07 14:41:52 -07:00
|
|
|
params: &P,
|
2022-10-10 11:38:43 -07:00
|
|
|
from_account: AccountId,
|
2021-01-12 17:24:18 -08:00
|
|
|
tx_ref: i64,
|
|
|
|
output_index: usize,
|
2022-10-10 11:38:43 -07:00
|
|
|
recipient: &Recipient,
|
2021-01-12 17:24:18 -08:00
|
|
|
value: Amount,
|
2021-03-17 08:21:10 -07:00
|
|
|
memo: Option<&MemoBytes>,
|
2021-01-12 17:24:18 -08:00
|
|
|
) -> Result<(), SqliteClientError> {
|
2023-06-07 14:41:52 -07:00
|
|
|
let mut stmt_upsert_sent_output = conn.prepare_cached(
|
|
|
|
"INSERT INTO sent_notes (
|
|
|
|
tx, output_pool, output_index, from_account,
|
|
|
|
to_address, to_account, value, memo)
|
|
|
|
VALUES (
|
|
|
|
:tx, :output_pool, :output_index, :from_account,
|
|
|
|
:to_address, :to_account, :value, :memo)
|
|
|
|
ON CONFLICT (tx, output_pool, output_index) DO UPDATE
|
|
|
|
SET from_account = :from_account,
|
|
|
|
to_address = :to_address,
|
|
|
|
to_account = :to_account,
|
|
|
|
value = :value,
|
|
|
|
memo = IFNULL(:memo, memo)",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let (to_address, to_account, pool_type) = recipient_params(params, recipient);
|
|
|
|
let sql_args = named_params![
|
|
|
|
":tx": &tx_ref,
|
|
|
|
":output_pool": &pool_code(pool_type),
|
|
|
|
":output_index": &i64::try_from(output_index).unwrap(),
|
|
|
|
":from_account": &u32::from(from_account),
|
|
|
|
":to_address": &to_address,
|
|
|
|
":to_account": &to_account,
|
|
|
|
":value": &i64::from(value),
|
2023-06-16 14:07:29 -07:00
|
|
|
":memo": memo_repr(memo)
|
2023-06-07 14:41:52 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
stmt_upsert_sent_output.execute(sql_args)?;
|
2022-10-10 11:38:43 -07:00
|
|
|
|
|
|
|
Ok(())
|
2021-04-13 10:02:35 -07:00
|
|
|
}
|
|
|
|
|
2023-07-21 11:01:52 -07:00
|
|
|
/// Inserts the given entries into the nullifier map.
|
|
|
|
///
|
|
|
|
/// Returns an error if the new entries conflict with existing ones. This indicates either
|
|
|
|
/// corrupted data, or that a reorg has occurred and the caller needs to repair the wallet
|
|
|
|
/// state with [`truncate_to_height`].
|
|
|
|
pub(crate) fn insert_nullifier_map<N: AsRef<[u8]>>(
|
|
|
|
conn: &rusqlite::Transaction<'_>,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
spend_pool: ShieldedProtocol,
|
|
|
|
new_entries: &[(TxId, u16, Vec<N>)],
|
|
|
|
) -> Result<(), SqliteClientError> {
|
|
|
|
let mut stmt_select_tx_locators = conn.prepare_cached(
|
|
|
|
"SELECT block_height, tx_index, txid
|
|
|
|
FROM tx_locator_map
|
|
|
|
WHERE (block_height = :block_height AND tx_index = :tx_index) OR txid = :txid",
|
|
|
|
)?;
|
|
|
|
let mut stmt_insert_tx_locator = conn.prepare_cached(
|
|
|
|
"INSERT INTO tx_locator_map
|
|
|
|
(block_height, tx_index, txid)
|
|
|
|
VALUES (:block_height, :tx_index, :txid)",
|
|
|
|
)?;
|
|
|
|
let mut stmt_insert_nullifier_mapping = conn.prepare_cached(
|
|
|
|
"INSERT INTO nullifier_map
|
|
|
|
(spend_pool, nf, block_height, tx_index)
|
|
|
|
VALUES (:spend_pool, :nf, :block_height, :tx_index)
|
|
|
|
ON CONFLICT (spend_pool, nf) DO UPDATE
|
|
|
|
SET block_height = :block_height,
|
|
|
|
tx_index = :tx_index",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
for (txid, tx_index, nullifiers) in new_entries {
|
|
|
|
let tx_args = named_params![
|
|
|
|
":block_height": u32::from(block_height),
|
|
|
|
":tx_index": tx_index,
|
|
|
|
":txid": txid.as_ref(),
|
|
|
|
];
|
|
|
|
|
|
|
|
// We cannot use an upsert here, because we use the tx locator as the foreign key
|
|
|
|
// in `nullifier_map` instead of `txid` for database size efficiency. If an insert
|
|
|
|
// into `tx_locator_map` were to conflict, we would need the resulting update to
|
|
|
|
// cascade into `nullifier_map` as either:
|
|
|
|
// - an update (if a transaction moved within a block), or
|
|
|
|
// - a deletion (if the locator now points to a different transaction).
|
|
|
|
//
|
|
|
|
// `ON UPDATE` has `CASCADE` to always update, but has no deletion option. So we
|
|
|
|
// instead set `ON UPDATE RESTRICT` on the foreign key relation, and require the
|
|
|
|
// caller to manually rewind the database in this situation.
|
|
|
|
let locator = stmt_select_tx_locators
|
|
|
|
.query_map(tx_args, |row| {
|
|
|
|
Ok((
|
|
|
|
BlockHeight::from_u32(row.get(0)?),
|
|
|
|
row.get::<_, u16>(1)?,
|
|
|
|
TxId::from_bytes(row.get(2)?),
|
|
|
|
))
|
|
|
|
})?
|
|
|
|
.fold(Ok(None), |acc: Result<_, SqliteClientError>, row| {
|
|
|
|
match (acc?, row?) {
|
|
|
|
(None, rhs) => Ok(Some(Some(rhs))),
|
|
|
|
// If there was more than one row, then due to the uniqueness
|
|
|
|
// constraints on the `tx_locator_map` table, all of the rows conflict
|
|
|
|
// with the locator being inserted.
|
|
|
|
(Some(_), _) => Ok(Some(None)),
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
|
|
|
match locator {
|
|
|
|
// If the locator in the table matches the one being inserted, do nothing.
|
|
|
|
Some(Some(loc)) if loc == (block_height, *tx_index, *txid) => (),
|
|
|
|
// If the locator being inserted would conflict, report it.
|
|
|
|
Some(_) => Err(SqliteClientError::DbError(rusqlite::Error::SqliteFailure(
|
|
|
|
rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_CONSTRAINT),
|
|
|
|
Some("UNIQUE constraint failed: tx_locator_map.block_height, tx_locator_map.tx_index".into()),
|
|
|
|
)))?,
|
|
|
|
// If the locator doesn't exist, insert it.
|
|
|
|
None => stmt_insert_tx_locator.execute(tx_args).map(|_| ())?,
|
|
|
|
}
|
|
|
|
|
|
|
|
for nf in nullifiers {
|
|
|
|
// Here it is okay to use an upsert, because per above we've confirmed that
|
|
|
|
// the locator points to the same transaction.
|
|
|
|
let nf_args = named_params![
|
|
|
|
":spend_pool": pool_code(PoolType::Shielded(spend_pool)),
|
|
|
|
":nf": nf.as_ref(),
|
|
|
|
":block_height": u32::from(block_height),
|
|
|
|
":tx_index": tx_index,
|
|
|
|
];
|
|
|
|
stmt_insert_nullifier_mapping.execute(nf_args)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the row of the `transactions` table corresponding to the transaction in which
|
|
|
|
/// this nullifier is revealed, if any.
|
|
|
|
pub(crate) fn query_nullifier_map<N: AsRef<[u8]>>(
|
|
|
|
conn: &rusqlite::Transaction<'_>,
|
|
|
|
spend_pool: ShieldedProtocol,
|
|
|
|
nf: &N,
|
|
|
|
) -> Result<Option<i64>, SqliteClientError> {
|
|
|
|
let mut stmt_select_locator = conn.prepare_cached(
|
|
|
|
"SELECT block_height, tx_index, txid
|
|
|
|
FROM nullifier_map
|
|
|
|
LEFT JOIN tx_locator_map USING (block_height, tx_index)
|
|
|
|
WHERE spend_pool = :spend_pool AND nf = :nf",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let sql_args = named_params![
|
|
|
|
":spend_pool": pool_code(PoolType::Shielded(spend_pool)),
|
|
|
|
":nf": nf.as_ref(),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Find the locator corresponding to this nullifier, if any.
|
|
|
|
let locator = stmt_select_locator
|
|
|
|
.query_row(sql_args, |row| {
|
|
|
|
Ok((
|
|
|
|
BlockHeight::from_u32(row.get(0)?),
|
|
|
|
row.get(1)?,
|
|
|
|
TxId::from_bytes(row.get(2)?),
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.optional()?;
|
|
|
|
let (height, index, txid) = match locator {
|
|
|
|
Some(res) => res,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Find or create a corresponding row in the `transactions` table. Usually a row will
|
|
|
|
// have been created during the same scan that the locator was added to the nullifier
|
|
|
|
// map, but it would not happen if the transaction in question spent the note with no
|
|
|
|
// change or explicit in-wallet recipient.
|
|
|
|
put_tx_meta(
|
|
|
|
conn,
|
|
|
|
&WalletTx::<N> {
|
|
|
|
txid,
|
|
|
|
index,
|
|
|
|
sapling_spends: vec![],
|
|
|
|
sapling_outputs: vec![],
|
|
|
|
},
|
|
|
|
height,
|
|
|
|
)
|
|
|
|
.map(Some)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deletes from the nullifier map any entries with a locator referencing a block height
|
|
|
|
/// lower than the pruning height.
|
|
|
|
pub(crate) fn prune_nullifier_map(
|
|
|
|
conn: &rusqlite::Transaction<'_>,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<(), SqliteClientError> {
|
2023-07-25 06:46:44 -07:00
|
|
|
let mut stmt_delete_locators = conn.prepare_cached(
|
2023-07-21 11:01:52 -07:00
|
|
|
"DELETE FROM tx_locator_map
|
|
|
|
WHERE block_height < :block_height",
|
|
|
|
)?;
|
|
|
|
|
2023-07-25 06:46:44 -07:00
|
|
|
stmt_delete_locators.execute(named_params![":block_height": u32::from(block_height)])?;
|
|
|
|
|
2023-07-21 11:01:52 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-03-08 18:53:38 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-06-30 11:37:41 -07:00
|
|
|
use std::num::NonZeroU32;
|
|
|
|
|
2021-03-31 14:59:36 -07:00
|
|
|
use zcash_primitives::transaction::components::Amount;
|
2019-03-08 18:53:38 -08:00
|
|
|
|
2023-08-16 10:15:10 -07:00
|
|
|
use zcash_client_backend::data_api::{AccountBirthday, WalletRead};
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2023-08-16 10:15:10 -07:00
|
|
|
use crate::{testing::TestBuilder, AccountId};
|
2019-03-08 18:53:38 -08:00
|
|
|
|
2023-04-03 12:31:45 -07:00
|
|
|
use super::get_balance;
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2022-10-01 11:58:01 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
use {
|
2023-08-16 10:15:10 -07:00
|
|
|
incrementalmerkletree::frontier::Frontier,
|
2023-08-31 16:56:04 -07:00
|
|
|
secrecy::Secret,
|
2022-10-13 08:13:14 -07:00
|
|
|
zcash_client_backend::{
|
|
|
|
data_api::WalletWrite, encoding::AddressCodec, wallet::WalletTransparentOutput,
|
|
|
|
},
|
2022-10-01 11:58:01 -07:00
|
|
|
zcash_primitives::{
|
|
|
|
consensus::BlockHeight,
|
|
|
|
transaction::components::{OutPoint, TxOut},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-08 18:53:38 -08:00
|
|
|
#[test]
|
|
|
|
fn empty_database_has_no_balance() {
|
2023-08-28 11:54:35 -07:00
|
|
|
let st = TestBuilder::new()
|
2023-08-16 10:15:10 -07:00
|
|
|
.with_test_account(AccountBirthday::from_sapling_activation)
|
2023-08-28 09:31:02 -07:00
|
|
|
.build();
|
2019-03-08 18:53:38 -08:00
|
|
|
|
|
|
|
// The account should be empty
|
2022-02-10 08:47:42 -08:00
|
|
|
assert_eq!(
|
2023-08-28 11:54:35 -07:00
|
|
|
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
|
2022-02-10 08:47:42 -08:00
|
|
|
Amount::zero()
|
|
|
|
);
|
2019-03-08 18:53:38 -08:00
|
|
|
|
2020-09-10 15:55:34 -07:00
|
|
|
// We can't get an anchor height, as we have not scanned any blocks.
|
2023-06-30 11:37:41 -07:00
|
|
|
assert_eq!(
|
2023-08-28 11:54:35 -07:00
|
|
|
st.wallet()
|
2023-06-30 11:37:41 -07:00
|
|
|
.get_target_and_anchor_heights(NonZeroU32::new(10).unwrap())
|
|
|
|
.unwrap(),
|
|
|
|
None
|
|
|
|
);
|
2019-03-08 18:53:38 -08:00
|
|
|
|
|
|
|
// An invalid account has zero balance
|
2023-06-09 10:02:00 -07:00
|
|
|
assert_matches!(
|
2023-08-28 11:54:35 -07:00
|
|
|
st.wallet().get_current_address(AccountId::from(1)),
|
2023-06-09 10:02:00 -07:00
|
|
|
Ok(None)
|
|
|
|
);
|
2022-02-10 08:47:42 -08:00
|
|
|
assert_eq!(
|
2023-08-28 11:54:35 -07:00
|
|
|
get_balance(&st.wallet().conn, AccountId::from(0)).unwrap(),
|
2022-02-10 08:47:42 -08:00
|
|
|
Amount::zero()
|
|
|
|
);
|
2019-03-08 18:53:38 -08:00
|
|
|
}
|
2022-10-01 11:58:01 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
fn put_received_transparent_utxo() {
|
2023-08-16 10:15:10 -07:00
|
|
|
use crate::testing::TestBuilder;
|
|
|
|
|
2023-08-28 11:54:35 -07:00
|
|
|
let mut st = TestBuilder::new().build();
|
2022-10-01 11:58:01 -07:00
|
|
|
|
|
|
|
// Add an account to the wallet
|
|
|
|
let seed = Secret::new([0u8; 32].to_vec());
|
2023-08-16 10:15:10 -07:00
|
|
|
let birthday =
|
|
|
|
AccountBirthday::from_parts(st.sapling_activation_height(), Frontier::empty(), None);
|
|
|
|
let (account_id, _usk) = st.wallet_mut().create_account(&seed, birthday).unwrap();
|
2023-08-28 11:54:35 -07:00
|
|
|
let uaddr = st
|
2023-08-28 09:31:02 -07:00
|
|
|
.wallet()
|
|
|
|
.get_current_address(account_id)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2022-10-01 11:58:01 -07:00
|
|
|
let taddr = uaddr.transparent().unwrap();
|
|
|
|
|
2023-08-28 11:54:35 -07:00
|
|
|
let bal_absent = st
|
2023-08-28 09:31:02 -07:00
|
|
|
.wallet()
|
2022-12-08 10:52:11 -08:00
|
|
|
.get_transparent_balances(account_id, BlockHeight::from_u32(12345))
|
|
|
|
.unwrap();
|
|
|
|
assert!(bal_absent.is_empty());
|
|
|
|
|
2022-10-14 10:08:32 -07:00
|
|
|
let utxo = WalletTransparentOutput::from_parts(
|
|
|
|
OutPoint::new([1u8; 32], 1),
|
|
|
|
TxOut {
|
2022-10-01 11:58:01 -07:00
|
|
|
value: Amount::from_u64(100000).unwrap(),
|
|
|
|
script_pubkey: taddr.script(),
|
|
|
|
},
|
2022-10-14 10:08:32 -07:00
|
|
|
BlockHeight::from_u32(12345),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-10-01 11:58:01 -07:00
|
|
|
|
2023-08-28 11:54:35 -07:00
|
|
|
let res0 = st.wallet_mut().put_received_transparent_utxo(&utxo);
|
2022-12-08 11:06:32 -08:00
|
|
|
assert_matches!(res0, Ok(_));
|
2022-10-01 11:58:01 -07:00
|
|
|
|
2022-10-14 10:08:32 -07:00
|
|
|
// Change the mined height of the UTXO and upsert; we should get back
|
2022-10-01 11:58:01 -07:00
|
|
|
// the same utxoid
|
2022-10-14 10:08:32 -07:00
|
|
|
let utxo2 = WalletTransparentOutput::from_parts(
|
|
|
|
OutPoint::new([1u8; 32], 1),
|
|
|
|
TxOut {
|
|
|
|
value: Amount::from_u64(100000).unwrap(),
|
|
|
|
script_pubkey: taddr.script(),
|
|
|
|
},
|
|
|
|
BlockHeight::from_u32(34567),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-08-28 11:54:35 -07:00
|
|
|
let res1 = st.wallet_mut().put_received_transparent_utxo(&utxo2);
|
2022-12-08 11:06:32 -08:00
|
|
|
assert_matches!(res1, Ok(id) if id == res0.unwrap());
|
2022-10-01 11:58:01 -07:00
|
|
|
|
2022-12-08 11:06:32 -08:00
|
|
|
assert_matches!(
|
2023-08-28 11:54:35 -07:00
|
|
|
st.wallet().get_unspent_transparent_outputs(
|
2022-10-01 11:58:01 -07:00
|
|
|
taddr,
|
2022-11-09 07:13:34 -08:00
|
|
|
BlockHeight::from_u32(12345),
|
|
|
|
&[]
|
2022-10-01 11:58:01 -07:00
|
|
|
),
|
|
|
|
Ok(utxos) if utxos.is_empty()
|
2022-12-08 11:06:32 -08:00
|
|
|
);
|
2022-10-01 11:58:01 -07:00
|
|
|
|
2022-12-08 11:06:32 -08:00
|
|
|
assert_matches!(
|
2023-08-28 11:54:35 -07:00
|
|
|
st.wallet().get_unspent_transparent_outputs(
|
2022-10-01 11:58:01 -07:00
|
|
|
taddr,
|
2022-11-09 07:13:34 -08:00
|
|
|
BlockHeight::from_u32(34567),
|
|
|
|
&[]
|
2022-10-01 11:58:01 -07:00
|
|
|
),
|
|
|
|
Ok(utxos) if {
|
|
|
|
utxos.len() == 1 &&
|
2022-10-14 10:08:32 -07:00
|
|
|
utxos.iter().any(|rutxo| rutxo.height() == utxo2.height())
|
2022-10-01 11:58:01 -07:00
|
|
|
}
|
2022-12-08 11:06:32 -08:00
|
|
|
);
|
2022-10-13 08:13:14 -07:00
|
|
|
|
2022-12-08 10:52:11 -08:00
|
|
|
assert_matches!(
|
2023-08-28 11:54:35 -07:00
|
|
|
st.wallet().get_transparent_balances(account_id, BlockHeight::from_u32(34567)),
|
2022-12-08 10:52:11 -08:00
|
|
|
Ok(h) if h.get(taddr) == Amount::from_u64(100000).ok().as_ref()
|
|
|
|
);
|
|
|
|
|
2022-10-13 08:13:14 -07:00
|
|
|
// Artificially delete the address from the addresses table so that
|
|
|
|
// we can ensure the update fails if the join doesn't work.
|
2023-08-28 11:54:35 -07:00
|
|
|
st.wallet()
|
2022-10-13 08:13:14 -07:00
|
|
|
.conn
|
|
|
|
.execute(
|
|
|
|
"DELETE FROM addresses WHERE cached_transparent_receiver_address = ?",
|
2023-08-28 11:54:35 -07:00
|
|
|
[Some(taddr.encode(&st.wallet().params))],
|
2022-10-13 08:13:14 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-08-28 11:54:35 -07:00
|
|
|
let res2 = st.wallet_mut().put_received_transparent_utxo(&utxo2);
|
2022-12-08 11:06:32 -08:00
|
|
|
assert_matches!(res2, Err(_));
|
2022-10-01 11:58:01 -07:00
|
|
|
}
|
2019-03-08 18:53:38 -08:00
|
|
|
}
|