Skip to content

Commit

Permalink
fix(Amounts): Add non-negativity test for outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
aszepieniec committed Feb 29, 2024
1 parent 643bdb6 commit f7206db
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/models/blockchain/type_scripts/native_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ impl ConsensusProgram for NativeCurrency {
if input_salted_utxos.utxos[i as usize].coins[j as usize].type_script_hash
== self_digest
{
// decode state to get amount
let amount: NeptuneCoins = *NeptuneCoins::decode(
&input_salted_utxos.utxos[i as usize].coins[j as usize].state,
)
.unwrap();

// safely add to total
total_input = total_input.safe_add(amount).unwrap();
j += 1;
}
Expand All @@ -133,10 +136,16 @@ impl ConsensusProgram for NativeCurrency {
if output_salted_utxos.utxos[i as usize].coins[j as usize].type_script_hash
== self_digest
{
// decode state to get amount
let amount: NeptuneCoins = *NeptuneCoins::decode(
&output_salted_utxos.utxos[i as usize].coins[j as usize].state,
)
.unwrap();

// make sure amount is positive (or zero)
assert!(!amount.is_negative());

// safely add to total
total_output = total_output.safe_add(amount).unwrap();
j += 1;
}
Expand All @@ -146,6 +155,7 @@ impl ConsensusProgram for NativeCurrency {

// test no-inflation equation
let total_input_plus_coinbase: NeptuneCoins = total_input.safe_add(some_coinbase).unwrap();
assert!(!fee.is_negative());
let total_output_plus_coinbase: NeptuneCoins = total_output.safe_add(fee).unwrap();
assert_eq!(total_input_plus_coinbase, total_output_plus_coinbase);
}
Expand Down

0 comments on commit f7206db

Please sign in to comment.