Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ pub fn caller() -> Address {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::caller)
}

/// Returns the block ref_time limit.
pub fn gas_limit() -> u64 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::gas_limit)
}

/// Returns the transferred value for the contract execution.
///
/// # Errors
Expand Down
7 changes: 7 additions & 0 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
4 changes: 4 additions & 0 deletions crates/env/src/engine/on_chain/pallet_revive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
31 changes: 31 additions & 0 deletions crates/ink/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions crates/ink/tests/ui/contract/pass/env-access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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);
}
Expand Down
27 changes: 27 additions & 0 deletions integration-tests/internal/gas-hostfns/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
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"
55 changes: 55 additions & 0 deletions integration-tests/internal/gas-hostfns/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#![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) -> u64 {
self.env().gas_limit()
}
}

#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
use ink_e2e::ContractsBackend;

type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;

#[ink_e2e::test]
async fn e2e_gas_limit_works<Client: E2EBackend>(
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::<GasHostfns>();

// then
let _call_res = client
.call(&ink_e2e::alice(), &call_builder.gas_limit())
.submit()
.await
.unwrap_or_else(|err| {
panic!("call failed: {:#?}", err);
});

assert!(_call_res.return_value() > 0);

Ok(())
}
}
}
Loading