Skip to content

Commit 2c797fb

Browse files
LucasGrassocmichi
andauthored
Add gas_limit host fn (#2691)
* implement `gas_limit` host fn * add tests for `gas_limit` host fn * remove off-chain tests for `gas_limit` * add changes to changelog * run formatting * fix error in env-access ui test * run formatting * Apply suggestions from code review Co-authored-by: Michael Müller <[email protected]> * update versions in gas and misc hostfns internal tests --------- Co-authored-by: Michael Müller <[email protected]>
1 parent c172637 commit 2c797fb

File tree

10 files changed

+139
-1
lines changed

10 files changed

+139
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
[Unreleased]
88

99
### Added
10+
- Implements the API for the `pallet-revive` host function `gas_limit` - [#2691](https://github.com/use-ink/ink/pull/2691)
1011
- Implements the API for the `pallet-revive` host function `to_account_id` - [#2578](https://github.com/use-ink/ink/pull/2578)
1112
- Add `#[ink::contract_ref]` attribute - [#2648](https://github.com/use-ink/ink/pull/2648)
1213
- Add `ink_revive_types` (and remove `pallet-revive` dependency from `ink_e2e`) - [#2657](https://github.com/use-ink/ink/pull/2657)

crates/env/src/api.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ pub fn caller() -> Address {
7373
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::caller)
7474
}
7575

76+
/// Returns the block's `ref_time` limit.
77+
///
78+
/// See <https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink> for more information.
79+
pub fn gas_limit() -> u64 {
80+
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::gas_limit)
81+
}
82+
7683
/// Returns the transferred value for the contract execution.
7784
///
7885
/// # Errors

crates/env/src/backend.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ pub trait TypedEnvBackend: EnvBackend {
228228
/// For more details visit: [`caller`][`crate::caller`]
229229
fn caller(&mut self) -> Address;
230230

231+
/// Returns the block's `ref_time` limit.
232+
///
233+
/// # Note
234+
///
235+
/// For more details visit: [`gas_limit`][`crate::gas_limit`]
236+
fn gas_limit(&mut self) -> u64;
237+
231238
/// Returns the transferred value for the contract execution.
232239
///
233240
/// # Note

crates/env/src/engine/off_chain/impls.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ impl TypedEnvBackend for EnvInstance {
550550
.unwrap_or_else(|error| panic!("could not read `caller` property: {error:?}"))
551551
}
552552

553+
fn gas_limit(&mut self) -> u64 {
554+
unimplemented!("not implemented, the off-chain environment will be removed");
555+
}
556+
553557
fn transferred_value(&mut self) -> U256 {
554558
self.get_property(Engine::value_transferred)
555559
.unwrap_or_else(|error| {

crates/env/src/engine/on_chain/pallet_revive.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,10 @@ impl TypedEnvBackend for EnvInstance {
887887
.expect("The executed contract must have a caller with a valid account id.")
888888
}
889889

890+
fn gas_limit(&mut self) -> u64 {
891+
ext::gas_limit()
892+
}
893+
890894
fn transferred_value(&mut self) -> U256 {
891895
let mut scope = self.scoped_buffer();
892896
let u256: &mut [u8; 32] = scope.take(32).try_into().unwrap();

crates/ink/src/env_access.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,37 @@ where
108108
ink_env::caller()
109109
}
110110

111+
/// Returns the block ref_time limit.
112+
///
113+
/// # Example
114+
///
115+
/// ```
116+
/// #[ink::contract]
117+
/// mod my_contract {
118+
/// #[ink(storage)]
119+
/// pub struct MyContract;
120+
///
121+
/// impl MyContract {
122+
/// #[ink(constructor)]
123+
/// pub fn new() -> Self {
124+
/// Self {}
125+
/// }
126+
///
127+
/// #[ink(message)]
128+
/// pub fn get_limit(&self) -> u64 {
129+
/// self.env().gas_limit()
130+
/// }
131+
/// }
132+
/// }
133+
/// ```
134+
///
135+
/// # Note
136+
///
137+
/// For more details visit: [`ink_env::gas_limit`]
138+
pub fn gas_limit(self) -> u64 {
139+
ink_env::gas_limit()
140+
}
141+
111142
/// Returns the transferred value for the contract execution.
112143
///
113144
/// # Example

crates/ink/tests/ui/contract/pass/env-access.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod contract {
1414
let _ = Self::env().block_number();
1515
let _ = Self::env().caller();
1616
let _ = Self::env().minimum_balance();
17+
let _ = Self::env().gas_limit();
1718
let _ = Self::env().transferred_value();
1819
let _ = Self::env().weight_to_fee(0);
1920
Self {}
@@ -27,6 +28,7 @@ mod contract {
2728
let _ = self.env().block_number();
2829
let _ = self.env().caller();
2930
let _ = self.env().minimum_balance();
31+
let _ = self.env().gas_limit();
3032
let _ = self.env().transferred_value();
3133
let _ = self.env().weight_to_fee(0);
3234
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "gas_hostfns"
3+
description = "E2E tests for gas related host functions"
4+
version = "6.0.0-alpha.4"
5+
authors = ["Use Ink <[email protected]>"]
6+
edition = "2021"
7+
publish = false
8+
9+
[dependencies]
10+
ink = { path = "../../../crates/ink", default-features = false, features = ["unstable-hostfn"] }
11+
12+
[dev-dependencies]
13+
ink_e2e = { path = "../../../crates/e2e" }
14+
15+
[lib]
16+
path = "lib.rs"
17+
18+
[features]
19+
default = ["std"]
20+
std = [
21+
"ink/std",
22+
]
23+
ink-as-dependency = []
24+
e2e-tests = []
25+
26+
[package.metadata.ink-lang]
27+
abi = "ink"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#![cfg_attr(not(feature = "std"), no_std, no_main)]
2+
#![allow(clippy::new_without_default)]
3+
4+
#[ink::contract]
5+
mod gas_hostfns {
6+
#[ink(storage)]
7+
pub struct GasHostfns {}
8+
9+
impl GasHostfns {
10+
#[ink(constructor)]
11+
pub fn new() -> Self {
12+
Self {}
13+
}
14+
15+
/// Checks that the host function `gas_limit` works
16+
#[ink(message)]
17+
pub fn gas_limit(&self) -> u64 {
18+
self.env().gas_limit()
19+
}
20+
}
21+
22+
#[cfg(all(test, feature = "e2e-tests"))]
23+
mod e2e_tests {
24+
use super::*;
25+
use ink_e2e::ContractsBackend;
26+
27+
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
28+
29+
#[ink_e2e::test]
30+
async fn e2e_gas_limit_works<Client: E2EBackend>(
31+
mut client: Client,
32+
) -> E2EResult<()> {
33+
// given
34+
let contract = client
35+
.instantiate("gas_hostfns", &ink_e2e::alice(), &mut GasHostfnsRef::new())
36+
.submit()
37+
.await
38+
.expect("instantiate failed");
39+
let call_builder = contract.call_builder::<GasHostfns>();
40+
41+
// then
42+
let call_res = client
43+
.call(&ink_e2e::alice(), &call_builder.gas_limit())
44+
.submit()
45+
.await
46+
.unwrap_or_else(|err| {
47+
panic!("call failed: {:#?}", err);
48+
});
49+
50+
assert!(call_res.return_value() > 0);
51+
52+
Ok(())
53+
}
54+
}
55+
}

integration-tests/internal/misc-hostfns/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "misc_hostfns"
33
description = "E2E tests for various host functions"
4-
version = "6.0.0-alpha.1"
4+
version = "6.0.0-alpha.4"
55
authors = ["Use Ink <[email protected]>"]
66
edition = "2021"
77
publish = false

0 commit comments

Comments
 (0)