Skip to content

Commit

Permalink
Avoids using from_nonnegative for U128Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Aug 28, 2024
1 parent efeae2e commit b22fefb
Showing 1 changed file with 9 additions and 35 deletions.
44 changes: 9 additions & 35 deletions crates/sdk/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,8 @@ pub fn is_amount_required(
0
};

// FIXME: no nonnegative if possible. Actually, maybe I need it
// here, but still, check all the occurences. No I don't need it
let change_amt = U128Sum::from_nonnegative(
asset_type.to_owned(),
unsigned_change_amt,
)
.expect("Change is guaranteed to be non-negative");
let change_amt =
U128Sum::from_pair(asset_type.to_owned(), unsigned_change_amt);
changes = changes
.map(|prev| prev + change_amt.clone())
.or(Some(change_amt));
Expand All @@ -320,11 +315,10 @@ pub fn is_amount_required(
changes = changes.map(|mut chngs| {
for (delta_asset_type, delta_amt) in delta.components() {
if !dest.asset_types().contains(delta_asset_type) {
let rmng = U128Sum::from_nonnegative(
let rmng = U128Sum::from_pair(
delta_asset_type.to_owned(),
*delta_amt as u128,
)
.expect("Change is guaranteed to be non-negative");
);
chngs += rmng;
}
}
Expand Down Expand Up @@ -1755,13 +1749,7 @@ impl<U: ShieldedUtils + MaybeSend + MaybeSync> ShieldedContext<U> {
let mut fees = U128Sum::zero();
// Convert the shortfall into a U128Sum
for (asset_type, val) in asset_types.iter().zip(raw_amount) {
fees += U128Sum::from_nonnegative(*asset_type, val.into())
.map_err(|()| {
TransferErr::General(Error::Other(
"Fee amount is expected expected to be non-negative"
.to_string(),
))
})?;
fees += U128Sum::from_pair(*asset_type, val.into());
}

// 1. Try to use the change to pay fees
Expand All @@ -1776,16 +1764,10 @@ impl<U: ShieldedUtils + MaybeSend + MaybeSync> ShieldedContext<U> {
{
// Get the minimum between the available change and
// the due fee
let output_amt = U128Sum::from_nonnegative(
let output_amt = U128Sum::from_pair(
asset_type.to_owned(),
*change.min(fee_amt),
)
.map_err(|()| {
TransferErr::General(Error::Other(
"Fee amount is expected to be non-negative"
.to_string(),
))
})?;
);
let denominated_output_amt = context
.shielded_mut()
.await
Expand Down Expand Up @@ -1834,16 +1816,8 @@ impl<U: ShieldedUtils + MaybeSend + MaybeSync> ShieldedContext<U> {
// Decrease the changes by the amounts used for fee payment
for (sp, temp_changes) in temp_changes.iter() {
for (asset_type, temp_change) in temp_changes.components() {
let output_amt = U128Sum::from_nonnegative(
asset_type.to_owned(),
*temp_change,
)
.map_err(|()| {
TransferErr::General(Error::Other(
"Fee amount is expected expected to be non-negative"
.to_string(),
))
})?;
let output_amt =
U128Sum::from_pair(asset_type.to_owned(), *temp_change);

// Entry is guaranteed to be in the map
changes.entry(*sp).and_modify(|amt| *amt -= &output_amt);
Expand Down

0 comments on commit b22fefb

Please sign in to comment.