Skip to content

Commit

Permalink
Add unstable feature everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichi committed Jan 8, 2025
1 parent d6c194f commit 0a1d59d
Show file tree
Hide file tree
Showing 29 changed files with 168 additions and 30 deletions.
2 changes: 1 addition & 1 deletion crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ std = [
ink-debug = []

# Enable functions that have are marked unstable in `pallet-revive.
unstable = []
unstable = ["ink_macro/unstable"]

# Disable the ink! provided global memory allocator.
no-allocator = [ "ink_allocator/no-allocator" ]
Expand Down
30 changes: 27 additions & 3 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ use crate::{
OnInstance,
},
event::Event,
types::Gas,
Environment,
Result,
};
#[cfg(feature = "unstable")]
use crate::{
hash::{
CryptoHash,
HashOutput,
},
types::Gas,
Environment,
Result,
};
use ink_primitives::{H160, H256, U256};
use ink_storage_traits::Storable;
Expand Down Expand Up @@ -142,6 +145,7 @@ pub fn balance() -> U256
/// # Errors
///
/// If the returned value cannot be properly decoded.
#[unstable_hostfn]
pub fn block_number<E>() -> E::BlockNumber
where
E: Environment,
Expand All @@ -157,6 +161,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
#[unstable_hostfn]
pub fn minimum_balance<E>() -> E::Balance
where
E: Environment,
Expand Down Expand Up @@ -214,6 +219,7 @@ where
/// # Errors
///
/// - If the decoding of the typed value failed (`KeyNotFound`)
#[unstable_hostfn]
pub fn take_contract_storage<K, R>(key: &K) -> Result<Option<R>>
where
K: scale::Encode,
Expand All @@ -228,6 +234,7 @@ where
/// storage.
///
/// If a value is stored under the specified key, the size of the value is returned.
#[unstable_hostfn]
pub fn contains_contract_storage<K>(key: &K) -> Option<u32>
where
K: scale::Encode,
Expand All @@ -241,6 +248,7 @@ where
///
/// If a value was stored under the specified storage key, the size of the value is
/// returned.
#[unstable_hostfn]
pub fn clear_contract_storage<K>(key: &K) -> Option<u32>
where
K: scale::Encode,
Expand Down Expand Up @@ -430,6 +438,7 @@ where
}

/// Appends the given message to the debug message buffer.
#[unstable_hostfn]
pub fn debug_message(message: &str) {
<EnvInstance as OnInstance>::on_instance(|instance| {
EnvBackend::debug_message(instance, message)
Expand All @@ -449,6 +458,7 @@ pub fn debug_message(message: &str) {
/// let mut output = <Sha2x256 as HashOutput>::Type::default(); // 256-bit buffer
/// let hash = ink_env::hash_bytes::<Sha2x256>(input, &mut output);
/// ```
#[unstable_hostfn]
pub fn hash_bytes<H>(input: &[u8], output: &mut <H as HashOutput>::Type)
where
H: CryptoHash,
Expand All @@ -473,6 +483,7 @@ where
/// ink_env::hash_encoded::<Sha2x256, _>(&encodable, &mut output);
/// assert_eq!(output, EXPECTED);
/// ```
#[unstable_hostfn]
pub fn hash_encoded<H, T>(input: &T, output: &mut <H as HashOutput>::Type)
where
H: CryptoHash,
Expand Down Expand Up @@ -507,6 +518,7 @@ where
/// ink_env::ecdsa_recover(&signature, &message_hash, &mut output);
/// assert_eq!(output, EXPECTED_COMPRESSED_PUBLIC_KEY);
/// ```
#[unstable_hostfn]
pub fn ecdsa_recover(
signature: &[u8; 65],
message_hash: &[u8; 32],
Expand Down Expand Up @@ -538,6 +550,7 @@ pub fn ecdsa_recover(
/// # Errors
///
/// - If the ECDSA public key cannot be recovered from the provided public key.
#[unstable_hostfn]
pub fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result<()> {
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.ecdsa_to_eth_address(pubkey, output)
Expand Down Expand Up @@ -571,6 +584,7 @@ pub fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result<
///
/// **WARNING**: this function is from the [unstable interface](https://github.com/paritytech/substrate/tree/master/frame/contracts#unstable-interfaces),
/// which is unsafe and normally is not available on production chains.
#[unstable_hostfn]
pub fn sr25519_verify(
signature: &[u8; 64],
message: &[u8],
Expand All @@ -586,6 +600,7 @@ pub fn sr25519_verify(
/// # Errors
///
/// If the returned value cannot be properly decoded.
#[unstable_hostfn]
pub fn is_contract(account: &H160) -> bool {
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::is_contract(instance, account)
Expand All @@ -609,6 +624,7 @@ pub fn code_hash(addr: &H160) -> Result<H256> {
/// # Errors
///
/// If the returned value cannot be properly decoded.
#[unstable_hostfn]
pub fn own_code_hash<E>() -> Result<H256>
where
E: Environment,
Expand All @@ -631,6 +647,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
#[unstable_hostfn]
pub fn caller_is_origin<E>() -> bool
where
E: Environment,
Expand All @@ -651,6 +668,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
#[unstable_hostfn]
pub fn caller_is_root<E>() -> bool
where
E: Environment,
Expand Down Expand Up @@ -761,6 +779,7 @@ where
/// Please refer to the
/// [Open Zeppelin docs](https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#modifying-your-contracts)
/// for more details and examples.
#[unstable_hostfn]
pub fn set_code_hash<E>(code_hash: &H256) -> Result<()>
where
E: Environment,
Expand All @@ -787,6 +806,7 @@ where
/// # Panics
///
/// Panics in the off-chain environment.
#[unstable_hostfn]
pub fn call_runtime<E, Call>(call: &Call) -> Result<()>
where
E: Environment,
Expand All @@ -809,6 +829,7 @@ where
/// - If the `code_hash` is the same as the calling contract.
/// - If the maximum number of delegate dependencies is reached.
/// - If the delegate dependency already exists.
#[unstable_hostfn]
pub fn lock_delegate_dependency<E>(code_hash: &H256)
where
E: Environment,
Expand All @@ -827,6 +848,7 @@ where
/// # Errors
///
/// - If the delegate dependency does not exist.
#[unstable_hostfn]
pub fn unlock_delegate_dependency<E>(code_hash: &H256)
where
E: Environment,
Expand All @@ -849,6 +871,7 @@ where
/// # Panics
///
/// Panics in the off-chain environment.
#[unstable_hostfn]
pub fn xcm_execute<E, Call>(msg: &xcm::VersionedXcm<Call>) -> Result<()>
where
E: Environment,
Expand All @@ -874,6 +897,7 @@ where
/// # Panics
///
/// Panics in the off-chain environment.
#[unstable_hostfn]
pub fn xcm_send<E, Call>(
dest: &xcm::VersionedLocation,
msg: &xcm::VersionedXcm<Call>,
Expand Down
29 changes: 27 additions & 2 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ use crate::{
LimitParamsV2,
},
event::Event,
Environment,
Result,
};
#[cfg(feature = "unstable")]
use crate::{
hash::{
CryptoHash,
HashOutput,
},
Environment,
Result,
};
use ink_primitives::{H160, H256, U256};
use ink_storage_traits::Storable;
Expand Down Expand Up @@ -62,19 +65,22 @@ pub trait EnvBackend {
/// # Errors
///
/// - If the decoding of the typed value failed
#[unstable_hostfn]
fn take_contract_storage<K, R>(&mut self, key: &K) -> Result<Option<R>>
where
K: scale::Encode,
R: Storable;

/// Returns the size of a value stored under the given storage key is returned if any.
#[unstable_hostfn]
fn contains_contract_storage<K>(&mut self, key: &K) -> Option<u32>
where
K: scale::Encode;

/// Clears the contract's storage key entry under the given storage key.
///
/// Returns the size of the previously stored value at the specified key if any.
#[unstable_hostfn]
fn clear_contract_storage<K>(&mut self, key: &K) -> Option<u32>
where
K: scale::Encode;
Expand Down Expand Up @@ -127,22 +133,26 @@ pub trait EnvBackend {
///
/// If debug message recording is disabled in the contracts pallet, which is always
/// the case when the code is executing on-chain, then this will have no effect.
#[unstable_hostfn]
fn debug_message(&mut self, content: &str);

/// Conducts the crypto hash of the given input and stores the result in `output`.
#[unstable_hostfn]
fn hash_bytes<H>(&mut self, input: &[u8], output: &mut <H as HashOutput>::Type)
where
H: CryptoHash;

/// Conducts the crypto hash of the given encoded input and stores the result in
/// `output`.
#[unstable_hostfn]
fn hash_encoded<H, T>(&mut self, input: &T, output: &mut <H as HashOutput>::Type)
where
H: CryptoHash,
T: scale::Encode;

/// Recovers the compressed ECDSA public key for given `signature` and `message_hash`,
/// and stores the result in `output`.
#[unstable_hostfn]
fn ecdsa_recover(
&mut self,
signature: &[u8; 65],
Expand All @@ -152,6 +162,7 @@ pub trait EnvBackend {

/// Retrieves an Ethereum address from the ECDSA compressed `pubkey`
/// and stores the result in `output`.
#[unstable_hostfn]
fn ecdsa_to_eth_address(
&mut self,
pubkey: &[u8; 33],
Expand All @@ -166,6 +177,7 @@ pub trait EnvBackend {
///
/// **WARNING**: this function is from the [unstable interface](https://github.com/paritytech/substrate/tree/master/frame/contracts#unstable-interfaces),
/// which is unsafe and normally is not available on production chains.
#[unstable_hostfn]
fn sr25519_verify(
&mut self,
signature: &[u8; 64],
Expand Down Expand Up @@ -193,6 +205,7 @@ pub trait EnvBackend {
/// successful call to the chain extension method is the resulting
/// output buffer passed to the `decode_to_result` closure, in order to
/// drive the decoding and error management process from the outside.
#[unstable_hostfn]
fn call_chain_extension<I, T, E, ErrorCode, F, D>(
&mut self,
id: u32,
Expand All @@ -214,6 +227,7 @@ pub trait EnvBackend {
/// # Errors
///
/// - If the supplied `code_hash` cannot be found on-chain.
#[unstable_hostfn]
fn set_code_hash(&mut self, code_hash: &[u8]) -> Result<()>;
}

Expand Down Expand Up @@ -273,6 +287,7 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`block_number`][`crate::block_number`]
#[unstable_hostfn]
fn block_number<E: Environment>(&mut self) -> E::BlockNumber;

/// Returns the minimum balance that is required for creating an account
Expand All @@ -281,6 +296,7 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`minimum_balance`][`crate::minimum_balance`]
#[unstable_hostfn]
fn minimum_balance<E: Environment>(&mut self) -> E::Balance;

/// Emits an event with the given event data.
Expand Down Expand Up @@ -366,6 +382,7 @@ pub trait TypedEnvBackend: EnvBackend {
///
/// For more details visit: [`is_contract`][`crate::is_contract`]
#[allow(clippy::wrong_self_convention)]
#[unstable_hostfn]
fn is_contract(&mut self, account: &H160) -> bool;

/// Checks whether the caller of the current contract is the origin of the whole call
Expand All @@ -374,6 +391,7 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`caller_is_origin`][`crate::caller_is_origin`]
#[unstable_hostfn]
fn caller_is_origin<E>(&mut self) -> bool
where
E: Environment;
Expand All @@ -383,6 +401,7 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`caller_is_root`][`crate::caller_is_root`]
#[unstable_hostfn]
fn caller_is_root<E>(&mut self) -> bool
where
E: Environment;
Expand All @@ -399,10 +418,12 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`own_code_hash`][`crate::own_code_hash`]
#[unstable_hostfn]
fn own_code_hash<E>(&mut self) -> Result<H256>
where
E: Environment;

#[unstable_hostfn]
fn call_runtime<E, Call>(&mut self, call: &Call) -> Result<()>
where
E: Environment,
Expand All @@ -414,6 +435,7 @@ pub trait TypedEnvBackend: EnvBackend {
///
/// For more details visit:
/// [`lock_delegate_dependency`][`crate::lock_delegate_dependency`]
#[unstable_hostfn]
fn lock_delegate_dependency<E>(&mut self, code_hash: &H256)
where
E: Environment;
Expand All @@ -424,6 +446,7 @@ pub trait TypedEnvBackend: EnvBackend {
///
/// For more details visit:
/// [`unlock_delegate_dependency`][`crate::unlock_delegate_dependency`].
#[unstable_hostfn]
fn unlock_delegate_dependency<E>(&mut self, code_hash: &H256)
where
E: Environment;
Expand All @@ -433,6 +456,7 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`xcm`][`crate::xcm_execute`].
#[unstable_hostfn]
fn xcm_execute<E, Call>(&mut self, msg: &xcm::VersionedXcm<Call>) -> Result<()>
where
E: Environment,
Expand All @@ -443,6 +467,7 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`xcm`][`crate::xcm_send`].
#[unstable_hostfn]
fn xcm_send<E, Call>(
&mut self,
dest: &xcm::VersionedLocation,
Expand Down
Loading

0 comments on commit 0a1d59d

Please sign in to comment.