Skip to content

Commit

Permalink
add maximum value to balance
Browse files Browse the repository at this point in the history
  • Loading branch information
artifex11 committed Aug 24, 2023
1 parent 61487fc commit 7b7e3e7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/dusk_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ jobs:
profile: minimal
toolchain: ${{ matrix.toolchain }}

- name: Add WASM target
run: rustup target add wasm32-unknown-unknown

- run: make wasm

- name: Run cargo check
Expand Down
13 changes: 11 additions & 2 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub fn balance(args: i32, len: i32) -> i32 {
};

let mut keys = unsafe { [mem::zeroed(); MAX_KEY] };
let mut values = Vec::with_capacity(notes.len());
let mut keys_len = 0;
let mut sum = 0u64;

Expand All @@ -71,6 +72,7 @@ pub fn balance(args: i32, len: i32) -> i32 {
}

if let Ok(v) = note.value(Some(&keys[idx])) {
values.push(v);
sum = sum.saturating_add(v);
continue 'outer;
}
Expand All @@ -79,7 +81,12 @@ pub fn balance(args: i32, len: i32) -> i32 {
return BalanceResponse::fail();
}

BalanceResponse::success(sum)
// the top 4 notes are the maximum value a transaction can have, given the
// circuit accepts up to 4 inputs
values.sort_by(|a, b| b.cmp(a));
let maximum = values.iter().take(4).sum::<u64>();

BalanceResponse::success(sum, maximum)
}

/// Computes a serialized unproven transaction from the given arguments.
Expand Down Expand Up @@ -218,10 +225,11 @@ impl BalanceResponse {

/// Returns a representation of a successful balance operation with the
/// computed value.
pub fn success(value: u64) -> i32 {
pub fn success(value: u64, maximum: u64) -> i32 {
Self {
success: true,
value,
maximum,
}
.as_i32_ptr()
}
Expand All @@ -231,6 +239,7 @@ impl BalanceResponse {
Self {
success: false,
value: 0,
maximum: 0,
}
.as_i32_ptr()
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub struct BalanceResponse {
pub success: bool,
/// Total computed balance
pub value: u64,
/// Maximum value per transaction
pub maximum: u64,
}

impl BalanceResponse {
Expand Down
5 changes: 3 additions & 2 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use wasmer::{imports, Function, Instance, Memory, Module, Store, Value};
#[test]
fn balance_works() {
let seed = [0xfa; utils::RNG_SEED];
let values = [10, 250, 15, 39];
let values = [10, 250, 15, 39, 55];

let notes = node::notes(&seed, values);

Expand All @@ -42,7 +42,8 @@ fn balance_works() {
wallet.call("free_mem", &[ptr, len]);

assert!(balance.success);
assert_eq!(balance.value, values.into_iter().sum::<u64>())
assert_eq!(balance.value, values.into_iter().sum::<u64>());
assert_eq!(balance.maximum, 359);
}

#[test]
Expand Down

0 comments on commit 7b7e3e7

Please sign in to comment.