Skip to content

Commit

Permalink
Apply suggestions from code review & Clippy fixes.
Browse files Browse the repository at this point in the history
Co-authored by: Jack Grig <[email protected]>
Co-authored-by: Daira-Emma Hopwood <[email protected]>
  • Loading branch information
nuttycom committed Nov 13, 2024
1 parent 3352671 commit 00cafa3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
6 changes: 4 additions & 2 deletions components/zcash_protocol/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ impl Zatoshis {
/// Divides this `Zatoshis` value by the given divisor and returns the quotient and remainder.
pub fn div_with_remainder(&self, divisor: NonZeroU64) -> QuotRem<Zatoshis> {
let divisor = u64::from(divisor);
// `self` is already bounds-checked, so we don't need to re-check it in division
// `self` is already bounds-checked, and both the quotient and remainder
// are <= self, so we don't need to re-check them in division.
QuotRem {
quotient: Zatoshis(self.0 / divisor),
remainder: Zatoshis(self.0 % divisor),
Expand Down Expand Up @@ -427,7 +428,8 @@ impl Div<NonZeroU64> for Zatoshis {
type Output = Zatoshis;

fn div(self, rhs: NonZeroU64) -> Zatoshis {
// `self` is already bounds-checked, so we don't need to re-check it
// `self` is already bounds-checked and the quotient is <= self, so
// we don't need to re-check it
Zatoshis(self.0 / u64::from(rhs))
}
}
Expand Down
8 changes: 5 additions & 3 deletions zcash_client_backend/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ impl SplitPolicy {
/// In the case that no other conditions provided by the user are available to fall back on,
/// a default value of [`MARGINAL_FEE`] * 100 will be used as the "minimum usable note value"
/// when retrieving wallet metadata.
///
/// [`MARGINAL_FEE`]: zcash_primitives::transaction::fees::zip317::MARGINAL_FEE
pub(crate) const MIN_NOTE_VALUE: NonNegativeAmount = NonNegativeAmount::const_from_u64(500000);

/// Constructs a new [`SplitPolicy`] that splits change to ensure the given number of spendable
Expand Down Expand Up @@ -404,9 +406,9 @@ impl SplitPolicy {
/// Returns the number of output notes to produce from the given total change value, given the
/// total value and number of existing unspent notes in the account and this policy.
///
/// If splitting change to produce [`Self::target_output_count`] would result in notes of value less than
/// [`Self::min_split_output_value`], then this will produce
///
/// If splitting change to produce [`Self::target_output_count`] would result in notes of value
/// less than [`Self::min_split_output_value`], then this will suggest a smaller number of
/// splits so that each resulting change note has sufficient value.
pub fn split_count(
&self,
existing_notes: Option<usize>,
Expand Down
2 changes: 1 addition & 1 deletion zcash_client_sqlite/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ and this library adheres to Rust's notion of
- MSRV is now 1.77.0.
- Migrated from `schemer` to our fork `schemerz`.
- Migrated to `rusqlite 0.32`.
- `error::SqliteClientError` has additional variant `NoteSelectorInvalid`
- `error::SqliteClientError` has additional variant `NoteFilterInvalid`

## [0.12.2] - 2024-10-21

Expand Down
2 changes: 1 addition & 1 deletion zcash_client_sqlite/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl fmt::Display for SqliteClientError {
SqliteClientError::ChainHeightUnknown => write!(f, "Chain height unknown; please call `update_chain_tip`"),
SqliteClientError::UnsupportedPoolType(t) => write!(f, "Pool type is not currently supported: {}", t),
SqliteClientError::BalanceError(e) => write!(f, "Balance error: {}", e),
SqliteClientError::NoteFilterInvalid(s) => write!(f, "Could not evaluate selection query: {:?}", s),
SqliteClientError::NoteFilterInvalid(s) => write!(f, "Could not evaluate filter query: {:?}", s),

Check warning on line 194 in zcash_client_sqlite/src/error.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/error.rs#L194

Added line #L194 was not covered by tests
#[cfg(feature = "transparent-inputs")]
SqliteClientError::ReachedGapLimit(account_id, bad_index) => write!(f,
"The proposal cannot be constructed until transactions with previously reserved ephemeral address outputs have been mined. \
Expand Down
66 changes: 26 additions & 40 deletions zcash_client_sqlite/src/wallet/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub(crate) fn spendable_notes_meta(
protocol: ShieldedProtocol,
chain_tip_height: BlockHeight,
account: AccountId,
selector: &NoteFilter,
filter: &NoteFilter,
exclude: &[ReceivedNoteId],
) -> Result<Option<PoolMeta>, SqliteClientError> {
let (table_prefix, _, _) = per_protocol_names(protocol);
Expand Down Expand Up @@ -304,12 +304,11 @@ pub(crate) fn spendable_notes_meta(
// determine the minimum value of notes to be produced by note splitting.
fn min_note_value(
conn: &rusqlite::Connection,
table_prefix: &str,
account: AccountId,
selector: &NoteFilter,
filter: &NoteFilter,
chain_tip_height: BlockHeight,
) -> Result<Option<NonNegativeAmount>, SqliteClientError> {
match selector {
match filter {
NoteFilter::ExceedsMinValue(v) => Ok(Some(*v)),
NoteFilter::ExceedsPriorSendPercentile(n) => {
let mut bucket_query = conn.prepare(
Expand Down Expand Up @@ -351,27 +350,24 @@ pub(crate) fn spendable_notes_meta(
}
NoteFilter::ExceedsBalancePercentage(p) => {
let balance = conn.query_row_and_then::<_, SqliteClientError, _, _>(
&format!(
"SELECT SUM(rn.value)
FROM v_received_outputs rn
INNER JOIN accounts a ON a.id = rn.account_id
INNER JOIN transactions ON transactions.id_tx = rn.transaction_id
WHERE rn.account_id = :account_id
AND a.ufvk IS NOT NULL
AND transactions.mined_height IS NOT NULL
AND rn.pool != :transparent_pool
AND rn.id_within_pool_table NOT IN (
SELECT rns.received_output_id
FROM v_received_output_spends rns
JOIN transactions stx ON stx.id_tx = rns.transaction_id
WHERE rns.pool == rn.pool
AND (
stx.block IS NOT NULL -- the spending tx is mined
OR stx.expiry_height IS NULL -- the spending tx will not expire
OR stx.expiry_height > :chain_tip_height -- the spending tx is unexpired
)
)"
),
"SELECT SUM(rn.value)
FROM v_received_outputs rn
INNER JOIN accounts a ON a.id = rn.account_id
INNER JOIN transactions ON transactions.id_tx = rn.transaction_id
WHERE rn.account_id = :account_id
AND a.ufvk IS NOT NULL
AND transactions.mined_height IS NOT NULL
AND rn.pool != :transparent_pool
AND (rn.pool, rn.id_within_pool_table) NOT IN (
SELECT rns.pool, rns.received_output_id
FROM v_received_output_spends rns
JOIN transactions stx ON stx.id_tx = rns.transaction_id
WHERE (
stx.block IS NOT NULL -- the spending tx is mined
OR stx.expiry_height IS NULL -- the spending tx will not expire
OR stx.expiry_height > :chain_tip_height -- the spending tx is unexpired
)
)",
named_params![
":account_id": account.0,
":chain_tip_height": u32::from(chain_tip_height),
Expand All @@ -391,10 +387,8 @@ pub(crate) fn spendable_notes_meta(
NoteFilter::Combine(a, b) => {

Check warning on line 387 in zcash_client_sqlite/src/wallet/common.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet/common.rs#L387

Added line #L387 was not covered by tests
// All the existing note selectors set lower bounds on note value, so the "and"
// operation is just taking the maximum of the two lower bounds.
let a_min_value =
min_note_value(conn, table_prefix, account, a.as_ref(), chain_tip_height)?;
let b_min_value =
min_note_value(conn, table_prefix, account, b.as_ref(), chain_tip_height)?;
let a_min_value = min_note_value(conn, account, a.as_ref(), chain_tip_height)?;
let b_min_value = min_note_value(conn, account, b.as_ref(), chain_tip_height)?;
Ok(a_min_value
.zip(b_min_value)
.map(|(av, bv)| std::cmp::max(av, bv))
Expand All @@ -405,15 +399,9 @@ pub(crate) fn spendable_notes_meta(
condition,
fallback,

Check warning on line 400 in zcash_client_sqlite/src/wallet/common.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet/common.rs#L399-L400

Added lines #L399 - L400 were not covered by tests
} => {
let cond = min_note_value(
conn,
table_prefix,
account,
condition.as_ref(),
chain_tip_height,
)?;
let cond = min_note_value(conn, account, condition.as_ref(), chain_tip_height)?;

Check warning on line 402 in zcash_client_sqlite/src/wallet/common.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet/common.rs#L402

Added line #L402 was not covered by tests
if cond.is_none() {
min_note_value(conn, table_prefix, account, fallback, chain_tip_height)
min_note_value(conn, account, fallback, chain_tip_height)

Check warning on line 404 in zcash_client_sqlite/src/wallet/common.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet/common.rs#L404

Added line #L404 was not covered by tests
} else {
Ok(cond)

Check warning on line 406 in zcash_client_sqlite/src/wallet/common.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_sqlite/src/wallet/common.rs#L406

Added line #L406 was not covered by tests
}
Expand All @@ -423,9 +411,7 @@ pub(crate) fn spendable_notes_meta(

// TODO: Simplify the query before executing it. Not worrying about this now because queries
// will be developer-configured, not end-user defined.
if let Some(min_value) =
min_note_value(conn, table_prefix, account, selector, chain_tip_height)?
{
if let Some(min_value) = min_note_value(conn, account, filter, chain_tip_height)? {
let (note_count, total_value) = run_selection(min_value)?;

Ok(Some(PoolMeta::new(
Expand Down

0 comments on commit 00cafa3

Please sign in to comment.