Skip to content

Commit

Permalink
transfer-contracts: add sync method
Browse files Browse the repository at this point in the history
Resolves #2240
  • Loading branch information
herr-seppia committed Sep 1, 2024
1 parent d59475d commit babe1bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 7 additions & 0 deletions contracts/transfer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ unsafe fn leaves_from_pos(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |pos| STATE.leaves_from_pos(pos))
}

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

// "Management" transactions

#[no_mangle]
Expand Down
20 changes: 18 additions & 2 deletions contracts/transfer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,24 @@ impl TransferState {
/// Feeds the host with the leaves in the tree, starting from the given
/// position.
pub fn leaves_from_pos(&self, pos: u64) {
for leaf in self.tree.leaves_pos(pos) {
rusk_abi::feed(leaf.clone());
self.sync(pos, 0)
}

/// Feeds the host with the leaves in the tree (up to `count_limit`
/// occurrences), starting from the given `from` position.
///
/// If `count_limit` is 0 there is no occurrences limit`
pub fn sync(&self, from: u64, count_limit: u64) {
let iter = self.tree.leaves_pos(from);

if count_limit == 0 {
for leaf in iter {
rusk_abi::feed(leaf.clone());
}
} else {
for leaf in iter.take(count_limit as usize) {
rusk_abi::feed(leaf.clone());
}
}
}

Expand Down

0 comments on commit babe1bc

Please sign in to comment.