-
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.
bugfix(runtime): Correct charging on delayed sendings from reservatio…
…ns (#3496)
- Loading branch information
Showing
12 changed files
with
454 additions
and
138 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
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,19 @@ | ||
[package] | ||
name = "demo-delayed-reservation-sender" | ||
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 = [] |
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) 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(); | ||
} |
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,50 @@ | ||
// 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/>. | ||
|
||
#![no_std] | ||
|
||
use gstd::codec::{Decode, Encode}; | ||
|
||
#[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; | ||
|
||
pub const SENDING_EXPECT: &str = "Failed to send delayed message from reservation"; | ||
|
||
#[derive(Encode, Decode, Debug, Clone, Copy)] | ||
#[codec(crate = gstd::codec)] | ||
pub enum ReservationSendingShowcase { | ||
ToSourceInPlace { | ||
reservation_amount: u64, | ||
reservation_delay: u32, | ||
sending_delay: u32, | ||
}, | ||
ToSourceAfterWait { | ||
reservation_amount: u64, | ||
reservation_delay: u32, | ||
wait_for: u32, | ||
sending_delay: u32, | ||
}, | ||
} | ||
|
||
#[cfg(not(feature = "std"))] | ||
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,75 @@ | ||
// 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 crate::{ReservationSendingShowcase, SENDING_EXPECT}; | ||
use gstd::{exec, msg, prelude::*, ReservationId}; | ||
|
||
static mut CALLED_BEFORE: bool = false; | ||
static mut RESERVATION_ID: Option<ReservationId> = None; | ||
|
||
#[no_mangle] | ||
extern "C" fn handle() { | ||
let showcase = msg::load().expect("Failed to load request"); | ||
|
||
match showcase { | ||
ReservationSendingShowcase::ToSourceInPlace { | ||
reservation_amount, | ||
reservation_delay, | ||
sending_delay, | ||
} => { | ||
let reservation_id = ReservationId::reserve(reservation_amount, reservation_delay) | ||
.expect("Failed to reserve gas"); | ||
|
||
msg::send_bytes_delayed_from_reservation( | ||
reservation_id, | ||
msg::source(), | ||
[], | ||
0, | ||
sending_delay, | ||
) | ||
.expect(SENDING_EXPECT); | ||
} | ||
ReservationSendingShowcase::ToSourceAfterWait { | ||
reservation_amount, | ||
reservation_delay, | ||
wait_for, | ||
sending_delay, | ||
} => { | ||
if unsafe { !CALLED_BEFORE } { | ||
let reservation_id = ReservationId::reserve(reservation_amount, reservation_delay) | ||
.expect("Failed to reserve gas"); | ||
|
||
unsafe { | ||
CALLED_BEFORE = true; | ||
RESERVATION_ID = Some(reservation_id); | ||
} | ||
|
||
exec::wait_for(wait_for); | ||
} | ||
|
||
msg::send_bytes_delayed_from_reservation( | ||
unsafe { RESERVATION_ID.expect("Unset") }, | ||
msg::source(), | ||
[], | ||
0, | ||
sending_delay, | ||
) | ||
.expect(SENDING_EXPECT); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.