From 6e1933b5aa7b4d31c837e1d3a3bcd91c50feb8da Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Tue, 26 Nov 2024 22:47:32 -0700 Subject: [PATCH] zcash_client_sqlite: Rename `AccountId` internal type to `AccountRef` This is now consistent with how we name other internal primary key type wrappers. --- zcash_client_sqlite/src/lib.rs | 8 +++---- zcash_client_sqlite/src/wallet.rs | 16 +++++++------- .../init/migrations/ephemeral_addresses.rs | 14 ++++++------ .../init/migrations/receiving_key_scopes.rs | 12 +++++----- .../init/migrations/tx_retrieval_queue.rs | 8 +++---- .../src/wallet/transparent/ephemeral.rs | 22 +++++++++---------- 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index 0bc77b210..16742cf22 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -205,18 +205,18 @@ impl AccountUuid { } } -/// The local identifier for an account. +/// A typesafe wrapper for the primary key identifier for a row in the `accounts` table. /// /// This is an ephemeral value for efficiently and generically working with accounts in a /// [`WalletDb`]. To reference accounts in external contexts, use [`AccountUuid`]. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Default)] -pub(crate) struct AccountId(u32); +pub(crate) struct AccountRef(u32); /// This implementation is retained under `#[cfg(test)]` for pre-AccountUuid testing. #[cfg(test)] -impl ConditionallySelectable for AccountId { +impl ConditionallySelectable for AccountRef { fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self { - AccountId(ConditionallySelectable::conditional_select( + AccountRef(ConditionallySelectable::conditional_select( &a.0, &b.0, choice, )) } diff --git a/zcash_client_sqlite/src/wallet.rs b/zcash_client_sqlite/src/wallet.rs index 04d9496f7..23efc47c7 100644 --- a/zcash_client_sqlite/src/wallet.rs +++ b/zcash_client_sqlite/src/wallet.rs @@ -117,7 +117,7 @@ use zip32::{self, DiversifierIndex, Scope}; use crate::{ error::SqliteClientError, wallet::commitment_tree::{get_max_checkpointed_height, SqliteShardStore}, - AccountId, SqlTransaction, TransferType, WalletCommitmentTrees, WalletDb, DEFAULT_UA_REQUEST, + AccountRef, SqlTransaction, TransferType, WalletCommitmentTrees, WalletDb, DEFAULT_UA_REQUEST, PRUNING_DEPTH, SAPLING_TABLES_PREFIX, }; use crate::{AccountUuid, TxRef, VERIFY_LOOKAHEAD}; @@ -441,7 +441,7 @@ pub(crate) fn add_account( ":recover_until_height": birthday.recover_until().map(u32::from), ":has_spend_key": spending_key_available as i64, ], - |row| row.get(0).map(AccountId), + |row| row.get(0).map(AccountRef), ) .map_err(|e| match e { rusqlite::Error::SqliteFailure(f, s) @@ -449,7 +449,7 @@ pub(crate) fn add_account( { // An account conflict occurred. This should already have been caught by // the check using `get_account_for_ufvk` above, but in case it wasn't, - // make a best effort to determine the AccountId of the pre-existing row + // make a best effort to determine the AccountRef of the pre-existing row // and provide that to our caller. if let Ok(colliding_uuid) = conn.query_row( "SELECT uuid FROM accounts WHERE ufvk = ?", @@ -630,7 +630,7 @@ pub(crate) fn get_current_address( pub(crate) fn insert_address( conn: &rusqlite::Connection, params: &P, - account_id: AccountId, + account_id: AccountRef, diversifier_index: DiversifierIndex, address: &UnifiedAddress, ) -> Result<(), SqliteClientError> { @@ -1839,11 +1839,11 @@ pub(crate) fn block_height_extrema( pub(crate) fn get_account_id( conn: &rusqlite::Connection, account_uuid: AccountUuid, -) -> Result { +) -> Result { conn.query_row( "SELECT id FROM accounts WHERE uuid = :account_uuid", named_params! {":account_uuid": account_uuid.0}, - |row| row.get("id").map(AccountId), + |row| row.get("id").map(AccountRef), ) .optional()? .ok_or(SqliteClientError::AccountUnknown) @@ -1852,7 +1852,7 @@ pub(crate) fn get_account_id( #[cfg(feature = "transparent-inputs")] pub(crate) fn get_account_uuid( conn: &rusqlite::Connection, - account_id: AccountId, + account_id: AccountRef, ) -> Result { conn.query_row( "SELECT uuid FROM accounts WHERE id = :account_id", @@ -3257,7 +3257,7 @@ fn recipient_params( params: &P, from: AccountUuid, to: &Recipient, -) -> Result<(AccountId, Option, Option, PoolType), SqliteClientError> { +) -> Result<(AccountRef, Option, Option, PoolType), SqliteClientError> { let from_account_id = get_account_id(conn, from)?; match to { Recipient::External(addr, pool) => Ok((from_account_id, Some(addr.encode()), None, *pool)), diff --git a/zcash_client_sqlite/src/wallet/init/migrations/ephemeral_addresses.rs b/zcash_client_sqlite/src/wallet/init/migrations/ephemeral_addresses.rs index b3fcf282b..a982a4905 100644 --- a/zcash_client_sqlite/src/wallet/init/migrations/ephemeral_addresses.rs +++ b/zcash_client_sqlite/src/wallet/init/migrations/ephemeral_addresses.rs @@ -8,7 +8,7 @@ use zcash_protocol::consensus; use crate::wallet::init::WalletMigrationError; #[cfg(feature = "transparent-inputs")] -use crate::{wallet::transparent::ephemeral, AccountId}; +use crate::{wallet::transparent::ephemeral, AccountRef}; use super::utxos_to_txos; @@ -69,7 +69,7 @@ impl RusqliteMigration for Migration

{ let mut stmt = transaction.prepare("SELECT id FROM accounts")?; let mut rows = stmt.query([])?; while let Some(row) = rows.next()? { - let account_id = AccountId(row.get(0)?); + let account_id = AccountRef(row.get(0)?); ephemeral::init_account(transaction, &self.params, account_id)?; } } @@ -107,7 +107,7 @@ mod tests { wallet::{ self, account_kind_code, init::init_wallet_db_internal, transparent::ephemeral, }, - AccountId, WalletDb, + AccountRef, WalletDb, }, zcash_client_backend::data_api::GAP_LIMIT, }; @@ -119,7 +119,7 @@ mod tests { wdb: &mut WalletDb, seed: &SecretVec, birthday: &AccountBirthday, - ) -> Result<(AccountId, UnifiedSpendingKey), SqliteClientError> { + ) -> Result<(AccountRef, UnifiedSpendingKey), SqliteClientError> { wdb.transactionally(|wdb| { let seed_fingerprint = SeedFingerprint::from_seed(seed.expose_secret()).ok_or_else(|| { @@ -158,7 +158,7 @@ mod tests { #[cfg(not(feature = "orchard"))] let birthday_orchard_tree_size: Option = None; - let account_id: AccountId = wdb.conn.0.query_row( + let account_id: AccountRef = wdb.conn.0.query_row( r#" INSERT INTO accounts ( account_kind, hd_seed_fingerprint, hd_account_index, @@ -190,7 +190,7 @@ mod tests { ":birthday_orchard_tree_size": birthday_orchard_tree_size, ":recover_until_height": birthday.recover_until().map(u32::from) ], - |row| Ok(AccountId(row.get(0)?)), + |row| Ok(AccountRef(row.get(0)?)), )?; // Initialize the `ephemeral_addresses` table. @@ -232,7 +232,7 @@ mod tests { account_index: account0_index, }); assert_eq!(u32::from(account0_index), 0); - let account0_id = crate::AccountId(0); + let account0_id = AccountRef(0); let usk0 = UnifiedSpendingKey::from_seed(&network, &seed0, account0_index).unwrap(); let ufvk0 = usk0.to_unified_full_viewing_key(); diff --git a/zcash_client_sqlite/src/wallet/init/migrations/receiving_key_scopes.rs b/zcash_client_sqlite/src/wallet/init/migrations/receiving_key_scopes.rs index f6fc1cd3a..778ea22c2 100644 --- a/zcash_client_sqlite/src/wallet/init/migrations/receiving_key_scopes.rs +++ b/zcash_client_sqlite/src/wallet/init/migrations/receiving_key_scopes.rs @@ -324,7 +324,7 @@ mod tests { memo_repr, parse_scope, sapling::ReceivedSaplingOutput, }, - AccountId, TxRef, WalletDb, + AccountRef, TxRef, WalletDb, }; // These must be different. @@ -413,7 +413,7 @@ mod tests { (ufvk0, height, res) } - fn put_received_note_before_migration>( + fn put_received_note_before_migration>( conn: &Connection, output: &T, tx_ref: i64, @@ -529,7 +529,7 @@ mod tests { let (ufvk0, height, res) = prepare_wallet_state(&mut db_data); let tx = res.transaction(); - let account_id = AccountId(0); + let account_id = AccountRef(0); // We can't use `decrypt_and_store_transaction` because we haven't migrated yet. // Replicate its relevant innards here. @@ -544,7 +544,7 @@ mod tests { .transactionally::<_, _, rusqlite::Error>(|wdb| { let tx_ref = put_tx_data(wdb.conn.0, d_tx.tx(), None, None).unwrap(); - let mut spending_account_id: Option = None; + let mut spending_account_id: Option = None; // Orchard outputs were not supported as of the wallet states that could require this // migration. @@ -644,7 +644,7 @@ mod tests { ..Default::default() }; block.vtx.push(compact_tx); - let scanning_keys = ScanningKeys::from_account_ufvks([(AccountId(0), ufvk0)]); + let scanning_keys = ScanningKeys::from_account_ufvks([(AccountRef(0), ufvk0)]); let scanned_block = scan_block( ¶ms, @@ -875,7 +875,7 @@ mod tests { /// updates to the database schema require incompatible changes to `put_tx_meta`. pub(crate) fn put_tx_meta( conn: &rusqlite::Connection, - tx: &WalletTx, + tx: &WalletTx, height: BlockHeight, ) -> Result { // It isn't there, so insert our transaction into the database. diff --git a/zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs b/zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs index edffe9d95..9b6eb5ce5 100644 --- a/zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs +++ b/zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs @@ -24,7 +24,7 @@ use { queue_transparent_input_retrieval, queue_unmined_tx_retrieval, transparent::{queue_transparent_spend_detection, uivk_legacy_transparent_address}, }, - AccountId, TxRef, + AccountRef, TxRef, }, rusqlite::OptionalExtension as _, std::convert::Infallible, @@ -147,16 +147,16 @@ impl RusqliteMigration for Migration

{ SELECT account_id from ephemeral_addresses WHERE address = :address", named_params![":address": address.encode(&self._params)], - |row| row.get(0).map(AccountId), + |row| row.get(0).map(AccountRef), ) .optional() }; let find_legacy_address_account = - || -> Result, SqliteClientError> { + || -> Result, SqliteClientError> { let mut stmt = conn.prepare("SELECT id, uivk FROM accounts")?; let mut rows = stmt.query([])?; while let Some(row) = rows.next()? { - let account_id = row.get(0).map(AccountId)?; + let account_id = row.get(0).map(AccountRef)?; let uivk_str = row.get::<_, String>(1)?; if let Some((legacy_taddr, _)) = diff --git a/zcash_client_sqlite/src/wallet/transparent/ephemeral.rs b/zcash_client_sqlite/src/wallet/transparent/ephemeral.rs index 44eb19c12..256f8bd3b 100644 --- a/zcash_client_sqlite/src/wallet/transparent/ephemeral.rs +++ b/zcash_client_sqlite/src/wallet/transparent/ephemeral.rs @@ -18,7 +18,7 @@ use zcash_protocol::consensus; use crate::wallet::{self, get_account_id}; use crate::AccountUuid; -use crate::{error::SqliteClientError, AccountId, TxRef}; +use crate::{error::SqliteClientError, AccountRef, TxRef}; // Returns `TransparentAddressMetadata` in the ephemeral scope for the // given address index. @@ -29,7 +29,7 @@ pub(crate) fn metadata(address_index: NonHardenedChildIndex) -> TransparentAddre /// Returns the first unstored ephemeral address index in the given account. pub(crate) fn first_unstored_index( conn: &rusqlite::Connection, - account_id: AccountId, + account_id: AccountRef, ) -> Result { match conn .query_row( @@ -53,7 +53,7 @@ pub(crate) fn first_unstored_index( /// Returns the first unreserved ephemeral address index in the given account. pub(crate) fn first_unreserved_index( conn: &rusqlite::Connection, - account_id: AccountId, + account_id: AccountRef, ) -> Result { first_unstored_index(conn, account_id)? .checked_sub(GAP_LIMIT) @@ -66,7 +66,7 @@ pub(crate) fn first_unreserved_index( /// would violate the gap invariant if used. pub(crate) fn first_unsafe_index( conn: &rusqlite::Connection, - account_id: AccountId, + account_id: AccountRef, ) -> Result { // The inner join with `transactions` excludes addresses for which // `seen_in_tx` is NULL. The query also excludes addresses observed @@ -114,7 +114,7 @@ pub(crate) fn range_from(i: u32, n: u32) -> Range { pub(crate) fn get_ephemeral_ivk( conn: &rusqlite::Connection, params: &P, - account_id: AccountId, + account_id: AccountRef, ) -> Result, SqliteClientError> { let ufvk = conn .query_row( @@ -151,7 +151,7 @@ pub(crate) fn get_ephemeral_ivk( pub(crate) fn get_known_ephemeral_addresses( conn: &rusqlite::Connection, params: &P, - account_id: AccountId, + account_id: AccountRef, index_range: Option>, ) -> Result, SqliteClientError> { let index_range = index_range.unwrap_or(0..(1 << 31)); @@ -237,7 +237,7 @@ pub(crate) fn find_index_for_ephemeral_address_str( pub(crate) fn reserve_next_n_ephemeral_addresses( conn: &rusqlite::Transaction, params: &P, - account_id: AccountId, + account_id: AccountRef, n: usize, ) -> Result, SqliteClientError> { if n == 0 { @@ -270,7 +270,7 @@ pub(crate) fn reserve_next_n_ephemeral_addresses( pub(crate) fn init_account( conn: &rusqlite::Transaction, params: &P, - account_id: AccountId, + account_id: AccountRef, ) -> Result<(), SqliteClientError> { reserve_until(conn, params, account_id, 0) } @@ -287,7 +287,7 @@ pub(crate) fn init_account( fn reserve_until( conn: &rusqlite::Transaction, params: &P, - account_id: AccountId, + account_id: AccountRef, next_to_reserve: u32, ) -> Result<(), SqliteClientError> { assert!(next_to_reserve <= 1 << 31); @@ -401,7 +401,7 @@ pub(crate) fn mark_ephemeral_address_as_used( WHERE address = :address RETURNING account_id, address_index", named_params![":tx_ref": tx_ref.0, ":address": address_str], - |row| Ok((AccountId(row.get::<_, u32>(0)?), row.get::<_, u32>(1)?)), + |row| Ok((AccountRef(row.get::<_, u32>(0)?), row.get::<_, u32>(1)?)), ) .optional()?; @@ -454,7 +454,7 @@ pub(crate) fn mark_ephemeral_address_as_seen( WHERE address = :address RETURNING account_id, address_index", named_params![":seen_in_tx": &earlier_ref, ":address": address_str], - |row| Ok((AccountId(row.get::<_, u32>(0)?), row.get::<_, u32>(1)?)), + |row| Ok((AccountRef(row.get::<_, u32>(0)?), row.get::<_, u32>(1)?)), ) .optional()?;