Skip to content

Commit

Permalink
more protection of logic calls, added num_notes
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Jan 18, 2024
1 parent 1c8c5c0 commit a6c8c3e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 15 deletions.
8 changes: 8 additions & 0 deletions contracts/transfer-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ unsafe fn root(arg_len: u32) -> u32 {
})
}

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

#[no_mangle]
unsafe fn module_balance(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |module| {
Expand Down
5 changes: 5 additions & 0 deletions contracts/transfer-data/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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-data/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,8 @@ impl Tree {
) -> Option<PoseidonOpening<(), TRANSFER_TREE_DEPTH, A>> {
self.tree.opening(pos)
}

pub fn leaves_len(&self) -> u64 {
self.leaves.len() as u64
}
}
5 changes: 5 additions & 0 deletions contracts/transfer-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ unsafe fn root(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| STATE.root())
}

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

#[no_mangle]
unsafe fn module_balance(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |module| STATE.balance(&module))
Expand Down
6 changes: 6 additions & 0 deletions contracts/transfer-proxy/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ impl TransferProxy {
.expect("root query should succeed")
}

/// Get the count of the notes in the tree.
pub fn num_notes(&self) -> u64 {
rusk_abi::call::<(), u64>(self.target, "num_notes", &())
.expect("num_notes query should succeed")
}

/// Get the opening
pub fn opening(
&self,
Expand Down
64 changes: 50 additions & 14 deletions contracts/transfer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,64 @@ static mut STATE: TransferOps = TransferOps {};

#[no_mangle]
unsafe fn mint(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |arg| STATE.mint(arg))
rusk_abi::wrap_call(arg_len, |arg| {
assert_transfer_caller();
STATE.mint(arg)
})
}

#[no_mangle]
unsafe fn stct(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |arg| STATE.send_to_contract_transparent(arg))
rusk_abi::wrap_call(arg_len, |arg| {
assert_transfer_caller();
STATE.send_to_contract_transparent(arg)
})
}

#[no_mangle]
unsafe fn wfct(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |(arg, from_address)| {
assert_transfer_caller();
STATE.withdraw_from_contract_transparent(arg, from_address)
})
}

#[no_mangle]
unsafe fn wfct_raw(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |(arg, from_address)| {
assert_transfer_caller();
STATE.withdraw_from_contract_transparent_raw(arg, from_address)
})
}

#[no_mangle]
unsafe fn stco(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |arg| STATE.send_to_contract_obfuscated(arg))
rusk_abi::wrap_call(arg_len, |arg| {
assert_transfer_caller();
STATE.send_to_contract_obfuscated(arg)
})
}

#[no_mangle]
unsafe fn wfco(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |(arg, from_address)| {
assert_transfer_caller();
STATE.withdraw_from_contract_obfuscated(arg, from_address)
})
}

#[no_mangle]
unsafe fn wfco_raw(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |(arg, from_address)| {
assert_transfer_caller();
STATE.withdraw_from_contract_obfuscated_raw(arg, from_address)
})
}

#[no_mangle]
unsafe fn wfctc(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |(arg, from_address)| {
assert_transfer_caller();
STATE.withdraw_from_contract_transparent_to_contract(arg, from_address)
})
}
Expand All @@ -80,46 +94,68 @@ unsafe fn wfctc(arg_len: u32) -> u32 {

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

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

#[no_mangle]
unsafe fn module_balance(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |module| STATE.balance(&module))
rusk_abi::wrap_call(arg_len, |module| {
assert_transfer_caller();
STATE.balance(&module)
})
}

#[no_mangle]
unsafe fn message(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |(module, pk)| STATE.message(&module, &pk))
rusk_abi::wrap_call(arg_len, |(module, pk)| {
assert_transfer_caller();
STATE.message(&module, &pk)
})
}

#[no_mangle]
unsafe fn opening(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |pos| STATE.opening(pos))
rusk_abi::wrap_call(arg_len, |pos| {
assert_transfer_caller();
STATE.opening(pos)
})
}

#[no_mangle]
unsafe fn existing_nullifiers(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |nullifiers| {
assert_transfer_caller();
STATE.existing_nullifiers(&nullifiers)
})
}

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

// "Feeder" queries

#[no_mangle]
unsafe fn leaves_from_height(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |height| STATE.leaves_from_height(height))
rusk_abi::wrap_call(arg_len, |height| {
assert_transfer_caller();
STATE.leaves_from_height(height)
})
}

#[no_mangle]
unsafe fn leaves_from_pos(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |pos| STATE.leaves_from_pos(pos))
rusk_abi::wrap_call(arg_len, |pos| {
assert_transfer_caller();
STATE.leaves_from_pos(pos)
})
}

// "Management" transactions
Expand Down
3 changes: 2 additions & 1 deletion contracts/transfer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ impl TransferOps {

/// Get the count of the notes in the tree.
pub fn num_notes(&self) -> u64 {
self.tree.leaves_len()
rusk_abi::call::<(), u64>(TRANSFER_DATA_CONTRACT, "num_notes", &())
.expect("num_notes query should succeed")
}

/// Get the opening
Expand Down

0 comments on commit a6c8c3e

Please sign in to comment.