Skip to content

Commit

Permalink
Refactoring of change calculation.
Browse files Browse the repository at this point in the history
Signed-off-by: Daira-Emma Hopwood <[email protected]>
  • Loading branch information
daira committed Jun 18, 2024
1 parent 4f935a7 commit 0b7f60d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
43 changes: 21 additions & 22 deletions zcash_client_backend/src/fees/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,35 +90,34 @@ where
})
}

pub(crate) fn single_change_output_policy<NoteRefT: Clone, F: FeeRule, E>(
/// Decide which shielded pool change should go to if there is any.
pub(crate) fn single_change_output_policy(
_net_flows: &NetFlows,
_fallback_change_pool: ShieldedProtocol,
) -> Result<(ShieldedProtocol, usize, usize), ChangeError<E, NoteRefT>>
where
E: From<F::Error> + From<BalanceError>,
{
) -> (ShieldedProtocol, usize, usize) {
// TODO: implement a less naive strategy for selecting the pool to which change will be sent.
#[cfg(feature = "orchard")]
let (change_pool, sapling_change, orchard_change) =
let change_pool = {
#[cfg(feature = "orchard")]
if _net_flows.orchard_in.is_positive() || _net_flows.orchard_out.is_positive() {
// Send change to Orchard if we're spending any Orchard inputs or creating any Orchard outputs
(ShieldedProtocol::Orchard, 0, 1)
// Send change to Orchard if we're spending any Orchard inputs or creating any Orchard outputs.
ShieldedProtocol::Orchard
} else if _net_flows.sapling_in.is_positive() || _net_flows.sapling_out.is_positive() {
// Otherwise, send change to Sapling if we're spending any Sapling inputs or creating any
// Sapling outputs, so that we avoid pool-crossing.
(ShieldedProtocol::Sapling, 1, 0)
ShieldedProtocol::Sapling
} else {
// This is a fully-transparent transaction, so the caller gets to decide
// where to shield change.
match _fallback_change_pool {
ShieldedProtocol::Orchard => (_fallback_change_pool, 0, 1),
ShieldedProtocol::Sapling => (_fallback_change_pool, 1, 0),
}
};
#[cfg(not(feature = "orchard"))]
let (change_pool, sapling_change, orchard_change) = (ShieldedProtocol::Sapling, 1, 0);

Ok((change_pool, sapling_change, orchard_change))
// The flows are transparent, so there may not be change. If there is, the caller
// gets to decide where to shield it.
_fallback_change_pool
}
#[cfg(not(feature = "orchard"))]
ShieldedProtocol::Sapling
};
(
change_pool,
(change_pool == ShieldedProtocol::Sapling).into(),
(change_pool == ShieldedProtocol::Orchard).into(),
)
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -154,7 +153,7 @@ where
orchard,
)?;
let (change_pool, sapling_change, _orchard_change) =
single_change_output_policy::<NoteRefT, F, E>(&net_flows, _fallback_change_pool)?;
single_change_output_policy(&net_flows, _fallback_change_pool);

let sapling_input_count = sapling
.bundle_type()
Expand Down
5 changes: 1 addition & 4 deletions zcash_client_backend/src/fees/zip317.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ impl ChangeStrategy for SingleOutputChangeStrategy {
orchard,
)?;
let (_, sapling_change, orchard_change) =
single_change_output_policy::<NoteRefT, Self::FeeRule, Self::Error>(
&net_flows,
self.fallback_change_pool,
)?;
single_change_output_policy(&net_flows, self.fallback_change_pool);

let s_non_dust = sapling.inputs().len() - sapling_dust.len();
let s_allowed_dust =
Expand Down

0 comments on commit 0b7f60d

Please sign in to comment.