Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 12 changed files with 649 additions and 508 deletions.
98 changes: 83 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions prdoc/pr_3679.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title: "[pallet-contracts] bump wasmi to 0.32"

doc:
- audience: Runtime Dev
description: |
- Bump wasmi to 0.32
- Turn on lazy and unchecked compilation when calling a contract.
See https://docs.rs/wasmi/0.32.0/wasmi/enum.CompilationMode.html#variant.Lazy
See https://docs.rs/wasmi/0.32.0/wasmi/struct.Module.html#method.new_unchecked
See https://wasmi-labs.github.io/blog/posts/wasmi-v0.32 for more details, on the wasmi update.

crates:
- name: pallet-contracts
- name: pallet-contracts-proc-macro
2 changes: 1 addition & 1 deletion substrate/frame/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ serde = { optional = true, features = ["derive"], workspace = true, default-feat
smallvec = { version = "1", default-features = false, features = [
"const_generics",
] }
wasmi = { version = "0.31", default-features = false }
wasmi = { version = "0.32.3", default-features = false }
impl-trait-for-tuples = "0.2"

# Only used in benchmarking to generate contract code
Expand Down
21 changes: 12 additions & 9 deletions substrate/frame/contracts/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl HostFnReturn {
Self::U64 => quote! { ::core::primitive::u64 },
};
quote! {
::core::result::Result<#ok, ::wasmi::core::Trap>
::core::result::Result<#ok, ::wasmi::Error>
}
}
}
Expand Down Expand Up @@ -694,7 +694,7 @@ fn expand_functions(def: &EnvDef, expand_mode: ExpandMode) -> TokenStream2 {
let into_host = if expand_blocks {
quote! {
|reason| {
::wasmi::core::Trap::from(reason)
::wasmi::Error::host(reason)
}
}
} else {
Expand All @@ -711,13 +711,13 @@ fn expand_functions(def: &EnvDef, expand_mode: ExpandMode) -> TokenStream2 {
quote! {
// Write gas from wasmi into pallet-contracts before entering the host function.
let __gas_left_before__ = {
let executor_total =
__caller__.fuel_consumed().expect("Fuel metering is enabled; qed");
let fuel =
__caller__.get_fuel().expect("Fuel metering is enabled; qed");
__caller__
.data_mut()
.ext()
.gas_meter_mut()
.sync_from_executor(executor_total)
.sync_from_executor(fuel)
.map_err(TrapReason::from)
.map_err(#into_host)?
};
Expand All @@ -733,15 +733,18 @@ fn expand_functions(def: &EnvDef, expand_mode: ExpandMode) -> TokenStream2 {
// Write gas from pallet-contracts into wasmi after leaving the host function.
let sync_gas_after = if expand_blocks {
quote! {
let fuel_consumed = __caller__
let fuel = __caller__
.data_mut()
.ext()
.gas_meter_mut()
.sync_to_executor(__gas_left_before__)
.map_err(TrapReason::from)?;
.map_err(|err| {
let err = TrapReason::from(err);
wasmi::Error::host(err)
})?;
__caller__
.consume_fuel(fuel_consumed.into())
.map_err(|_| TrapReason::from(Error::<E::T>::OutOfGas))?;
.set_fuel(fuel.into())
.expect("Fuel metering is enabled; qed");
}
} else {
quote! { }
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,15 +1278,14 @@ mod benchmarks {
// s: size of salt in bytes
#[benchmark(pov_mode = Measured)]
fn seal_instantiate(
t: Linear<0, 1>,
i: Linear<0, { (code::max_pages::<T>() - 1) * 64 * 1024 }>,
s: Linear<0, { (code::max_pages::<T>() - 1) * 64 * 1024 }>,
) -> Result<(), BenchmarkError> {
let hash = Contract::<T>::with_index(1, WasmModule::dummy(), vec![])?.info()?.code_hash;
let hash_bytes = hash.encode();
let hash_len = hash_bytes.len() as u32;

let value: BalanceOf<T> = t.into();
let value: BalanceOf<T> = 1u32.into();
let value_bytes = value.encode();
let value_len = value_bytes.len() as u32;

Expand Down Expand Up @@ -1341,6 +1340,7 @@ mod benchmarks {

assert_ok!(result);
assert!(ContractInfoOf::<T>::get(&addr).is_some());
assert_eq!(T::Currency::balance(&addr), Pallet::<T>::min_balance() + value);
Ok(())
}

Expand Down
5 changes: 3 additions & 2 deletions substrate/frame/contracts/src/benchmarking/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::wasm::{
LoadingMode, WasmBlob,
};
use sp_core::Get;
use wasmi::{errors::LinkerError, Func, Linker, StackLimits, Store};
use wasmi::{errors::LinkerError, CompilationMode, Func, Linker, StackLimits, Store};

/// Minimal execution environment without any imported functions.
pub struct Sandbox {
Expand All @@ -48,6 +48,7 @@ impl<T: Config> From<&WasmModule<T>> for Sandbox {
Determinism::Relaxed,
Some(StackLimits::default()),
LoadingMode::Checked,
CompilationMode::Eager,
)
.expect("Failed to load Wasm module");

Expand All @@ -62,7 +63,7 @@ impl<T: Config> From<&WasmModule<T>> for Sandbox {

// Set fuel for wasmi execution.
store
.add_fuel(u64::MAX)
.set_fuel(u64::MAX)
.expect("We've set up engine to fuel consuming mode; qed");

let entry_point = instance
Expand Down
Loading

0 comments on commit 1973ecc

Please sign in to comment.