Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize call_runtime #1749

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Stabilize `call_runtime` ‒ [#1749](https://github.com/paritytech/ink/pull/1749)

### Added
- Schema generation - [#1765](https://github.com/paritytech/ink/pull/1765)
Expand Down
3 changes: 0 additions & 3 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ std = [
"blake2",
]

# Enable direct call to a pallet dispatchable via `call_runtime()`.
call-runtime = []

# Enable contract debug messages via `debug_print!` and `debug_println!`.
ink-debug = []

Expand Down
6 changes: 0 additions & 6 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,15 +750,9 @@ where
/// - If the runtime doesn't allow for the contract unstable feature.
/// - If the runtime doesn't allow for dispatching this call from a contract.
///
/// # Note
///
/// The `call_runtime` host function is still part of `pallet-contracts`' unstable
/// interface and thus can be changed at anytime.
///
/// # Panics
///
/// Panics in the off-chain environment.
#[cfg(feature = "call-runtime")]
pub fn call_runtime<E, Call>(call: &Call) -> Result<()>
where
E: Environment,
Expand Down
1 change: 0 additions & 1 deletion crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ pub trait TypedEnvBackend: EnvBackend {
where
E: Environment;

#[cfg(feature = "call-runtime")]
fn call_runtime<E, Call>(&mut self, call: &Call) -> Result<()>
where
E: Environment,
Expand Down
1 change: 0 additions & 1 deletion crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,6 @@ impl TypedEnvBackend for EnvInstance {
unimplemented!("off-chain environment does not support `own_code_hash`")
}

#[cfg(feature = "call-runtime")]
fn call_runtime<E, Call>(&mut self, _call: &Call) -> Result<()>
where
E: Environment,
Expand Down
1 change: 0 additions & 1 deletion crates/env/src/engine/on_chain/ext/riscv32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ pub fn return_value(flags: ReturnFlags, return_value: &[u8]) -> ! {
}
}

#[cfg(feature = "call-runtime")]
pub fn call_runtime(call: &[u8]) -> Result {
let ret_code = (Ptr32::from_slice(call), call.len() as u32)
.using_encoded(|in_data| sys::call(FUNC_ID, Ptr32::from_slice(in_data)));
Expand Down
2 changes: 0 additions & 2 deletions crates/env/src/engine/on_chain/ext/wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ mod sys {
out_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;

#[cfg(feature = "call-runtime")]
pub fn call_runtime(call_ptr: Ptr32<[u8]>, call_len: u32) -> ReturnCode;
}

Expand Down Expand Up @@ -465,7 +464,6 @@ pub fn return_value(flags: ReturnFlags, return_value: &[u8]) -> ! {
}
}

#[cfg(feature = "call-runtime")]
pub fn call_runtime(call: &[u8]) -> Result {
let ret_code =
unsafe { sys::call_runtime(Ptr32::from_slice(call), call.len() as u32) };
Expand Down
1 change: 0 additions & 1 deletion crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,6 @@ impl TypedEnvBackend for EnvInstance {
Ok(hash)
}

#[cfg(feature = "call-runtime")]
fn call_runtime<E, Call>(&mut self, call: &Call) -> Result<()>
where
E: Environment,
Expand Down
5 changes: 0 additions & 5 deletions crates/ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ ink-debug = [
"ink_env/ink-debug",
]

# Enable direct call to a pallet dispatchable via `call_runtime()`.
call-runtime = [
"ink_env/call-runtime",
]

show-codegen-docs = []

# Disable the ink! provided global memory allocator.
Expand Down
1 change: 0 additions & 1 deletion crates/ink/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,6 @@ where
ink_env::set_code_hash2::<E>(code_hash)
}

#[cfg(feature = "call-runtime")]
pub fn call_runtime<Call: scale::Encode>(self, call: &Call) -> Result<()> {
ink_env::call_runtime::<E, _>(call)
}
Expand Down
6 changes: 1 addition & 5 deletions integration-tests/call-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
publish = false

[dependencies]
ink = { path = "../../crates/ink", default-features = false, features = ["call-runtime"] }
ink = { path = "../../crates/ink", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }
Expand Down Expand Up @@ -37,7 +37,3 @@ std = [
]
ink-as-dependency = []
e2e-tests = []

# Assumes that the node used in E2E testing allows using the `call-runtime` API, including triggering
# `Balances::transfer` extrinsic.
permissive-node = []
45 changes: 2 additions & 43 deletions integration-tests/call-runtime/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ mod runtime_call {
///
/// Fails if:
/// - called in the off-chain environment
/// - the chain doesn't allow `call-runtime` API (`UnsafeUnstableInterface` is
/// turned off)
/// - the chain forbids contracts to call `Balances::transfer` (`CallFilter` is
/// too restrictive)
/// - after the transfer, `receiver` doesn't have at least existential deposit
Expand Down Expand Up @@ -132,7 +130,6 @@ mod runtime_call {
const UNIT: Balance = 1_000_000_000_000;

/// The contract will be given 1000 tokens during instantiation.
#[cfg(feature = "permissive-node")]
const CONTRACT_BALANCE: Balance = 1_000 * UNIT;

/// The receiver will get enough funds to have the required existential deposit.
Expand All @@ -144,14 +141,11 @@ mod runtime_call {
/// empty account fails.
///
/// Must not be zero, because such an operation would be a successful no-op.
#[cfg(feature = "permissive-node")]
const INSUFFICIENT_TRANSFER_VALUE: Balance = 1;

/// Positive case scenario:
/// - `call_runtime` is enabled
/// - the call is valid
/// - the call execution succeeds
#[cfg(feature = "permissive-node")]
#[ink_e2e::test]
async fn transfer_with_call_runtime_works(
mut client: Client<C, E>,
Expand All @@ -173,7 +167,7 @@ mod runtime_call {
let receiver: AccountId = default_accounts::<DefaultEnvironment>().bob;

let contract_balance_before = client
.balance(contract_acc_id)
.balance(contract.account_id)
.await
.expect("Failed to get account balance");
let receiver_balance_before = client
Expand All @@ -194,7 +188,7 @@ mod runtime_call {

// then
let contract_balance_after = client
.balance(contract_acc_id)
.balance(contract.account_id)
.await
.expect("Failed to get account balance");
let receiver_balance_after = client
Expand All @@ -215,10 +209,8 @@ mod runtime_call {
}

/// Negative case scenario:
/// - `call_runtime` is enabled
/// - the call is valid
/// - the call execution fails
#[cfg(feature = "permissive-node")]
#[ink_e2e::test]
async fn transfer_with_call_runtime_fails_when_execution_fails(
mut client: Client<C, E>,
Expand Down Expand Up @@ -255,9 +247,7 @@ mod runtime_call {
}

/// Negative case scenario:
/// - `call_runtime` is enabled
/// - the call is invalid
#[cfg(feature = "permissive-node")]
#[ink_e2e::test]
async fn transfer_with_call_runtime_fails_when_call_is_invalid(
mut client: Client<C, E>,
Expand Down Expand Up @@ -288,36 +278,5 @@ mod runtime_call {

Ok(())
}

/// Negative case scenario:
/// - `call_runtime` is disabled
#[cfg(not(feature = "permissive-node"))]
#[ink_e2e::test]
async fn call_runtime_fails_when_forbidden(
mut client: Client<C, E>,
) -> E2EResult<()> {
// given
let constructor = RuntimeCallerRef::new();
let contract = client
.instantiate("call-runtime", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiate failed");
let mut call = contract.call::<RuntimeCaller>();

let receiver: AccountId = default_accounts::<DefaultEnvironment>().bob;

let transfer_message =
call.transfer_through_runtime(receiver, TRANSFER_VALUE);

// when
let call_res = client
.call(&ink_e2e::alice(), &transfer_message, 0, None)
.await;

// then
assert!(matches!(call_res, Err(ink_e2e::Error::CallExtrinsic(_))));

Ok(())
}
}
}