Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transfer-contract: add next_pos contract call #1253

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions contracts/transfer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ unsafe fn existing_nullifiers(arg_len: u32) -> u32 {
})
}

#[no_mangle]
unsafe fn num_notes(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| STATE.num_notes())
}

// "Feeder" queries

#[no_mangle]
Expand Down
5 changes: 5 additions & 0 deletions contracts/transfer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ impl TransferState {
self.tree.root()
}

/// Get the count of the notes in the tree.
pub fn num_notes(&self) -> u64 {
self.tree.leaves_len()
}

/// Get the opening
pub fn opening(
&self,
Expand Down
4 changes: 4 additions & 0 deletions contracts/transfer/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ impl Tree {
) -> Option<PoseidonOpening<(), TRANSFER_TREE_DEPTH, A>> {
self.tree.opening(pos)
}

pub fn leaves_len(&self) -> u64 {
self.leaves.len() as u64
}
}
22 changes: 21 additions & 1 deletion contracts/transfer/tests/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ fn leaves_from_pos(session: &mut Session, pos: u64) -> Result<Vec<TreeLeaf>> {
.collect())
}

fn num_notes(session: &mut Session) -> Result<u64> {
session
.call(TRANSFER_CONTRACT, "num_notes", &(), u64::MAX)
.map(|r| r.data)
}

fn update_root(session: &mut Session) -> Result<()> {
session
.call(TRANSFER_CONTRACT, "update_root", &(), POINT_LIMIT)
Expand Down Expand Up @@ -254,6 +260,13 @@ fn transfer() {

assert_eq!(leaves.len(), 1, "There should be one note in the state");

let pos = num_notes(session).expect("Getting num_notes should succeed");
assert_eq!(
pos,
leaves.last().expect("note to exists").note.pos() + 1,
"num_notes should match position of last note + 1"
);

let leaves = leaves_from_pos(session, 0)
.expect("Getting leaves in the given range should succeed");

Expand Down Expand Up @@ -364,7 +377,14 @@ fn transfer() {
"There should be three notes in the tree at this block height"
);

let leaves = leaves_from_height(session, input_note.pos() + 1)
let pos = num_notes(session).expect("Getting num_notes should succeed");
assert_eq!(
pos,
leaves.last().expect("note to exists").note.pos() + 1,
"num_notes should match position of last note + 1"
);

let leaves = leaves_from_pos(session, input_note.pos() + 1)
.expect("Getting the notes should succeed");
assert_eq!(
leaves.len(),
Expand Down