Skip to content
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
5030d89
implement `gas_limit` host fn
LucasGrasso Oct 24, 2025
f04d729
add tests for `gas_limit` host fn
LucasGrasso Oct 24, 2025
0eec69d
remove off-chain tests for `gas_limit`
LucasGrasso Oct 24, 2025
fddfab0
Merge branch 'use-ink:master' into master
LucasGrasso Oct 24, 2025
95162a9
add changes to changelog
LucasGrasso Oct 24, 2025
1e63416
run formatting
LucasGrasso Oct 26, 2025
9c3ab41
fix error in env-access ui test
LucasGrasso Oct 26, 2025
e0c8351
run formatting
LucasGrasso Oct 27, 2025
eb2f8a3
Apply suggestions from code review
LucasGrasso Oct 28, 2025
4a1f7e3
update versions in gas and misc hostfns internal tests
LucasGrasso Oct 28, 2025
6609561
Merge branch 'master' of https://github.com/LucasGrasso/ink
LucasGrasso Oct 28, 2025
dc87592
Merge remote-tracking branch 'upstream/master'
LucasGrasso Oct 29, 2025
b408418
Merge branch 'use-ink:master' into master
LucasGrasso Oct 30, 2025
ab37628
Merge branch 'use-ink:master' into master
LucasGrasso Nov 7, 2025
23c0ba3
Merge branch 'use-ink:master' into master
LucasGrasso Nov 8, 2025
560cf92
impl and test `chain_id` hostfn
LucasGrasso Nov 9, 2025
aced728
add entry to changelog
LucasGrasso Nov 9, 2025
b970f07
impl and test `balance_of` host fn
LucasGrasso Nov 9, 2025
8e5e5eb
impl and test `base_fee`
LucasGrasso Nov 9, 2025
efaa0b8
impl and test `origin`
LucasGrasso Nov 9, 2025
071db36
impl and test `code_size` hostfn
LucasGrasso Nov 9, 2025
64c31f9
impl and test `block_author`
LucasGrasso Nov 9, 2025
2b92a3f
impl and test `block_hash`
LucasGrasso Nov 9, 2025
28e25f9
run formatting
LucasGrasso Nov 9, 2025
7b06709
Merge remote-tracking branch 'upstream/master'
LucasGrasso Nov 10, 2025
ca4eb06
fix some ci/cd issues
LucasGrasso Nov 10, 2025
d9b2e3d
fix error[E0412]: cannot find type in this scope
LucasGrasso Nov 10, 2025
a0cd8c5
fix docs for chain_id
LucasGrasso Nov 11, 2025
ef52f4c
fix `block_hash` doc error
LucasGrasso Nov 11, 2025
196e9d7
Merge branch 'master' into master
LucasGrasso Nov 11, 2025
c908a73
run formatting
LucasGrasso Nov 11, 2025
399341d
Merge branch 'master' of https://github.com/LucasGrasso/ink
LucasGrasso Nov 11, 2025
a6f75b4
Merge branch 'master' into master
LucasGrasso Nov 24, 2025
06ab2c5
fix merge errors on changelog
LucasGrasso Nov 25, 2025
c50d31d
Merge branch 'use-ink:master' into master
LucasGrasso Nov 25, 2025
f6e3345
start refactoring block_hash
LucasGrasso Nov 25, 2025
a2cc91d
Merge branch 'master' of https://github.com/LucasGrasso/ink
LucasGrasso Nov 25, 2025
e15d33d
finish block_hash refatoring
LucasGrasso Nov 25, 2025
00c0d7f
use take_encoded to avoid allocation
LucasGrasso Nov 25, 2025
6971272
run formatting
LucasGrasso Nov 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 functions `chain_id`, `balance_of`, `base_fee`, `origin`, `code_size`, `block_hash`, `block_author` - [#2719](https://github.com/use-ink/ink/pull/2719)
- Implement `From<ink::Address>` for "ink-as-dependency" contract refs - [#2728](https://github.com/use-ink/ink/pull/2728)

## Version 6.0.0-beta.1
Expand Down
59 changes: 59 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,65 @@ pub fn return_data_size() -> u64 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::return_data_size)
}

/// Returns the [EIP-155](https://eips.ethereum.org/EIPS/eip-155) chain ID,
/// akin to the EVM [CHAINID](https://www.evm.codes/?fork=cancun#46) opcode.
pub fn chain_id() -> U256 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::chain_id)
}

/// Returns the **reducible** native balance of the supplied address,
/// akin to the EVM [BALANCE](https://www.evm.codes/?fork=cancun#31) opcode.
pub fn balance_of(addr: Address) -> U256 {
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::balance_of(instance, addr)
})
}

/// Returns the base fee.
/// This is akin to the EVM [BASEFEE](https://www.evm.codes/?fork=cancun#48) opcode.
pub fn base_fee() -> U256 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::base_fee)
}

/// Returns the origin address (initator of the call stack).
/// This is akin to the EVM [ORIGIN](https://www.evm.codes/?fork=cancun#32) opcode.
///
/// # Errors
///
/// - If there is no address associated with the origin (e.g. because the origin is root).
pub fn origin() -> Address {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::origin)
}

/// Returns the code size for a specified contract address.
/// This is akin to the EVM [CODESIZE](https://www.evm.codes/?fork=cancun#38) opcode.
///
/// # Note
///
/// If `addr` is not a contract the `output` will be zero.
pub fn code_size(addr: Address) -> u64 {
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::code_size(instance, addr)
})
}

/// Returns the block hash of the given block number.
/// This is akin to the EVM [BLOCKHASH](https://www.evm.codes/?fork=cancun#40) opcode.
pub fn block_hash<E>(block_number: E::BlockNumber) -> H256
where
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::block_hash::<E>(instance, block_number)
})
}

/// Returns the current block author.
/// This is akin to the EVM [COINBASE](https://www.evm.codes/?fork=cancun#41) opcode.
pub fn block_author() -> Address {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::block_author)
}

/// Returns the transferred value for the contract execution.
///
/// # Errors
Expand Down
64 changes: 63 additions & 1 deletion crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,71 @@ pub trait TypedEnvBackend: EnvBackend {
///
/// # Note
///
/// For more details visit: [`return_data_size`][`crate::return_data_size]
/// For more details visit: [`return_data_size`][`crate::return_data_size`]
fn return_data_size(&mut self) -> u64;

/// Returns the [EIP-155](https://eips.ethereum.org/EIPS/eip-155) chain ID.
/// This is akin to the EVM [CHAINID](https://www.evm.codes/?fork=cancun#46) opcode.
///
/// # Note
///
/// For more details visit: [`chain_id`][`crate::chain_id`]
fn chain_id(&mut self) -> U256;

/// Returns the **reducible** native balance of the supplied address.
/// This is akin to the EVM [BALANCE](https://www.evm.codes/?fork=cancun#31) opcode.
///
/// # Note
///
/// For more details visit: [`balance_of`][`crate::balance_of`]
fn balance_of(&mut self, addr: Address) -> U256;

/// Returns the base fee.
/// This is akin to the EVM [BASEFEE](https://www.evm.codes/?fork=cancun#48) opcode.
///
/// # Note
///
/// For more details visit: [`base_fee`][`crate::base_fee`]
fn base_fee(&mut self) -> U256;

/// Returns the origin address (initator of the call stack).
/// This is akin to the EVM [ORIGIN](https://www.evm.codes/?fork=cancun#32) opcode.
///
/// # Errors
///
/// - If there is no address associated with the origin (e.g. because the origin is
/// root).
///
/// # Note
///
/// For more details visit: [`origin`][`crate::origin`]
fn origin(&mut self) -> Address;

/// Returns the code size for a specified contract address.
/// This is akin to the EVM [CODESIZE](https://www.evm.codes/?fork=cancun#38) opcode.
///
/// # Note
///
/// If `addr` is not a contract the `output` will be zero.
/// For more details visit: [`code_size`][`crate::code_size`]
fn code_size(&mut self, addr: Address) -> u64;

/// Returns the block hash of the given block number.
/// This is akin to the EVM [BLOCKHASH](https://www.evm.codes/?fork=cancun#40) opcode.
///
/// # Note
///
/// For more details visit: [`block_hash`][`crate::block_hash`]
fn block_hash<E: Environment>(&mut self, block_number: E::BlockNumber) -> H256;

/// Returns the current block author.
/// This is akin to the EVM [COINBASE](https://www.evm.codes/?fork=cancun#41) opcode.
///
/// # Note
///
/// For more details visit: [`block_author`][`crate::block_author`]
fn block_author(&mut self) -> Address;

/// Returns the transferred value for the contract execution.
///
/// # Note
Expand Down
28 changes: 28 additions & 0 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,34 @@ impl TypedEnvBackend for EnvInstance {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn chain_id(&mut self) -> U256 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn balance_of(&mut self, _addr: Address) -> U256 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn base_fee(&mut self) -> U256 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn origin(&mut self) -> Address {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn block_hash<E: Environment>(&mut self, _block_number: E::BlockNumber) -> H256 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn block_author(&mut self) -> Address {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn code_size(&mut self, _addr: Address) -> u64 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn call_data_size(&mut self) -> u64 {
unimplemented!("not implemented, the off-chain environment will be removed");
}
Expand Down
67 changes: 67 additions & 0 deletions crates/env/src/engine/on_chain/pallet_revive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ use crate::{
types::FromLittleEndian,
};


/// The code hash of an existing account without code.
/// This is the `keccak256` hash of empty data.
/// ```no_compile
Expand Down Expand Up @@ -1027,6 +1028,72 @@ impl TypedEnvBackend for EnvInstance {
ext::return_data_size()
}

fn chain_id(&mut self) -> U256 {
let mut scope = self.scoped_buffer();
let u256: &mut [u8; 32] = scope.take(32).try_into().unwrap();

ext::chain_id(u256);
U256::from_le_bytes(*u256)
}

fn balance_of(&mut self, addr: Address) -> U256 {
let mut scope = self.scoped_buffer();
let u256: &mut [u8; 32] = scope.take(32).try_into().unwrap();

let addr = addr.as_fixed_bytes();

ext::balance_of(&addr, u256);
U256::from_le_bytes(*u256)
}

fn base_fee(&mut self) -> U256 {
let mut scope = self.scoped_buffer();
let u256: &mut [u8; 32] = scope.take(32).try_into().unwrap();

ext::base_fee(u256);
U256::from_le_bytes(*u256)
}

fn origin(&mut self) -> Address {
let mut scope = self.scoped_buffer();
let h160: &mut [u8; 20] = scope.take(20).try_into().unwrap();

ext::origin(h160);
h160.into()
}

fn code_size(&mut self, addr: Address) -> u64 {
let addr = addr.as_fixed_bytes();

ext::code_size(addr)
}

fn block_hash<E: Environment>(&mut self, block_number: E::BlockNumber) -> H256 {
let mut scope = self.scoped_buffer();
let output: &mut [u8; 32] = scope.take(32).try_into().unwrap();

let block_number = {
let mut bytes = [0u8; 32];
let encoded = <E::BlockNumber as scale::Encode>::encode(&block_number);
// NOTE: panics if encoding is bigger than 32 bytes.
bytes[..encoded.len()].copy_from_slice(&encoded);
bytes
};

ext::block_hash(&block_number, output);
H256::from_slice(output)
}

fn block_author(&mut self) -> Address {
let h160 = {
let mut scope = self.scoped_buffer();
let h160: &mut [u8; 20] = scope.take(20).try_into().unwrap();
ext::block_author(h160);
*h160
};
h160.into()
}

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
Loading
Loading