From 5030d89738ceaaaf8192b82bdcbfb9e449beeaea Mon Sep 17 00:00:00 2001 From: Lucas Grasso Date: Fri, 24 Oct 2025 00:09:12 -0300 Subject: [PATCH 1/9] implement `gas_limit` host fn --- crates/env/src/api.rs | 5 +++ crates/env/src/backend.rs | 7 +++++ .../env/src/engine/on_chain/pallet_revive.rs | 4 +++ crates/ink/src/env_access.rs | 31 +++++++++++++++++++ .../ink/tests/ui/contract/pass/env-access.rs | 2 ++ 5 files changed, 49 insertions(+) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index c4d1aaacec3..728f057da77 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -73,6 +73,11 @@ pub fn caller() -> Address { ::on_instance(TypedEnvBackend::caller) } +/// Returns the block ref_time limit. +pub fn gas_limit() -> u64 { + ::on_instance(TypedEnvBackend::gas_limit) +} + /// Returns the transferred value for the contract execution. /// /// # Errors diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 71e63a9a4df..8735e3c5f05 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -228,6 +228,13 @@ pub trait TypedEnvBackend: EnvBackend { /// For more details visit: [`caller`][`crate::caller`] fn caller(&mut self) -> Address; + /// Returns the block ref_time limit. + /// + /// # Note + /// + /// For more details visit: [`gas_limit`][`crate::gas_limit`] + fn gas_limit(&mut self) -> u64; + /// Returns the transferred value for the contract execution. /// /// # Note diff --git a/crates/env/src/engine/on_chain/pallet_revive.rs b/crates/env/src/engine/on_chain/pallet_revive.rs index e06196b0cc5..a61d1e83879 100644 --- a/crates/env/src/engine/on_chain/pallet_revive.rs +++ b/crates/env/src/engine/on_chain/pallet_revive.rs @@ -887,6 +887,10 @@ impl TypedEnvBackend for EnvInstance { .expect("The executed contract must have a caller with a valid account id.") } + fn gas_limit(&mut self) -> u64 { + ext::gas_limit() + } + fn transferred_value(&mut self) -> U256 { let mut scope = self.scoped_buffer(); let u256: &mut [u8; 32] = scope.take(32).try_into().unwrap(); diff --git a/crates/ink/src/env_access.rs b/crates/ink/src/env_access.rs index d0544b0c539..6d5f8b0c814 100644 --- a/crates/ink/src/env_access.rs +++ b/crates/ink/src/env_access.rs @@ -108,6 +108,37 @@ where ink_env::caller() } + /// Returns the block ref_time limit. + /// + /// # Example + /// + /// ``` + /// #[ink::contract] + /// mod my_contract { + /// #[ink(storage)] + /// pub struct MyContract; + /// + /// impl MyContract { + /// #[ink(constructor)] + /// pub fn new() -> Self { + /// Self {} + /// } + /// + /// #[ink(message)] + /// pub fn get_limit(&self) -> u64 { + /// self.env().gas_limit() + /// } + /// } + /// } + /// ``` + /// + /// # Note + /// + /// For more details visit: [`ink_env::gas_limit`] + pub fn gas_limit(self) -> u64 { + ink_env::gas_limit() + } + /// Returns the transferred value for the contract execution. /// /// # Example diff --git a/crates/ink/tests/ui/contract/pass/env-access.rs b/crates/ink/tests/ui/contract/pass/env-access.rs index ce657d817e1..764c47103c7 100644 --- a/crates/ink/tests/ui/contract/pass/env-access.rs +++ b/crates/ink/tests/ui/contract/pass/env-access.rs @@ -14,6 +14,7 @@ mod contract { let _ = Self::env().block_number(); let _ = Self::env().caller(); let _ = Self::env().minimum_balance(); + let _ = Self::env().gas_limit(); let _ = Self::env().transferred_value(); let _ = Self::env().weight_to_fee(0); Self {} @@ -27,6 +28,7 @@ mod contract { let _ = self.env().block_number(); let _ = self.env().caller(); let _ = self.env().minimum_balance(); + let _ = Self.env().gas_limit(); let _ = self.env().transferred_value(); let _ = self.env().weight_to_fee(0); } From f04d729c045988a9de691608d8a7337002341544 Mon Sep 17 00:00:00 2001 From: Lucas Grasso Date: Fri, 24 Oct 2025 01:05:07 -0300 Subject: [PATCH 2/9] add tests for `gas_limit` host fn --- .../internal/gas-hostfns/Cargo.toml | 27 ++++++++ integration-tests/internal/gas-hostfns/lib.rs | 68 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100755 integration-tests/internal/gas-hostfns/Cargo.toml create mode 100644 integration-tests/internal/gas-hostfns/lib.rs diff --git a/integration-tests/internal/gas-hostfns/Cargo.toml b/integration-tests/internal/gas-hostfns/Cargo.toml new file mode 100755 index 00000000000..7ee2db9bc72 --- /dev/null +++ b/integration-tests/internal/gas-hostfns/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "gas_hostfns" +description = "E2E tests for gas related host functions" +version = "6.0.0-alpha.1" +authors = ["Use Ink "] +edition = "2021" +publish = false + +[dependencies] +ink = { path = "../../../crates/ink", default-features = false, features = ["unstable-hostfn"] } + +[dev-dependencies] +ink_e2e = { path = "../../../crates/e2e" } + +[lib] +path = "lib.rs" + +[features] +default = ["std"] +std = [ + "ink/std", +] +ink-as-dependency = [] +e2e-tests = [] + +[package.metadata.ink-lang] +abi = "ink" diff --git a/integration-tests/internal/gas-hostfns/lib.rs b/integration-tests/internal/gas-hostfns/lib.rs new file mode 100644 index 00000000000..abc1dc15029 --- /dev/null +++ b/integration-tests/internal/gas-hostfns/lib.rs @@ -0,0 +1,68 @@ +#![cfg_attr(not(feature = "std"), no_std, no_main)] +#![allow(clippy::new_without_default)] + +#[ink::contract] +mod gas_hostfns { + #[ink(storage)] + pub struct GasHostfns {} + + impl GasHostfns { + #[ink(constructor)] + pub fn new() -> Self { + Self {} + } + + /// Checks that the host function `gas_limit` works + #[ink(message)] + pub fn gas_limit(self) { + self.env.gas_limit() + } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[ink::test] + fn works() { + let contract = GasHostfns::new(); + contract.gas_limit(); + } + } + + #[cfg(all(test, feature = "e2e-tests"))] + mod e2e_tests { + use super::*; + use ink_e2e::ContractsBackend; + + type E2EResult = std::result::Result>; + + #[ink_e2e::test] + async fn e2e_gas_limit_works( + mut client: Client, + ) -> E2EResult<()> { + // given + let contract = client + .instantiate( + "gas_hostfns", + &ink_e2e::alice(), + &mut GasHostfnsRef::new(), + ) + .submit() + .await + .expect("instantiate failed"); + let call_builder = contract.call_builder::(); + + // then + let _call_res = client + .call(&ink_e2e::alice(), &call_builder.gas_limit()) + .submit() + .await + .unwrap_or_else(|err| { + panic!("call failed: {:#?}", err); + }); + + Ok(()) + } + } +} From 0eec69da8eb6e3d2b346723ae8fcdc62359d2d4c Mon Sep 17 00:00:00 2001 From: Lucas Grasso Date: Fri, 24 Oct 2025 11:37:30 -0300 Subject: [PATCH 3/9] remove off-chain tests for `gas_limit` --- crates/env/src/engine/off_chain/impls.rs | 5 +++++ integration-tests/internal/gas-hostfns/lib.rs | 17 ++++------------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 930b92164a0..cb88e8ca1b1 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -550,6 +550,11 @@ impl TypedEnvBackend for EnvInstance { .unwrap_or_else(|error| panic!("could not read `caller` property: {error:?}")) } + fn gas_limit(&mut self) -> u64 { + // Panic because of future depecration / removal + panic!(); + } + fn transferred_value(&mut self) -> U256 { self.get_property(Engine::value_transferred) .unwrap_or_else(|error| { diff --git a/integration-tests/internal/gas-hostfns/lib.rs b/integration-tests/internal/gas-hostfns/lib.rs index abc1dc15029..0711cc6fdd5 100644 --- a/integration-tests/internal/gas-hostfns/lib.rs +++ b/integration-tests/internal/gas-hostfns/lib.rs @@ -14,22 +14,11 @@ mod gas_hostfns { /// Checks that the host function `gas_limit` works #[ink(message)] - pub fn gas_limit(self) { - self.env.gas_limit() + pub fn gas_limit(&self) -> u64 { + self.env().gas_limit() } } - #[cfg(test)] - mod tests { - use super::*; - - #[ink::test] - fn works() { - let contract = GasHostfns::new(); - contract.gas_limit(); - } - } - #[cfg(all(test, feature = "e2e-tests"))] mod e2e_tests { use super::*; @@ -61,6 +50,8 @@ mod gas_hostfns { .unwrap_or_else(|err| { panic!("call failed: {:#?}", err); }); + + assert!(_call_res.return_value() > 0); Ok(()) } From 95162a99e6a35faba495c556e7c09f018e01d022 Mon Sep 17 00:00:00 2001 From: Lucas Grasso Date: Fri, 24 Oct 2025 11:45:21 -0300 Subject: [PATCH 4/9] add changes to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56489af38e5..99b18543920 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [Unreleased] ### Added +- Implements the API for the `pallet-revive` host function `gas_limit` - [#2691](https://github.com/use-ink/ink/pull/2691) - Implements the API for the `pallet-revive` host function `to_account_id` - [#2578](https://github.com/use-ink/ink/pull/2578) - Add `#[ink::contract_ref]` attribute - [#2648](https://github.com/use-ink/ink/pull/2648) - Add `ink_revive_types` (and remove `pallet-revive` dependency from `ink_e2e`) - [#2657](https://github.com/use-ink/ink/pull/2657) From 1e63416f20ef29c675f7ffcee8bac2a6d4f223b9 Mon Sep 17 00:00:00 2001 From: Lucas Grasso Date: Sat, 25 Oct 2025 21:11:49 -0300 Subject: [PATCH 5/9] run formatting --- crates/env/src/api.rs | 2 +- crates/ink/src/env_access.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 728f057da77..a72868452bf 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -75,7 +75,7 @@ pub fn caller() -> Address { /// Returns the block ref_time limit. pub fn gas_limit() -> u64 { - ::on_instance(TypedEnvBackend::gas_limit) + ::on_instance(TypedEnvBackend::gas_limit) } /// Returns the transferred value for the contract execution. diff --git a/crates/ink/src/env_access.rs b/crates/ink/src/env_access.rs index 6d5f8b0c814..4f990d84865 100644 --- a/crates/ink/src/env_access.rs +++ b/crates/ink/src/env_access.rs @@ -117,13 +117,13 @@ where /// mod my_contract { /// #[ink(storage)] /// pub struct MyContract; - /// + /// /// impl MyContract { /// #[ink(constructor)] /// pub fn new() -> Self { /// Self {} /// } - /// + /// /// #[ink(message)] /// pub fn get_limit(&self) -> u64 { /// self.env().gas_limit() From 9c3ab4177e2bc37790048f019fc743a51dfa82ba Mon Sep 17 00:00:00 2001 From: Lucas Grasso Date: Sat, 25 Oct 2025 21:24:33 -0300 Subject: [PATCH 6/9] fix error in env-access ui test --- crates/ink/tests/ui/contract/pass/env-access.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ink/tests/ui/contract/pass/env-access.rs b/crates/ink/tests/ui/contract/pass/env-access.rs index 764c47103c7..e2afe0b8b40 100644 --- a/crates/ink/tests/ui/contract/pass/env-access.rs +++ b/crates/ink/tests/ui/contract/pass/env-access.rs @@ -28,7 +28,7 @@ mod contract { let _ = self.env().block_number(); let _ = self.env().caller(); let _ = self.env().minimum_balance(); - let _ = Self.env().gas_limit(); + let _ = self.env().gas_limit(); let _ = self.env().transferred_value(); let _ = self.env().weight_to_fee(0); } From e0c83515a2a6035c2ca6c8dfb846f8eaea903f55 Mon Sep 17 00:00:00 2001 From: Lucas Grasso Date: Mon, 27 Oct 2025 13:27:46 -0300 Subject: [PATCH 7/9] run formatting --- integration-tests/internal/gas-hostfns/lib.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/integration-tests/internal/gas-hostfns/lib.rs b/integration-tests/internal/gas-hostfns/lib.rs index 0711cc6fdd5..88e46d5ec5d 100644 --- a/integration-tests/internal/gas-hostfns/lib.rs +++ b/integration-tests/internal/gas-hostfns/lib.rs @@ -12,11 +12,11 @@ mod gas_hostfns { Self {} } - /// Checks that the host function `gas_limit` works - #[ink(message)] - pub fn gas_limit(&self) -> u64 { - self.env().gas_limit() - } + /// Checks that the host function `gas_limit` works + #[ink(message)] + pub fn gas_limit(&self) -> u64 { + self.env().gas_limit() + } } #[cfg(all(test, feature = "e2e-tests"))] @@ -32,11 +32,7 @@ mod gas_hostfns { ) -> E2EResult<()> { // given let contract = client - .instantiate( - "gas_hostfns", - &ink_e2e::alice(), - &mut GasHostfnsRef::new(), - ) + .instantiate("gas_hostfns", &ink_e2e::alice(), &mut GasHostfnsRef::new()) .submit() .await .expect("instantiate failed"); @@ -50,7 +46,7 @@ mod gas_hostfns { .unwrap_or_else(|err| { panic!("call failed: {:#?}", err); }); - + assert!(_call_res.return_value() > 0); Ok(()) From eb2f8a327f87433969dfcbe1343067035f46bd26 Mon Sep 17 00:00:00 2001 From: Lucas Grasso Date: Tue, 28 Oct 2025 12:07:05 -0300 Subject: [PATCH 8/9] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- crates/env/src/api.rs | 4 +++- crates/env/src/backend.rs | 2 +- crates/env/src/engine/off_chain/impls.rs | 3 +-- integration-tests/internal/gas-hostfns/lib.rs | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index a72868452bf..8490199e7dd 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -73,7 +73,9 @@ pub fn caller() -> Address { ::on_instance(TypedEnvBackend::caller) } -/// Returns the block ref_time limit. +/// Returns the block's `ref_time` limit. +/// +/// See for more information. pub fn gas_limit() -> u64 { ::on_instance(TypedEnvBackend::gas_limit) } diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 8735e3c5f05..3c02d58efb2 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -228,7 +228,7 @@ pub trait TypedEnvBackend: EnvBackend { /// For more details visit: [`caller`][`crate::caller`] fn caller(&mut self) -> Address; - /// Returns the block ref_time limit. + /// Returns the block's `ref_time` limit. /// /// # Note /// diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index cb88e8ca1b1..c7b3cfcc2ae 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -551,8 +551,7 @@ impl TypedEnvBackend for EnvInstance { } fn gas_limit(&mut self) -> u64 { - // Panic because of future depecration / removal - panic!(); + unimplemented!("not implemented, the off-chain environment will be removed"); } fn transferred_value(&mut self) -> U256 { diff --git a/integration-tests/internal/gas-hostfns/lib.rs b/integration-tests/internal/gas-hostfns/lib.rs index 88e46d5ec5d..8c5046541ac 100644 --- a/integration-tests/internal/gas-hostfns/lib.rs +++ b/integration-tests/internal/gas-hostfns/lib.rs @@ -39,7 +39,7 @@ mod gas_hostfns { let call_builder = contract.call_builder::(); // then - let _call_res = client + let call_res = client .call(&ink_e2e::alice(), &call_builder.gas_limit()) .submit() .await @@ -47,7 +47,7 @@ mod gas_hostfns { panic!("call failed: {:#?}", err); }); - assert!(_call_res.return_value() > 0); + assert!(call_res.return_value() > 0); Ok(()) } From 4a1f7e3cca03d017f640f1ba8ab8dacc94e43f48 Mon Sep 17 00:00:00 2001 From: Lucas Grasso Date: Tue, 28 Oct 2025 12:08:21 -0300 Subject: [PATCH 9/9] update versions in gas and misc hostfns internal tests --- integration-tests/internal/gas-hostfns/Cargo.toml | 2 +- integration-tests/internal/misc-hostfns/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/integration-tests/internal/gas-hostfns/Cargo.toml b/integration-tests/internal/gas-hostfns/Cargo.toml index 7ee2db9bc72..e85364d40ba 100755 --- a/integration-tests/internal/gas-hostfns/Cargo.toml +++ b/integration-tests/internal/gas-hostfns/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "gas_hostfns" description = "E2E tests for gas related host functions" -version = "6.0.0-alpha.1" +version = "6.0.0-alpha.4" authors = ["Use Ink "] edition = "2021" publish = false diff --git a/integration-tests/internal/misc-hostfns/Cargo.toml b/integration-tests/internal/misc-hostfns/Cargo.toml index 27e7b955c88..8ec9020bdc9 100755 --- a/integration-tests/internal/misc-hostfns/Cargo.toml +++ b/integration-tests/internal/misc-hostfns/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "misc_hostfns" description = "E2E tests for various host functions" -version = "6.0.0-alpha.1" +version = "6.0.0-alpha.4" authors = ["Use Ink "] edition = "2021" publish = false