Skip to content

Commit

Permalink
contracts: add increment_left_and_call to double counter
Browse files Browse the repository at this point in the history
The function is intended to test calls to contracts that do not exist,
whilst allowing for an easy check on the state.
  • Loading branch information
Eduardo Leegwater Simões committed Aug 21, 2024
1 parent decf50b commit 78993a8
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions contracts/double_counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![no_std]

use piecrust_uplink as uplink;
use uplink::{ContractError, ContractId};

/// Struct that describes the state of the DoubleCounter contract
pub struct DoubleCounter {
Expand Down Expand Up @@ -40,6 +41,20 @@ impl DoubleCounter {
let value = self.right_value + 1;
self.right_value = value;
}

/// Increment the counter by 1 and call the given contract, with the given
/// arguments.
///
/// This is intended to test the behavior of the contract on calling a
/// contract that doesn't exist.
pub fn increment_left_and_call(
&mut self,
contract: ContractId,
) -> Result<(), ContractError> {
let value = self.left_value + 1;
self.left_value = value;
uplink::call(contract, "hello", &())
}
}

/// Expose `Counter::read_value()` to the host
Expand All @@ -59,3 +74,11 @@ unsafe fn increment_left(arg_len: u32) -> u32 {
unsafe fn increment_right(arg_len: u32) -> u32 {
uplink::wrap_call(arg_len, |_: ()| STATE.increment_right())
}

/// Expose `Counter::increment_and_call()` to the host
#[no_mangle]
unsafe fn increment_left_and_call(arg_len: u32) -> u32 {
uplink::wrap_call_unchecked(arg_len, |arg| {
STATE.increment_left_and_call(arg)
})
}

0 comments on commit 78993a8

Please sign in to comment.