Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
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
12 changes: 6 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion demo/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
init_logger(log_pattern);

// Create client
let executor = NativeExecutor::with_heap_pages(8, 8);
let executor = NativeExecutor::with_heap_pages(8);

let god_key = hex!["3d866ec8a9190c8343c2fc593d21d8a6d0c5c4763aaab2349de3a6111d64d124"];
let genesis_config = GenesisConfig {
Expand Down
20 changes: 10 additions & 10 deletions demo/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ mod tests {
}

fn executor() -> ::substrate_executor::NativeExecutor<Executor> {
::substrate_executor::NativeExecutor::with_heap_pages(8, 8)
::substrate_executor::NativeExecutor::with_heap_pages(8)
}

#[test]
Expand Down Expand Up @@ -312,14 +312,14 @@ mod tests {
fn full_wasm_block_import_works() {
let mut t = new_test_ext();

WasmExecutor::new(8, 8).call(&mut t, COMPACT_CODE, "execute_block", &block1().0, true).0.unwrap();
WasmExecutor::new(8).call(&mut t, COMPACT_CODE, "execute_block", &block1().0).unwrap();

runtime_io::with_externalities(&mut t, || {
assert_eq!(Staking::voting_balance(&alice()), 41);
assert_eq!(Staking::voting_balance(&bob()), 69);
});

WasmExecutor::new(8, 8).call(&mut t, COMPACT_CODE, "execute_block", &block2().0, true).0.unwrap();
WasmExecutor::new(8).call(&mut t, COMPACT_CODE, "execute_block", &block2().0).unwrap();

runtime_io::with_externalities(&mut t, || {
assert_eq!(Staking::voting_balance(&alice()), 30);
Expand All @@ -331,23 +331,23 @@ mod tests {
fn wasm_big_block_import_fails() {
let mut t = new_test_ext();

let r = WasmExecutor::new(8, 8).call(&mut t, COMPACT_CODE, "execute_block", &block1big().0, true).0;
let r = WasmExecutor::new(8).call(&mut t, COMPACT_CODE, "execute_block", &block1big().0);
assert!(!r.is_ok());
}

#[test]
fn native_big_block_import_succeeds() {
let mut t = new_test_ext();

let r = Executor::with_heap_pages(8, 8).call(&mut t, COMPACT_CODE, "execute_block", &block1big().0, true).0;
let r = Executor::with_heap_pages(8).call(&mut t, COMPACT_CODE, "execute_block", &block1big().0, true).0;
assert!(r.is_ok());
}

#[test]
fn native_big_block_import_fails_on_fallback() {
let mut t = new_test_ext();

let r = Executor::with_heap_pages(8, 8).call(&mut t, COMPACT_CODE, "execute_block", &block1big().0, false).0;
let r = Executor::with_heap_pages(8).call(&mut t, COMPACT_CODE, "execute_block", &block1big().0, false).0;
assert!(!r.is_ok());
}

Expand All @@ -364,9 +364,9 @@ mod tests {
];

let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm");
let r = WasmExecutor::new(8, 8).call(&mut t, &foreign_code[..], "initialise_block", &vec![].and(&from_block_number(1u64)), true).0;
let r = WasmExecutor::new(8).call(&mut t, &foreign_code[..], "initialise_block", &vec![].and(&from_block_number(1u64)));
assert!(r.is_ok());
let r = WasmExecutor::new(8, 8).call(&mut t, &foreign_code[..], "apply_extrinsic", &vec![].and(&xt()), true).0.unwrap();
let r = WasmExecutor::new(8).call(&mut t, &foreign_code[..], "apply_extrinsic", &vec![].and(&xt())).unwrap();
let r = ApplyResult::decode(&mut &r[..]).unwrap();
assert_eq!(r, Err(ApplyError::CantPay));
}
Expand All @@ -384,9 +384,9 @@ mod tests {
];

let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm");
let r = WasmExecutor::new(8, 8).call(&mut t, &foreign_code[..], "initialise_block", &vec![].and(&from_block_number(1u64)), true).0;
let r = WasmExecutor::new(8).call(&mut t, &foreign_code[..], "initialise_block", &vec![].and(&from_block_number(1u64)));
assert!(r.is_ok());
let r = WasmExecutor::new(8, 8).call(&mut t, &foreign_code[..], "apply_extrinsic", &vec![].and(&xt()), true).0.unwrap();
let r = WasmExecutor::new(8).call(&mut t, &foreign_code[..], "apply_extrinsic", &vec![].and(&xt())).unwrap();
let r = ApplyResult::decode(&mut &r[..]).unwrap();
assert_eq!(r, Ok(ApplyOutcome::Success));

Expand Down
2 changes: 1 addition & 1 deletion polkadot/api/src/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ mod tests {
timestamp: Some(Default::default()),
};

::client::new_in_mem(LocalDispatch::with_heap_pages(8, 8), genesis_config).unwrap()
::client::new_in_mem(LocalDispatch::with_heap_pages(8), genesis_config).unwrap()
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion polkadot/parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Types and utilities for creating and working with parachains"

[dependencies]
substrate-codec = { path = "../../substrate/codec", default-features = false }
wasmi = { version = "0.3", optional = true }
wasmi = { version = "0.4", optional = true }
error-chain = { version = "0.12", optional = true }

[dev-dependencies]
Expand Down
8 changes: 0 additions & 8 deletions substrate/cli/src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ args:
long: execution
value_name: STRATEGY
help: The means of execution used when calling into the runtime. Can be either wasm, native or both.
- min-heap-pages:
long: min-heap-pages
value_name: COUNT
help: The number of 64KB pages to allocate for Wasm execution initially.
- max-heap-pages:
long: max-heap-pages
value_name: COUNT
Expand Down Expand Up @@ -170,10 +166,6 @@ subcommands:
long: execution
value_name: STRATEGY
help: The means of execution used when calling into the runtime. Can be either wasm, native or both.
- min-heap-pages:
long: min-heap-pages
value_name: COUNT
help: The number of 64KB pages to allocate for Wasm execution initially.
- max-heap-pages:
long: max-heap-pages
value_name: COUNT
Expand Down
6 changes: 0 additions & 6 deletions substrate/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,6 @@ where
service::Roles::FULL
};

if let Some(v) = matches.value_of("min-heap-pages") {
config.min_heap_pages = v.parse().map_err(|_| "Invalid --min-heap-pages argument")?;
}
if let Some(v) = matches.value_of("max-heap-pages") {
config.max_heap_pages = v.parse().map_err(|_| "Invalid --max-heap-pages argument")?;
}
Expand Down Expand Up @@ -352,9 +349,6 @@ fn import_blocks<F, E>(matches: &clap::ArgMatches, spec: ChainSpec<FactoryGenesi
let mut config = service::Configuration::default_with_spec(spec);
config.database_path = db_path(&base_path, config.chain_spec.id()).to_string_lossy().into();

if let Some(v) = matches.value_of("min-heap-pages") {
config.min_heap_pages = v.parse().map_err(|_| "Invalid --min-heap-pages argument")?;
}
if let Some(v) = matches.value_of("max-heap-pages") {
config.max_heap_pages = v.parse().map_err(|_| "Invalid --max-heap-pages argument")?;
}
Expand Down
10 changes: 5 additions & 5 deletions substrate/client/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod tests {
use super::*;
use codec::{Encode, Decode, Joiner};
use keyring::Keyring;
use executor::{WasmExecutor, NativeExecutionDispatch};
use executor::NativeExecutionDispatch;
use state_machine::{execute, OverlayedChanges, ExecutionStrategy};
use state_machine::backend::InMemory;
use test_client;
Expand All @@ -55,7 +55,7 @@ mod tests {
native_executor_instance!(Executor, test_client::runtime::api::dispatch, test_client::runtime::VERSION, include_bytes!("../../test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm"));

fn executor() -> ::executor::NativeExecutor<Executor> {
NativeExecutionDispatch::with_heap_pages(8, 8)
NativeExecutionDispatch::with_heap_pages(8)
}

fn construct_block(backend: &InMemory, number: BlockNumber, parent_hash: Hash, state_root: Hash, txs: Vec<Transfer>) -> (Vec<u8>, Hash) {
Expand Down Expand Up @@ -169,10 +169,10 @@ mod tests {
let _ = execute(
&backend,
&mut overlay,
&WasmExecutor::new(8, 8),
&executor(),
"execute_block",
&b1data,
ExecutionStrategy::NativeWhenPossible,
ExecutionStrategy::AlwaysWasm,
).unwrap();
}

Expand All @@ -193,7 +193,7 @@ mod tests {
let _ = execute(
&backend,
&mut overlay,
&Executor::with_heap_pages(8, 8),
&Executor::with_heap_pages(8),
"execute_block",
&b1data,
ExecutionStrategy::NativeWhenPossible,
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/src/light/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ mod tests {
let remote_execution_proof = remote_client.execution_proof(&remote_block_id, "authorities", &[]).unwrap().1;

// check remote execution proof locally
let local_executor = test_client::LocalExecutor::with_heap_pages(8, 8);
let local_executor = test_client::LocalExecutor::with_heap_pages(8);
do_check_execution_proof(remote_block_storage_root.into(), &local_executor, &RemoteCallRequest {
block: test_client::runtime::Hash::default(),
method: "authorities".into(),
Expand Down
4 changes: 2 additions & 2 deletions substrate/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ substrate-runtime-version = { path = "../runtime/version" }
ed25519 = { path = "../ed25519" }
serde = "1.0"
serde_derive = "1.0"
wasmi = "0.3"
wasmi = "0.4"
byteorder = "1.1"
rustc-hex = "1.0.0"
triehash = "0.1.0"
Expand All @@ -30,4 +30,4 @@ wabt = "0.4"

[features]
default = []
wasm-extern-trace = []
wasm-extern-trace = []
16 changes: 1 addition & 15 deletions substrate/executor/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,7 @@ error_chain! {
description("invalid memory reference"),
display("Invalid memory reference"),
}

/// Retry, please.
PleaseRetry {
description("retry needed"),
display("Retry needed"),
}
}
}

impl state_machine::Error for Error {
fn needs_retry(&self) -> bool {
if let ErrorKind::PleaseRetry = self.0 {
true
} else {
false
}
}
}
impl state_machine::Error for Error {}
37 changes: 17 additions & 20 deletions substrate/executor/src/native_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ use RuntimeInfo;

// For the internal Runtime Cache:
// Is it compatible enough to run this natively or do we need to fall back on the WasmModule

enum Compatibility {
InvalidVersion(WasmModule),
IsCompatible(RuntimeVersion),
IsCompatible(RuntimeVersion, WasmModule),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If natve+wasm execution is requested this should still cache the module. Wasm execution will be performed even if the version is compatible.

NotCompatible(RuntimeVersion, WasmModule)
}

unsafe impl Send for Compatibility {}

type CacheType = HashMap<u64, Compatibility>;

lazy_static! {
Expand All @@ -54,6 +57,7 @@ fn gen_cache_key(code: &[u8]) -> u64 {
/// the runtime version entry for `code`, determines whether `Compatibility::IsCompatible`
/// can be used by by comparing returned RuntimeVersion to `ref_version`
fn fetch_cached_runtime_version<'a, E: Externalities>(
wasm_executor: &WasmExecutor,
cache: &'a mut MutexGuard<CacheType>,
ext: &mut E,
code: &[u8],
Expand All @@ -62,12 +66,12 @@ fn fetch_cached_runtime_version<'a, E: Externalities>(
cache.entry(gen_cache_key(code))
.or_insert_with(|| {
let module = WasmModule::from_buffer(code).expect("all modules compiled with rustc are valid wasm code; qed");
let version = WasmExecutor::new(8, 8).call_in_wasm_module(ext, &module, "version", &[]).ok()
let version = wasm_executor.call_in_wasm_module(ext, &module, "version", &[]).ok()
.and_then(|v| RuntimeVersion::decode(&mut v.as_slice()));

if let Some(v) = version {
if ref_version.can_call_with(&v) {
Compatibility::IsCompatible(v)
Compatibility::IsCompatible(v, module)
} else {
Compatibility::NotCompatible(v, module)
}
Expand Down Expand Up @@ -109,8 +113,8 @@ pub trait NativeExecutionDispatch: Send + Sync {
const VERSION: RuntimeVersion;

/// Construct corresponding `NativeExecutor` with given `heap_pages`.
fn with_heap_pages(min_heap_pages: usize, max_heap_pages: usize) -> NativeExecutor<Self> where Self: Sized {
NativeExecutor::with_heap_pages(min_heap_pages, max_heap_pages)
fn with_heap_pages(max_heap_pages: usize) -> NativeExecutor<Self> where Self: Sized {
NativeExecutor::with_heap_pages(max_heap_pages)
}
}

Expand All @@ -126,15 +130,10 @@ pub struct NativeExecutor<D: NativeExecutionDispatch> {

impl<D: NativeExecutionDispatch> NativeExecutor<D> {
/// Create new instance with specific number of pages for wasm fallback's heap.
pub fn with_heap_pages(min_heap_pages: usize, max_heap_pages: usize) -> Self {
// FIXME: set this entry at compile time
RUNTIMES_CACHE.lock().insert(
gen_cache_key(D::native_equivalent()),
Compatibility::IsCompatible(D::VERSION));

pub fn with_heap_pages(max_heap_pages: usize) -> Self {
NativeExecutor {
_dummy: Default::default(),
fallback: WasmExecutor::new(min_heap_pages, max_heap_pages),
fallback: WasmExecutor::new(max_heap_pages),
}
}
}
Expand All @@ -157,8 +156,8 @@ impl<D: NativeExecutionDispatch> RuntimeInfo for NativeExecutor<D> {
code: &[u8],
) -> Option<RuntimeVersion> {
let mut c = RUNTIMES_CACHE.lock();
match fetch_cached_runtime_version(&mut c, ext, code, D::VERSION) {
Compatibility::IsCompatible(v) | Compatibility::NotCompatible(v, _) => Some(v.clone()),
match fetch_cached_runtime_version(&self.fallback, &mut c, ext, code, D::VERSION) {
Compatibility::IsCompatible(v, _) | Compatibility::NotCompatible(v, _) => Some(v.clone()),
Compatibility::InvalidVersion(_m) => None
}
}
Expand All @@ -176,11 +175,9 @@ impl<D: NativeExecutionDispatch> CodeExecutor for NativeExecutor<D> {
use_native: bool,
) -> (Result<Vec<u8>>, bool) {
let mut c = RUNTIMES_CACHE.lock();
match (use_native, fetch_cached_runtime_version(&mut c, ext, code, D::VERSION)) {
(_, Compatibility::NotCompatible(_, m)) | (_, Compatibility::InvalidVersion(m)) =>
match (use_native, fetch_cached_runtime_version(&self.fallback, &mut c, ext, code, D::VERSION)) {
(_, Compatibility::NotCompatible(_, m)) | (_, Compatibility::InvalidVersion(m)) | (false, Compatibility::IsCompatible(_, m)) =>
(self.fallback.call_in_wasm_module(ext, m, method, data), false),
(false, _) =>
(self.fallback.call(ext, code, method, data, false).0, false),
_ => (D::dispatch(ext, method, data), true),
}
}
Expand Down Expand Up @@ -211,8 +208,8 @@ macro_rules! native_executor_instance {
.ok_or_else(|| $crate::error::ErrorKind::MethodNotFound(method.to_owned()).into())
}

fn with_heap_pages(min_heap_pages: usize, max_heap_pages: usize) -> $crate::NativeExecutor<$name> {
$crate::NativeExecutor::with_heap_pages(min_heap_pages, max_heap_pages)
fn with_heap_pages(max_heap_pages: usize) -> $crate::NativeExecutor<$name> {
$crate::NativeExecutor::with_heap_pages(max_heap_pages)
}
}
}
Expand Down
Loading