2019-03-08 18:23:31 -08:00
|
|
|
//! Functions for querying information in the data database.
|
|
|
|
|
2020-08-06 17:13:24 -07:00
|
|
|
use rusqlite::{OptionalExtension, NO_PARAMS};
|
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,
|
|
|
|
consensus::{self, BlockHeight, NetworkUpgrade},
|
2020-08-18 12:48:59 -07:00
|
|
|
merkle_tree::{CommitmentTree, IncrementalWitness},
|
2020-08-05 16:01:22 -07:00
|
|
|
note_encryption::Memo,
|
|
|
|
primitives::PaymentAddress,
|
2020-08-06 17:13:24 -07:00
|
|
|
sapling::Node,
|
2020-08-25 14:20:12 -07:00
|
|
|
transaction::{components::Amount, TxId},
|
2020-08-06 13:11:25 -07:00
|
|
|
zip32::ExtendedFullViewingKey,
|
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::{
|
|
|
|
data_api::{chain::get_target_and_anchor_heights, error::Error},
|
2020-08-06 13:11:25 -07:00
|
|
|
encoding::{decode_extended_full_viewing_key, decode_payment_address},
|
2020-08-05 16:01:22 -07:00
|
|
|
};
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2020-08-06 13:11:25 -07:00
|
|
|
use crate::{error::SqliteClientError, AccountId, DataConnection, NoteId};
|
2019-03-08 18:23:31 -08:00
|
|
|
|
2020-08-25 14:29:01 -07:00
|
|
|
pub mod init;
|
2020-08-25 14:43:28 -07:00
|
|
|
pub mod transact;
|
2020-08-25 14:29:01 -07:00
|
|
|
|
2019-03-08 18:23:31 -08:00
|
|
|
/// Returns the address for the account.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2020-08-05 18:14:45 -07:00
|
|
|
/// use tempfile::NamedTempFile;
|
2020-08-05 16:01:22 -07:00
|
|
|
/// use zcash_primitives::{
|
|
|
|
/// consensus::{self, Network},
|
|
|
|
/// };
|
2020-08-05 18:14:45 -07:00
|
|
|
/// use zcash_client_sqlite::{
|
2020-08-06 13:11:25 -07:00
|
|
|
/// AccountId,
|
2020-08-05 18:14:45 -07:00
|
|
|
/// DataConnection,
|
|
|
|
/// query::get_address,
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// let data_file = NamedTempFile::new().unwrap();
|
|
|
|
/// let db = DataConnection::for_path(data_file).unwrap();
|
2020-08-06 13:11:25 -07:00
|
|
|
/// let addr = get_address(&db, &Network::TestNetwork, AccountId(0));
|
2019-03-08 18:23:31 -08:00
|
|
|
/// ```
|
2020-08-05 16:01:22 -07:00
|
|
|
pub fn get_address<P: consensus::Parameters>(
|
|
|
|
data: &DataConnection,
|
|
|
|
params: &P,
|
2020-08-06 13:11:25 -07:00
|
|
|
account: AccountId,
|
2020-08-05 16:01:22 -07:00
|
|
|
) -> Result<Option<PaymentAddress>, SqliteClientError> {
|
|
|
|
let addr: String = data.0.query_row(
|
2019-03-08 18:23:31 -08:00
|
|
|
"SELECT address FROM accounts
|
|
|
|
WHERE account = ?",
|
2020-08-05 16:01:22 -07:00
|
|
|
&[account.0],
|
2019-03-08 18:23:31 -08:00
|
|
|
|row| row.get(0),
|
|
|
|
)?;
|
|
|
|
|
2020-08-05 16:01:22 -07:00
|
|
|
decode_payment_address(params.hrp_sapling_payment_address(), &addr)
|
|
|
|
.map_err(|e| SqliteClientError(e.into()))
|
2019-03-08 18:23:31 -08: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
|
|
|
|
/// caveat. Use [`get_verified_balance`] where you need a more reliable indication of the
|
|
|
|
/// wallet balance.
|
|
|
|
///
|
2019-03-08 18:53:38 -08:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2020-08-05 18:14:45 -07:00
|
|
|
/// use tempfile::NamedTempFile;
|
|
|
|
/// use zcash_client_sqlite::{
|
2020-08-06 13:11:25 -07:00
|
|
|
/// AccountId,
|
2020-08-05 18:14:45 -07:00
|
|
|
/// DataConnection,
|
|
|
|
/// query::get_balance,
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// let data_file = NamedTempFile::new().unwrap();
|
|
|
|
/// let db = DataConnection::for_path(data_file).unwrap();
|
2020-08-06 13:11:25 -07:00
|
|
|
/// let addr = get_balance(&db, AccountId(0));
|
2019-03-08 18:53:38 -08:00
|
|
|
/// ```
|
2020-08-06 13:11:25 -07:00
|
|
|
pub fn get_balance(data: &DataConnection, account: AccountId) -> Result<Amount, SqliteClientError> {
|
2020-08-05 18:14:45 -07:00
|
|
|
let balance = data.0.query_row(
|
2019-03-08 18:53:38 -08:00
|
|
|
"SELECT SUM(value) FROM received_notes
|
|
|
|
INNER JOIN transactions ON transactions.id_tx = received_notes.tx
|
|
|
|
WHERE account = ? AND spent IS NULL AND transactions.block IS NOT NULL",
|
2020-08-06 09:23:15 -07:00
|
|
|
&[account.0],
|
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),
|
2020-08-05 18:14:45 -07:00
|
|
|
_ => Err(SqliteClientError(Error::CorruptedData(
|
2019-03-08 18:53:38 -08:00
|
|
|
"Sum of values in received_notes is out of range",
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the verified balance for the account, which ignores notes that have been
|
|
|
|
/// received too recently and are not yet deemed spendable.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2020-08-05 18:14:45 -07:00
|
|
|
/// use tempfile::NamedTempFile;
|
|
|
|
/// use zcash_client_sqlite::{
|
2020-08-06 13:11:25 -07:00
|
|
|
/// AccountId,
|
2020-08-05 18:14:45 -07:00
|
|
|
/// DataConnection,
|
|
|
|
/// query::get_verified_balance,
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// let data_file = NamedTempFile::new().unwrap();
|
|
|
|
/// let db = DataConnection::for_path(data_file).unwrap();
|
2020-08-06 13:11:25 -07:00
|
|
|
/// let addr = get_verified_balance(&db, AccountId(0));
|
2019-03-08 18:53:38 -08:00
|
|
|
/// ```
|
2020-08-05 18:14:45 -07:00
|
|
|
pub fn get_verified_balance(
|
|
|
|
data: &DataConnection,
|
2020-08-06 13:11:25 -07:00
|
|
|
account: AccountId,
|
2020-08-05 18:14:45 -07:00
|
|
|
) -> Result<Amount, SqliteClientError> {
|
2020-08-04 14:52:56 -07:00
|
|
|
let (_, anchor_height) = get_target_and_anchor_heights(data)?;
|
2019-03-08 18:53:38 -08:00
|
|
|
|
2020-08-05 18:14:45 -07:00
|
|
|
let balance = data.0.query_row(
|
2019-03-08 18:53:38 -08:00
|
|
|
"SELECT SUM(value) FROM received_notes
|
|
|
|
INNER JOIN transactions ON transactions.id_tx = received_notes.tx
|
|
|
|
WHERE account = ? AND spent IS NULL AND transactions.block <= ?",
|
2020-08-06 09:23:15 -07:00
|
|
|
&[account.0, 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),
|
2020-08-05 18:14:45 -07:00
|
|
|
_ => Err(SqliteClientError(Error::CorruptedData(
|
2019-03-08 18:53:38 -08:00
|
|
|
"Sum of values in received_notes is out of range",
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 19:22:35 -08:00
|
|
|
/// Returns the memo for a received note, if it is known and a valid UTF-8 string.
|
|
|
|
///
|
|
|
|
/// The note is identified by its row index in the `received_notes` table within the data
|
|
|
|
/// database.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2020-08-05 18:14:45 -07:00
|
|
|
/// use tempfile::NamedTempFile;
|
|
|
|
/// use zcash_client_sqlite::{
|
2020-08-06 13:11:25 -07:00
|
|
|
/// NoteId,
|
2020-08-05 18:14:45 -07:00
|
|
|
/// DataConnection,
|
|
|
|
/// query::get_received_memo_as_utf8,
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// let data_file = NamedTempFile::new().unwrap();
|
|
|
|
/// let db = DataConnection::for_path(data_file).unwrap();
|
2020-08-06 13:11:25 -07:00
|
|
|
/// let memo = get_received_memo_as_utf8(&db, NoteId(27));
|
2020-08-05 18:14:45 -07:00
|
|
|
/// ```
|
|
|
|
pub fn get_received_memo_as_utf8(
|
|
|
|
data: &DataConnection,
|
2020-08-06 13:11:25 -07:00
|
|
|
id_note: NoteId,
|
2020-08-05 18:14:45 -07:00
|
|
|
) -> Result<Option<String>, SqliteClientError> {
|
|
|
|
let memo: Vec<_> = data.0.query_row(
|
2019-03-08 19:22:35 -08:00
|
|
|
"SELECT memo FROM received_notes
|
|
|
|
WHERE id_note = ?",
|
2020-08-06 13:11:25 -07:00
|
|
|
&[id_note.0],
|
2019-03-08 19:22:35 -08:00
|
|
|
|row| row.get(0),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
match Memo::from_bytes(&memo) {
|
|
|
|
Some(memo) => match memo.to_utf8() {
|
|
|
|
Some(Ok(res)) => Ok(Some(res)),
|
2020-08-05 18:14:45 -07:00
|
|
|
Some(Err(e)) => Err(SqliteClientError(Error::InvalidMemo(e))),
|
2019-03-08 19:22:35 -08:00
|
|
|
None => Ok(None),
|
|
|
|
},
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the memo for a sent note, if it is known and a valid UTF-8 string.
|
|
|
|
///
|
|
|
|
/// The note is identified by its row index in the `sent_notes` table within the data
|
|
|
|
/// database.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2020-08-05 18:14:45 -07:00
|
|
|
/// use tempfile::NamedTempFile;
|
|
|
|
/// use zcash_client_sqlite::{
|
2020-08-06 13:11:25 -07:00
|
|
|
/// NoteId,
|
2020-08-05 18:14:45 -07:00
|
|
|
/// DataConnection,
|
|
|
|
/// query::get_sent_memo_as_utf8,
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// let data_file = NamedTempFile::new().unwrap();
|
|
|
|
/// let db = DataConnection::for_path(data_file).unwrap();
|
2020-08-06 13:11:25 -07:00
|
|
|
/// let memo = get_sent_memo_as_utf8(&db, NoteId(12));
|
2020-08-05 18:14:45 -07:00
|
|
|
/// ```
|
|
|
|
pub fn get_sent_memo_as_utf8(
|
|
|
|
data: &DataConnection,
|
2020-08-06 13:11:25 -07:00
|
|
|
id_note: NoteId,
|
2020-08-05 18:14:45 -07:00
|
|
|
) -> Result<Option<String>, SqliteClientError> {
|
|
|
|
let memo: Vec<_> = data.0.query_row(
|
2019-03-08 19:22:35 -08:00
|
|
|
"SELECT memo FROM sent_notes
|
|
|
|
WHERE id_note = ?",
|
2020-08-06 13:11:25 -07:00
|
|
|
&[id_note.0],
|
2019-03-08 19:22:35 -08:00
|
|
|
|row| row.get(0),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
match Memo::from_bytes(&memo) {
|
|
|
|
Some(memo) => match memo.to_utf8() {
|
|
|
|
Some(Ok(res)) => Ok(Some(res)),
|
2020-08-05 18:14:45 -07:00
|
|
|
Some(Err(e)) => Err(SqliteClientError(Error::InvalidMemo(e))),
|
2019-03-08 19:22:35 -08:00
|
|
|
None => Ok(None),
|
|
|
|
},
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 14:20:12 -07:00
|
|
|
pub fn block_height_extrema(
|
|
|
|
conn: &DataConnection,
|
|
|
|
) -> Result<Option<(BlockHeight, BlockHeight)>, rusqlite::Error> {
|
|
|
|
conn.0
|
|
|
|
.query_row(
|
|
|
|
"SELECT MIN(height), MAX(height) FROM blocks",
|
|
|
|
NO_PARAMS,
|
|
|
|
|row| {
|
|
|
|
let min_height: u32 = row.get(0)?;
|
|
|
|
let max_height: u32 = row.get(1)?;
|
|
|
|
Ok(Some((
|
|
|
|
BlockHeight::from(min_height),
|
|
|
|
BlockHeight::from(max_height),
|
|
|
|
)))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
//.optional() doesn't work here because a failed aggregate function
|
|
|
|
//produces a runtime error, not an empty set of rows.
|
|
|
|
.or(Ok(None))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_tx_height(
|
|
|
|
conn: &DataConnection,
|
|
|
|
txid: TxId,
|
|
|
|
) -> Result<Option<BlockHeight>, rusqlite::Error> {
|
|
|
|
conn.0
|
|
|
|
.query_row(
|
|
|
|
"SELECT block FROM transactions WHERE txid = ?",
|
|
|
|
&[txid.0.to_vec()],
|
|
|
|
|row| row.get(0).map(u32::into),
|
|
|
|
)
|
|
|
|
.optional()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_block_hash(
|
|
|
|
conn: &DataConnection,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<Option<BlockHash>, rusqlite::Error> {
|
|
|
|
conn.0
|
|
|
|
.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()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rewinds the database to the given height.
|
|
|
|
///
|
|
|
|
/// If the requested height is greater than or equal to the height of the last scanned
|
|
|
|
/// block, this function does nothing.
|
|
|
|
pub fn rewind_to_height<P: consensus::Parameters>(
|
|
|
|
conn: &DataConnection,
|
|
|
|
parameters: &P,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<(), SqliteClientError> {
|
|
|
|
let sapling_activation_height = parameters
|
|
|
|
.activation_height(NetworkUpgrade::Sapling)
|
|
|
|
.ok_or(SqliteClientError(Error::SaplingNotActive))?;
|
|
|
|
|
|
|
|
// Recall where we synced up to previously.
|
|
|
|
// If we have never synced, use Sapling activation height.
|
|
|
|
let last_scanned_height =
|
|
|
|
conn.0
|
|
|
|
.query_row("SELECT MAX(height) FROM blocks", NO_PARAMS, |row| {
|
|
|
|
row.get(0)
|
|
|
|
.map(u32::into)
|
|
|
|
.or(Ok(sapling_activation_height - 1))
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if block_height >= last_scanned_height {
|
|
|
|
// Nothing to do.
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start an SQL transaction for rewinding.
|
|
|
|
conn.0.execute("BEGIN IMMEDIATE", NO_PARAMS)?;
|
|
|
|
|
|
|
|
// Decrement witnesses.
|
|
|
|
conn.0.execute(
|
|
|
|
"DELETE FROM sapling_witnesses WHERE block > ?",
|
|
|
|
&[u32::from(block_height)],
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Un-mine transactions.
|
|
|
|
conn.0.execute(
|
|
|
|
"UPDATE transactions SET block = NULL, tx_index = NULL WHERE block > ?",
|
|
|
|
&[u32::from(block_height)],
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Now that they aren't depended on, delete scanned blocks.
|
|
|
|
conn.0.execute(
|
|
|
|
"DELETE FROM blocks WHERE height > ?",
|
|
|
|
&[u32::from(block_height)],
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Commit the SQL transaction, rewinding atomically.
|
|
|
|
conn.0.execute("COMMIT", NO_PARAMS)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-06 13:11:25 -07:00
|
|
|
pub fn get_extended_full_viewing_keys<P: consensus::Parameters>(
|
|
|
|
data: &DataConnection,
|
|
|
|
params: &P,
|
|
|
|
) -> Result<Vec<ExtendedFullViewingKey>, SqliteClientError> {
|
|
|
|
// Fetch the ExtendedFullViewingKeys we are tracking
|
|
|
|
let mut stmt_fetch_accounts = data
|
|
|
|
.0
|
|
|
|
.prepare("SELECT extfvk FROM accounts ORDER BY account ASC")?;
|
|
|
|
|
|
|
|
let rows = stmt_fetch_accounts
|
|
|
|
.query_map(NO_PARAMS, |row| {
|
|
|
|
row.get(0).map(|extfvk: String| {
|
|
|
|
decode_extended_full_viewing_key(
|
|
|
|
params.hrp_sapling_extended_full_viewing_key(),
|
|
|
|
&extfvk,
|
|
|
|
)
|
|
|
|
.map_err(|e| Error::Bech32(e))
|
|
|
|
.and_then(|k| k.ok_or(Error::IncorrectHRPExtFVK))
|
|
|
|
.map_err(SqliteClientError)
|
|
|
|
})
|
|
|
|
})
|
2020-08-17 10:46:44 -07:00
|
|
|
.map_err(SqliteClientError::from)?;
|
2020-08-06 13:11:25 -07:00
|
|
|
|
|
|
|
rows.collect::<Result<Result<_, _>, _>>()?
|
|
|
|
}
|
|
|
|
|
2020-08-06 17:13:24 -07:00
|
|
|
pub fn get_commitment_tree(
|
|
|
|
data: &DataConnection,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<Option<CommitmentTree<Node>>, SqliteClientError> {
|
|
|
|
data.0
|
|
|
|
.query_row_and_then(
|
|
|
|
"SELECT sapling_tree FROM blocks WHERE height = ?",
|
|
|
|
&[u32::from(block_height)],
|
|
|
|
|row| {
|
|
|
|
let row_data: Vec<u8> = row.get(0)?;
|
|
|
|
CommitmentTree::read(&row_data[..]).map_err(|e| {
|
|
|
|
rusqlite::Error::FromSqlConversionFailure(
|
|
|
|
row_data.len(),
|
|
|
|
rusqlite::types::Type::Blob,
|
|
|
|
Box::new(e),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.optional()
|
|
|
|
.map_err(SqliteClientError::from)
|
|
|
|
}
|
|
|
|
|
2020-08-18 12:48:59 -07:00
|
|
|
pub fn get_witnesses(
|
|
|
|
data: &DataConnection,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<Vec<(NoteId, IncrementalWitness<Node>)>, SqliteClientError> {
|
|
|
|
let mut stmt_fetch_witnesses = data
|
|
|
|
.0
|
|
|
|
.prepare("SELECT note, witness FROM sapling_witnesses WHERE block = ?")?;
|
|
|
|
let witnesses = stmt_fetch_witnesses
|
|
|
|
.query_map(&[u32::from(block_height)], |row| {
|
|
|
|
let id_note = NoteId(row.get(0)?);
|
|
|
|
let data: Vec<u8> = row.get(1)?;
|
|
|
|
Ok(IncrementalWitness::read(&data[..]).map(|witness| (id_note, witness)))
|
|
|
|
})
|
|
|
|
.map_err(SqliteClientError::from)?;
|
|
|
|
|
|
|
|
let mut res = vec![];
|
|
|
|
for witness in witnesses {
|
|
|
|
// unwrap database error & IO error from IncrementalWitness::read
|
|
|
|
res.push(witness??);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:49:00 -07:00
|
|
|
pub fn get_nullifiers(
|
|
|
|
data: &DataConnection,
|
|
|
|
) -> Result<Vec<(Vec<u8>, AccountId)>, SqliteClientError> {
|
|
|
|
// Get the nullifiers for the notes we are tracking
|
|
|
|
let mut stmt_fetch_nullifiers = data
|
|
|
|
.0
|
|
|
|
.prepare("SELECT id_note, nf, account FROM received_notes WHERE spent IS NULL")?;
|
|
|
|
let nullifiers = stmt_fetch_nullifiers.query_map(NO_PARAMS, |row| {
|
|
|
|
let nf: Vec<u8> = row.get(1)?;
|
|
|
|
let account = AccountId(row.get(2)?);
|
|
|
|
Ok((nf, account))
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut res = vec![];
|
|
|
|
for nullifier in nullifiers {
|
|
|
|
// unwrap database error
|
|
|
|
res.push(nullifier?);
|
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2019-03-08 18:53:38 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-05 18:14:45 -07:00
|
|
|
use rusqlite::Connection;
|
2019-03-08 18:53:38 -08:00
|
|
|
use tempfile::NamedTempFile;
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2019-03-08 18:53:38 -08:00
|
|
|
use zcash_primitives::{
|
|
|
|
transaction::components::Amount,
|
|
|
|
zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
|
|
|
|
};
|
|
|
|
|
2020-08-05 18:14:45 -07:00
|
|
|
use zcash_client_backend::data_api::error::Error;
|
|
|
|
|
2019-03-08 18:53:38 -08:00
|
|
|
use crate::{
|
2020-08-25 14:29:01 -07:00
|
|
|
tests,
|
|
|
|
wallet::init::{init_accounts_table, init_data_database},
|
|
|
|
AccountId, DataConnection,
|
2019-03-08 18:53:38 -08:00
|
|
|
};
|
|
|
|
|
2020-08-05 18:14:45 -07:00
|
|
|
use super::{get_address, get_balance, get_verified_balance};
|
|
|
|
|
2019-03-08 18:53:38 -08:00
|
|
|
#[test]
|
|
|
|
fn empty_database_has_no_balance() {
|
|
|
|
let data_file = NamedTempFile::new().unwrap();
|
2020-08-05 18:14:45 -07:00
|
|
|
let db_data = DataConnection(Connection::open(data_file.path()).unwrap());
|
2019-03-08 18:53:38 -08:00
|
|
|
init_data_database(&db_data).unwrap();
|
|
|
|
|
|
|
|
// Add an account to the wallet
|
|
|
|
let extsk = ExtendedSpendingKey::master(&[]);
|
|
|
|
let extfvks = [ExtendedFullViewingKey::from(&extsk)];
|
2020-08-05 13:27:40 -07:00
|
|
|
init_accounts_table(&db_data, &tests::network(), &extfvks).unwrap();
|
2019-03-08 18:53:38 -08:00
|
|
|
|
|
|
|
// The account should be empty
|
2020-08-06 13:11:25 -07:00
|
|
|
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), Amount::zero());
|
2019-03-08 18:53:38 -08:00
|
|
|
|
|
|
|
// The account should have no verified balance, as we haven't scanned any blocks
|
2020-08-06 13:11:25 -07:00
|
|
|
let e = get_verified_balance(&db_data, AccountId(0)).unwrap_err();
|
2020-08-05 18:14:45 -07:00
|
|
|
match e.0 {
|
|
|
|
Error::ScanRequired => (),
|
2019-03-08 18:53:38 -08:00
|
|
|
_ => panic!("Unexpected error: {:?}", e),
|
|
|
|
}
|
|
|
|
|
|
|
|
// An invalid account has zero balance
|
2020-08-06 13:11:25 -07:00
|
|
|
assert!(get_address(&db_data, &tests::network(), AccountId(1)).is_err());
|
|
|
|
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), Amount::zero());
|
2019-03-08 18:53:38 -08:00
|
|
|
}
|
|
|
|
}
|