-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gstd): Add handle_reply to MessageFuture (#4046)
- Loading branch information
Showing
16 changed files
with
496 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "demo-async-reply-hook" | ||
version = "0.1.0" | ||
authors.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
gstd.workspace = true | ||
parity-scale-codec.workspace = true | ||
futures.workspace = true | ||
|
||
[build-dependencies] | ||
gear-wasm-builder.workspace = true | ||
|
||
[features] | ||
debug = ["gstd/debug"] | ||
default = ["std"] | ||
std = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// This file is part of Gear. | ||
|
||
// Copyright (C) 2023-2024 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// This file is part of Gear. | ||
|
||
// Copyright (C) 2023-2024 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(target_arch = "wasm32")] | ||
mod wasm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// This file is part of Gear. | ||
|
||
// Copyright (C) 2023-2024 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::{critical, debug, exec, msg, prelude::*, ActorId}; | ||
|
||
#[gstd::async_main] | ||
async fn main() { | ||
let source = msg::source(); | ||
|
||
// Case 1: Message without reply hook | ||
let m1 = gstd::msg::send_bytes_for_reply(source, b"for_reply_1", 0, 0) | ||
.expect("Failed to send message"); | ||
|
||
// Case 2: Message with reply hook but we don't reply to it | ||
let m2 = gstd::msg::send_bytes_for_reply(source, b"for_reply_2", 0, 1_000_000_000) | ||
.expect("Failed to send message") | ||
.up_to(Some(5)) | ||
.expect("Failed to set timeout") | ||
.handle_reply(|| { | ||
// This should be called in gas / 100 blocks, but the program exits by that time | ||
unreachable!("This should not be called"); | ||
}) | ||
.expect("Failed to set reply hook"); | ||
|
||
// Case 3: Message with reply hook and we reply to it | ||
let for_reply_3 = gstd::rc::Rc::new(core::cell::RefCell::new(false)); | ||
let for_reply_3_clone = for_reply_3.clone(); | ||
let m3 = gstd::msg::send_bytes_for_reply(source, b"for_reply_3", 0, 1_000_000_000) | ||
.expect("Failed to send message") | ||
.handle_reply(move || { | ||
debug!("reply message_id: {:?}", msg::id()); | ||
debug!("reply payload: {:?}", msg::load_bytes()); | ||
|
||
assert_eq!(msg::load_bytes().unwrap(), [3]); | ||
|
||
msg::send_bytes(msg::source(), b"saw_reply_3", 0); | ||
for_reply_3_clone.replace(true); | ||
}) | ||
.expect("Failed to set reply hook"); | ||
|
||
// Case 4: We reply to message after timeout | ||
let m4 = gstd::msg::send_bytes_for_reply(source, b"for_reply_4", 0, 1_000_000_000) | ||
.expect("Failed to send message") | ||
.up_to(Some(5)) | ||
.expect("Failed to set timeout") | ||
.handle_reply(|| { | ||
debug!("reply message_id: {:?}", msg::id()); | ||
debug!("reply payload: {:?}", msg::load_bytes()); | ||
|
||
assert_eq!(msg::load_bytes().unwrap(), [4]); | ||
|
||
msg::send_bytes(msg::source(), b"saw_reply_4", 0); | ||
}) | ||
.expect("Failed to set reply hook"); | ||
|
||
m1.await.expect("Received error reply"); | ||
|
||
assert_eq!( | ||
m2.await.expect_err("Should receive timeout"), | ||
gstd::errors::Error::Timeout(8, 8) | ||
); | ||
|
||
m3.await.expect("Received error reply"); | ||
// check for_reply_3 handle_reply executed | ||
assert!(for_reply_3.replace(false)); | ||
|
||
assert_eq!( | ||
m4.await.expect_err("Should receive timeout"), | ||
gstd::errors::Error::Timeout(8, 8) | ||
); | ||
|
||
msg::send_bytes(source, b"completed", 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// This file is part of Gear. | ||
|
||
// Copyright (C) 2023-2024 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 crate::MessageId; | ||
use alloc::boxed::Box; | ||
use hashbrown::HashMap; | ||
|
||
pub(crate) struct HooksMap(HashMap<MessageId, Box<dyn FnOnce()>>); | ||
|
||
impl HooksMap { | ||
pub fn new() -> Self { | ||
Self(HashMap::new()) | ||
} | ||
|
||
/// Register hook to be executed when a reply for message_id is received. | ||
pub(crate) fn register<F: FnOnce() + 'static>(&mut self, mid: MessageId, f: F) { | ||
if self.0.contains_key(&mid) { | ||
panic!("handle_reply: reply hook for this message_id is already registered"); | ||
} | ||
self.0.insert(mid, Box::new(f)); | ||
} | ||
|
||
/// Execute hook for message_id (if registered) | ||
pub(crate) fn execute_and_remove(&mut self, message_id: MessageId) { | ||
if let Some(f) = self.0.remove(&message_id) { | ||
f(); | ||
} | ||
} | ||
|
||
/// Clear hook for message_id without executing it. | ||
pub(crate) fn remove(&mut self, message_id: MessageId) { | ||
self.0.remove(&message_id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.