Skip to content

Commit

Permalink
feat(demo-constructor): Add support for handle_signal entry point a…
Browse files Browse the repository at this point in the history
…nd `system_reserve_gas` syscall. (#3469)

Co-authored-by: Dmitry Novikov <[email protected]>
  • Loading branch information
mqxf and breathx authored Nov 9, 2023
1 parent c910fe8 commit b5b4028
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 18 deletions.
4 changes: 4 additions & 0 deletions examples/constructor/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,8 @@ impl Calls {
pub fn infinite_loop(self) -> Self {
self.add_call(Call::Loop)
}

pub fn system_reserve_gas(self, gas: impl Into<Arg<u64>>) -> Self {
self.add_call(Call::SystemReserveGas(gas.into()))
}
}
13 changes: 13 additions & 0 deletions examples/constructor/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum Call {
Wake(Arg<[u8; 32]>),
MessageId,
Loop,
SystemReserveGas(Arg<u64>),
}

#[cfg(not(feature = "wasm-wrapper"))]
Expand Down Expand Up @@ -317,6 +318,17 @@ mod wasm {
Some(msg::id().encode())
}

fn system_reserve_gas(self) -> Option<Vec<u8>> {
let Self::SystemReserveGas(gas) = self else {
unreachable!()
};

let gas = gas.value();
exec::system_reserve_gas(gas).expect("Failed to reserve gas");

None
}

pub(crate) fn process(self, previous: Option<CallResult>) -> CallResult {
debug!("\t[CONSTRUCTOR] >> Processing {self:?}");
let call = self.clone();
Expand Down Expand Up @@ -347,6 +359,7 @@ mod wasm {
Call::MessageId => self.message_id(),
#[allow(clippy::empty_loop)]
Call::Loop => loop {},
Call::SystemReserveGas(..) => self.system_reserve_gas(),
};

(call, value)
Expand Down
6 changes: 5 additions & 1 deletion examples/constructor/src/scheme/demo_exit_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub fn handle_reply() -> Calls {
Calls::builder().noop()
}

pub fn handle_signal() -> Calls {
Calls::builder().noop()
}

pub fn scheme() -> Scheme {
Scheme::predefined(init(), handle(), handle_reply())
Scheme::predefined(init(), handle(), handle_reply(), handle_signal())
}
11 changes: 10 additions & 1 deletion examples/constructor/src/scheme/demo_exit_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ pub fn handle_reply() -> Calls {
Calls::builder().noop()
}

pub fn handle_signal() -> Calls {
Calls::builder().noop()
}

pub fn scheme(send_before_exit: bool) -> Scheme {
Scheme::predefined(init(send_before_exit), handle(), handle_reply())
Scheme::predefined(
init(send_before_exit),
handle(),
handle_reply(),
handle_signal(),
)
}
6 changes: 5 additions & 1 deletion examples/constructor/src/scheme/demo_ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub fn handle_reply() -> Calls {
Calls::builder().noop()
}

pub fn handle_signal() -> Calls {
Calls::builder().noop()
}

pub fn scheme() -> Scheme {
Scheme::predefined(init(), handle(), handle_reply())
Scheme::predefined(init(), handle(), handle_reply(), handle_signal())
}
11 changes: 10 additions & 1 deletion examples/constructor/src/scheme/demo_proxy_with_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ pub fn handle_reply() -> Calls {
)
}

pub fn handle_signal() -> Calls {
Calls::builder().noop()
}

pub fn scheme(destination: [u8; 32], delay: u32) -> Scheme {
Scheme::predefined(init(destination, delay), handle(), handle_reply())
Scheme::predefined(
init(destination, delay),
handle(),
handle_reply(),
handle_signal(),
)
}
5 changes: 5 additions & 0 deletions examples/constructor/src/scheme/demo_reply_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ pub fn handle_reply(checker: [u8; 32]) -> Calls {
.send_wgas(checker, Arg::bytes(SUCCESS_MESSAGE), 10_000)
}

pub fn handle_signal() -> Calls {
Calls::builder().noop()
}

pub fn scheme(checker: [u8; 32], destination: [u8; 32], gas_to_send: u64) -> Scheme {
Scheme::predefined(
init(),
handle(destination, gas_to_send),
handle_reply(checker),
handle_signal(),
)
}
6 changes: 5 additions & 1 deletion examples/constructor/src/scheme/demo_wait_init_exit_reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub fn handle_reply() -> Calls {
.exit(source_var)
}

pub fn handle_signal() -> Calls {
Calls::builder().noop()
}

pub fn scheme() -> Scheme {
Scheme::predefined(init(), handle(), handle_reply())
Scheme::predefined(init(), handle(), handle_reply(), handle_signal())
}
27 changes: 22 additions & 5 deletions examples/constructor/src/scheme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum Scheme {
///
/// Better to use this scheme if you need common-purpose program that
/// executes the same commands across different incoming payloads.
Predefined(Vec<Call>, Vec<Call>, Vec<Call>),
Predefined(Vec<Call>, Vec<Call>, Vec<Call>, Vec<Call>),
}

impl Scheme {
Expand All @@ -38,8 +38,18 @@ impl Scheme {
Self::Direct(init.calls())
}

pub fn predefined(init: Calls, handle: Calls, handle_reply: Calls) -> Self {
Self::Predefined(init.calls(), handle.calls(), handle_reply.calls())
pub fn predefined(
init: Calls,
handle: Calls,
handle_reply: Calls,
handle_signal: Calls,
) -> Self {
Self::Predefined(
init.calls(),
handle.calls(),
handle_reply.calls(),
handle_signal.calls(),
)
}

pub fn init(&self) -> &Vec<Call> {
Expand All @@ -51,14 +61,21 @@ impl Scheme {

pub fn handle(&self) -> Option<&Vec<Call>> {
match self {
Self::Predefined(_, handle, _) => Some(handle),
Self::Predefined(_, handle, _, _) => Some(handle),
_ => None,
}
}

pub fn handle_reply(&self) -> Option<&Vec<Call>> {
match self {
Self::Predefined(_, _, handle_reply) => Some(handle_reply),
Self::Predefined(_, _, handle_reply, _) => Some(handle_reply),
_ => None,
}
}

pub fn handle_signal(&self) -> Option<&Vec<Call>> {
match self {
Self::Predefined(_, _, _, handle_signal) => Some(handle_signal),
_ => None,
}
}
Expand Down
5 changes: 5 additions & 0 deletions examples/constructor/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ extern "C" fn handle() {
extern "C" fn handle_reply() {
process_fn(Scheme::handle_reply);
}

#[no_mangle]
extern "C" fn handle_signal() {
process_fn(Scheme::handle_signal);
}
36 changes: 28 additions & 8 deletions pallets/gear/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ fn value_counter_set_correctly_for_interruptions() {
.send_value(Arg::new([0u8; 32]), Arg::new(vec![]), "value_store")
.wait_for(1);

let scheme = Scheme::predefined(Calls::builder().noop(), handle, Calls::builder().noop());
let scheme = Scheme::predefined(
Calls::builder().noop(),
handle,
Calls::builder().noop(),
Calls::builder().noop(),
);

init_logger();
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -5937,7 +5942,12 @@ fn pause_terminated_exited_program() {
let (_, terminated_pid) = submit_constructor_with_args(
USER_1,
DEFAULT_SALT,
Scheme::predefined(init, Default::default(), Default::default()),
Scheme::predefined(
init,
Default::default(),
Default::default(),
Default::default(),
),
0,
);

Expand Down Expand Up @@ -8555,7 +8565,7 @@ fn demo_constructor_is_demo_ping() {

let handle_reply = Calls::builder().panic("I don't like replies");

let scheme = Scheme::predefined(init, handle, handle_reply);
let scheme = Scheme::predefined(init, handle, handle_reply, Default::default());

// checking init
let (_init_mid, constructor_id) = utils::init_constructor(scheme);
Expand Down Expand Up @@ -14105,7 +14115,12 @@ fn double_read_works() {
.load("read2")
.bytes_eq("is_eq", "read1", "read2")
.if_else("is_eq", noop_branch, panic_branch);
let predefined_scheme = Scheme::predefined(Default::default(), handle, Default::default());
let predefined_scheme = Scheme::predefined(
Default::default(),
handle,
Default::default(),
Default::default(),
);

let (_, pid) = utils::init_constructor(predefined_scheme);

Expand Down Expand Up @@ -14334,7 +14349,7 @@ fn test_send_to_terminated_from_program() {
// Using `USER_2` not to pollute `USER_1` mailbox to make test easier.
USER_2,
b"salt1",
Scheme::predefined(init, handle, Calls::default()),
Scheme::predefined(init, handle, Calls::default(), Calls::default()),
0,
);

Expand All @@ -14351,7 +14366,7 @@ fn test_send_to_terminated_from_program() {
// Using `USER_2` not to pollute `USER_1` mailbox to make test easier.
USER_2,
b"salt2",
Scheme::predefined(Calls::default(), handle, handle_reply),
Scheme::predefined(Calls::default(), handle, handle_reply, Calls::default()),
0,
);

Expand Down Expand Up @@ -14650,7 +14665,7 @@ fn test_gas_info_of_terminated_program() {
let (_, pid_dead) = utils::submit_constructor_with_args(
USER_1,
b"salt1",
Scheme::predefined(init_dead, handle_dead, Calls::default()),
Scheme::predefined(init_dead, handle_dead, Calls::default(), Calls::default()),
0,
);

Expand All @@ -14659,7 +14674,12 @@ fn test_gas_info_of_terminated_program() {
let (_, proxy_pid) = utils::submit_constructor_with_args(
USER_1,
b"salt2",
Scheme::predefined(Calls::default(), handle_proxy, Calls::default()),
Scheme::predefined(
Calls::default(),
handle_proxy,
Calls::default(),
Calls::default(),
),
0,
);

Expand Down

0 comments on commit b5b4028

Please sign in to comment.