@@ -25,7 +25,7 @@ use masp_primitives::transaction::fees::fixed::FeeRule;
25
25
use masp_primitives:: transaction:: { builder, Transaction } ;
26
26
use masp_primitives:: zip32:: { ExtendedKey , PseudoExtendedKey } ;
27
27
use namada_core:: address:: Address ;
28
- use namada_core:: arith:: checked;
28
+ use namada_core:: arith:: { checked, CheckedAdd , CheckedSub } ;
29
29
use namada_core:: borsh:: { BorshDeserialize , BorshSerialize } ;
30
30
use namada_core:: chain:: BlockHeight ;
31
31
use namada_core:: collections:: { HashMap , HashSet } ;
@@ -367,11 +367,24 @@ impl<U: ShieldedUtils + MaybeSend + MaybeSync> ShieldedWallet<U> {
367
367
// Forget about the trace amount left over because we cannot
368
368
// realize its value
369
369
let trace = I128Sum :: from_pair ( asset_type, value % threshold) ;
370
- // Record how much more of the given conversion has been used
371
- * usage += required;
372
- // Apply the conversions to input and move the trace amount to output
373
- * input += conv * required - trace. clone ( ) ;
374
- * output += trace;
370
+ match checked ! ( input + & ( conv * required) - & trace) {
371
+ // If applying the conversion does not overflow or result in
372
+ // negative input
373
+ Ok ( new_input) if new_input >= I128Sum :: zero ( ) => {
374
+ // Record how much more of the given conversion has been used
375
+ * usage += required;
376
+ // Apply conversions to input and move trace amount to output
377
+ * input = new_input;
378
+ * output += trace;
379
+ }
380
+ _ => {
381
+ // Otherwise don't apply the conversion and simply move value
382
+ // over to output
383
+ let comp = I128Sum :: from_pair ( asset_type, value) ;
384
+ * output += comp. clone ( ) ;
385
+ * input -= comp;
386
+ }
387
+ }
375
388
Ok ( ( ) )
376
389
}
377
390
0 commit comments