Skip to content

Commit

Permalink
zcash_client_sqlite: Rename AccountId internal type to AccountRef
Browse files Browse the repository at this point in the history
This is now consistent with how we name other internal primary key type
wrappers.
  • Loading branch information
nuttycom committed Nov 27, 2024
1 parent 546481e commit 6e1933b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions zcash_client_sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}
Expand Down
16 changes: 8 additions & 8 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -441,15 +441,15 @@ pub(crate) fn add_account<P: consensus::Parameters>(
":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)
if f.code == rusqlite::ErrorCode::ConstraintViolation =>
{
// 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 = ?",

Check warning on line 455 in zcash_client_sqlite/src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet.rs#L454-L455

Added lines #L454 - L455 were not covered by tests
Expand Down Expand Up @@ -630,7 +630,7 @@ pub(crate) fn get_current_address<P: consensus::Parameters>(
pub(crate) fn insert_address<P: consensus::Parameters>(
conn: &rusqlite::Connection,
params: &P,
account_id: AccountId,
account_id: AccountRef,
diversifier_index: DiversifierIndex,
address: &UnifiedAddress,
) -> Result<(), SqliteClientError> {
Expand Down Expand Up @@ -1839,11 +1839,11 @@ pub(crate) fn block_height_extrema(
pub(crate) fn get_account_id(
conn: &rusqlite::Connection,
account_uuid: AccountUuid,
) -> Result<AccountId, SqliteClientError> {
) -> Result<AccountRef, SqliteClientError> {
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)
Expand All @@ -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<AccountUuid, SqliteClientError> {
conn.query_row(
"SELECT uuid FROM accounts WHERE id = :account_id",
Expand Down Expand Up @@ -3257,7 +3257,7 @@ fn recipient_params<P: consensus::Parameters>(
params: &P,
from: AccountUuid,
to: &Recipient<AccountUuid, Note, OutPoint>,
) -> Result<(AccountId, Option<String>, Option<AccountId>, PoolType), SqliteClientError> {
) -> Result<(AccountRef, Option<String>, Option<AccountRef>, 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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -69,7 +69,7 @@ impl<P: consensus::Parameters> RusqliteMigration for Migration<P> {
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)?;

Check warning on line 73 in zcash_client_sqlite/src/wallet/init/migrations/ephemeral_addresses.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet/init/migrations/ephemeral_addresses.rs#L73

Added line #L73 was not covered by tests
}
}
Expand Down Expand Up @@ -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,
};
Expand All @@ -119,7 +119,7 @@ mod tests {
wdb: &mut WalletDb<Connection, Network>,
seed: &SecretVec<u8>,
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(|| {
Expand Down Expand Up @@ -158,7 +158,7 @@ mod tests {
#[cfg(not(feature = "orchard"))]
let birthday_orchard_tree_size: Option<u64> = 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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ mod tests {
memo_repr, parse_scope,
sapling::ReceivedSaplingOutput,
},
AccountId, TxRef, WalletDb,
AccountRef, TxRef, WalletDb,
};

// These must be different.
Expand Down Expand Up @@ -413,7 +413,7 @@ mod tests {
(ufvk0, height, res)
}

fn put_received_note_before_migration<T: ReceivedSaplingOutput<AccountId = AccountId>>(
fn put_received_note_before_migration<T: ReceivedSaplingOutput<AccountId = AccountRef>>(
conn: &Connection,
output: &T,
tx_ref: i64,
Expand Down Expand Up @@ -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.
Expand All @@ -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<AccountId> = None;
let mut spending_account_id: Option<AccountRef> = None;

// Orchard outputs were not supported as of the wallet states that could require this
// migration.
Expand Down Expand Up @@ -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(
&params,
Expand Down Expand Up @@ -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<AccountId>,
tx: &WalletTx<AccountRef>,
height: BlockHeight,
) -> Result<i64, SqliteClientError> {
// It isn't there, so insert our transaction into the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -147,16 +147,16 @@ impl<P: consensus::Parameters> RusqliteMigration for Migration<P> {
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 =

Check warning on line 154 in zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs#L154

Added line #L154 was not covered by tests
|| -> Result<Option<AccountId>, SqliteClientError> {
|| -> Result<Option<AccountRef>, 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)?;

Check warning on line 160 in zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs#L159-L160

Added lines #L159 - L160 were not covered by tests

if let Some((legacy_taddr, _)) =

Check warning on line 162 in zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet/init/migrations/tx_retrieval_queue.rs#L162

Added line #L162 was not covered by tests
Expand Down
22 changes: 11 additions & 11 deletions zcash_client_sqlite/src/wallet/transparent/ephemeral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<u32, SqliteClientError> {
match conn
.query_row(
Expand All @@ -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<u32, SqliteClientError> {
first_unstored_index(conn, account_id)?
.checked_sub(GAP_LIMIT)
Expand All @@ -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<u32, SqliteClientError> {
// The inner join with `transactions` excludes addresses for which
// `seen_in_tx` is NULL. The query also excludes addresses observed
Expand Down Expand Up @@ -114,7 +114,7 @@ pub(crate) fn range_from(i: u32, n: u32) -> Range<u32> {
pub(crate) fn get_ephemeral_ivk<P: consensus::Parameters>(
conn: &rusqlite::Connection,
params: &P,
account_id: AccountId,
account_id: AccountRef,
) -> Result<Option<EphemeralIvk>, SqliteClientError> {
let ufvk = conn
.query_row(
Expand Down Expand Up @@ -151,7 +151,7 @@ pub(crate) fn get_ephemeral_ivk<P: consensus::Parameters>(
pub(crate) fn get_known_ephemeral_addresses<P: consensus::Parameters>(
conn: &rusqlite::Connection,
params: &P,
account_id: AccountId,
account_id: AccountRef,
index_range: Option<Range<u32>>,
) -> Result<Vec<(TransparentAddress, TransparentAddressMetadata)>, SqliteClientError> {
let index_range = index_range.unwrap_or(0..(1 << 31));
Expand Down Expand Up @@ -237,7 +237,7 @@ pub(crate) fn find_index_for_ephemeral_address_str(
pub(crate) fn reserve_next_n_ephemeral_addresses<P: consensus::Parameters>(
conn: &rusqlite::Transaction,
params: &P,
account_id: AccountId,
account_id: AccountRef,
n: usize,
) -> Result<Vec<(TransparentAddress, TransparentAddressMetadata)>, SqliteClientError> {
if n == 0 {
Expand Down Expand Up @@ -270,7 +270,7 @@ pub(crate) fn reserve_next_n_ephemeral_addresses<P: consensus::Parameters>(
pub(crate) fn init_account<P: consensus::Parameters>(
conn: &rusqlite::Transaction,
params: &P,
account_id: AccountId,
account_id: AccountRef,
) -> Result<(), SqliteClientError> {
reserve_until(conn, params, account_id, 0)
}
Expand All @@ -287,7 +287,7 @@ pub(crate) fn init_account<P: consensus::Parameters>(
fn reserve_until<P: consensus::Parameters>(
conn: &rusqlite::Transaction,
params: &P,
account_id: AccountId,
account_id: AccountRef,
next_to_reserve: u32,
) -> Result<(), SqliteClientError> {
assert!(next_to_reserve <= 1 << 31);
Expand Down Expand Up @@ -401,7 +401,7 @@ pub(crate) fn mark_ephemeral_address_as_used<P: consensus::Parameters>(
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()?;

Expand Down Expand Up @@ -454,7 +454,7 @@ pub(crate) fn mark_ephemeral_address_as_seen<P: consensus::Parameters>(
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()?;

Expand Down

0 comments on commit 6e1933b

Please sign in to comment.