Skip to content

Commit

Permalink
feat(pallet-gear): Added example and test to test signal weak case (#…
Browse files Browse the repository at this point in the history
…3421)

Co-authored-by: StackOverflowExcept1on <[email protected]>
Co-authored-by: Georgy Shepelev <[email protected]>
Co-authored-by: clearloop <[email protected]>
  • Loading branch information
4 people authored Nov 27, 2023
1 parent 2f398cf commit 103b349
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ members = [
"examples/rwlock",
"examples/send-from-reservation",
"examples/signal-entry",
"examples/signal-wait",
"examples/state-rollback",
"examples/sync-duplicate",
"examples/sys-calls",
Expand Down Expand Up @@ -416,6 +417,7 @@ demo-reserve-gas = { path = "examples/reserve-gas", default-features = false }
demo-rwlock = { path = "examples/rwlock" }
demo-send-from-reservation = { path = "examples/send-from-reservation" }
demo-signal-entry = { path = "examples/signal-entry" }
demo-signal-wait = { path = "examples/signal-wait" }
demo-state-rollback = { path = "examples/state-rollback" }
demo-sync-duplicate = { path = "examples/sync-duplicate" }
demo-vec = { path = "examples/vec" }
Expand Down
19 changes: 19 additions & 0 deletions examples/signal-wait/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "demo-signal-wait"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
gstd.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true

[features]
debug = ["gstd/debug"]
default = ["std"]
std = []
21 changes: 21 additions & 0 deletions examples/signal-wait/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is part of Gear.

// Copyright (C) 2021-2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

fn main() {
gear_wasm_builder::build();
}
30 changes: 30 additions & 0 deletions examples/signal-wait/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This file is part of Gear.

// Copyright (C) 2022-2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![no_std]

#[cfg(feature = "std")]
mod code {
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
}

#[cfg(feature = "std")]
pub use code::WASM_BINARY_OPT as WASM_BINARY;

#[cfg(not(feature = "std"))]
mod wasm;
39 changes: 39 additions & 0 deletions examples/signal-wait/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of Gear.

// Copyright (C) 2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use gstd::{exec, prelude::*};

static mut FIRST_EXEC: bool = true;

#[no_mangle]
extern "C" fn handle() {
if unsafe { FIRST_EXEC } {
unsafe {
FIRST_EXEC = false;
}
exec::system_reserve_gas(1_000_000_000).unwrap();
exec::wait_for(1);
} else {
panic!();
}
}

#[no_mangle]
extern "C" fn handle_signal() {
exec::wait();
}
1 change: 1 addition & 0 deletions pallets/gear/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ demo-rwlock.workspace = true
demo-reservation-manager.workspace = true
demo-send-from-reservation.workspace = true
demo-signal-entry.workspace = true
demo-signal-wait.workspace = true
demo-state-rollback.workspace = true
demo-async-signal-entry.workspace = true
demo-async-custom-entry.workspace = true
Expand Down
58 changes: 58 additions & 0 deletions pallets/gear/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15028,6 +15028,64 @@ fn test_gas_info_of_terminated_program() {
})
}

#[test]
fn test_handle_signal_wait() {
use demo_signal_wait::WASM_BINARY;

init_logger();
new_test_ext().execute_with(|| {
assert_ok!(Gear::upload_program(
RuntimeOrigin::signed(USER_1),
WASM_BINARY.to_vec(),
DEFAULT_SALT.to_vec(),
EMPTY_PAYLOAD.to_vec(),
100_000_000_000,
0,
false,
));

let pid = get_last_program_id();

run_to_next_block(None);

assert!(Gear::is_active(pid));
assert!(Gear::is_initialized(pid));

assert_ok!(Gear::send_message(
RuntimeOrigin::signed(USER_1),
pid,
EMPTY_PAYLOAD.to_vec(),
50_000_000_000,
0,
false,
));

let mid = get_last_message_id();

run_to_next_block(None);

assert_ok!(GasHandlerOf::<Test>::get_system_reserve(mid));
assert!(WaitlistOf::<Test>::contains(&pid, &mid));

run_to_next_block(None);

let signal_mid = MessageId::generate_signal(mid);
assert!(WaitlistOf::<Test>::contains(&pid, &signal_mid));

let (mid, block) = get_last_message_waited();

assert_eq!(mid, signal_mid);

System::set_block_number(block - 1);
Gear::set_block_number(block - 1);
run_to_next_block(None);

assert!(!WaitlistOf::<Test>::contains(&pid, &signal_mid));

assert_total_dequeued(4);
});
}

mod utils {
#![allow(unused)]

Expand Down

0 comments on commit 103b349

Please sign in to comment.