From e6d90dba2d41b99c1c0fd00296976b18644b3813 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sat, 22 May 2021 23:46:22 +0200 Subject: [PATCH 01/24] Working draft with some consideration about names. --- Cargo.lock | 21 +- bin/node/testing/src/bench.rs | 38 +-- client/api/src/call_executor.rs | 9 +- client/api/src/execution_extensions.rs | 87 +++--- client/api/src/lib.rs | 3 +- client/cli/Cargo.toml | 1 + client/cli/src/arg_enums.rs | 32 ++- client/cli/src/config.rs | 14 +- client/cli/src/params/import_params.rs | 69 +++-- client/finality-grandpa/src/lib.rs | 8 +- client/light/src/call_executor.rs | 36 +-- client/rpc/src/state/state_full.rs | 33 ++- client/service/src/builder.rs | 6 +- client/service/src/client/call_executor.rs | 37 +-- client/service/src/config.rs | 15 +- client/service/test/src/client/light.rs | 2 +- client/service/test/src/lib.rs | 5 +- frame/merkle-mountain-range/src/lib.rs | 3 +- frame/system/src/lib.rs | 40 +-- primitives/state-machine/src/backend.rs | 23 +- primitives/state-machine/src/lib.rs | 296 +++++++++++++------- primitives/storage/src/lib.rs | 8 +- test-utils/client/src/lib.rs | 45 ++- test-utils/test-runner/src/lib.rs | 24 +- test-utils/test-runner/src/utils.rs | 19 +- utils/browser/src/lib.rs | 2 +- utils/frame/benchmarking-cli/src/command.rs | 9 +- utils/frame/try-runtime/cli/src/lib.rs | 4 +- 28 files changed, 525 insertions(+), 364 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9019acfcbb239..0914ba72ac5d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1725,7 +1725,7 @@ dependencies = [ "linregress", "log", "parity-scale-codec", - "paste 1.0.4", + "paste 1.0.5", "serde", "sp-api", "sp-io", @@ -1813,7 +1813,7 @@ dependencies = [ "once_cell", "parity-scale-codec", "parity-util-mem", - "paste 1.0.4", + "paste 1.0.5", "pretty_assertions 0.6.1", "serde", "smallvec 1.6.1", @@ -4793,7 +4793,7 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "parity-wasm 0.42.2", - "paste 1.0.4", + "paste 1.0.5", "pretty_assertions 0.7.2", "pwasm-utils 0.17.0", "rand 0.8.3", @@ -4893,7 +4893,7 @@ dependencies = [ "pallet-balances", "parity-scale-codec", "parking_lot 0.11.1", - "paste 1.0.4", + "paste 1.0.5", "rand 0.7.3", "sp-arithmetic", "sp-core", @@ -5405,7 +5405,7 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "parking_lot 0.11.1", - "paste 1.0.4", + "paste 1.0.5", "rand_chacha 0.2.2", "serde", "sp-application-crypto", @@ -5841,9 +5841,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5d65c4d95931acda4498f675e332fcbdc9a06705cd07086c510e9b6009cd1c1" +checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58" [[package]] name = "paste-impl" @@ -6983,6 +6983,7 @@ dependencies = [ "log", "names", "parity-scale-codec", + "paste 1.0.5", "rand 0.7.3", "regex", "rpassword", @@ -7343,7 +7344,7 @@ dependencies = [ "parity-scale-codec", "parity-wasm 0.41.0", "parking_lot 0.11.1", - "paste 1.0.4", + "paste 1.0.5", "sc-executor-common", "sc-executor-wasmi", "sc-executor-wasmtime", @@ -9011,7 +9012,7 @@ dependencies = [ "log", "parity-scale-codec", "parity-util-mem", - "paste 1.0.4", + "paste 1.0.5", "rand 0.7.3", "serde", "serde_json", @@ -10879,7 +10880,7 @@ dependencies = [ "indexmap", "libc", "log", - "paste 1.0.4", + "paste 1.0.5", "region", "rustc-demangle", "serde", diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index edb99c617771a..07631e7a71aad 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -55,8 +55,8 @@ use sp_api::ProvideRuntimeApi; use sp_block_builder::BlockBuilder; use sp_inherents::InherentData; use sc_client_api::{ - ExecutionStrategy, BlockBackend, - execution_extensions::{ExecutionExtensions, ExecutionStrategies}, + ExecutionStrategy, BlockBackend, ExecutionConfig, + execution_extensions::{ExecutionExtensions, ExecutionConfigs}, }; use sc_block_builder::BlockBuilderProvider; use futures::executor; @@ -424,7 +424,7 @@ impl BenchDb { &keyring.generate_genesis(), None, None, - ExecutionExtensions::new(profile.into_execution_strategies(), None, None), + ExecutionExtensions::new(profile.into_execution_configs(), None, None), Box::new(task_executor.clone()), None, None, @@ -616,7 +616,7 @@ impl BenchKeyring { } } -/// Profile for exetion strategies. +/// Profile for execution configurations. #[derive(Clone, Copy, Debug)] pub enum Profile { /// As native as possible. @@ -626,22 +626,24 @@ pub enum Profile { } impl Profile { - fn into_execution_strategies(self) -> ExecutionStrategies { + fn into_execution_configs(self) -> ExecutionConfigs { match self { - Profile::Wasm => ExecutionStrategies { - syncing: ExecutionStrategy::AlwaysWasm, - importing: ExecutionStrategy::AlwaysWasm, - block_construction: ExecutionStrategy::AlwaysWasm, - offchain_worker: ExecutionStrategy::AlwaysWasm, - other: ExecutionStrategy::AlwaysWasm, + Profile::Wasm => ExecutionConfigs { + syncing: ExecutionConfig::new_offchain(ExecutionStrategy::AlwaysWasm), + importing: ExecutionConfig::new_offchain(ExecutionStrategy::AlwaysWasm), + block_construction: ExecutionConfig::new_offchain(ExecutionStrategy::AlwaysWasm), + offchain_worker: ExecutionConfig::new_offchain(ExecutionStrategy::AlwaysWasm), + other: ExecutionConfig::new_offchain(ExecutionStrategy::AlwaysWasm), + }, + Profile::Native => ExecutionConfigs { + syncing: ExecutionConfig::new_offchain(ExecutionStrategy::NativeElseWasm), + importing: ExecutionConfig::new_offchain(ExecutionStrategy::NativeElseWasm), + block_construction: ExecutionConfig::new_offchain( + ExecutionStrategy::NativeElseWasm, + ), + offchain_worker: ExecutionConfig::new_offchain(ExecutionStrategy::NativeElseWasm), + other: ExecutionConfig::new_offchain(ExecutionStrategy::NativeElseWasm), }, - Profile::Native => ExecutionStrategies { - syncing: ExecutionStrategy::NativeElseWasm, - importing: ExecutionStrategy::NativeElseWasm, - block_construction: ExecutionStrategy::NativeElseWasm, - offchain_worker: ExecutionStrategy::NativeElseWasm, - other: ExecutionStrategy::NativeElseWasm, - } } } } diff --git a/client/api/src/call_executor.rs b/client/api/src/call_executor.rs index 3b725bf8773a8..df9feda72b127 100644 --- a/client/api/src/call_executor.rs +++ b/client/api/src/call_executor.rs @@ -21,11 +21,10 @@ use std::{panic::UnwindSafe, result, cell::RefCell}; use codec::{Encode, Decode}; use sp_runtime::{ - generic::BlockId, traits::{Block as BlockT, HashFor}, -}; -use sp_state_machine::{ - OverlayedChanges, ExecutionManager, ExecutionStrategy, StorageProof, + generic::BlockId, + traits::{Block as BlockT, HashFor}, }; +use sp_state_machine::{OverlayedChanges, ExecutionManager, ExecutionConfig, StorageProof}; use sc_executor::{RuntimeVersion, NativeVersion}; use sp_externalities::Extensions; use sp_core::NativeOrEncoded; @@ -61,7 +60,7 @@ pub trait CallExecutor { id: &BlockId, method: &str, call_data: &[u8], - strategy: ExecutionStrategy, + config: ExecutionConfig, extensions: Option, ) -> Result, sp_blockchain::Error>; diff --git a/client/api/src/execution_extensions.rs b/client/api/src/execution_extensions.rs index 2f17408b7d7c1..fc093ca9206c3 100644 --- a/client/api/src/execution_extensions.rs +++ b/client/api/src/execution_extensions.rs @@ -29,37 +29,35 @@ use sp_core::{ offchain::{self, OffchainWorkerExt, TransactionPoolExt, OffchainDbExt}, }; use sp_keystore::{KeystoreExt, SyncCryptoStorePtr}; -use sp_runtime::{ - generic::BlockId, - traits, -}; -use sp_state_machine::{ExecutionStrategy, ExecutionManager, DefaultHandler}; +use sp_runtime::{generic::BlockId, traits}; +use sp_state_machine::{ExecutionStrategy, ExecutionConfig, ExecutionManager, DefaultHandler}; use sp_externalities::Extensions; use parking_lot::RwLock; /// Execution strategies settings. #[derive(Debug, Clone)] -pub struct ExecutionStrategies { - /// Execution strategy used when syncing. - pub syncing: ExecutionStrategy, - /// Execution strategy used when importing blocks. - pub importing: ExecutionStrategy, - /// Execution strategy used when constructing blocks. - pub block_construction: ExecutionStrategy, - /// Execution strategy used for offchain workers. - pub offchain_worker: ExecutionStrategy, - /// Execution strategy used in other cases. - pub other: ExecutionStrategy, +pub struct ExecutionConfigs { + /// Execution config used when syncing. + pub syncing: ExecutionConfig, + /// Execution config used when importing blocks. + pub importing: ExecutionConfig, + /// Execution config used when constructing blocks. + pub block_construction: ExecutionConfig, + /// Execution config used for offchain workers. + pub offchain_worker: ExecutionConfig, + /// Execution config used in other cases. + pub other: ExecutionConfig, } -impl Default for ExecutionStrategies { - fn default() -> ExecutionStrategies { - ExecutionStrategies { - syncing: ExecutionStrategy::NativeElseWasm, - importing: ExecutionStrategy::NativeElseWasm, - block_construction: ExecutionStrategy::AlwaysWasm, - offchain_worker: ExecutionStrategy::NativeWhenPossible, - other: ExecutionStrategy::NativeElseWasm, +impl Default for ExecutionConfigs { + fn default() -> ExecutionConfigs { + // TODO: this should use our defaults consts. + ExecutionConfigs { + syncing: ExecutionStrategy::NativeElseWasm.in_consensus(), + importing: ExecutionStrategy::NativeElseWasm.in_consensus(), + block_construction: ExecutionStrategy::AlwaysWasm.in_consensus(), + offchain_worker: ExecutionStrategy::NativeWhenPossible.in_offchain(), + other: ExecutionStrategy::NativeElseWasm.in_consensus(), } } } @@ -94,7 +92,7 @@ impl DbExternaliti /// and is responsible for producing a correct `Extensions` object. /// for each call, based on required `Capabilities`. pub struct ExecutionExtensions { - strategies: ExecutionStrategies, + configs: ExecutionConfigs, keystore: Option, offchain_db: Option>, // FIXME: these two are only RwLock because of https://github.com/paritytech/substrate/issues/4587 @@ -103,14 +101,15 @@ pub struct ExecutionExtensions { // extension to be a `Weak` reference. // That's also the reason why it's being registered lazily instead of // during initialization. - transaction_pool: RwLock>>>, + transaction_pool: + RwLock>>>, extensions_factory: RwLock>, } impl Default for ExecutionExtensions { fn default() -> Self { Self { - strategies: Default::default(), + configs: Default::default(), keystore: None, offchain_db: None, transaction_pool: RwLock::new(None), @@ -120,16 +119,16 @@ impl Default for ExecutionExtensions { } impl ExecutionExtensions { - /// Create new `ExecutionExtensions` given a `keystore` and `ExecutionStrategies`. + /// Create new `ExecutionExtensions` given a `keystore` and `ExecutionConfigs`. pub fn new( - strategies: ExecutionStrategies, + configs: ExecutionConfigs, keystore: Option, offchain_db: Option>, ) -> Self { let transaction_pool = RwLock::new(None); let extensions_factory = Box::new(()); Self { - strategies, + configs, keystore, offchain_db, extensions_factory: RwLock::new(extensions_factory), @@ -137,9 +136,9 @@ impl ExecutionExtensions { } } - /// Get a reference to the execution strategies. - pub fn strategies(&self) -> &ExecutionStrategies { - &self.strategies + /// Get a reference to the execution configs. + pub fn configs(&self) -> &ExecutionConfigs { + &self.configs } /// Set the new extensions_factory @@ -210,21 +209,15 @@ impl ExecutionExtensions { &self, at: &BlockId, context: ExecutionContext, - ) -> ( - ExecutionManager>, - Extensions, - ) { + ) -> (ExecutionManager>, Extensions) { let manager = match context { - ExecutionContext::BlockConstruction => - self.strategies.block_construction.get_manager(), - ExecutionContext::Syncing => - self.strategies.syncing.get_manager(), - ExecutionContext::Importing => - self.strategies.importing.get_manager(), - ExecutionContext::OffchainCall(Some((_, capabilities))) if capabilities.has_all() => - self.strategies.offchain_worker.get_manager(), - ExecutionContext::OffchainCall(_) => - self.strategies.other.get_manager(), + ExecutionContext::BlockConstruction => self.configs.block_construction.get_manager(), + ExecutionContext::Syncing => self.configs.syncing.get_manager(), + ExecutionContext::Importing => self.configs.importing.get_manager(), + ExecutionContext::OffchainCall(Some((_, capabilities))) if capabilities.has_all() => { + self.configs.offchain_worker.get_manager() + } + ExecutionContext::OffchainCall(_) => self.configs.other.get_manager(), }; (manager, self.extensions(at, context)) diff --git a/client/api/src/lib.rs b/client/api/src/lib.rs index 0f860b95e7805..59525acbb1e88 100644 --- a/client/api/src/lib.rs +++ b/client/api/src/lib.rs @@ -39,10 +39,9 @@ pub use light::*; pub use notifications::*; pub use proof_provider::*; -pub use sp_state_machine::{StorageProof, ExecutionStrategy}; +pub use sp_state_machine::{StorageProof, ExecutionStrategy, ExecutionConfig, ExecutionContext}; /// Usage Information Provider interface -/// pub trait UsageProvider { /// Get usage info about current client. fn usage_info(&self) -> ClientInfo; diff --git a/client/cli/Cargo.toml b/client/cli/Cargo.toml index 00a56e5fa9b86..1d76139dd3d4c 100644 --- a/client/cli/Cargo.toml +++ b/client/cli/Cargo.toml @@ -43,6 +43,7 @@ sc-tracing = { version = "3.0.0", path = "../tracing" } chrono = "0.4.10" serde = "1.0.111" thiserror = "1.0.21" +paste = "1.0.5" [target.'cfg(not(target_os = "unknown"))'.dependencies] rpassword = "5.0.0" diff --git a/client/cli/src/arg_enums.rs b/client/cli/src/arg_enums.rs index fb2f8fdbc21d8..c643913c2ccfe 100644 --- a/client/cli/src/arg_enums.rs +++ b/client/cli/src/arg_enums.rs @@ -18,6 +18,7 @@ // NOTE: we allow missing docs here because arg_enum! creates the function variants without doc #![allow(missing_docs)] +use sc_client_api::ExecutionContext; use structopt::clap::arg_enum; /// How to execute Wasm runtime code. @@ -232,15 +233,22 @@ arg_enum! { } } -/// Default value for the `--execution-syncing` parameter. -pub const DEFAULT_EXECUTION_SYNCING: ExecutionStrategy = ExecutionStrategy::NativeElseWasm; -/// Default value for the `--execution-import-block` parameter. -pub const DEFAULT_EXECUTION_IMPORT_BLOCK: ExecutionStrategy = ExecutionStrategy::NativeElseWasm; -/// Default value for the `--execution-import-block` parameter when the node is a validator. -pub const DEFAULT_EXECUTION_IMPORT_BLOCK_VALIDATOR: ExecutionStrategy = ExecutionStrategy::Wasm; -/// Default value for the `--execution-block-construction` parameter. -pub const DEFAULT_EXECUTION_BLOCK_CONSTRUCTION: ExecutionStrategy = ExecutionStrategy::Wasm; -/// Default value for the `--execution-offchain-worker` parameter. -pub const DEFAULT_EXECUTION_OFFCHAIN_WORKER: ExecutionStrategy = ExecutionStrategy::Native; -/// Default value for the `--execution-other` parameter. -pub const DEFAULT_EXECUTION_OTHER: ExecutionStrategy = ExecutionStrategy::Native; +macro_rules! generate_config_const { + ($( $role:ident => $strategy:ident, $context:ident ),*) => { + paste::paste! { + $( + pub const []: ExecutionStrategy = ExecutionStrategy::$strategy; + pub const []: ExecutionContext = ExecutionContext::$context; + )* + } + }; +} + +generate_config_const!( + syncing => NativeElseWasm, Consensus, + import_block => NativeElseWasm, Consensus, + import_block_validator => Wasm, Consensus, + block_construction => Wasm, Consensus, + offchain_worker => Native, Offchain, + other => Native, Consensus +); diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index a21a79afe9fdb..2b631d97f6c76 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -26,7 +26,7 @@ use crate::{ }; use log::warn; use names::{Generator, Name}; -use sc_client_api::execution_extensions::ExecutionStrategies; +use sc_client_api::execution_extensions::ExecutionConfigs; use sc_service::config::{ BasePath, Configuration, DatabaseConfig, ExtTransport, KeystoreConfig, NetworkConfiguration, NodeKeyConfig, OffchainWorkerConfig, PrometheusConfig, PruningMode, Role, RpcMethods, @@ -310,15 +310,11 @@ pub trait CliConfiguration: Sized { /// Get the execution strategies. /// /// By default this is retrieved from `ImportParams` if it is available. Otherwise its - /// `ExecutionStrategies::default()`. - fn execution_strategies( - &self, - is_dev: bool, - is_validator: bool, - ) -> Result { + /// `ExecutionConfigs::default()`. + fn execution_configs(&self, is_dev: bool, is_validator: bool) -> Result { Ok(self .import_params() - .map(|x| x.execution_strategies(is_dev, is_validator)) + .map(|x| x.execution_configs(is_dev, is_validator)) .unwrap_or_default()) } @@ -520,7 +516,7 @@ pub trait CliConfiguration: Sized { transaction_storage: self.database_transaction_storage()?, wasm_method: self.wasm_method()?, wasm_runtime_overrides: self.wasm_runtime_overrides(), - execution_strategies: self.execution_strategies(is_dev, is_validator)?, + execution_configs: self.execution_configs(is_dev, is_validator)?, rpc_http: self.rpc_http(DCV::rpc_http_listen_port())?, rpc_ws: self.rpc_ws(DCV::rpc_ws_listen_port())?, rpc_ipc: self.rpc_ipc()?, diff --git a/client/cli/src/params/import_params.rs b/client/cli/src/params/import_params.rs index a62ec98a97029..b1eb24c171cfc 100644 --- a/client/cli/src/params/import_params.rs +++ b/client/cli/src/params/import_params.rs @@ -17,13 +17,15 @@ // along with this program. If not, see . use crate::arg_enums::{ - ExecutionStrategy, WasmExecutionMethod, DEFAULT_EXECUTION_BLOCK_CONSTRUCTION, - DEFAULT_EXECUTION_IMPORT_BLOCK, DEFAULT_EXECUTION_IMPORT_BLOCK_VALIDATOR, - DEFAULT_EXECUTION_OFFCHAIN_WORKER, DEFAULT_EXECUTION_OTHER, DEFAULT_EXECUTION_SYNCING, + ExecutionStrategy, WasmExecutionMethod, DEFAULT_STRATEGY_BLOCK_CONSTRUCTION, + DEFAULT_STRATEGY_IMPORT_BLOCK, DEFAULT_STRATEGY_IMPORT_BLOCK_VALIDATOR, + DEFAULT_STRATEGY_OFFCHAIN_WORKER, DEFAULT_STRATEGY_OTHER, DEFAULT_STRATEGY_SYNCING, + DEFAULT_CONTEXT_SYNCING, DEFAULT_CONTEXT_IMPORT_BLOCK, DEFAULT_CONTEXT_IMPORT_BLOCK_VALIDATOR, + DEFAULT_CONTEXT_BLOCK_CONSTRUCTION, DEFAULT_CONTEXT_OFFCHAIN_WORKER, DEFAULT_CONTEXT_OTHER, }; use crate::params::DatabaseParams; use crate::params::PruningParams; -use sc_client_api::execution_extensions::ExecutionStrategies; +use sc_client_api::execution_extensions::ExecutionConfigs; use structopt::StructOpt; use std::path::PathBuf; @@ -99,32 +101,53 @@ impl ImportParams { } /// Get execution strategies for the parameters - pub fn execution_strategies(&self, is_dev: bool, is_validator: bool) -> ExecutionStrategies { + pub fn execution_configs(&self, is_dev: bool, is_validator: bool) -> ExecutionConfigs { + use sc_client_api::ExecutionConfig; let exec = &self.execution_strategies; - let exec_all_or = |strat: Option, default: ExecutionStrategy| { - let default = if is_dev { - ExecutionStrategy::Native - } else { - default - }; - - exec.execution.unwrap_or_else(|| strat.unwrap_or(default)).into() + let exec_all_or = |start: Option, default: ExecutionStrategy| { + let default = if is_dev { ExecutionStrategy::Native } else { default }; + + exec.execution.unwrap_or_else(|| start.unwrap_or(default)).into() }; let default_execution_import_block = if is_validator { - DEFAULT_EXECUTION_IMPORT_BLOCK_VALIDATOR + DEFAULT_STRATEGY_IMPORT_BLOCK_VALIDATOR + } else { + DEFAULT_STRATEGY_IMPORT_BLOCK + }; + let default_context_import_block = if is_validator { + DEFAULT_CONTEXT_IMPORT_BLOCK_VALIDATOR } else { - DEFAULT_EXECUTION_IMPORT_BLOCK + DEFAULT_CONTEXT_IMPORT_BLOCK }; - ExecutionStrategies { - syncing: exec_all_or(exec.execution_syncing, DEFAULT_EXECUTION_SYNCING), - importing: exec_all_or(exec.execution_import_block, default_execution_import_block), - block_construction: - exec_all_or(exec.execution_block_construction, DEFAULT_EXECUTION_BLOCK_CONSTRUCTION), - offchain_worker: - exec_all_or(exec.execution_offchain_worker, DEFAULT_EXECUTION_OFFCHAIN_WORKER), - other: exec_all_or(exec.execution_other, DEFAULT_EXECUTION_OTHER), + ExecutionConfigs { + syncing: ExecutionConfig { + strategy: exec_all_or(exec.execution_syncing, DEFAULT_STRATEGY_SYNCING), + context: DEFAULT_CONTEXT_SYNCING, + }, + importing: ExecutionConfig { + strategy: exec_all_or(exec.execution_import_block, default_execution_import_block), + context: default_context_import_block, + }, + block_construction: ExecutionConfig { + strategy: exec_all_or( + exec.execution_block_construction, + DEFAULT_STRATEGY_BLOCK_CONSTRUCTION, + ), + context: DEFAULT_CONTEXT_BLOCK_CONSTRUCTION, + }, + offchain_worker: ExecutionConfig { + strategy: exec_all_or( + exec.execution_offchain_worker, + DEFAULT_STRATEGY_OFFCHAIN_WORKER, + ), + context: DEFAULT_CONTEXT_OFFCHAIN_WORKER, + }, + other: ExecutionConfig { + strategy: exec_all_or(exec.execution_other, DEFAULT_STRATEGY_OTHER), + context: DEFAULT_CONTEXT_OTHER, + }, } } } diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 672b08d0b7142..9bd14a495b3bf 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -475,8 +475,10 @@ pub trait GenesisAuthoritySetProvider { fn get(&self) -> Result; } -impl GenesisAuthoritySetProvider for Arc> - where E: CallExecutor, +impl GenesisAuthoritySetProvider + for Arc> +where + E: CallExecutor, { fn get(&self) -> Result { // This implementation uses the Grandpa runtime API instead of reading directly from the @@ -487,7 +489,7 @@ impl GenesisAuthoritySetProvider for Arc Clone for GenesisCallExecutor { } } -impl CallExecutor for - GenesisCallExecutor - where - Block: BlockT, - B: RemoteBackend, - Local: CallExecutor, +impl CallExecutor for GenesisCallExecutor +where + Block: BlockT, + B: RemoteBackend, + Local: CallExecutor, { type Error = ClientError; @@ -87,7 +86,7 @@ impl CallExecutor for id: &BlockId, method: &str, call_data: &[u8], - strategy: ExecutionStrategy, + strategy: ExecutionConfig, extensions: Option, ) -> ClientResult> { match self.backend.is_local_state_available(id) { @@ -101,7 +100,7 @@ impl CallExecutor for IB: Fn() -> ClientResult<()>, EM: Fn( Result, Self::Error>, - Result, Self::Error> + Result, Self::Error>, ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, NC: FnOnce() -> result::Result + UnwindSafe, @@ -140,7 +139,7 @@ impl CallExecutor for changes, None, initialize_block, - ExecutionManager::NativeWhenPossible, + ExecutionConfig::new_consensus(ExecutionStrategy::NativeWhenPossible).get_manager(), // TODO: why are we not using the given manager. native_call, recorder, extensions, @@ -256,12 +255,12 @@ pub fn check_execution_proof_with_make_header( remote_proof: StorageProof, make_next_header: MakeNextHeader, ) -> ClientResult> - where - E: CodeExecutor + Clone + 'static, - H: Hasher, - Header: HeaderT, - H::Out: Ord + codec::Codec + 'static, - MakeNextHeader: Fn(&Header) -> Header, +where + E: CodeExecutor + Clone + 'static, + H: Hasher, + Header: HeaderT, + H::Out: Ord + codec::Codec + 'static, + MakeNextHeader: Fn(&Header) -> Header, { let local_state_root = request.header.state_root(); let root: H::Out = convert_hash(&local_state_root); @@ -273,7 +272,8 @@ pub fn check_execution_proof_with_make_header( // TODO: Remove when solved: https://github.com/paritytech/substrate/issues/5047 let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&trie_backend); - let runtime_code = backend_runtime_code.runtime_code() + let runtime_code = backend_runtime_code + .runtime_code(sp_state_machine::ExecutionContext::Consensus) // TODO: ? which one is it, and I should get this maybe from `E`. .map_err(|_e| ClientError::RuntimeCodeMissing)?; execution_proof_check_on_trie_backend::( diff --git a/client/rpc/src/state/state_full.rs b/client/rpc/src/state/state_full.rs index bea7ddfbb3b76..9f2ca9030d04b 100644 --- a/client/rpc/src/state/state_full.rs +++ b/client/rpc/src/state/state_full.rs @@ -71,11 +71,13 @@ pub struct FullState { } impl FullState - where - BE: Backend, - Client: StorageProvider + HeaderBackend + BlockBackend - + HeaderMetadata, - Block: BlockT + 'static, +where + BE: Backend, + Client: StorageProvider + + HeaderBackend + + BlockBackend + + HeaderMetadata, + Block: BlockT + 'static, { /// Create new state API backend for full nodes. pub fn new(client: Arc, subscriptions: SubscriptionManager) -> Self { @@ -222,15 +224,22 @@ impl FullState } } -impl StateBackend for FullState where +impl StateBackend for FullState +where Block: BlockT + 'static, BE: Backend + 'static, - Client: ExecutorProvider + StorageProvider - + ProofProvider + HeaderBackend - + HeaderMetadata + BlockchainEvents - + CallApiAt + ProvideRuntimeApi + Client: ExecutorProvider + + StorageProvider + + ProofProvider + + HeaderBackend + + HeaderMetadata + + BlockchainEvents + + CallApiAt + + ProvideRuntimeApi + BlockBackend - + Send + Sync + 'static, + + Send + + Sync + + 'static, Client::Api: Metadata, { fn call( @@ -247,7 +256,7 @@ impl StateBackend for FullState( pub fn new_full_parts( config: &Configuration, telemetry: Option, -) -> Result, Error> where +) -> Result, Error> +where TBl: BlockT, TExecDisp: NativeExecutionDispatch + 'static, { @@ -340,11 +341,10 @@ pub fn new_full_parts( transaction_storage: config.transaction_storage.clone(), }; - let backend = new_db_backend(db_config)?; let extensions = sc_client_api::execution_extensions::ExecutionExtensions::new( - config.execution_strategies.clone(), + config.execution_configs.clone(), Some(keystore_container.sync_keystore()), sc_offchain::OffchainDb::factory_from_backend(&*backend), ); diff --git a/client/service/src/client/call_executor.rs b/client/service/src/client/call_executor.rs index b48ff028cda0e..a8879a423d64d 100644 --- a/client/service/src/client/call_executor.rs +++ b/client/service/src/client/call_executor.rs @@ -19,16 +19,18 @@ use std::{sync::Arc, panic::UnwindSafe, result, cell::RefCell}; use codec::{Encode, Decode}; use sp_runtime::{ - generic::BlockId, traits::{Block as BlockT, HashFor, NumberFor}, + generic::BlockId, + traits::{Block as BlockT, HashFor, NumberFor}, }; use sp_state_machine::{ - self, OverlayedChanges, Ext, ExecutionManager, StateMachine, ExecutionStrategy, - backend::Backend as _, StorageProof, + self, OverlayedChanges, Ext, ExecutionManager, StateMachine, backend::Backend as _, + StorageProof, }; use sc_executor::{RuntimeVersion, RuntimeInfo, NativeVersion}; use sp_externalities::Extensions; use sp_core::{ - NativeOrEncoded, NeverNativeValue, traits::{CodeExecutor, SpawnNamed, RuntimeCode}, + NativeOrEncoded, NeverNativeValue, + traits::{CodeExecutor, SpawnNamed, RuntimeCode}, }; use sp_api::{ProofRecorder, InitializeBlock, StorageTransactionCache}; use sc_client_api::{backend, call_executor::CallExecutor}; @@ -131,7 +133,7 @@ where id: &BlockId, method: &str, call_data: &[u8], - strategy: ExecutionStrategy, + strategy: sp_state_machine::ExecutionConfig, extensions: Option, ) -> sp_blockchain::Result> { let mut changes = OverlayedChanges::default(); @@ -140,7 +142,8 @@ where )?; let state = self.backend.state_at(*id)?; let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); - let runtime_code = state_runtime_code.runtime_code() + let runtime_code = state_runtime_code + .runtime_code(strategy.context) .map_err(sp_blockchain::Error::RuntimeCode)?; let runtime_code = self.check_override(runtime_code, id)?; @@ -167,7 +170,7 @@ where IB: Fn() -> sp_blockchain::Result<()>, EM: Fn( Result, Self::Error>, - Result, Self::Error> + Result, Self::Error>, ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, NC: FnOnce() -> result::Result + UnwindSafe, @@ -178,9 +181,7 @@ where method: &str, call_data: &[u8], changes: &RefCell, - storage_transaction_cache: Option<&RefCell< - StorageTransactionCache - >>, + storage_transaction_cache: Option<&RefCell>>, initialize_block: InitializeBlock<'a, Block>, execution_manager: ExecutionManager, native_call: Option, @@ -200,7 +201,6 @@ where let mut storage_transaction_cache = storage_transaction_cache.map(|c| c.borrow_mut()); let mut state = self.backend.state_at(*at)?; - let changes = &mut *changes.borrow_mut(); match recorder { @@ -213,7 +213,7 @@ where let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&trie_state); // It is important to extract the runtime code here before we create the proof // recorder. - let runtime_code = state_runtime_code.runtime_code() + let runtime_code = state_runtime_code.runtime_code(execution_manager.context) .map_err(sp_blockchain::Error::RuntimeCode)?; let runtime_code = self.check_override(runtime_code, at)?; @@ -242,7 +242,7 @@ where }, None => { let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); - let runtime_code = state_runtime_code.runtime_code() + let runtime_code = state_runtime_code.runtime_code(execution_manager.context) .map_err(sp_blockchain::Error::RuntimeCode)?; let runtime_code = self.check_override(runtime_code, at)?; @@ -281,9 +281,11 @@ where None, ); let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); - let runtime_code = state_runtime_code.runtime_code() + let runtime_code = state_runtime_code + .runtime_code(sp_state_machine::ExecutionContext::Consensus) .map_err(sp_blockchain::Error::RuntimeCode)?; - self.executor.runtime_version(&mut ext, &runtime_code) + self.executor + .runtime_version(&mut ext, &runtime_code) .map_err(|e| sp_blockchain::Error::VersionInvalid(format!("{:?}", e)).into()) } @@ -292,10 +294,11 @@ where trie_state: &sp_state_machine::TrieBackend>, overlay: &mut OverlayedChanges, method: &str, - call_data: &[u8] + call_data: &[u8], ) -> Result<(Vec, StorageProof), sp_blockchain::Error> { let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(trie_state); - let runtime_code = state_runtime_code.runtime_code() + let runtime_code = state_runtime_code + .runtime_code(sp_state_machine::ExecutionContext::Consensus) .map_err(sp_blockchain::Error::RuntimeCode)?; sp_state_machine::prove_execution_on_trie_backend::<_, _, NumberFor, _, _>( trie_state, diff --git a/client/service/src/config.rs b/client/service/src/config.rs index f82a877545e8c..b798261fcdb04 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -19,13 +19,15 @@ //! Service configuration. pub use sc_client_db::{ - Database, PruningMode, DatabaseSettingsSrc as DatabaseConfig, - KeepBlocks, TransactionStorageMode + Database, PruningMode, DatabaseSettingsSrc as DatabaseConfig, KeepBlocks, + TransactionStorageMode, }; pub use sc_network::Multiaddr; -pub use sc_network::config::{ExtTransport, MultiaddrWithPeerId, NetworkConfiguration, Role, NodeKeyConfig}; +pub use sc_network::config::{ + ExtTransport, MultiaddrWithPeerId, NetworkConfiguration, Role, NodeKeyConfig, +}; pub use sc_executor::WasmExecutionMethod; -use sc_client_api::execution_extensions::ExecutionStrategies; +use sc_client_api::execution_extensions::ExecutionConfigs; use std::{io, future::Future, path::{PathBuf, Path}, pin::Pin, net::SocketAddr, sync::Arc}; pub use sc_transaction_pool::txpool::Options as TransactionPoolOptions; @@ -76,7 +78,7 @@ pub struct Configuration { /// disable overrides (default). pub wasm_runtime_overrides: Option, /// Execution strategies. - pub execution_strategies: ExecutionStrategies, + pub execution_configs: ExecutionConfigs, /// RPC over HTTP binding address. `None` if disabled. pub rpc_http: Option, /// RPC over Websockets binding address. `None` if disabled. @@ -106,7 +108,8 @@ pub struct Configuration { pub disable_grandpa: bool, /// Development key seed. /// - /// When running in development mode, the seed will be used to generate authority keys by the keystore. + /// When running in development mode, the seed will be used to generate authority keys by the + /// keystore. /// /// Should only be set when `node` is running development mode. pub dev_key_seed: Option, diff --git a/client/service/test/src/client/light.rs b/client/service/test/src/client/light.rs index a183cbce62bdb..52a82c4310a35 100644 --- a/client/service/test/src/client/light.rs +++ b/client/service/test/src/client/light.rs @@ -202,7 +202,7 @@ impl CallExecutor for DummyCallExecutor { _id: &BlockId, _method: &str, _call_data: &[u8], - _strategy: ExecutionStrategy, + _strategy: Execution, _extensions: Option, ) -> Result, ClientError> { Ok(vec![42]) diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index a80c53a8c21c5..e24d41c116790 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -208,8 +208,7 @@ fn node_config, base_port: u16, root: &TempDir, -) -> Configuration -{ +) -> Configuration { let root = root.path().join(format!("node-{}", index)); let mut network_config = NetworkConfiguration::new( @@ -257,7 +256,7 @@ fn node_config, I: Instance = DefaultInstance> for enum Call where origin: T::Origin { - fn on_initialize(n: T::BlockNumber) -> Weight { + fn on_initialize(_n: T::BlockNumber) -> Weight { use primitives::LeafDataProvider; let leaves = Self::mmr_leaves(); let peaks_before = mmr::utils::NodesUtils::new(leaves).number_of_peaks(); diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 8595b94c08deb..2c6f83dea3025 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -27,8 +27,8 @@ //! The System pallet defines the core data types used in a Substrate runtime. //! It also provides several utility functions (see [`Pallet`]) for other FRAME pallets. //! -//! In addition, it manages the storage items for extrinsics data, indexes, event records, and digest items, -//! among other things that support the execution of the current block. +//! In addition, it manages the storage items for extrinsics data, indexes, event records, and +//! digest items, among other things that support the execution of the current block. //! //! It also handles low-level tasks like depositing logs, basic set up and take down of //! temporary storage entries, and access to previous block hashes. @@ -54,10 +54,10 @@ //! - [`CheckEra`]: Checks the era of the transaction. Contains a single payload of type `Era`. //! - [`CheckGenesis`]: Checks the provided genesis hash of the transaction. Must be a part of the //! signed payload of the transaction. -//! - [`CheckSpecVersion`]: Checks that the runtime version is the same as the one used to sign the -//! transaction. -//! - [`CheckTxVersion`]: Checks that the transaction version is the same as the one used to sign the -//! transaction. +//! - [`CheckSpecVersion`]: Checks that the runtime version is the same as the one used to sign +//! the transaction. +//! - [`CheckTxVersion`]: Checks that the transaction version is the same as the one used to sign +//! the transaction. //! //! Lookup the runtime aggregator file (e.g. `node/runtime`) to see the full list of signed //! extensions included in a chain. @@ -317,15 +317,11 @@ pub mod pallet { } /// Set the number of pages in the WebAssembly environment's heap. - /// - /// # - /// - `O(1)` - /// - 1 storage write. - /// - Base Weight: 1.405 µs - /// - 1 write to HEAP_PAGES - /// # #[pallet::weight((T::SystemWeightInfo::set_heap_pages(), DispatchClass::Operational))] - pub(crate) fn set_heap_pages(origin: OriginFor, pages: u64) -> DispatchResultWithPostInfo { + pub(crate) fn set_heap_pages( + origin: OriginFor, + pages: u64, + ) -> DispatchResultWithPostInfo { ensure_root(origin)?; storage::unhashed::put_raw(well_known_keys::HEAP_PAGES, &pages.encode()); Ok(().into()) @@ -467,11 +463,6 @@ pub mod pallet { } /// Make some on-chain remark and emit event. - /// - /// # - /// - `O(b)` where b is the length of the remark. - /// - 1 event. - /// # #[pallet::weight(T::SystemWeightInfo::remark_with_event(remark.len() as u32))] pub(crate) fn remark_with_event(origin: OriginFor, remark: Vec) -> DispatchResultWithPostInfo { let who = ensure_signed(origin)?; @@ -479,6 +470,17 @@ pub mod pallet { Self::deposit_event(Event::Remarked(who, hash)); Ok(().into()) } + + /// Set the number of pages in the WebAssembly environment's heap, in offchain context. + #[pallet::weight((T::SystemWeightInfo::set_heap_pages(), DispatchClass::Operational))] + pub(crate) fn set_heap_pages_offchain( + origin: OriginFor, + pages: u64, + ) -> DispatchResultWithPostInfo { + ensure_root(origin)?; + storage::unhashed::put_raw(well_known_keys::OFFCHAIN_HEAP_PAGES, &pages.encode()); + Ok(().into()) + } } /// Event for the System pallet. diff --git a/primitives/state-machine/src/backend.rs b/primitives/state-machine/src/backend.rs index 1a8892f8dd141..a553dc8113f7a 100644 --- a/primitives/state-machine/src/backend.rs +++ b/primitives/state-machine/src/backend.rs @@ -399,7 +399,10 @@ impl<'a, B: Backend, H: Hasher> sp_core::traits::FetchRuntimeCode for } #[cfg(feature = "std")] -impl<'a, B: Backend, H: Hasher> BackendRuntimeCode<'a, B, H> where H::Out: Encode { +impl<'a, B: Backend, H: Hasher> BackendRuntimeCode<'a, B, H> +where + H::Out: Encode, +{ /// Create a new instance. pub fn new(backend: &'a B) -> Self { Self { @@ -409,13 +412,25 @@ impl<'a, B: Backend, H: Hasher> BackendRuntimeCode<'a, B, H> where H::Out: En } /// Return the [`RuntimeCode`] build from the wrapped `backend`. - pub fn runtime_code(&self) -> Result { - let hash = self.backend.storage_hash(well_known_keys::CODE) + pub fn runtime_code( + &self, + context: crate::ExecutionContext, + ) -> Result { + let hash = self + .backend + .storage_hash(well_known_keys::CODE) .ok() .flatten() .ok_or("`:code` hash not found")? .encode(); - let heap_pages = self.backend.storage(well_known_keys::HEAP_PAGES) + + let heap_pages_key = match context { + ExecutionContext::Consensus => well_known_keys::HEAP_PAGES, + ExecutionContext::Offchain => well_known_keys::OFFCHAIN_HEAP_PAGES, + }; + let heap_pages = self + .backend + .storage(heap_pages_key) .ok() .flatten() .and_then(|d| Decode::decode(&mut &d[..]).ok()); diff --git a/primitives/state-machine/src/lib.rs b/primitives/state-machine/src/lib.rs index 479184b4b9905..02e2c545ff0f5 100644 --- a/primitives/state-machine/src/lib.rs +++ b/primitives/state-machine/src/lib.rs @@ -201,6 +201,28 @@ mod execution { /// Trie backend with in-memory storage. pub type InMemoryBackend = TrieBackend, H>; + /// Configurations of an execution. + /// + /// Combination of a strategy, and a context. + #[derive(Copy, Clone, Eq, PartialEq, Debug)] + pub struct ExecutionConfig { + /// The strategy of the execution's runtime. + pub strategy: ExecutionStrategy, + /// The context at which the aforementioned strategy is being used. + pub context: ExecutionContext, + } + + /// The context of the execution. + #[derive(Copy, Clone, Eq, PartialEq, Debug)] + pub enum ExecutionContext { + /// Consensus critical task. Resources should be sufficient, but not not more, and trust + /// levels should be high. The need for determinism is absolute. + Consensus, + /// Offchain operations that do not affect the chain state directly. Can be used for + /// offchain worker threads, and other utility tools. + Offchain, + } + /// Strategy for executing a call into the runtime. #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum ExecutionStrategy { @@ -209,94 +231,168 @@ mod execution { NativeWhenPossible, /// Use the given wasm module. AlwaysWasm, - /// Run with both the wasm and the native variant (if compatible). Report any discrepancy as an error. + /// Run with both the wasm and the native variant (if compatible). Report any discrepancy + /// as an error. Both, /// First native, then if that fails or is not possible, wasm. NativeElseWasm, } + impl ExecutionStrategy { + /// Consumes self and returned the corresponding [`ExecutionConfig`], when executed in a + /// consensus context. + /// + /// This is merely a syntactic sugar around [`ExecutionConfig::new_consensus`]. + pub fn in_consensus(self) -> ExecutionConfig { + ExecutionConfig::new_consensus(self) + } + + /// Consumes self and returned the corresponding [`ExecutionConfig`], when executed in a + /// offchain context. + /// + /// This is merely a syntactic sugar around [`ExecutionConfig::new_offchain`]. + pub fn in_offchain(self) -> ExecutionConfig { + ExecutionConfig::new_offchain(self) + } + } + + impl ExecutionConfig { + /// Build a new [`ExecutionConfig`] with the given strategy and + /// [`ExecutionContext::Consensus`]. + pub fn new_consensus(strategy: ExecutionStrategy) -> Self { + Self { strategy, context: ExecutionContext::Consensus } + } + + /// Build a new [`ExecutionConfig`] with the given strategy and + /// [`ExecutionContext::Offchain`]. + pub fn new_offchain(strategy: ExecutionStrategy) -> Self { + Self { strategy, context: ExecutionContext::Offchain } + } + + /// Gets the corresponding manager for the execution config. + pub fn get_manager( + self, + ) -> ExecutionManager> { + let failure_handler: DefaultHandler = |wasm_result, native_result| { + warn!( + "Consensus error between wasm {:?} and native {:?}. Using wasm.", + wasm_result, native_result, + ); + warn!(" Native result {:?}", native_result); + warn!(" Wasm result {:?}", wasm_result); + wasm_result + }; + + let strategy = match self.strategy { + ExecutionStrategy::AlwaysWasm => { + ExecutionStrategyWithHandler::AlwaysWasm(BackendTrustLevel::Trusted) + } + ExecutionStrategy::NativeWhenPossible => { + ExecutionStrategyWithHandler::NativeWhenPossible + } + ExecutionStrategy::NativeElseWasm => ExecutionStrategyWithHandler::NativeElseWasm, + ExecutionStrategy::Both => ExecutionStrategyWithHandler::Both(failure_handler), + }; + let context = self.context; + + ExecutionManager { strategy, context } + } + } + /// Storage backend trust level. #[derive(Debug, Clone)] pub enum BackendTrustLevel { /// Panics from trusted backends are considered justified, and never caught. Trusted, - /// Panics from untrusted backend are caught and interpreted as runtime error. - /// Untrusted backend may be missing some parts of the trie, so panics are not considered - /// fatal. + /// Panics from untrusted backend are caught and interpreted as runtime error. Untrusted + /// backend may be missing some parts of the trie, so panics are not considered fatal. Untrusted, } - /// Like `ExecutionStrategy` only it also stores a handler in case of consensus failure. + /// Like `ExecutionStrategy`, only it also stores a handler in case of consensus failure. #[derive(Clone)] - pub enum ExecutionManager { + pub enum ExecutionStrategyWithHandler { /// Execute with the native equivalent if it is compatible with the given wasm module; /// otherwise fall back to the wasm. NativeWhenPossible, - /// Use the given wasm module. The backend on which code is executed code could be - /// trusted to provide all storage or not (i.e. the light client cannot be trusted to provide - /// for all storage queries since the storage entries it has come from an external node). + /// Use the given wasm module. The backend on which code is executed code could be trusted + /// to provide all storage or not (i.e. the light client cannot be trusted to provide for + /// all storage queries since the storage entries it has come from an external node). AlwaysWasm(BackendTrustLevel), - /// Run with both the wasm and the native variant (if compatible). Call `F` in the case of any discrepancy. + /// Run with both the wasm and the native variant (if compatible). Call `F` in the case of + /// any discrepancy. Both(F), /// First native, then if that fails or is not possible, wasm. NativeElseWasm, } - impl<'a, F> From<&'a ExecutionManager> for ExecutionStrategy { - fn from(s: &'a ExecutionManager) -> Self { - match *s { - ExecutionManager::NativeWhenPossible => ExecutionStrategy::NativeWhenPossible, - ExecutionManager::AlwaysWasm(_) => ExecutionStrategy::AlwaysWasm, - ExecutionManager::NativeElseWasm => ExecutionStrategy::NativeElseWasm, - ExecutionManager::Both(_) => ExecutionStrategy::Both, - } + /// The execution manager. + /// + /// Same as [`ExecutionConfig`], but the inner `strategy` contains failure handler as well. + #[derive(Clone)] + pub struct ExecutionManager { + /// The strategy of the execution's runtime. + pub strategy: ExecutionStrategyWithHandler, + /// The context at which the aforementioned strategy is being used. + pub context: ExecutionContext, + } + + #[cfg(test)] + impl ExecutionManager { + /// Create a new instance of [`Self`] from the given strategy for testing. + pub(crate) fn new(strategy: ExecutionStrategyWithHandler) -> Self { + Self { strategy, context: ExecutionContext::Offchain } } } - impl ExecutionStrategy { - /// Gets the corresponding manager for the execution strategy. - pub fn get_manager( - self, - ) -> ExecutionManager> { - match self { - ExecutionStrategy::AlwaysWasm => ExecutionManager::AlwaysWasm(BackendTrustLevel::Trusted), - ExecutionStrategy::NativeWhenPossible => ExecutionManager::NativeWhenPossible, - ExecutionStrategy::NativeElseWasm => ExecutionManager::NativeElseWasm, - ExecutionStrategy::Both => ExecutionManager::Both(|wasm_result, native_result| { - warn!( - "Consensus error between wasm {:?} and native {:?}. Using wasm.", - wasm_result, - native_result, - ); - warn!(" Native result {:?}", native_result); - warn!(" Wasm result {:?}", wasm_result); - wasm_result - }), - } + impl<'a, F> From<&'a ExecutionManager> for ExecutionConfig { + fn from(s: &'a ExecutionManager) -> Self { + let strategy = match s.strategy { + ExecutionStrategyWithHandler::NativeWhenPossible => { + ExecutionStrategy::NativeWhenPossible + } + ExecutionStrategyWithHandler::AlwaysWasm(_) => ExecutionStrategy::AlwaysWasm, + ExecutionStrategyWithHandler::NativeElseWasm => ExecutionStrategy::NativeElseWasm, + ExecutionStrategyWithHandler::Both(_) => ExecutionStrategy::Both, + }; + let context = s.context; + + Self { strategy, context } } } - /// Evaluate to ExecutionManager::NativeElseWasm, without having to figure out the type. + /// Helper to create the [`ExecutionStrategyWithHandler::NativeElseWasm`] without naming types. pub fn native_else_wasm() -> ExecutionManager> { - ExecutionManager::NativeElseWasm + ExecutionManager { + strategy: ExecutionStrategyWithHandler::NativeElseWasm, + context: ExecutionContext::Consensus, + } } - /// Evaluate to ExecutionManager::AlwaysWasm with trusted backend, without having to figure out the type. + /// Helper to create the [`ExecutionStrategyWithHandler::AlwaysWasm(Trusted)`] without naming + /// types. fn always_wasm() -> ExecutionManager> { - ExecutionManager::AlwaysWasm(BackendTrustLevel::Trusted) + ExecutionManager { + strategy: ExecutionStrategyWithHandler::AlwaysWasm(BackendTrustLevel::Trusted), + context: ExecutionContext::Consensus, + } } - /// Evaluate ExecutionManager::AlwaysWasm with untrusted backend, without having to figure out the type. + /// Helper to create the [`ExecutionStrategyWithHandler::AlwaysWasm(Untrsuted)`] without naming + /// types. fn always_untrusted_wasm() -> ExecutionManager> { - ExecutionManager::AlwaysWasm(BackendTrustLevel::Untrusted) + ExecutionManager { + strategy: ExecutionStrategyWithHandler::AlwaysWasm(BackendTrustLevel::Untrusted), + context: ExecutionContext::Consensus, + } } /// The substrate state machine. pub struct StateMachine<'a, B, H, N, Exec> - where - H: Hasher, - B: Backend, - N: ChangesTrieBlockNumber, + where + H: Hasher, + B: Backend, + N: ChangesTrieBlockNumber, { backend: &'a B, exec: &'a Exec, @@ -310,7 +406,8 @@ mod execution { stats: StateMachineStats, } - impl<'a, B, H, N, Exec> Drop for StateMachine<'a, B, H, N, Exec> where + impl<'a, B, H, N, Exec> Drop for StateMachine<'a, B, H, N, Exec> + where H: Hasher, B: Backend, N: ChangesTrieBlockNumber, @@ -320,7 +417,8 @@ mod execution { } } - impl<'a, B, H, N, Exec> StateMachine<'a, B, H, N, Exec> where + impl<'a, B, H, N, Exec> StateMachine<'a, B, H, N, Exec> + where H: Hasher, H::Out: Ord + 'static + codec::Codec, Exec: CodeExecutor + Clone + 'static, @@ -377,13 +475,14 @@ mod execution { /// blocks (e.g. a transaction at a time), ensure a different method is used. /// /// Returns the SCALE encoded result of the executed function. - pub fn execute(&mut self, strategy: ExecutionStrategy) -> Result, Box> { - // We are not giving a native call and thus we are sure that the result can never be a native - // value. + pub fn execute(&mut self, config: ExecutionConfig) -> Result, Box> { + // We are not giving a native call and thus we are sure that the result can never be a + // native value. self.execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>( - strategy.get_manager(), + config.get_manager(), None, - ).map(NativeOrEncoded::into_encoded) + ) + .map(NativeOrEncoded::into_encoded) } fn execute_aux( @@ -523,40 +622,36 @@ mod execution { manager: ExecutionManager, mut native_call: Option, ) -> Result, Box> - where - R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result> + UnwindSafe, - Handler: FnOnce( - CallResult, - CallResult, - ) -> CallResult + where + R: Decode + Encode + PartialEq, + NC: FnOnce() -> result::Result> + UnwindSafe, + Handler: FnOnce( + CallResult, + CallResult, + ) -> CallResult { let changes_tries_enabled = self.changes_trie_state.is_some(); self.overlay.set_collect_extrinsics(changes_tries_enabled); let result = { - match manager { - ExecutionManager::Both(on_consensus_failure) => { - self.execute_call_with_both_strategy( - native_call.take(), - on_consensus_failure, - ) - }, - ExecutionManager::NativeElseWasm => { - self.execute_call_with_native_else_wasm_strategy( - native_call.take(), - ) - }, - ExecutionManager::AlwaysWasm(trust_level) => { + match manager.strategy { + ExecutionStrategyWithHandler::Both(on_consensus_failure) => self + .execute_call_with_both_strategy(native_call.take(), on_consensus_failure), + ExecutionStrategyWithHandler::NativeElseWasm => { + self.execute_call_with_native_else_wasm_strategy(native_call.take()) + } + ExecutionStrategyWithHandler::AlwaysWasm(trust_level) => { let _abort_guard = match trust_level { BackendTrustLevel::Trusted => None, - BackendTrustLevel::Untrusted => Some(sp_panic_handler::AbortGuard::never_abort()), + BackendTrustLevel::Untrusted => { + Some(sp_panic_handler::AbortGuard::never_abort()) + } }; self.execute_aux(false, native_call).0 - }, - ExecutionManager::NativeWhenPossible => { + } + ExecutionStrategyWithHandler::NativeWhenPossible => { self.execute_aux(true, native_call).0 - }, + } } }; @@ -977,12 +1072,11 @@ mod tests { ); assert_eq!( - state_machine.execute(ExecutionStrategy::NativeWhenPossible).unwrap(), + state_machine.execute(ExecutionStrategy::NativeWhenPossible.in_consensus()).unwrap(), vec![66], ); } - #[test] fn execute_works_with_native_else_wasm() { let backend = trie_backend::tests::test_trie(); @@ -1006,7 +1100,10 @@ mod tests { TaskExecutor::new(), ); - assert_eq!(state_machine.execute(ExecutionStrategy::NativeElseWasm).unwrap(), vec![66]); + assert_eq!( + state_machine.execute(ExecutionStrategy::NativeElseWasm.in_consensus()).unwrap(), + vec![66] + ); } #[test] @@ -1033,15 +1130,15 @@ mod tests { TaskExecutor::new(), ); - assert!( - state_machine.execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>( - ExecutionManager::Both(|we, _ne| { + assert!(state_machine + .execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>( + ExecutionManager::new(ExecutionStrategyWithHandler::Both(|we, _ne| { consensus_failed = true; we - }), + })), None, - ).is_err() - ); + ) + .is_err()); assert!(consensus_failed); } @@ -1557,16 +1654,19 @@ mod tests { ); let run_state_machine = |state_machine: &mut StateMachine<_, _, _, _>| { - state_machine.execute_using_consensus_failure_handler:: _, _, _>( - ExecutionManager::NativeWhenPossible, - Some(|| { - sp_externalities::with_externalities(|mut ext| { - ext.register_extension(DummyExt(2)).unwrap(); - }).unwrap(); - - Ok(()) - }), - ).unwrap(); + state_machine + .execute_using_consensus_failure_handler:: _, _, _>( + ExecutionManager::new(ExecutionStrategyWithHandler::NativeWhenPossible), + Some(|| { + sp_externalities::with_externalities(|mut ext| { + ext.register_extension(DummyExt(2)).unwrap(); + }) + .unwrap(); + + Ok(()) + }), + ) + .unwrap(); }; run_state_machine(&mut state_machine); diff --git a/primitives/storage/src/lib.rs b/primitives/storage/src/lib.rs index ced8d8c02a80b..2ffa18e439abf 100644 --- a/primitives/storage/src/lib.rs +++ b/primitives/storage/src/lib.rs @@ -163,8 +163,15 @@ pub mod well_known_keys { /// Number of wasm linear memory pages required for execution of the runtime. /// /// The type of this value is encoded `u64`. + /// + /// Note that this value is used for consensus-related runtime operations, such as block import. pub const HEAP_PAGES: &'static [u8] = b":heappages"; + /// Number of wasm linear memory pages given to offchain workers. + /// + /// The type of this value is encoded `u64`. + pub const OFFCHAIN_HEAP_PAGES: &'static [u8] = b":offchain_heappages"; + /// Current extrinsic index (u32) is stored under this key. pub const EXTRINSIC_INDEX: &'static [u8] = b":extrinsic_index"; @@ -194,7 +201,6 @@ pub mod well_known_keys { CHILD_STORAGE_KEY_PREFIX.starts_with(key) } } - } /// Information related to a child state. diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index d8cc40d5561c1..1ac5a0064954d 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -22,7 +22,7 @@ pub mod client_ext; pub use sc_client_api::{ - execution_extensions::{ExecutionStrategies, ExecutionExtensions}, + execution_extensions::{ExecutionConfigs, ExecutionExtensions}, ForkBlocks, BadBlocks, }; pub use sc_client_db::{Backend, self}; @@ -35,7 +35,7 @@ pub use sp_keyring::{ }; pub use sp_keystore::{SyncCryptoStorePtr, SyncCryptoStore}; pub use sp_runtime::{Storage, StorageChild}; -pub use sp_state_machine::ExecutionStrategy; +pub use sp_state_machine::{ExecutionStrategy, ExecutionConfig}; pub use sc_service::{RpcHandlers, RpcSession, client}; pub use self::client_ext::{ClientExt, ClientBlockImportExt}; @@ -69,7 +69,7 @@ impl GenesisInit for () { /// A builder for creating a test client instance. pub struct TestClientBuilder { - execution_strategies: ExecutionStrategies, + execution_configs: ExecutionConfigs, genesis_init: G, /// The key is an unprefixed storage key, this only contains /// default child trie content. @@ -89,7 +89,9 @@ impl Default } } -impl TestClientBuilder, G> { +impl + TestClientBuilder, G> +{ /// Create new `TestClientBuilder` with default backend. pub fn with_default_backend() -> Self { let backend = Arc::new(Backend::new_test(std::u32::MAX, std::u64::MAX)); @@ -103,12 +105,14 @@ impl TestClientBuilder TestClientBuilder { +impl + TestClientBuilder +{ /// Create a new instance of the test client builder. pub fn with_backend(backend: Arc) -> Self { TestClientBuilder { backend, - execution_strategies: ExecutionStrategies::default(), + execution_configs: ExecutionConfigs::default(), child_storage_extension: Default::default(), genesis_init: Default::default(), _executor: Default::default(), @@ -153,16 +157,13 @@ impl TestClientBuilder Self { - self.execution_strategies = ExecutionStrategies { - syncing: execution_strategy, - importing: execution_strategy, - block_construction: execution_strategy, - offchain_worker: execution_strategy, - other: execution_strategy, + pub fn set_execution_strategy(mut self, execution_config: ExecutionConfig) -> Self { + self.execution_configs = ExecutionConfigs { + syncing: execution_config, + importing: execution_config, + block_construction: execution_config, + offchain_worker: execution_config, + other: execution_config, }; self } @@ -188,14 +189,10 @@ impl TestClientBuilder ( - client::Client< - Backend, - Executor, - Block, - RuntimeApi, - >, + client::Client, sc_consensus::LongestChain, - ) where + ) + where Executor: sc_client_api::CallExecutor + 'static, Backend: sc_client_api::backend::Backend, >::OffchainStorage: 'static, @@ -224,7 +221,7 @@ impl TestClientBuilder //! -//! - Migrations -//! - Runtime Upgrades -//! - Pallets and general runtime functionality. +//! - Migrations +//! - Runtime Upgrades +//! - Pallets and general runtime functionality. //! //! This works by running a full node with a Manual Seal-BABE™ hybrid consensus for block authoring. //! //!

Note

-//! The running node has no signature verification, which allows us author extrinsics for any account on chain. -//!
+//! The running node has no signature verification, which allows us author extrinsics for any +//! account on chain.
//!
//! //!

How do I Use this?

@@ -55,7 +55,7 @@ //! use sp_runtime::{traits::IdentifyAccount, MultiSigner, generic::Era}; //! use sc_executor::WasmExecutionMethod; //! use sc_network::{multiaddr, config::TransportConfig}; -//! use sc_client_api::execution_extensions::ExecutionStrategies; +//! use sc_client_api::execution_extensions::ExecutionConfigs; //! use sc_informant::OutputFormat; //! use sp_api::TransactionFor; //! @@ -186,12 +186,12 @@ //! fn simple_balances_test() { //! // given //! let config = NodeConfig { -//! execution_strategies: ExecutionStrategies { -//! syncing: sc_client_api::ExecutionStrategy::NativeWhenPossible, -//! importing: sc_client_api::ExecutionStrategy::NativeWhenPossible, -//! block_construction: sc_client_api::ExecutionStrategy::NativeWhenPossible, -//! offchain_worker: sc_client_api::ExecutionStrategy::NativeWhenPossible, -//! other: sc_client_api::ExecutionStrategy::NativeWhenPossible, +//! execution_configs: ExecutionConfigs { +//! syncing: sc_client_api::ExecutionStrategy::NativeWhenPossible, +//! importing: sc_client_api::ExecutionStrategy::NativeWhenPossible, +//! block_construction: sc_client_api::ExecutionStrategy::NativeWhenPossible, +//! offchain_worker: sc_client_api::ExecutionStrategy::NativeWhenPossible, +//! other: sc_client_api::ExecutionStrategy::NativeWhenPossible, //! }, //! chain_spec: Box::new(development_config()), //! log_targets: vec![], diff --git a/test-utils/test-runner/src/utils.rs b/test-utils/test-runner/src/utils.rs index d8ab3860f28a8..66def5260d9ee 100644 --- a/test-utils/test-runner/src/utils.rs +++ b/test-utils/test-runner/src/utils.rs @@ -26,7 +26,7 @@ use sc_network::{multiaddr, config::{NetworkConfiguration, TransportConfig, Role use sc_informant::OutputFormat; use sc_service::config::KeystoreConfig; use sc_executor::WasmExecutionMethod; -use sc_client_api::execution_extensions::ExecutionStrategies; +use sc_client_api::execution_extensions::ExecutionConfigs; /// Base db path gotten from env pub fn base_path() -> BasePath { @@ -67,7 +67,10 @@ where } /// Produces a default configuration object, suitable for use with most set ups. -pub fn default_config(task_executor: TaskExecutor, mut chain_spec: Box) -> Configuration { +pub fn default_config( + task_executor: TaskExecutor, + mut chain_spec: Box, +) -> Configuration { let base_path = base_path(); let root_path = base_path.path().to_path_buf().join("chains").join(chain_spec.id()); @@ -113,12 +116,12 @@ pub fn default_config(task_executor: TaskExecutor, mut chain_spec: Box, String> as Decode>::decode(&mut &result[..]) - .map_err(|e| format!("Failed to decode benchmark results: {:?}", e))?; + let results = + , String> as Decode>::decode(&mut &result[..]) + .map_err(|e| format!("Failed to decode benchmark results: {:?}", e))?; match results { Ok(batches) => { diff --git a/utils/frame/try-runtime/cli/src/lib.rs b/utils/frame/try-runtime/cli/src/lib.rs index 9e41a3fd87e77..de687172969a2 100644 --- a/utils/frame/try-runtime/cli/src/lib.rs +++ b/utils/frame/try-runtime/cli/src/lib.rs @@ -188,10 +188,10 @@ impl TryRuntimeCmd { &[], ext.extensions, &sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend) - .runtime_code()?, + .runtime_code(sp_state_machine::ExecutionContext::Offchain)?, sp_core::testing::TaskExecutor::new(), ) - .execute(execution.into()) + .execute(sp_state_machine::ExecutionConfig::new_offchain(execution.into())) .map_err(|e| format!("failed to execute 'TryRuntime_on_runtime_upgrade' due to {:?}", e))?; let (weight, total_weight) = <(u64, u64) as Decode>::decode(&mut &*encoded_result) From e70e4dbf7d4edd81303d817579e0f9d799506245 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 28 Jun 2021 09:42:25 +0200 Subject: [PATCH 02/24] Fix all tests --- client/api/src/execution_extensions.rs | 2 +- client/service/src/config.rs | 18 ++++++++++++------ client/service/test/src/client/light.rs | 6 +++--- client/service/test/src/client/mod.rs | 22 ++++++++++++---------- frame/system/src/mock.rs | 7 ++++--- primitives/state-machine/src/lib.rs | 2 +- test-utils/client/src/lib.rs | 13 +++++++------ utils/frame/try-runtime/cli/src/lib.rs | 19 ++++++++++--------- 8 files changed, 50 insertions(+), 39 deletions(-) diff --git a/client/api/src/execution_extensions.rs b/client/api/src/execution_extensions.rs index a8fbfab151cd2..14a23846c631c 100644 --- a/client/api/src/execution_extensions.rs +++ b/client/api/src/execution_extensions.rs @@ -37,7 +37,7 @@ use sp_state_machine::{ExecutionManager, ExecutionStrategy, ExecutionConfig, Def use sp_externalities::Extensions; use parking_lot::RwLock; -/// Execution strategies settings. +/// Execution configurations, per operation type. #[derive(Debug, Clone)] pub struct ExecutionConfigs { /// Execution config used when syncing. diff --git a/client/service/src/config.rs b/client/service/src/config.rs index db091b24c6d43..0002e4f8645bd 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -24,14 +24,20 @@ pub use sc_client_db::{ }; pub use sc_network::Multiaddr; pub use sc_network::config::{ - ExtTransport, MultiaddrWithPeerId, NetworkConfiguration, Role, NodeKeyConfig, - SetConfig, NonDefaultSetConfig, TransportConfig, - RequestResponseConfig, IncomingRequest, OutgoingResponse, + ExtTransport, MultiaddrWithPeerId, NetworkConfiguration, Role, NodeKeyConfig, SetConfig, + NonDefaultSetConfig, TransportConfig, RequestResponseConfig, IncomingRequest, OutgoingResponse, }; pub use sc_executor::WasmExecutionMethod; -pub use sc_client_api::execution_extensions::{ExecutionStrategies, ExecutionStrategy, ExecutionConfigs}; +pub use sc_client_api::execution_extensions::ExecutionConfigs; -use std::{io, future::Future, path::{PathBuf, Path}, pin::Pin, net::SocketAddr, sync::Arc}; +use std::{ + io, + future::Future, + path::{PathBuf, Path}, + pin::Pin, + net::SocketAddr, + sync::Arc, +}; pub use sc_transaction_pool::txpool::Options as TransactionPoolOptions; use sc_chain_spec::ChainSpec; use sp_core::crypto::SecretString; @@ -79,7 +85,7 @@ pub struct Configuration { /// over on-chain runtimes when the spec version matches. Set to `None` to /// disable overrides (default). pub wasm_runtime_overrides: Option, - /// Execution strategies. + /// Execution configurations. pub execution_configs: ExecutionConfigs, /// RPC over HTTP binding address. `None` if disabled. pub rpc_http: Option, diff --git a/client/service/test/src/client/light.rs b/client/service/test/src/client/light.rs index ba3fb892a6db8..ccc333e1a8a4f 100644 --- a/client/service/test/src/client/light.rs +++ b/client/service/test/src/client/light.rs @@ -202,7 +202,7 @@ impl CallExecutor for DummyCallExecutor { _id: &BlockId, _method: &str, _call_data: &[u8], - _strategy: Execution, + _strategy: sp_state_machine::ExecutionConfig, _extensions: Option, ) -> Result, ClientError> { Ok(vec![42]) @@ -423,7 +423,7 @@ fn code_is_executed_at_genesis_only() { &BlockId::Number(0), "test_method", &[], - ExecutionStrategy::NativeElseWasm, + ExecutionStrategy::NativeElseWasm.in_consensus(), None, ).unwrap(), vec![42], @@ -433,7 +433,7 @@ fn code_is_executed_at_genesis_only() { &BlockId::Number(1), "test_method", &[], - ExecutionStrategy::NativeElseWasm, + ExecutionStrategy::NativeElseWasm.in_consensus(), None, ); diff --git a/client/service/test/src/client/mod.rs b/client/service/test/src/client/mod.rs index 9cd0e193fcd03..6f1d7adbe58bf 100644 --- a/client/service/test/src/client/mod.rs +++ b/client/service/test/src/client/mod.rs @@ -168,7 +168,9 @@ fn construct_block( let hash = header.hash(); let mut overlay = OverlayedChanges::default(); let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(backend); - let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend"); + let runtime_code = backend_runtime_code + .runtime_code(sp_state_machine::ExecutionContext::Consensus) + .expect("Code is part of the backend"); let task_executor = Box::new(TaskExecutor::new()); StateMachine::new( @@ -182,7 +184,7 @@ fn construct_block( &runtime_code, task_executor.clone() as Box<_>, ).execute( - ExecutionStrategy::NativeElseWasm, + ExecutionStrategy::NativeElseWasm.in_consensus(), ).unwrap(); for tx in transactions.iter() { @@ -197,7 +199,7 @@ fn construct_block( &runtime_code, task_executor.clone() as Box<_>, ).execute( - ExecutionStrategy::NativeElseWasm, + ExecutionStrategy::NativeElseWasm.in_consensus(), ).unwrap(); } @@ -212,7 +214,7 @@ fn construct_block( &runtime_code, task_executor.clone() as Box<_>, ).execute( - ExecutionStrategy::NativeElseWasm, + ExecutionStrategy::NativeElseWasm.in_consensus(), ).unwrap(); header = Header::decode(&mut &ret_data[..]).unwrap(); @@ -249,7 +251,7 @@ fn construct_genesis_should_work_with_native() { let backend = InMemoryBackend::from(storage); let (b1data, _b1hash) = block1(genesis_hash, &backend); let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend); - let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend"); + let runtime_code = backend_runtime_code.runtime_code(sp_state_machine::ExecutionContext::Consensus).expect("Code is part of the backend"); let mut overlay = OverlayedChanges::default(); @@ -264,7 +266,7 @@ fn construct_genesis_should_work_with_native() { &runtime_code, TaskExecutor::new(), ).execute( - ExecutionStrategy::NativeElseWasm, + ExecutionStrategy::NativeElseWasm.in_consensus(), ).unwrap(); } @@ -283,7 +285,7 @@ fn construct_genesis_should_work_with_wasm() { let backend = InMemoryBackend::from(storage); let (b1data, _b1hash) = block1(genesis_hash, &backend); let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend); - let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend"); + let runtime_code = backend_runtime_code.runtime_code(sp_state_machine::ExecutionContext::Consensus).expect("Code is part of the backend"); let mut overlay = OverlayedChanges::default(); @@ -298,7 +300,7 @@ fn construct_genesis_should_work_with_wasm() { &runtime_code, TaskExecutor::new(), ).execute( - ExecutionStrategy::AlwaysWasm, + ExecutionStrategy::AlwaysWasm.in_consensus(), ).unwrap(); } @@ -317,7 +319,7 @@ fn construct_genesis_with_bad_transaction_should_panic() { let backend = InMemoryBackend::from(storage); let (b1data, _b1hash) = block1(genesis_hash, &backend); let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend); - let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend"); + let runtime_code = backend_runtime_code.runtime_code(sp_state_machine::ExecutionContext::Consensus).expect("Code is part of the backend"); let mut overlay = OverlayedChanges::default(); @@ -332,7 +334,7 @@ fn construct_genesis_with_bad_transaction_should_panic() { &runtime_code, TaskExecutor::new(), ).execute( - ExecutionStrategy::NativeElseWasm, + ExecutionStrategy::NativeElseWasm.in_consensus(), ); assert!(r.is_err()); } diff --git a/frame/system/src/mock.rs b/frame/system/src/mock.rs index 0f53532eb8f6b..4b2c9df13e9fb 100644 --- a/frame/system/src/mock.rs +++ b/frame/system/src/mock.rs @@ -113,12 +113,13 @@ impl Config for Test { pub type SysEvent = frame_system::Event; /// A simple call, which one doesn't matter. -pub const CALL: &::Call = &Call::System(frame_system::Call::set_heap_pages(0u64)); +pub const CALL: &::Call = + &Call::System(frame_system::Call::set_heap_pages(0u64, 0u64)); /// Create new externalities for `System` module tests. pub fn new_test_ext() -> sp_io::TestExternalities { - let mut ext: sp_io::TestExternalities = GenesisConfig::default() - .build_storage().unwrap().into(); + let mut ext: sp_io::TestExternalities = + GenesisConfig::default().build_storage().unwrap().into(); // Add to each test the initial weight of a block ext.execute_with(|| System::register_extra_weight_unchecked( ::BlockWeights::get().base_block, diff --git a/primitives/state-machine/src/lib.rs b/primitives/state-machine/src/lib.rs index 1d871c29cd7ec..852e29ba6618a 100644 --- a/primitives/state-machine/src/lib.rs +++ b/primitives/state-machine/src/lib.rs @@ -333,7 +333,7 @@ mod execution { pub struct ExecutionManager { /// The strategy of the execution's runtime. pub strategy: ExecutionStrategyWithHandler, - /// The context at which the aforementioned strategy is being used. + /// The context in which the aforementioned strategy is being used. pub context: ExecutionContext, } diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index bad8cc7758520..82b62af253fbb 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -159,13 +159,14 @@ impl } /// Set the execution strategy that should be used by all contexts. - pub fn set_execution_strategy(mut self, execution_config: ExecutionConfig) -> Self { + pub fn set_execution_strategy(mut self, execution_strategy: ExecutionStrategy) -> Self { self.execution_configs = ExecutionConfigs { - syncing: execution_config, - importing: execution_config, - block_construction: execution_config, - offchain_worker: execution_config, - other: execution_config, + // NOTE: we set the context to consensus for all of them. + syncing: execution_strategy.in_consensus(), + importing: execution_strategy.in_consensus(), + block_construction: execution_strategy.in_consensus(), + offchain_worker: execution_strategy.in_consensus(), + other: execution_strategy.in_consensus(), }; self } diff --git a/utils/frame/try-runtime/cli/src/lib.rs b/utils/frame/try-runtime/cli/src/lib.rs index e0d09ff7fbcf4..447afac9a5a3a 100644 --- a/utils/frame/try-runtime/cli/src/lib.rs +++ b/utils/frame/try-runtime/cli/src/lib.rs @@ -187,7 +187,7 @@ where ExecDispatch: NativeExecutionDispatch + 'static, { let wasm_method = shared.wasm_method; - let execution = shared.execution; + let execution: sp_state_machine::ExecutionStrategy = shared.execution.into(); let heap_pages = shared.heap_pages.or(config.default_heap_pages); let mut changes = Default::default(); @@ -233,10 +233,10 @@ where &[], ext.extensions, &sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend) - .runtime_code()?, + .runtime_code(sp_state_machine::ExecutionContext::Consensus)?, sp_core::testing::TaskExecutor::new(), ) - .execute(execution.into()) + .execute(execution.in_consensus()) .map_err(|e| format!("failed to execute 'TryRuntime_on_runtime_upgrade': {:?}", e))?; let (weight, total_weight) = <(u64, u64) as Decode>::decode(&mut &*encoded_result) @@ -266,7 +266,7 @@ where ExecDispatch: NativeExecutionDispatch + 'static, { let wasm_method = shared.wasm_method; - let execution = shared.execution; + let execution: sp_state_machine::ExecutionStrategy = shared.execution.into(); let heap_pages = shared.heap_pages.or(config.default_heap_pages); let mut changes = Default::default(); @@ -333,10 +333,10 @@ where header.encode().as_ref(), ext.extensions, &sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend) - .runtime_code()?, + .runtime_code(sp_state_machine::ExecutionContext::Offchain)?, sp_core::testing::TaskExecutor::new(), ) - .execute(execution.into()) + .execute(execution.in_offchain()) .map_err(|e| format!("failed to execute 'OffchainWorkerApi_offchain_worker': {:?}", e))?; log::info!("OffchainWorkerApi_offchain_worker executed without errors."); @@ -358,7 +358,7 @@ where ExecDispatch: NativeExecutionDispatch + 'static, { let wasm_method = shared.wasm_method; - let execution = shared.execution; + let execution: sp_state_machine::ExecutionStrategy = shared.execution.into(); let heap_pages = shared.heap_pages.or(config.default_heap_pages); let mut changes = Default::default(); @@ -435,10 +435,11 @@ where "Core_execute_block", block.encode().as_ref(), ext.extensions, - &sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend).runtime_code()?, + &sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend) + .runtime_code(sp_state_machine::ExecutionContext::Consensus)?, sp_core::testing::TaskExecutor::new(), ) - .execute(execution.into()) + .execute(execution.in_consensus()) .map_err(|e| format!("failed to execute 'Core_execute_block': {:?}", e))?; debug_assert!(_encoded_result == vec![1]); From 1f7fabdd1954174844915aea6798983b60db6713 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 28 Jun 2021 10:08:48 +0200 Subject: [PATCH 03/24] fix some line width --- client/api/src/execution_extensions.rs | 1 - client/cli/src/arg_enums.rs | 6 ++++-- client/light/src/call_executor.rs | 4 ++-- client/service/src/client/call_executor.rs | 12 ++++++------ client/service/test/src/client/mod.rs | 12 +++++++++--- frame/system/src/lib.rs | 6 +++++- utils/frame/benchmarking-cli/src/command.rs | 3 ++- 7 files changed, 28 insertions(+), 16 deletions(-) diff --git a/client/api/src/execution_extensions.rs b/client/api/src/execution_extensions.rs index 14a23846c631c..564ba3d1e5083 100644 --- a/client/api/src/execution_extensions.rs +++ b/client/api/src/execution_extensions.rs @@ -54,7 +54,6 @@ pub struct ExecutionConfigs { impl Default for ExecutionConfigs { fn default() -> ExecutionConfigs { - // TODO: this should use our defaults consts. ExecutionConfigs { syncing: ExecutionStrategy::NativeElseWasm.in_consensus(), importing: ExecutionStrategy::NativeElseWasm.in_consensus(), diff --git a/client/cli/src/arg_enums.rs b/client/cli/src/arg_enums.rs index f1da199cdd9a0..42a79420bd71a 100644 --- a/client/cli/src/arg_enums.rs +++ b/client/cli/src/arg_enums.rs @@ -261,8 +261,10 @@ macro_rules! generate_config_const { ($( $role:ident => $strategy:ident, $context:ident ),*) => { paste::paste! { $( - pub const []: ExecutionStrategy = ExecutionStrategy::$strategy; - pub const []: ExecutionContext = ExecutionContext::$context; + pub const []: ExecutionStrategy + = ExecutionStrategy::$strategy; + pub const []: ExecutionContext + = ExecutionContext::$context; )* } }; diff --git a/client/light/src/call_executor.rs b/client/light/src/call_executor.rs index 503d6a7845df9..b8cfbf16eddc8 100644 --- a/client/light/src/call_executor.rs +++ b/client/light/src/call_executor.rs @@ -139,7 +139,7 @@ where changes, None, initialize_block, - ExecutionConfig::new_consensus(ExecutionStrategy::NativeWhenPossible).get_manager(), // TODO: why are we not using the given manager. + ExecutionConfig::new_consensus(ExecutionStrategy::NativeWhenPossible).get_manager(), native_call, recorder, extensions, @@ -273,7 +273,7 @@ where // TODO: Remove when solved: https://github.com/paritytech/substrate/issues/5047 let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&trie_backend); let runtime_code = backend_runtime_code - .runtime_code(sp_state_machine::ExecutionContext::Consensus) // TODO: ? which one is it, and I should get this maybe from `E`. + .runtime_code(sp_state_machine::ExecutionContext::Consensus) .map_err(|_e| ClientError::RuntimeCodeMissing)?; execution_proof_check_on_trie_backend::( diff --git a/client/service/src/client/call_executor.rs b/client/service/src/client/call_executor.rs index 9aad9accac40a..0ac7630b31e6e 100644 --- a/client/service/src/client/call_executor.rs +++ b/client/service/src/client/call_executor.rs @@ -80,9 +80,9 @@ where }) } - /// Check if local runtime code overrides are enabled and one is available - /// for the given `BlockId`. If yes, return it; otherwise return the same - /// `RuntimeCode` instance that was passed. + /// Check if local runtime code overrides are enabled and one is available for the given + /// `BlockId`. If yes, return it; otherwise return the same `RuntimeCode` instance that was + /// passed. fn check_override<'a>( &'a self, onchain_code: RuntimeCode<'a>, @@ -143,7 +143,7 @@ where id: &BlockId, method: &str, call_data: &[u8], - strategy: sp_state_machine::ExecutionConfig, + config: sp_state_machine::ExecutionConfig, extensions: Option, ) -> sp_blockchain::Result> { let mut changes = OverlayedChanges::default(); @@ -153,7 +153,7 @@ where let state = self.backend.state_at(*id)?; let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); let runtime_code = state_runtime_code - .runtime_code(strategy.context) + .runtime_code(config.context) .map_err(sp_blockchain::Error::RuntimeCode)?; let runtime_code = self.check_override(runtime_code, id)?; @@ -168,7 +168,7 @@ where &runtime_code, self.spawn_handle.clone(), ).execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>( - strategy.get_manager(), + config.get_manager(), None, )?; diff --git a/client/service/test/src/client/mod.rs b/client/service/test/src/client/mod.rs index 6f1d7adbe58bf..6343abb7f7c06 100644 --- a/client/service/test/src/client/mod.rs +++ b/client/service/test/src/client/mod.rs @@ -251,7 +251,9 @@ fn construct_genesis_should_work_with_native() { let backend = InMemoryBackend::from(storage); let (b1data, _b1hash) = block1(genesis_hash, &backend); let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend); - let runtime_code = backend_runtime_code.runtime_code(sp_state_machine::ExecutionContext::Consensus).expect("Code is part of the backend"); + let runtime_code = backend_runtime_code + .runtime_code(sp_state_machine::ExecutionContext::Consensus) + .expect("Code is part of the backend"); let mut overlay = OverlayedChanges::default(); @@ -285,7 +287,9 @@ fn construct_genesis_should_work_with_wasm() { let backend = InMemoryBackend::from(storage); let (b1data, _b1hash) = block1(genesis_hash, &backend); let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend); - let runtime_code = backend_runtime_code.runtime_code(sp_state_machine::ExecutionContext::Consensus).expect("Code is part of the backend"); + let runtime_code = backend_runtime_code + .runtime_code(sp_state_machine::ExecutionContext::Consensus) + .expect("Code is part of the backend"); let mut overlay = OverlayedChanges::default(); @@ -319,7 +323,9 @@ fn construct_genesis_with_bad_transaction_should_panic() { let backend = InMemoryBackend::from(storage); let (b1data, _b1hash) = block1(genesis_hash, &backend); let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend); - let runtime_code = backend_runtime_code.runtime_code(sp_state_machine::ExecutionContext::Consensus).expect("Code is part of the backend"); + let runtime_code = backend_runtime_code + .runtime_code(sp_state_machine::ExecutionContext::Consensus) + .expect("Code is part of the backend"); let mut overlay = OverlayedChanges::default(); diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 24690ac9335b5..436b2675ccf70 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -322,7 +322,11 @@ pub mod pallet { /// Two separate heap page limits can be set, one for offchain context and one for consensus /// context. #[pallet::weight((T::SystemWeightInfo::set_heap_pages(), DispatchClass::Operational))] - pub fn set_heap_pages(origin: OriginFor, consensus: u64, offchain: u64) -> DispatchResultWithPostInfo { + pub fn set_heap_pages( + origin: OriginFor, + consensus: u64, + offchain: u64, + ) -> DispatchResultWithPostInfo { ensure_root(origin)?; storage::unhashed::put_raw(well_known_keys::HEAP_PAGES, &consensus.encode()); storage::unhashed::put_raw(well_known_keys::OFFCHAIN_HEAP_PAGES, &offchain.encode()); diff --git a/utils/frame/benchmarking-cli/src/command.rs b/utils/frame/benchmarking-cli/src/command.rs index f352230bd3fa8..a3e65d3c9d6f4 100644 --- a/utils/frame/benchmarking-cli/src/command.rs +++ b/utils/frame/benchmarking-cli/src/command.rs @@ -92,7 +92,8 @@ impl BenchmarkCmd { self.extra, ).encode(), extensions, - &sp_state_machine::backend::BackendRuntimeCode::new(&state).runtime_code(sp_state_machine::ExecutionContext::Offchain)?, + &sp_state_machine::backend::BackendRuntimeCode::new(&state) + .runtime_code(sp_state_machine::ExecutionContext::Offchain)?, sp_core::testing::TaskExecutor::new(), ) .execute(sp_state_machine::ExecutionConfig::new_offchain(strategy.into())) From 98897fa7118a8cf25ccadc74cd22c17682fefe9a Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 28 Jun 2021 11:06:24 +0200 Subject: [PATCH 04/24] Fix --- client/executor/src/native_executor.rs | 17 +++++++++++------ frame/system/benchmarking/src/lib.rs | 2 +- frame/system/src/lib.rs | 22 +++++++++++----------- frame/system/src/mock.rs | 2 +- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/client/executor/src/native_executor.rs b/client/executor/src/native_executor.rs index 6fc34b6f1a322..cd126273303f9 100644 --- a/client/executor/src/native_executor.rs +++ b/client/executor/src/native_executor.rs @@ -191,11 +191,11 @@ impl WasmExecutor { /// Perform a call into the given runtime. /// - /// The runtime is passed as a [`RuntimeBlob`]. The runtime will be isntantiated with the + /// The runtime is passed as a [`RuntimeBlob`]. The runtime will be instantiated with the /// parameters this `WasmExecutor` was initialized with. /// - /// In case of problems with during creation of the runtime or instantation, a `Err` is returned. - /// that describes the message. + /// In case of problems with during creation of the runtime or instantiation, a `Err` is + /// returned. that describes the message. #[doc(hidden)] // We use this function for tests across multiple crates. pub fn uncached_call( &self, @@ -266,8 +266,8 @@ impl sp_core::traits::ReadRuntimeVersion for WasmExecutor { } } -/// A generic `CodeExecutor` implementation that uses a delegate to determine wasm code equivalence -/// and dispatch to native code when possible, falling back on `WasmExecutor` when not. +/// A generic `CodeExecutor` implementation that uses a delegate `D` to determine wasm code +/// equivalence and dispatch to native code when possible, falling back on `WasmExecutor` when not. pub struct NativeExecutor { /// Dummy field to avoid the compiler complaining about us not using `D`. _dummy: std::marker::PhantomData, @@ -283,7 +283,6 @@ impl NativeExecutor { /// # Parameters /// /// `fallback_method` - Method used to execute fallback Wasm code. - /// /// `default_heap_pages` - Number of 64KB pages to allocate for Wasm execution. /// Defaults to `DEFAULT_HEAP_PAGES` if `None` is provided. pub fn new( @@ -489,6 +488,12 @@ impl CodeExecutor for NativeExecutor { use_native: bool, native_call: Option, ) -> (Result>, bool) { + trace!( + target: "executor", + "call({}) [use_native = {}]", + method, + use_native, + ); let mut used_native = false; let result = self.wasm.with_instance( runtime_code, diff --git a/frame/system/benchmarking/src/lib.rs b/frame/system/benchmarking/src/lib.rs index ddffa6e86c896..7146bcd60645b 100644 --- a/frame/system/benchmarking/src/lib.rs +++ b/frame/system/benchmarking/src/lib.rs @@ -51,7 +51,7 @@ benchmarks! { }: _(RawOrigin::Signed(caller), remark_message) set_heap_pages { - }: _(RawOrigin::Root, Default::default(), Default::default()) + }: _(RawOrigin::Root, Default::default()) // `set_code` was not benchmarked because it is pretty hard to come up with a real // Wasm runtime to test the upgrade with. But this is okay because we will make diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 436b2675ccf70..e8cc56b4b5f49 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -317,19 +317,11 @@ pub mod pallet { Ok(().into()) } - /// Set the number of pages in the WebAssembly environment's heap. - /// - /// Two separate heap page limits can be set, one for offchain context and one for consensus - /// context. + /// Set the number of pages in the consensus WebAssembly environment's heap. #[pallet::weight((T::SystemWeightInfo::set_heap_pages(), DispatchClass::Operational))] - pub fn set_heap_pages( - origin: OriginFor, - consensus: u64, - offchain: u64, - ) -> DispatchResultWithPostInfo { + pub fn set_heap_pages(origin: OriginFor, pages: u64) -> DispatchResultWithPostInfo { ensure_root(origin)?; - storage::unhashed::put_raw(well_known_keys::HEAP_PAGES, &consensus.encode()); - storage::unhashed::put_raw(well_known_keys::OFFCHAIN_HEAP_PAGES, &offchain.encode()); + storage::unhashed::put_raw(well_known_keys::HEAP_PAGES, &pages.encode()); Ok(().into()) } @@ -476,6 +468,14 @@ pub mod pallet { Self::deposit_event(Event::Remarked(who, hash)); Ok(().into()) } + + /// Set the number of pages in the offchain WebAssembly environment's heap. + #[pallet::weight((T::SystemWeightInfo::set_heap_pages(), DispatchClass::Operational))] + pub fn set_heap_pages_offchain(origin: OriginFor, pages: u64) -> DispatchResultWithPostInfo { + ensure_root(origin)?; + storage::unhashed::put_raw(well_known_keys::OFFCHAIN_HEAP_PAGES, &pages.encode()); + Ok(().into()) + } } /// Event for the System pallet. diff --git a/frame/system/src/mock.rs b/frame/system/src/mock.rs index 4b2c9df13e9fb..92690e4e3684f 100644 --- a/frame/system/src/mock.rs +++ b/frame/system/src/mock.rs @@ -114,7 +114,7 @@ pub type SysEvent = frame_system::Event; /// A simple call, which one doesn't matter. pub const CALL: &::Call = - &Call::System(frame_system::Call::set_heap_pages(0u64, 0u64)); + &Call::System(frame_system::Call::set_heap_pages(0u64)); /// Create new externalities for `System` module tests. pub fn new_test_ext() -> sp_io::TestExternalities { From 9ce9263f6a0ad2eeb3eac56d9c7938fd6ad3066d Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Fri, 2 Jul 2021 09:43:52 +0200 Subject: [PATCH 05/24] Update primitives/state-machine/src/lib.rs Co-authored-by: cheme --- primitives/state-machine/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/state-machine/src/lib.rs b/primitives/state-machine/src/lib.rs index 852e29ba6618a..c6b92da83f240 100644 --- a/primitives/state-machine/src/lib.rs +++ b/primitives/state-machine/src/lib.rs @@ -378,7 +378,7 @@ mod execution { } } - /// Helper to create the [`ExecutionStrategyWithHandler::AlwaysWasm(Untrsuted)`] without naming + /// Helper to create the [`ExecutionStrategyWithHandler::AlwaysWasm(Untrusted)`] without naming /// types. fn always_untrusted_wasm() -> ExecutionManager> { ExecutionManager { From f6a185c347d868552dcc302519a116b19b408fa4 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sun, 4 Jul 2021 01:11:46 +0200 Subject: [PATCH 06/24] New version --- bin/node/executor/tests/common.rs | 6 +++-- client/executor/src/integration_tests/mod.rs | 22 ++++++----------- client/executor/src/lib.rs | 4 +-- client/executor/src/native_executor.rs | 26 +++++++++----------- client/service/src/client/call_executor.rs | 1 + primitives/api/test/tests/runtime_calls.rs | 1 + primitives/runtime-interface/test/src/lib.rs | 2 +- primitives/state-machine/src/lib.rs | 1 - test-utils/runtime/src/system.rs | 2 ++ 9 files changed, 30 insertions(+), 35 deletions(-) diff --git a/bin/node/executor/tests/common.rs b/bin/node/executor/tests/common.rs index 635155b5d00b2..e44d02d4a2ae5 100644 --- a/bin/node/executor/tests/common.rs +++ b/bin/node/executor/tests/common.rs @@ -101,8 +101,9 @@ pub fn executor() -> NativeExecutor { } pub fn executor_call< - R:Decode + Encode + PartialEq, - NC: FnOnce() -> std::result::Result> + std::panic::UnwindSafe + R: Decode + Encode + PartialEq, + NC: FnOnce() -> std::result::Result> + + std::panic::UnwindSafe, >( t: &mut TestExternalities, method: &str, @@ -116,6 +117,7 @@ pub fn executor_call< let heap_pages = t.storage(sp_core::storage::well_known_keys::HEAP_PAGES); let runtime_code = RuntimeCode { code_fetcher: &sp_core::traits::WrappedRuntimeCode(code.as_slice().into()), + context: sp_core::traits::CodeContext::Consensus, hash: sp_core::blake2_256(&code).to_vec(), heap_pages: heap_pages.and_then(|hp| Decode::decode(&mut &hp[..]).ok()), }; diff --git a/client/executor/src/integration_tests/mod.rs b/client/executor/src/integration_tests/mod.rs index 0762306309df4..4246174c868ba 100644 --- a/client/executor/src/integration_tests/mod.rs +++ b/client/executor/src/integration_tests/mod.rs @@ -75,19 +75,15 @@ fn call_in_wasm( execution_method: WasmExecutionMethod, ext: &mut E, ) -> Result, String> { - let executor = crate::WasmExecutor::new( - execution_method, - Some(1024), - HostFunctions::host_functions(), - 8, - None, - ); + let executor = + crate::WasmExecutor::new(execution_method, HostFunctions::host_functions(), 8, None); executor.uncached_call( RuntimeBlob::uncompress_if_needed(&wasm_binary_unwrap()[..]).unwrap(), ext, true, function, call_data, + 1024, ) } @@ -537,13 +533,7 @@ test_wasm_execution!(should_trap_when_heap_exhausted); fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) { let mut ext = TestExternalities::default(); - let executor = crate::WasmExecutor::new( - wasm_method, - Some(17), // `17` is the initial number of pages compiled into the binary. - HostFunctions::host_functions(), - 8, - None, - ); + let executor = crate::WasmExecutor::new(wasm_method, HostFunctions::host_functions(), 8, None); let err = executor .uncached_call( @@ -552,6 +542,8 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) { true, "test_exhaust_heap", &[0], + // `17` is the initial number of pages compiled into the binary. + 17, ) .unwrap_err(); @@ -655,7 +647,6 @@ test_wasm_execution!(parallel_execution); fn parallel_execution(wasm_method: WasmExecutionMethod) { let executor = std::sync::Arc::new(crate::WasmExecutor::new( wasm_method, - Some(1024), HostFunctions::host_functions(), 8, None, @@ -674,6 +665,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) { true, "test_twox_128", &[0], + 1024 ) .unwrap(), hex!("99e9d85137db46ef4bbea33613baafd5").to_vec().encode(), diff --git a/client/executor/src/lib.rs b/client/executor/src/lib.rs index c0cbf9c94dafd..fd722be5de0fb 100644 --- a/client/executor/src/lib.rs +++ b/client/executor/src/lib.rs @@ -29,7 +29,7 @@ //! wasm engine used, instance cache. #![warn(missing_docs)] -#![recursion_limit="128"] +#![recursion_limit = "128"] #[macro_use] mod native_executor; @@ -80,7 +80,6 @@ mod tests { let executor = WasmExecutor::new( WasmExecutionMethod::Interpreted, - Some(8), sp_io::SubstrateHostFunctions::host_functions(), 8, None, @@ -92,6 +91,7 @@ mod tests { true, "test_empty_return", &[], + 8, ) .unwrap(); assert_eq!(res, vec![0u8; 0]); diff --git a/client/executor/src/native_executor.rs b/client/executor/src/native_executor.rs index e969547c1db41..536edc0710b31 100644 --- a/client/executor/src/native_executor.rs +++ b/client/executor/src/native_executor.rs @@ -96,8 +96,6 @@ pub trait NativeExecutionDispatch: Send + Sync { pub struct WasmExecutor { /// Method used to execute fallback Wasm code. method: WasmExecutionMethod, - /// The number of 64KB pages to allocate for Wasm execution. - // default_heap_pages: Option, /// The host functions registered with this instance. host_functions: Arc>, /// WASM runtime cache. @@ -129,14 +127,12 @@ impl WasmExecutor { /// compiled execution method is used. pub fn new( method: WasmExecutionMethod, - default_heap_pages: Option, host_functions: Vec<&'static dyn Function>, max_runtime_instances: usize, cache_path: Option, ) -> Self { WasmExecutor { method, - // default_heap_pages: default_heap_pages.unwrap_or(DEFAULT_HEAP_PAGES), host_functions: Arc::new(host_functions), cache: Arc::new(RuntimeCache::new(max_runtime_instances, cache_path.clone())), max_runtime_instances, @@ -151,6 +147,8 @@ impl WasmExecutor { /// prevent any poisoned state. Native runtime execution does not need to report back /// any `panic!`. /// + /// The number of heap pages to use is deduced from `runtime_code`'s context. + /// /// # Safety /// /// `runtime` and `ext` are given as `AssertUnwindSafe` to the closure. As described above, the @@ -202,7 +200,8 @@ impl WasmExecutor { /// In case of problems during creation of the runtime or instantiation, a `Err` is /// returned. that describes the message. /// - /// This runtime is always initiated with the number of heap pages of consensus context. + /// Unlike `with_instance`, the number of heap pages to use must be explicitly given to this + /// function. #[doc(hidden)] // We use this function for tests across multiple crates. pub fn uncached_call( &self, @@ -211,10 +210,11 @@ impl WasmExecutor { allow_missing_host_functions: bool, export_name: &str, call_data: &[u8], + heap_pages: u64, ) -> std::result::Result, String> { let module = crate::wasm_runtime::create_wasm_runtime_with_code( self.method, - DEFAULT_HEAP_PAGES_CONSENSUS, + heap_pages, runtime_blob, self.host_functions.to_vec(), allow_missing_host_functions, @@ -269,6 +269,7 @@ impl sp_core::traits::ReadRuntimeVersion for WasmExecutor { true, "Core_version", &[], + DEFAULT_HEAP_PAGES_CONSENSUS, ) } } @@ -294,7 +295,9 @@ impl NativeExecutor { /// Defaults to `DEFAULT_HEAP_PAGES` if `None` is provided. pub fn new( fallback_method: WasmExecutionMethod, - default_heap_pages: Option, + // deprecated: this is the CLI configurable heap pages, which no longer exists and is + // ALWAYS NONE. This parameter should be removed. + _default_heap_pages: Option, max_runtime_instances: usize, ) -> Self { let extended = D::ExtendHostFunctions::host_functions(); @@ -311,13 +314,8 @@ impl NativeExecutor { // Add the custom host functions provided by the user. host_functions.extend(extended); - let wasm_executor = WasmExecutor::new( - fallback_method, - default_heap_pages, - host_functions, - max_runtime_instances, - None, - ); + let wasm_executor = + WasmExecutor::new(fallback_method, host_functions, max_runtime_instances, None); NativeExecutor { _dummy: Default::default(), diff --git a/client/service/src/client/call_executor.rs b/client/service/src/client/call_executor.rs index 267b19b4c7f72..0d2b86ddf8eba 100644 --- a/client/service/src/client/call_executor.rs +++ b/client/service/src/client/call_executor.rs @@ -352,6 +352,7 @@ mod tests { let onchain_code = RuntimeCode { code_fetcher: &onchain_code, heap_pages: Some(128), + context: sp_core::traits::CodeContext::Consensus, hash: vec![0, 0, 0, 0], }; diff --git a/primitives/api/test/tests/runtime_calls.rs b/primitives/api/test/tests/runtime_calls.rs index b60c7a09cb616..0fa115187f4f2 100644 --- a/primitives/api/test/tests/runtime_calls.rs +++ b/primitives/api/test/tests/runtime_calls.rs @@ -156,6 +156,7 @@ fn record_proof_works() { code_fetcher: &sp_core::traits::WrappedRuntimeCode( client.code_at(&block_id).unwrap().into(), ), + context: sp_core::traits::CodeContext::Consensus, hash: vec![1], heap_pages: None, }; diff --git a/primitives/runtime-interface/test/src/lib.rs b/primitives/runtime-interface/test/src/lib.rs index a021a93939a10..531e6a170d8f1 100644 --- a/primitives/runtime-interface/test/src/lib.rs +++ b/primitives/runtime-interface/test/src/lib.rs @@ -41,7 +41,6 @@ fn call_wasm_method_with_result( let executor = sc_executor::WasmExecutor::new( sc_executor::WasmExecutionMethod::Interpreted, - Some(8), host_functions, 8, None, @@ -53,6 +52,7 @@ fn call_wasm_method_with_result( false, method, &[], + 8 ) .map_err(|e| format!("Failed to execute `{}`: {}", method, e))?; Ok(ext) diff --git a/primitives/state-machine/src/lib.rs b/primitives/state-machine/src/lib.rs index 3d60e9a9f5d63..3104017132a74 100644 --- a/primitives/state-machine/src/lib.rs +++ b/primitives/state-machine/src/lib.rs @@ -1078,7 +1078,6 @@ mod tests { _: &RuntimeCode, _method: &str, _data: &[u8], - _heap_pages: u64, use_native: bool, native_call: Option, ) -> (CallResult, bool) { diff --git a/test-utils/runtime/src/system.rs b/test-utils/runtime/src/system.rs index ae35ded83bfc7..88dae5fd76325 100644 --- a/test-utils/runtime/src/system.rs +++ b/test-utils/runtime/src/system.rs @@ -420,6 +420,7 @@ mod tests { let runtime_code = RuntimeCode { code_fetcher: &sp_core::traits::WrappedRuntimeCode(wasm_binary_unwrap().into()), hash: Vec::new(), + context: sp_core::traits::CodeContext::Consensus, heap_pages: None, }; @@ -520,6 +521,7 @@ mod tests { let runtime_code = RuntimeCode { code_fetcher: &sp_core::traits::WrappedRuntimeCode(wasm_binary_unwrap().into()), hash: Vec::new(), + context: sp_core::traits::CodeContext::Consensus, heap_pages: None, }; From 79f6cf9c6dc899dff1cbaa118dc8298fba2a749a Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sun, 4 Jul 2021 02:18:47 +0200 Subject: [PATCH 07/24] Add log --- client/executor/src/wasm_runtime.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client/executor/src/wasm_runtime.rs b/client/executor/src/wasm_runtime.rs index d01132da180a4..c77800727c249 100644 --- a/client/executor/src/wasm_runtime.rs +++ b/client/executor/src/wasm_runtime.rs @@ -223,6 +223,13 @@ impl RuntimeCache { let code_hash = &runtime_code.hash; let heap_pages = runtime_code.heap_pages.unwrap_or(default_heap_pages); + log::debug!( + target: "wasm-runtime", + "with_instance: using {} heap pages in {:?} context", + heap_pages, + runtime_code.context, + ); + let mut runtimes = self.runtimes.lock(); // this must be released prior to calling f let pos = runtimes.iter().position(|r| r.as_ref().map_or( false, From 8a133b1a72b656d64d043ffdebcbc71875862ee1 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Mon, 5 Jul 2021 19:27:41 +0200 Subject: [PATCH 08/24] Update client/executor/src/wasm_runtime.rs --- client/executor/src/wasm_runtime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/executor/src/wasm_runtime.rs b/client/executor/src/wasm_runtime.rs index c77800727c249..65b1aa379d9a1 100644 --- a/client/executor/src/wasm_runtime.rs +++ b/client/executor/src/wasm_runtime.rs @@ -223,7 +223,7 @@ impl RuntimeCache { let code_hash = &runtime_code.hash; let heap_pages = runtime_code.heap_pages.unwrap_or(default_heap_pages); - log::debug!( + log::trace!( target: "wasm-runtime", "with_instance: using {} heap pages in {:?} context", heap_pages, From 2eca696dafe79fb0a709b34f06d54b0a85c28c3a Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 8 Jul 2021 13:57:23 +0200 Subject: [PATCH 09/24] Update client/executor/src/wasm_runtime.rs --- client/executor/src/wasm_runtime.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/client/executor/src/wasm_runtime.rs b/client/executor/src/wasm_runtime.rs index 8e4d3b4c307a3..4e6febbf15b67 100644 --- a/client/executor/src/wasm_runtime.rs +++ b/client/executor/src/wasm_runtime.rs @@ -223,13 +223,6 @@ impl RuntimeCache { let code_hash = &runtime_code.hash; let heap_pages = runtime_code.heap_pages.unwrap_or(default_heap_pages); - log::trace!( - target: "wasm-runtime", - "with_instance: using {} heap pages in {:?} context", - heap_pages, - runtime_code.context, - ); - let mut runtimes = self.runtimes.lock(); // this must be released prior to calling f let pos = runtimes.iter().position(|r| r.as_ref().map_or( false, From b96ecab7d31aeac93da23f02326b0ced06af853f Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 8 Jul 2021 13:57:33 +0200 Subject: [PATCH 10/24] Update client/service/src/builder.rs --- client/service/src/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index c93f24aa49e0e..a560561eb4907 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -321,7 +321,7 @@ pub fn new_full_parts( let executor = NativeExecutor::::new( config.wasm_method, - config.default_heap_pages, // TODO: this is ALWAYS None now. Maybe we should actually remove it??? + config.default_heap_pages,remove it??? config.max_runtime_instances, ); From 2be3e7d30995ed3d0fa759d2a714f7c3edae77b2 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 8 Jul 2021 13:57:56 +0200 Subject: [PATCH 11/24] Update client/service/src/client/wasm_override.rs --- client/service/src/client/wasm_override.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/service/src/client/wasm_override.rs b/client/service/src/client/wasm_override.rs index 04daa7d458a05..a25fd38baa9e5 100644 --- a/client/service/src/client/wasm_override.rs +++ b/client/service/src/client/wasm_override.rs @@ -65,7 +65,7 @@ impl WasmBlob { RuntimeCode { code_fetcher: self, hash: self.hash.clone(), - context: sp_core::traits::CodeContext::Consensus, // TODO: re-think this. + context: sp_core::traits::CodeContext::Consensus, heap_pages, } } From 5d3e8a3e7ad676894117cdcf2c63d5a16ef05fe6 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 8 Jul 2021 13:58:03 +0200 Subject: [PATCH 12/24] Update client/service/test/src/client/light.rs --- client/service/test/src/client/light.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/service/test/src/client/light.rs b/client/service/test/src/client/light.rs index b05a777cb2c77..2297653c49d3b 100644 --- a/client/service/test/src/client/light.rs +++ b/client/service/test/src/client/light.rs @@ -201,7 +201,7 @@ impl CallExecutor for DummyCallExecutor { _id: &BlockId, _method: &str, _call_data: &[u8], - _strategy: sp_state_machine::ExecutionConfig, + _config: sp_state_machine::ExecutionConfig, _extensions: Option, ) -> Result, ClientError> { Ok(vec![42]) From d688a1b51f39a69534231b495d0591a599dcef60 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Thu, 8 Jul 2021 13:58:50 +0200 Subject: [PATCH 13/24] Fix build --- client/service/src/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index a560561eb4907..b65f27f957a44 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -321,7 +321,7 @@ pub fn new_full_parts( let executor = NativeExecutor::::new( config.wasm_method, - config.default_heap_pages,remove it??? + config.default_heap_pages, config.max_runtime_instances, ); From 7c87ab1e366097dcdc3fe73472a0470481baeb52 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 29 Jul 2021 12:19:13 +0200 Subject: [PATCH 14/24] Update client/finality-grandpa/src/lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/finality-grandpa/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index f0f8f09551f2b..e57c78af0a83b 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -501,7 +501,7 @@ where &BlockId::Number(Zero::zero()), "GrandpaApi_grandpa_authorities", &[], - sc_client_api::ExecutionConfig::new_consensus(ExecutionStrategy::NativeElseWasm), + ExecutionStrategy::NativeElseWasm.in_consensus(), None, ) .and_then(|call_result| { From ce484210247dc9d22965022fb0c1357cf3baa073 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 29 Jul 2021 12:19:46 +0200 Subject: [PATCH 15/24] Update frame/system/src/lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- frame/system/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index f4fdfeb172d58..53291fa1727c6 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -503,7 +503,7 @@ pub mod pallet { /// Set the number of pages in the offchain WebAssembly environment's heap. #[pallet::weight((T::SystemWeightInfo::set_heap_pages(), DispatchClass::Operational))] - pub fn set_heap_pages_offchain(origin: OriginFor, pages: u64) -> DispatchResultWithPostInfo { + pub fn set_heap_pages_offchain(origin: OriginFor, pages: u64) -> DispatchResult { ensure_root(origin)?; storage::unhashed::put_raw(well_known_keys::OFFCHAIN_HEAP_PAGES, &pages.encode()); Ok(().into()) From 573cc75add3f89d57ef57d5516cb4f2ad93dd105 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 29 Jul 2021 12:19:54 +0200 Subject: [PATCH 16/24] Update primitives/storage/src/lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/storage/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/storage/src/lib.rs b/primitives/storage/src/lib.rs index 45e054f011053..3f97dccc3ebd5 100644 --- a/primitives/storage/src/lib.rs +++ b/primitives/storage/src/lib.rs @@ -206,7 +206,7 @@ pub mod well_known_keys { pub const HEAP_PAGES: &'static [u8] = b":heappages"; /// Number of wasm linear memory pages used required for the execution of the runtime in - /// offchain context. By offchain we mean anything other than `consensus` (see `HEAP_PAGES`). + /// offchain context. By offchain we mean anything other than `consensus` (see [`HEAP_PAGES`]). /// /// The type of this value is encoded `u64`. pub const OFFCHAIN_HEAP_PAGES: &'static [u8] = b":offchain_heappages"; From 5918d2d33e287a0b3c54972b588dde87f8984a92 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 29 Jul 2021 12:20:48 +0200 Subject: [PATCH 17/24] Update primitives/storage/src/lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/storage/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/storage/src/lib.rs b/primitives/storage/src/lib.rs index 3f97dccc3ebd5..51185480f1381 100644 --- a/primitives/storage/src/lib.rs +++ b/primitives/storage/src/lib.rs @@ -205,7 +205,7 @@ pub mod well_known_keys { /// Note that this value is used for consensus-related runtime operations, such as block import. pub const HEAP_PAGES: &'static [u8] = b":heappages"; - /// Number of wasm linear memory pages used required for the execution of the runtime in + /// Number of wasm linear memory pages required for the execution of the runtime in /// offchain context. By offchain we mean anything other than `consensus` (see [`HEAP_PAGES`]). /// /// The type of this value is encoded `u64`. From e3d9f504e196ecd73595514ea66a196db6a352c4 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 29 Jul 2021 12:20:57 +0200 Subject: [PATCH 18/24] Update test-utils/test-runner/src/lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- test-utils/test-runner/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test-utils/test-runner/src/lib.rs b/test-utils/test-runner/src/lib.rs index 759e80334f689..dccb9ad01f07f 100644 --- a/test-utils/test-runner/src/lib.rs +++ b/test-utils/test-runner/src/lib.rs @@ -31,8 +31,9 @@ //! //!

Note

//! The running node has no signature verification, which allows us author extrinsics for any -//! account on chain.
-//!
+//! account on chain. +//! +//! //! //!

How do I Use this?

//! From 12dde901bd5b7b3e6c44b7485039f9bb81964403 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 29 Jul 2021 12:21:05 +0200 Subject: [PATCH 19/24] Update client/light/src/call_executor.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/light/src/call_executor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/light/src/call_executor.rs b/client/light/src/call_executor.rs index 2c7b14a77a0c2..b37bd3bc0cd3d 100644 --- a/client/light/src/call_executor.rs +++ b/client/light/src/call_executor.rs @@ -132,7 +132,7 @@ where call_data, changes, None, - ExecutionConfig::new_consensus(ExecutionStrategy::NativeWhenPossible).get_manager(), + ExecutionStrategy::NativeWhenPossible.in_consensus().get_manager(), native_call, recorder, extensions, From b0d1f66e38b9fd0f939a1614e3f40e8b7e08a43d Mon Sep 17 00:00:00 2001 From: kianenigma Date: Thu, 29 Jul 2021 14:17:19 +0200 Subject: [PATCH 20/24] Add log --- client/executor/src/wasm_runtime.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/executor/src/wasm_runtime.rs b/client/executor/src/wasm_runtime.rs index 8674e7239255b..6878dd118789b 100644 --- a/client/executor/src/wasm_runtime.rs +++ b/client/executor/src/wasm_runtime.rs @@ -116,9 +116,10 @@ impl VersionedRuntime { if new_inst { log::debug!( target: "wasm-runtime", - "Allocated WASM instance {}/{}", + "Allocated WASM instance {}/{} [pages = {}]", index + 1, self.instances.len(), + self.heap_pages, ); } } From e35326c8ba1a2f58f8c11eec9d1e6cd4cc4d18de Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sat, 31 Jul 2021 14:59:05 +0200 Subject: [PATCH 21/24] Try something new --- client/executor/src/wasm_runtime.rs | 50 ++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/client/executor/src/wasm_runtime.rs b/client/executor/src/wasm_runtime.rs index 6878dd118789b..17b5a8e57beab 100644 --- a/client/executor/src/wasm_runtime.rs +++ b/client/executor/src/wasm_runtime.rs @@ -127,7 +127,11 @@ impl VersionedRuntime { result }, None => { - log::warn!(target: "wasm-runtime", "Ran out of free WASM instances"); + log::warn!( + target: "wasm-runtime", + "Ran out of free WASM instances with {} pages", + self.heap_pages, + ); // Allocate a new instance let instance = self.module.new_instance()?; @@ -138,7 +142,9 @@ impl VersionedRuntime { } } -const MAX_RUNTIMES: usize = 2; +const MAX_CONSENSUS_RUNTIMES: usize = 2; +const MAX_OFFCHAIN_RUNTIMES: usize = 2; // TODO: this is kinda pointless, as it cannot be different than the consensus one. +const MAX_OFFCHAIN_RUNTIME_INSTANCES: usize = 1; /// Cache for the runtimes. /// @@ -149,15 +155,20 @@ const MAX_RUNTIMES: usize = 2; /// values of mutable globals. Follow-up requests to fetch a runtime return this one instance with /// the memory reset to the initial memory. So, one runtime instance is reused for every fetch /// request. -/// -/// The size of cache is equal to `MAX_RUNTIMES`. pub struct RuntimeCache { /// A cache of runtimes along with metadata. /// /// Runtimes sorted by recent usage. The most recently used is at the front. - runtimes: Mutex<[Option>; MAX_RUNTIMES]>, + /// + /// These runtime instances are only used in consensus code context and their count is limited + /// by `max_consensus_runtime_instances`. + consensus_runtimes: Mutex<[Option>; MAX_CONSENSUS_RUNTIMES]>, /// The size of the instances cache for each runtime. - max_runtime_instances: usize, + max_consensus_runtime_instances: usize, + /// Same as `consensus_runtimes`, but used in offchain code context. Their size is always bound + /// by 1 (can easily be configurable, but we don't need this right now). + offchain_runtimes: Mutex<[Option>; MAX_OFFCHAIN_RUNTIMES]>, + /// Optional path used for caching artifacts. cache_path: Option, } @@ -169,8 +180,13 @@ impl RuntimeCache { /// /// `cache_path` allows to specify an optional directory where the executor can store files /// for caching. - pub fn new(max_runtime_instances: usize, cache_path: Option) -> RuntimeCache { - RuntimeCache { runtimes: Default::default(), max_runtime_instances, cache_path } + pub fn new(max_consensus_runtime_instances: usize, cache_path: Option) -> RuntimeCache { + RuntimeCache { + consensus_runtimes: Default::default(), + offchain_runtimes: Default::default(), + max_consensus_runtime_instances, + cache_path, + } } /// Prepares a WASM module instance and executes given function for it. @@ -222,7 +238,17 @@ impl RuntimeCache { let code_hash = &runtime_code.hash; let heap_pages = runtime_code.heap_pages.unwrap_or(default_heap_pages); - let mut runtimes = self.runtimes.lock(); // this must be released prior to calling f + // this must be released prior to calling f. + let mut runtimes = match runtime_code.context { + sp_core::traits::CodeContext::Consensus => self.consensus_runtimes.lock(), + sp_core::traits::CodeContext::Offchain => self.offchain_runtimes.lock(), + }; + + let max_runtime_instances = match runtime_code.context { + sp_core::traits::CodeContext::Consensus => self.max_consensus_runtime_instances, + sp_core::traits::CodeContext::Offchain => MAX_OFFCHAIN_RUNTIME_INSTANCES, + }; + let pos = runtimes.iter().position(|r| { r.as_ref().map_or(false, |r| { r.wasm_method == wasm_method && @@ -249,7 +275,7 @@ impl RuntimeCache { heap_pages, host_functions.into(), allow_missing_func_imports, - self.max_runtime_instances, + max_runtime_instances, self.cache_path.as_deref(), ); @@ -280,8 +306,8 @@ impl RuntimeCache { runtimes.swap(i, i - 1); }, None => { - runtimes[MAX_RUNTIMES - 1] = Some(runtime.clone()); - for i in (1..MAX_RUNTIMES).rev() { + runtimes[MAX_CONSENSUS_RUNTIMES - 1] = Some(runtime.clone()); + for i in (1..MAX_CONSENSUS_RUNTIMES).rev() { runtimes.swap(i, i - 1); } }, From db1f7a3d237fa49dbcacb8a146e07a62d927bd5e Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sat, 31 Jul 2021 15:35:04 +0200 Subject: [PATCH 22/24] A new approach --- client/executor/src/native_executor.rs | 9 ++++-- client/executor/src/wasm_runtime.rs | 14 ++++----- client/service/src/client/call_executor.rs | 4 +-- client/service/src/client/wasm_override.rs | 30 ++++++++++--------- client/service/src/client/wasm_substitutes.rs | 21 +++++++------ 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/client/executor/src/native_executor.rs b/client/executor/src/native_executor.rs index 1445730f80ebf..48e0e6c5f888c 100644 --- a/client/executor/src/native_executor.rs +++ b/client/executor/src/native_executor.rs @@ -48,9 +48,14 @@ use sp_tasks::new_async_externalities; use sp_version::{NativeVersion, RuntimeVersion}; use sp_wasm_interface::{Function, HostFunctions}; -/// Default num of pages for the heap +/// Default num of pages for the heap of the runtime, when used for consensus operations. +/// +/// 128mb per instance. const DEFAULT_HEAP_PAGES_CONSENSUS: u64 = 2048; -const DEFAULT_HEAP_PAGES_OFFCHAIN: u64 = DEFAULT_HEAP_PAGES_CONSENSUS * 4; +/// Default num of pages for the heap of the runtime, when used for offchain operations. +/// +/// 256mb per instance. +const DEFAULT_HEAP_PAGES_OFFCHAIN: u64 = DEFAULT_HEAP_PAGES_CONSENSUS * 2; /// Set up the externalities and safe calling environment to execute runtime calls. /// diff --git a/client/executor/src/wasm_runtime.rs b/client/executor/src/wasm_runtime.rs index 17b5a8e57beab..fd4ddaeb7e348 100644 --- a/client/executor/src/wasm_runtime.rs +++ b/client/executor/src/wasm_runtime.rs @@ -142,8 +142,7 @@ impl VersionedRuntime { } } -const MAX_CONSENSUS_RUNTIMES: usize = 2; -const MAX_OFFCHAIN_RUNTIMES: usize = 2; // TODO: this is kinda pointless, as it cannot be different than the consensus one. +const MAX_RUNTIMES: usize = 2; const MAX_OFFCHAIN_RUNTIME_INSTANCES: usize = 1; /// Cache for the runtimes. @@ -162,12 +161,12 @@ pub struct RuntimeCache { /// /// These runtime instances are only used in consensus code context and their count is limited /// by `max_consensus_runtime_instances`. - consensus_runtimes: Mutex<[Option>; MAX_CONSENSUS_RUNTIMES]>, + consensus_runtimes: Mutex<[Option>; MAX_RUNTIMES]>, /// The size of the instances cache for each runtime. max_consensus_runtime_instances: usize, /// Same as `consensus_runtimes`, but used in offchain code context. Their size is always bound /// by 1 (can easily be configurable, but we don't need this right now). - offchain_runtimes: Mutex<[Option>; MAX_OFFCHAIN_RUNTIMES]>, + offchain_runtimes: Mutex<[Option>; MAX_RUNTIMES]>, /// Optional path used for caching artifacts. cache_path: Option, } @@ -284,8 +283,9 @@ impl RuntimeCache { #[cfg(not(target_os = "unknown"))] log::debug!( target: "wasm-runtime", - "Prepared new runtime version {:?} in {} ms.", + "Prepared new runtime version {:?} for {:?} in {} ms.", result.version, + runtime_code.context, time.elapsed().as_millis(), ); }, @@ -306,8 +306,8 @@ impl RuntimeCache { runtimes.swap(i, i - 1); }, None => { - runtimes[MAX_CONSENSUS_RUNTIMES - 1] = Some(runtime.clone()); - for i in (1..MAX_CONSENSUS_RUNTIMES).rev() { + runtimes[MAX_RUNTIMES - 1] = Some(runtime.clone()); + for i in (1..MAX_RUNTIMES).rev() { runtimes.swap(i, i - 1); } }, diff --git a/client/service/src/client/call_executor.rs b/client/service/src/client/call_executor.rs index 700777e13677a..96f643378a9df 100644 --- a/client/service/src/client/call_executor.rs +++ b/client/service/src/client/call_executor.rs @@ -97,12 +97,12 @@ where let code = if let Some(d) = self .wasm_override .as_ref() - .map(|o| o.get(&spec, onchain_code.heap_pages)) + .map(|o| o.get(&spec, onchain_code.heap_pages, onchain_code.context)) .flatten() { log::debug!(target: "wasm_overrides", "using WASM override for block {}", id); d - } else if let Some(s) = self.wasm_substitutes.get(spec, onchain_code.heap_pages, id) { + } else if let Some(s) = self.wasm_substitutes.get(spec, onchain_code.heap_pages, onchain_code.context, id) { log::debug!(target: "wasm_substitutes", "Using WASM substitute for block {:?}", id); s } else { diff --git a/client/service/src/client/wasm_override.rs b/client/service/src/client/wasm_override.rs index d43c4bb9edcfc..9913d1da0a26f 100644 --- a/client/service/src/client/wasm_override.rs +++ b/client/service/src/client/wasm_override.rs @@ -37,7 +37,7 @@ //! needed must be provided in the given directory. use sc_executor::RuntimeInfo; use sp_blockchain::Result; -use sp_core::traits::{FetchRuntimeCode, RuntimeCode}; +use sp_core::traits::{CodeContext, FetchRuntimeCode, RuntimeCode}; use sp_state_machine::BasicExternalities; use sp_version::RuntimeVersion; use std::{ @@ -60,13 +60,8 @@ impl WasmBlob { Self { code, hash } } - fn runtime_code(&self, heap_pages: Option) -> RuntimeCode { - RuntimeCode { - code_fetcher: self, - hash: self.hash.clone(), - context: sp_core::traits::CodeContext::Consensus, - heap_pages, - } + fn runtime_code(&self, heap_pages: Option, context: CodeContext) -> RuntimeCode { + RuntimeCode { code_fetcher: self, hash: self.hash.clone(), context, heap_pages } } } @@ -130,8 +125,13 @@ where /// Gets an override by it's runtime spec version. /// /// Returns `None` if an override for a spec version does not exist. - pub fn get<'a, 'b: 'a>(&'b self, spec: &u32, pages: Option) -> Option> { - self.overrides.get(spec).map(|w| w.runtime_code(pages)) + pub fn get<'a, 'b: 'a>( + &'b self, + spec: &u32, + pages: Option, + context: CodeContext, + ) -> Option> { + self.overrides.get(spec).map(|w| w.runtime_code(pages, context)) } /// Scrapes a folder for WASM runtimes. @@ -153,7 +153,7 @@ where match path.extension().map(|e| e.to_str()).flatten() { Some("wasm") => { let wasm = WasmBlob::new(fs::read(&path).map_err(handle_err)?); - let version = Self::runtime_version(executor, &wasm, Some(128))?; + let version = Self::runtime_version(executor, &wasm, Some(128), CodeContext::Consensus)?; log::info!( target: "wasm_overrides", "Found wasm override in file: `{:?}`, version: {}", @@ -185,10 +185,11 @@ where executor: &E, code: &WasmBlob, heap_pages: Option, + context: CodeContext, ) -> Result { let mut ext = BasicExternalities::default(); executor - .runtime_version(&mut ext, &code.runtime_code(heap_pages)) + .runtime_version(&mut ext, &code.runtime_code(heap_pages, context)) .map_err(|e| WasmOverrideError::VersionInvalid(format!("{:?}", e)).into()) } } @@ -234,8 +235,9 @@ mod tests { let executor = NativeExecutor::::new(WasmExecutionMethod::Interpreted, Some(128), 1); - let version = WasmOverride::runtime_version(&executor, &wasm, Some(128)) - .expect("should get the `RuntimeVersion` of the test-runtime wasm blob"); + let version = + WasmOverride::runtime_version(&executor, &wasm, Some(128), CodeContext::Consensus) + .expect("should get the `RuntimeVersion` of the test-runtime wasm blob"); assert_eq!(version.spec_version, 2); } diff --git a/client/service/src/client/wasm_substitutes.rs b/client/service/src/client/wasm_substitutes.rs index 025e931547702..1a9b6ef1740bb 100644 --- a/client/service/src/client/wasm_substitutes.rs +++ b/client/service/src/client/wasm_substitutes.rs @@ -57,13 +57,12 @@ impl WasmSubstitute { Ok(Self { code, hash, block_hash, block_number }) } - fn runtime_code(&self, heap_pages: Option) -> RuntimeCode { - RuntimeCode { - code_fetcher: self, - hash: self.hash.clone(), - heap_pages, - context: sp_core::traits::CodeContext::Consensus, - } + fn runtime_code( + &self, + heap_pages: Option, + context: sp_core::traits::CodeContext, + ) -> RuntimeCode { + RuntimeCode { code_fetcher: self, hash: self.hash.clone(), heap_pages, context } } /// Returns `true` when the substitute matches for the given `block_id`. @@ -173,10 +172,11 @@ where &self, spec: u32, pages: Option, + context: sp_core::traits::CodeContext, block_id: &BlockId, ) -> Option> { let s = self.substitutes.get(&spec)?; - s.matches(block_id, &*self.backend).then(|| s.runtime_code(pages)) + s.matches(block_id, &*self.backend).then(|| s.runtime_code(pages, context)) } fn runtime_version( @@ -185,7 +185,10 @@ where ) -> Result { let mut ext = BasicExternalities::default(); executor - .runtime_version(&mut ext, &code.runtime_code(None)) + .runtime_version( + &mut ext, + &code.runtime_code(None, sp_core::traits::CodeContext::Consensus), + ) .map_err(|e| WasmSubstituteError::VersionInvalid(format!("{:?}", e)).into()) } } From a4fb6c6c3152715465c5c018d062f32a5afe56e0 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sat, 31 Jul 2021 15:58:14 +0200 Subject: [PATCH 23/24] Remove the stale heap pages param --- bin/node/executor/tests/common.rs | 2 +- bin/node/testing/src/bench.rs | 2 +- client/cli/src/config.rs | 1 - client/executor/src/native_executor.rs | 5 +---- client/service/src/builder.rs | 2 -- client/service/src/client/call_executor.rs | 2 +- client/service/src/client/wasm_override.rs | 3 +-- client/service/src/config.rs | 2 -- client/service/test/src/client/light.rs | 2 +- client/service/test/src/client/mod.rs | 2 +- client/service/test/src/lib.rs | 1 - primitives/api/test/tests/runtime_calls.rs | 2 +- test-utils/client/src/lib.rs | 2 +- test-utils/runtime/client/src/lib.rs | 2 +- test-utils/runtime/src/system.rs | 2 +- test-utils/test-runner/src/utils.rs | 1 - utils/browser/src/lib.rs | 1 - utils/frame/benchmarking-cli/src/command.rs | 2 +- utils/frame/try-runtime/cli/src/lib.rs | 12 ++++++------ 19 files changed, 18 insertions(+), 30 deletions(-) diff --git a/bin/node/executor/tests/common.rs b/bin/node/executor/tests/common.rs index c72b617d34ab3..857f3a81af1d1 100644 --- a/bin/node/executor/tests/common.rs +++ b/bin/node/executor/tests/common.rs @@ -97,7 +97,7 @@ pub fn from_block_number(n: u32) -> Header { } pub fn executor() -> NativeExecutor { - NativeExecutor::new(WasmExecutionMethod::Interpreted, None, 8) + NativeExecutor::new(WasmExecutionMethod::Interpreted, 8) } pub fn executor_call< diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index d10f81f241e16..c9390b5157442 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -391,7 +391,7 @@ impl BenchDb { let backend = sc_service::new_db_backend(db_config).expect("Should not fail"); let client = sc_service::new_client( backend.clone(), - NativeExecutor::new(WasmExecutionMethod::Compiled, None, 8), + NativeExecutor::new(WasmExecutionMethod::Compiled, 8), &keyring.generate_genesis(), None, None, diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index 56c5f99c69f57..8baee21d376f8 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -520,7 +520,6 @@ pub trait CliConfiguration: Sized { prometheus_config: self.prometheus_config(DCV::prometheus_listen_port())?, telemetry_endpoints, telemetry_external_transport: self.telemetry_external_transport()?, - default_heap_pages: self.default_heap_pages()?, offchain_worker: self.offchain_worker(&role)?, force_authoring: self.force_authoring()?, disable_grandpa: self.disable_grandpa()?, diff --git a/client/executor/src/native_executor.rs b/client/executor/src/native_executor.rs index 48e0e6c5f888c..90b775f633836 100644 --- a/client/executor/src/native_executor.rs +++ b/client/executor/src/native_executor.rs @@ -303,9 +303,6 @@ impl NativeExecutor { /// Defaults to `DEFAULT_HEAP_PAGES` if `None` is provided. pub fn new( fallback_method: WasmExecutionMethod, - // deprecated: this is the CLI configurable heap pages, which no longer exists and is - // ALWAYS NONE. This parameter should be removed. - _default_heap_pages: Option, max_runtime_instances: usize, ) -> Self { let extended = D::ExtendHostFunctions::host_functions(); @@ -671,7 +668,7 @@ mod tests { #[test] fn native_executor_registers_custom_interface() { - let executor = NativeExecutor::::new(WasmExecutionMethod::Interpreted, None, 8); + let executor = NativeExecutor::::new(WasmExecutionMethod::Interpreted, 8); my_interface::HostFunctions::host_functions().iter().for_each(|function| { assert_eq!(executor.wasm.host_functions.iter().filter(|f| f == &function).count(), 2); }); diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 7844610dabbcc..eec1270e6d3ff 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -292,7 +292,6 @@ where let executor = NativeExecutor::::new( config.wasm_method, - config.default_heap_pages, config.max_runtime_instances, ); @@ -383,7 +382,6 @@ where let executor = NativeExecutor::::new( config.wasm_method, - config.default_heap_pages, config.max_runtime_instances, ); diff --git a/client/service/src/client/call_executor.rs b/client/service/src/client/call_executor.rs index 96f643378a9df..4a4e05d6e6ad1 100644 --- a/client/service/src/client/call_executor.rs +++ b/client/service/src/client/call_executor.rs @@ -368,7 +368,7 @@ mod tests { #[test] fn should_get_override_if_exists() { let executor = - NativeExecutor::::new(WasmExecutionMethod::Interpreted, Some(128), 1); + NativeExecutor::::new(WasmExecutionMethod::Interpreted, 1); let overrides = crate::client::wasm_override::dummy_overrides(&executor); let onchain_code = WrappedRuntimeCode(substrate_test_runtime::wasm_binary_unwrap().into()); diff --git a/client/service/src/client/wasm_override.rs b/client/service/src/client/wasm_override.rs index 9913d1da0a26f..884894c940545 100644 --- a/client/service/src/client/wasm_override.rs +++ b/client/service/src/client/wasm_override.rs @@ -220,7 +220,6 @@ mod tests { { let exec = NativeExecutor::::new( WasmExecutionMethod::Interpreted, - Some(128), 1, ); let bytes = substrate_test_runtime::wasm_binary_unwrap(); @@ -233,7 +232,7 @@ mod tests { fn should_get_runtime_version() { let wasm = WasmBlob::new(substrate_test_runtime::wasm_binary_unwrap().to_vec()); let executor = - NativeExecutor::::new(WasmExecutionMethod::Interpreted, Some(128), 1); + NativeExecutor::::new(WasmExecutionMethod::Interpreted, 1); let version = WasmOverride::runtime_version(&executor, &wasm, Some(128), CodeContext::Consensus) diff --git a/client/service/src/config.rs b/client/service/src/config.rs index ce8bed14ea3a2..44554743bea0c 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -114,8 +114,6 @@ pub struct Configuration { /// External WASM transport for the telemetry. If `Some`, when connection to a telemetry /// endpoint, this transport will be tried in priority before all others. pub telemetry_external_transport: Option, - /// The default number of 64KB pages to allocate for Wasm execution - pub default_heap_pages: Option, /// Should offchain workers be executed. pub offchain_worker: OffchainWorkerConfig, /// Enable authoring even when offline. diff --git a/client/service/test/src/client/light.rs b/client/service/test/src/client/light.rs index 485102ef22e19..04b1339b0b135 100644 --- a/client/service/test/src/client/light.rs +++ b/client/service/test/src/client/light.rs @@ -263,7 +263,7 @@ impl CallExecutor for DummyCallExecutor { } fn local_executor() -> NativeExecutor { - NativeExecutor::new(WasmExecutionMethod::Interpreted, None, 8) + NativeExecutor::new(WasmExecutionMethod::Interpreted, 8) } #[test] diff --git a/client/service/test/src/client/mod.rs b/client/service/test/src/client/mod.rs index ce66816c81f3d..9f54d1a067362 100644 --- a/client/service/test/src/client/mod.rs +++ b/client/service/test/src/client/mod.rs @@ -70,7 +70,7 @@ native_executor_instance!( ); fn executor() -> sc_executor::NativeExecutor { - sc_executor::NativeExecutor::new(sc_executor::WasmExecutionMethod::Interpreted, None, 8) + sc_executor::NativeExecutor::new(sc_executor::WasmExecutionMethod::Interpreted, 8) } pub fn prepare_client_with_key_changes() -> ( diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index 51b1f4f805153..030e18a34ace7 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -257,7 +257,6 @@ fn node_config< prometheus_config: None, telemetry_endpoints: None, telemetry_external_transport: None, - default_heap_pages: None, offchain_worker: Default::default(), force_authoring: false, disable_grandpa: false, diff --git a/primitives/api/test/tests/runtime_calls.rs b/primitives/api/test/tests/runtime_calls.rs index dfd71bd6dea2d..9bb8a88b73703 100644 --- a/primitives/api/test/tests/runtime_calls.rs +++ b/primitives/api/test/tests/runtime_calls.rs @@ -207,7 +207,7 @@ fn record_proof_works() { // Use the proof backend to execute `execute_block`. let mut overlay = Default::default(); - let executor = NativeExecutor::::new(WasmExecutionMethod::Interpreted, None, 8); + let executor = NativeExecutor::::new(WasmExecutionMethod::Interpreted, 8); execution_proof_check_on_trie_backend::<_, u64, _, _>( &backend, &mut overlay, diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index 42fd56c7c8a7b..92d99814dee3f 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -288,7 +288,7 @@ impl { let executor = executor .into() - .unwrap_or_else(|| NativeExecutor::new(WasmExecutionMethod::Interpreted, None, 8)); + .unwrap_or_else(|| NativeExecutor::new(WasmExecutionMethod::Interpreted, 8)); let executor = LocalCallExecutor::new( self.backend.clone(), executor, diff --git a/test-utils/runtime/client/src/lib.rs b/test-utils/runtime/client/src/lib.rs index 3db433968c9f8..2ac42a60438e6 100644 --- a/test-utils/runtime/client/src/lib.rs +++ b/test-utils/runtime/client/src/lib.rs @@ -427,5 +427,5 @@ pub fn new_light_fetcher() -> LightFetcher { /// Create a new native executor. pub fn new_native_executor() -> sc_executor::NativeExecutor { - sc_executor::NativeExecutor::new(sc_executor::WasmExecutionMethod::Interpreted, None, 8) + sc_executor::NativeExecutor::new(sc_executor::WasmExecutionMethod::Interpreted, 8) } diff --git a/test-utils/runtime/src/system.rs b/test-utils/runtime/src/system.rs index 0daace6d6ee8f..ddb752b216ed4 100644 --- a/test-utils/runtime/src/system.rs +++ b/test-utils/runtime/src/system.rs @@ -362,7 +362,7 @@ mod tests { native_executor_instance!(NativeDispatch, crate::api::dispatch, crate::native_version); fn executor() -> NativeExecutor { - NativeExecutor::new(WasmExecutionMethod::Interpreted, None, 8) + NativeExecutor::new(WasmExecutionMethod::Interpreted, 8) } fn new_test_ext() -> TestExternalities { diff --git a/test-utils/test-runner/src/utils.rs b/test-utils/test-runner/src/utils.rs index d4a8481d60298..1f547b1ec149b 100644 --- a/test-utils/test-runner/src/utils.rs +++ b/test-utils/test-runner/src/utils.rs @@ -102,7 +102,6 @@ pub fn default_config( prometheus_config: None, telemetry_endpoints: None, telemetry_external_transport: None, - default_heap_pages: None, offchain_worker: Default::default(), force_authoring: false, disable_grandpa: false, diff --git a/utils/browser/src/lib.rs b/utils/browser/src/lib.rs index 2a0227cb08f7d..c8229881288e5 100644 --- a/utils/browser/src/lib.rs +++ b/utils/browser/src/lib.rs @@ -87,7 +87,6 @@ where }, keystore_remote: Default::default(), keystore: KeystoreConfig::InMemory, - default_heap_pages: Default::default(), dev_key_seed: Default::default(), disable_grandpa: Default::default(), execution_configs: Default::default(), diff --git a/utils/frame/benchmarking-cli/src/command.rs b/utils/frame/benchmarking-cli/src/command.rs index 52b4db513d357..9521f378c33f6 100644 --- a/utils/frame/benchmarking-cli/src/command.rs +++ b/utils/frame/benchmarking-cli/src/command.rs @@ -70,7 +70,7 @@ impl BenchmarkCmd { let state = BenchmarkingState::::new(genesis_storage, cache_size, self.record_proof)?; let executor = NativeExecutor::::new( wasm_method, - self.heap_pages, + // TODO: set the heap pages via state alteration 2, // The runtime instances cache size. ); diff --git a/utils/frame/try-runtime/cli/src/lib.rs b/utils/frame/try-runtime/cli/src/lib.rs index 7b75e3abfc2cd..0c68200196117 100644 --- a/utils/frame/try-runtime/cli/src/lib.rs +++ b/utils/frame/try-runtime/cli/src/lib.rs @@ -187,12 +187,12 @@ where { let wasm_method = shared.wasm_method; let execution: sp_state_machine::ExecutionStrategy = shared.execution.into(); - let heap_pages = shared.heap_pages.or(config.default_heap_pages); + // TODO: set the heap pages. let mut changes = Default::default(); let max_runtime_instances = config.max_runtime_instances; let executor = - NativeExecutor::::new(wasm_method.into(), heap_pages, max_runtime_instances); + NativeExecutor::::new(wasm_method.into(), max_runtime_instances); let ext = { let builder = match command.state { @@ -261,12 +261,12 @@ where { let wasm_method = shared.wasm_method; let execution: sp_state_machine::ExecutionStrategy = shared.execution.into(); - let heap_pages = shared.heap_pages.or(config.default_heap_pages); + // TODO: set heap pages. let mut changes = Default::default(); let max_runtime_instances = config.max_runtime_instances; let executor = - NativeExecutor::::new(wasm_method.into(), heap_pages, max_runtime_instances); + NativeExecutor::::new(wasm_method.into(), max_runtime_instances); let mode = match command.state { State::Live { snapshot_path, modules } => { @@ -343,12 +343,12 @@ where { let wasm_method = shared.wasm_method; let execution: sp_state_machine::ExecutionStrategy = shared.execution.into(); - let heap_pages = shared.heap_pages.or(config.default_heap_pages); + // TODO: set heap pages. let mut changes = Default::default(); let max_runtime_instances = config.max_runtime_instances; let executor = - NativeExecutor::::new(wasm_method.into(), heap_pages, max_runtime_instances); + NativeExecutor::::new(wasm_method.into(), max_runtime_instances); let block_hash = shared.block_at::()?; let block: Block = rpc_api::get_block::(shared.url.clone(), block_hash).await?; From 946cfde824da6bc5947279f0af5f8bffc0dbe751 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Thu, 5 Aug 2021 16:51:44 +0200 Subject: [PATCH 24/24] fmt it properly --- bin/node/testing/src/bench.rs | 2 +- client/api/src/call_executor.rs | 2 +- client/api/src/execution_extensions.rs | 2 +- client/api/src/lib.rs | 2 +- client/cli/src/params/import_params.rs | 10 ++--- client/executor/src/native_executor.rs | 5 +-- client/light/src/call_executor.rs | 4 +- client/rpc/src/state/state_full.rs | 28 +++++++------ client/service/src/builder.rs | 12 ++---- client/service/src/client/call_executor.rs | 28 ++++++++----- client/service/src/client/wasm_override.rs | 6 +-- client/service/src/config.rs | 2 +- client/service/test/src/client/light.rs | 16 ++++---- client/service/test/src/client/mod.rs | 23 +++++------ frame/system/src/mock.rs | 3 +- primitives/runtime-interface/test/src/lib.rs | 2 +- primitives/state-machine/src/lib.rs | 43 +++++++++----------- test-utils/client/src/lib.rs | 4 +- test-utils/test-runner/src/utils.rs | 22 +++++++--- utils/frame/try-runtime/cli/src/lib.rs | 9 ++-- 20 files changed, 115 insertions(+), 110 deletions(-) diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index a20a33491b028..28c65a8735417 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -41,7 +41,7 @@ use node_runtime::{ }; use sc_block_builder::BlockBuilderProvider; use sc_client_api::{ - execution_extensions::{ExecutionExtensions, ExecutionConfigs, ExecutionConfig}, + execution_extensions::{ExecutionConfig, ExecutionConfigs, ExecutionExtensions}, BlockBackend, ExecutionStrategy, }; use sc_client_db::PruningMode; diff --git a/client/api/src/call_executor.rs b/client/api/src/call_executor.rs index 889159d4b0328..7d08d9a6b5312 100644 --- a/client/api/src/call_executor.rs +++ b/client/api/src/call_executor.rs @@ -23,7 +23,7 @@ use sc_executor::{NativeVersion, RuntimeVersion}; use sp_core::NativeOrEncoded; use sp_externalities::Extensions; use sp_runtime::{generic::BlockId, traits::Block as BlockT}; -use sp_state_machine::{ExecutionManager, OverlayedChanges, StorageProof, ExecutionConfig}; +use sp_state_machine::{ExecutionConfig, ExecutionManager, OverlayedChanges, StorageProof}; use std::{cell::RefCell, panic::UnwindSafe, result}; use crate::execution_extensions::ExecutionExtensions; diff --git a/client/api/src/execution_extensions.rs b/client/api/src/execution_extensions.rs index ef0afcde37911..eaf4620df186d 100644 --- a/client/api/src/execution_extensions.rs +++ b/client/api/src/execution_extensions.rs @@ -32,8 +32,8 @@ use sp_core::{ use sp_externalities::Extensions; use sp_keystore::{KeystoreExt, SyncCryptoStorePtr}; use sp_runtime::{generic::BlockId, traits}; -pub use sp_state_machine::{ExecutionStrategy, ExecutionConfig}; use sp_state_machine::{DefaultHandler, ExecutionManager}; +pub use sp_state_machine::{ExecutionConfig, ExecutionStrategy}; use std::sync::{Arc, Weak}; /// Execution configurations, per operation type. diff --git a/client/api/src/lib.rs b/client/api/src/lib.rs index 385d2d5ba1a7f..69b0f6dc56ee3 100644 --- a/client/api/src/lib.rs +++ b/client/api/src/lib.rs @@ -39,7 +39,7 @@ pub use proof_provider::*; pub use sp_blockchain as blockchain; pub use sp_blockchain::HeaderBackend; -pub use sp_state_machine::{ExecutionStrategy, StorageProof, ExecutionConfig}; +pub use sp_state_machine::{ExecutionConfig, ExecutionStrategy, StorageProof}; pub use sp_storage::{ChildInfo, PrefixedStorageKey, StorageData, StorageKey}; /// Usage Information Provider interface diff --git a/client/cli/src/params/import_params.rs b/client/cli/src/params/import_params.rs index 3300ee1f4a3f9..5a4fbaadc2c0d 100644 --- a/client/cli/src/params/import_params.rs +++ b/client/cli/src/params/import_params.rs @@ -18,16 +18,16 @@ use crate::{ arg_enums::{ - ExecutionStrategy, WasmExecutionMethod, DEFAULT_STRATEGY_BLOCK_CONSTRUCTION, + ExecutionStrategy, WasmExecutionMethod, DEFAULT_CODE_CONTEXT_BLOCK_CONSTRUCTION, + DEFAULT_CODE_CONTEXT_IMPORT_BLOCK, DEFAULT_CODE_CONTEXT_IMPORT_BLOCK_VALIDATOR, + DEFAULT_CODE_CONTEXT_OFFCHAIN_WORKER, DEFAULT_CODE_CONTEXT_OTHER, + DEFAULT_CODE_CONTEXT_SYNCING, DEFAULT_STRATEGY_BLOCK_CONSTRUCTION, DEFAULT_STRATEGY_IMPORT_BLOCK, DEFAULT_STRATEGY_IMPORT_BLOCK_VALIDATOR, DEFAULT_STRATEGY_OFFCHAIN_WORKER, DEFAULT_STRATEGY_OTHER, DEFAULT_STRATEGY_SYNCING, - DEFAULT_CODE_CONTEXT_SYNCING, DEFAULT_CODE_CONTEXT_IMPORT_BLOCK, - DEFAULT_CODE_CONTEXT_IMPORT_BLOCK_VALIDATOR,DEFAULT_CODE_CONTEXT_BLOCK_CONSTRUCTION, - DEFAULT_CODE_CONTEXT_OFFCHAIN_WORKER, DEFAULT_CODE_CONTEXT_OTHER, }, params::{DatabaseParams, PruningParams}, }; -use sc_client_api::execution_extensions::{ExecutionConfigs, ExecutionConfig}; +use sc_client_api::execution_extensions::{ExecutionConfig, ExecutionConfigs}; use std::path::PathBuf; use structopt::StructOpt; diff --git a/client/executor/src/native_executor.rs b/client/executor/src/native_executor.rs index 90b775f633836..cb5914ab35e4f 100644 --- a/client/executor/src/native_executor.rs +++ b/client/executor/src/native_executor.rs @@ -301,10 +301,7 @@ impl NativeExecutor { /// `fallback_method` - Method used to execute fallback Wasm code. /// `default_heap_pages` - Number of 64KB pages to allocate for Wasm execution. /// Defaults to `DEFAULT_HEAP_PAGES` if `None` is provided. - pub fn new( - fallback_method: WasmExecutionMethod, - max_runtime_instances: usize, - ) -> Self { + pub fn new(fallback_method: WasmExecutionMethod, max_runtime_instances: usize) -> Self { let extended = D::ExtendHostFunctions::host_functions(); let mut host_functions = sp_io::SubstrateHostFunctions::host_functions() .into_iter() diff --git a/client/light/src/call_executor.rs b/client/light/src/call_executor.rs index b37bd3bc0cd3d..d88423a8f18dc 100644 --- a/client/light/src/call_executor.rs +++ b/client/light/src/call_executor.rs @@ -33,8 +33,8 @@ use sp_runtime::{ traits::{Block as BlockT, Header as HeaderT}, }; use sp_state_machine::{ - create_proof_check_backend, execution_proof_check_on_trie_backend, ExecutionManager, - ExecutionStrategy, OverlayedChanges, StorageProof, ExecutionConfig, + create_proof_check_backend, execution_proof_check_on_trie_backend, ExecutionConfig, + ExecutionManager, ExecutionStrategy, OverlayedChanges, StorageProof, }; use sp_api::{ProofRecorder, StorageTransactionCache}; diff --git a/client/rpc/src/state/state_full.rs b/client/rpc/src/state/state_full.rs index 88680e4ac9016..7555463a002e1 100644 --- a/client/rpc/src/state/state_full.rs +++ b/client/rpc/src/state/state_full.rs @@ -283,19 +283,21 @@ where method: String, call_data: Bytes, ) -> FutureResult { - let r = self.block_or_best(block) - .and_then(|block| self - .client - .executor() - .call( - &BlockId::Hash(block), - &method, - &*call_data, - self.client.execution_extensions().configs().other, - None, - ) - .map(Into::into) - ).map_err(client_err); + let r = self + .block_or_best(block) + .and_then(|block| { + self.client + .executor() + .call( + &BlockId::Hash(block), + &method, + &*call_data, + self.client.execution_extensions().configs().other, + None, + ) + .map(Into::into) + }) + .map_err(client_err); Box::new(result(r)) } diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index e7e3e57dfecac..99fa110ff1571 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -291,10 +291,8 @@ where TaskManager::new(config.task_executor.clone(), registry)? }; - let executor = NativeExecutor::::new( - config.wasm_method, - config.max_runtime_instances, - ); + let executor = + NativeExecutor::::new(config.wasm_method, config.max_runtime_instances); let chain_spec = &config.chain_spec; let fork_blocks = get_extension::>(chain_spec.extensions()) @@ -381,10 +379,8 @@ where TaskManager::new(config.task_executor.clone(), registry)? }; - let executor = NativeExecutor::::new( - config.wasm_method, - config.max_runtime_instances, - ); + let executor = + NativeExecutor::::new(config.wasm_method, config.max_runtime_instances); let db_storage = { let db_settings = sc_client_db::DatabaseSettings { diff --git a/client/service/src/client/call_executor.rs b/client/service/src/client/call_executor.rs index 4a4e05d6e6ad1..cc44d16220045 100644 --- a/client/service/src/client/call_executor.rs +++ b/client/service/src/client/call_executor.rs @@ -31,8 +31,8 @@ use sp_runtime::{ traits::{Block as BlockT, NumberFor}, }; use sp_state_machine::{ - self, backend::Backend as _, ExecutionManager, Ext, OverlayedChanges, - StateMachine, StorageProof, + self, backend::Backend as _, ExecutionManager, Ext, OverlayedChanges, StateMachine, + StorageProof, }; use std::{cell::RefCell, panic::UnwindSafe, result, sync::Arc}; @@ -102,7 +102,10 @@ where { log::debug!(target: "wasm_overrides", "using WASM override for block {}", id); d - } else if let Some(s) = self.wasm_substitutes.get(spec, onchain_code.heap_pages, onchain_code.context, id) { + } else if let Some(s) = + self.wasm_substitutes + .get(spec, onchain_code.heap_pages, onchain_code.context, id) + { log::debug!(target: "wasm_substitutes", "Using WASM substitute for block {:?}", id); s } else { @@ -157,8 +160,9 @@ where backend::changes_tries_state_at_block(at, self.backend.changes_trie_storage())?; let state = self.backend.state_at(*at)?; let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); - let runtime_code = - state_runtime_code.runtime_code(config.context).map_err(sp_blockchain::Error::RuntimeCode)?; + let runtime_code = state_runtime_code + .runtime_code(config.context) + .map_err(sp_blockchain::Error::RuntimeCode)?; let runtime_code = self.check_override(runtime_code, at)?; @@ -230,7 +234,8 @@ where sp_state_machine::backend::BackendRuntimeCode::new(trie_state); // It is important to extract the runtime code here before we create the proof // recorder. - let runtime_code = state_runtime_code.runtime_code(execution_manager.context) + let runtime_code = state_runtime_code + .runtime_code(execution_manager.context) .map_err(sp_blockchain::Error::RuntimeCode)?; let runtime_code = self.check_override(runtime_code, at)?; @@ -260,7 +265,8 @@ where }, None => { let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); - let runtime_code = state_runtime_code.runtime_code(execution_manager.context) + let runtime_code = state_runtime_code + .runtime_code(execution_manager.context) .map_err(sp_blockchain::Error::RuntimeCode)?; let runtime_code = self.check_override(runtime_code, at)?; @@ -318,8 +324,9 @@ where })?; let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(trie_backend); - let runtime_code = - state_runtime_code.runtime_code(sp_core::traits::CodeContext::Consensus).map_err(sp_blockchain::Error::RuntimeCode)?; + let runtime_code = state_runtime_code + .runtime_code(sp_core::traits::CodeContext::Consensus) + .map_err(sp_blockchain::Error::RuntimeCode)?; let runtime_code = self.check_override(runtime_code, at)?; sp_state_machine::prove_execution_on_trie_backend::<_, _, NumberFor, _, _>( @@ -367,8 +374,7 @@ mod tests { #[test] fn should_get_override_if_exists() { - let executor = - NativeExecutor::::new(WasmExecutionMethod::Interpreted, 1); + let executor = NativeExecutor::::new(WasmExecutionMethod::Interpreted, 1); let overrides = crate::client::wasm_override::dummy_overrides(&executor); let onchain_code = WrappedRuntimeCode(substrate_test_runtime::wasm_binary_unwrap().into()); diff --git a/client/service/src/client/wasm_override.rs b/client/service/src/client/wasm_override.rs index 884894c940545..982f2b6fba382 100644 --- a/client/service/src/client/wasm_override.rs +++ b/client/service/src/client/wasm_override.rs @@ -153,7 +153,8 @@ where match path.extension().map(|e| e.to_str()).flatten() { Some("wasm") => { let wasm = WasmBlob::new(fs::read(&path).map_err(handle_err)?); - let version = Self::runtime_version(executor, &wasm, Some(128), CodeContext::Consensus)?; + let version = + Self::runtime_version(executor, &wasm, Some(128), CodeContext::Consensus)?; log::info!( target: "wasm_overrides", "Found wasm override in file: `{:?}`, version: {}", @@ -231,8 +232,7 @@ mod tests { #[test] fn should_get_runtime_version() { let wasm = WasmBlob::new(substrate_test_runtime::wasm_binary_unwrap().to_vec()); - let executor = - NativeExecutor::::new(WasmExecutionMethod::Interpreted, 1); + let executor = NativeExecutor::::new(WasmExecutionMethod::Interpreted, 1); let version = WasmOverride::runtime_version(&executor, &wasm, Some(128), CodeContext::Consensus) diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 44554743bea0c..b301404cdfbf5 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -34,8 +34,8 @@ pub use sc_network::{ }; use prometheus_endpoint::Registry; -pub use sc_client_api::execution_extensions::ExecutionConfigs; use sc_chain_spec::ChainSpec; +pub use sc_client_api::execution_extensions::ExecutionConfigs; pub use sc_telemetry::TelemetryEndpoints; pub use sc_transaction_pool::Options as TransactionPoolOptions; use sp_core::crypto::SecretString; diff --git a/client/service/test/src/client/light.rs b/client/service/test/src/client/light.rs index 04b1339b0b135..ec28892ddf532 100644 --- a/client/service/test/src/client/light.rs +++ b/client/service/test/src/client/light.rs @@ -429,13 +429,15 @@ fn code_is_executed_at_genesis_only() { let genesis_executor = GenesisCallExecutor::new(backend, DummyCallExecutor); assert_eq!( - genesis_executor.call( - &BlockId::Number(0), - "test_method", - &[], - ExecutionStrategy::NativeElseWasm.in_consensus(), - None, - ).unwrap(), + genesis_executor + .call( + &BlockId::Number(0), + "test_method", + &[], + ExecutionStrategy::NativeElseWasm.in_consensus(), + None, + ) + .unwrap(), vec![42], ); diff --git a/client/service/test/src/client/mod.rs b/client/service/test/src/client/mod.rs index 30b6e7a1d4b9f..672cf1c43ee89 100644 --- a/client/service/test/src/client/mod.rs +++ b/client/service/test/src/client/mod.rs @@ -220,9 +220,9 @@ fn construct_block( Default::default(), &runtime_code, task_executor.clone() as Box<_>, - ).execute( - ExecutionStrategy::NativeElseWasm.in_consensus(), - ).unwrap(); + ) + .execute(ExecutionStrategy::NativeElseWasm.in_consensus()) + .unwrap(); header = Header::decode(&mut &ret_data[..]).unwrap(); (vec![].and(&Block { header, extrinsics: transactions }), hash) @@ -275,9 +275,9 @@ fn construct_genesis_should_work_with_native() { Default::default(), &runtime_code, TaskExecutor::new(), - ).execute( - ExecutionStrategy::NativeElseWasm.in_consensus(), - ).unwrap(); + ) + .execute(ExecutionStrategy::NativeElseWasm.in_consensus()) + .unwrap(); } #[test] @@ -312,9 +312,9 @@ fn construct_genesis_should_work_with_wasm() { Default::default(), &runtime_code, TaskExecutor::new(), - ).execute( - ExecutionStrategy::AlwaysWasm.in_consensus(), - ).unwrap(); + ) + .execute(ExecutionStrategy::AlwaysWasm.in_consensus()) + .unwrap(); } #[test] @@ -349,9 +349,8 @@ fn construct_genesis_with_bad_transaction_should_panic() { Default::default(), &runtime_code, TaskExecutor::new(), - ).execute( - ExecutionStrategy::NativeElseWasm.in_consensus(), - ); + ) + .execute(ExecutionStrategy::NativeElseWasm.in_consensus()); assert!(r.is_err()); } diff --git a/frame/system/src/mock.rs b/frame/system/src/mock.rs index 5446adddf8447..480e8b1a26bae 100644 --- a/frame/system/src/mock.rs +++ b/frame/system/src/mock.rs @@ -116,8 +116,7 @@ impl Config for Test { pub type SysEvent = frame_system::Event; /// A simple call, which one doesn't matter. -pub const CALL: &::Call = - &Call::System(frame_system::Call::set_heap_pages(0u64)); +pub const CALL: &::Call = &Call::System(frame_system::Call::set_heap_pages(0u64)); /// Create new externalities for `System` module tests. pub fn new_test_ext() -> sp_io::TestExternalities { diff --git a/primitives/runtime-interface/test/src/lib.rs b/primitives/runtime-interface/test/src/lib.rs index 9471c11d94903..21c016d723162 100644 --- a/primitives/runtime-interface/test/src/lib.rs +++ b/primitives/runtime-interface/test/src/lib.rs @@ -55,7 +55,7 @@ fn call_wasm_method_with_result( false, method, &[], - 8 + 8, ) .map_err(|e| format!("Failed to execute `{}`: {}", method, e))?; Ok(ext) diff --git a/primitives/state-machine/src/lib.rs b/primitives/state-machine/src/lib.rs index 384eac1c8f337..79516087ebff6 100644 --- a/primitives/state-machine/src/lib.rs +++ b/primitives/state-machine/src/lib.rs @@ -175,7 +175,7 @@ mod execution { use sp_core::{ hexdisplay::HexDisplay, storage::ChildInfo, - traits::{CodeExecutor, ReadRuntimeVersionExt, RuntimeCode, SpawnNamed, CodeContext}, + traits::{CodeContext, CodeExecutor, ReadRuntimeVersionExt, RuntimeCode, SpawnNamed}, NativeOrEncoded, NeverNativeValue, }; use sp_externalities::Extensions; @@ -270,12 +270,10 @@ mod execution { }; let strategy = match self.strategy { - ExecutionStrategy::AlwaysWasm => { - ExecutionStrategyWithHandler::AlwaysWasm(BackendTrustLevel::Trusted) - } - ExecutionStrategy::NativeWhenPossible => { - ExecutionStrategyWithHandler::NativeWhenPossible - } + ExecutionStrategy::AlwaysWasm => + ExecutionStrategyWithHandler::AlwaysWasm(BackendTrustLevel::Trusted), + ExecutionStrategy::NativeWhenPossible => + ExecutionStrategyWithHandler::NativeWhenPossible, ExecutionStrategy::NativeElseWasm => ExecutionStrategyWithHandler::NativeElseWasm, ExecutionStrategy::Both => ExecutionStrategyWithHandler::Both(failure_handler), }; @@ -334,9 +332,8 @@ mod execution { impl<'a, F> From<&'a ExecutionManager> for ExecutionConfig { fn from(s: &'a ExecutionManager) -> Self { let strategy = match s.strategy { - ExecutionStrategyWithHandler::NativeWhenPossible => { - ExecutionStrategy::NativeWhenPossible - } + ExecutionStrategyWithHandler::NativeWhenPossible => + ExecutionStrategy::NativeWhenPossible, ExecutionStrategyWithHandler::AlwaysWasm(_) => ExecutionStrategy::AlwaysWasm, ExecutionStrategyWithHandler::NativeElseWasm => ExecutionStrategy::NativeElseWasm, ExecutionStrategyWithHandler::Both(_) => ExecutionStrategy::Both, @@ -621,11 +618,12 @@ mod execution { ) -> Result, Box> where R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result> + UnwindSafe, + NC: FnOnce() -> result::Result> + + UnwindSafe, Handler: FnOnce( CallResult, CallResult, - ) -> CallResult + ) -> CallResult, { let changes_tries_enabled = self.changes_trie_state.is_some(); self.overlay.set_collect_extrinsics(changes_tries_enabled); @@ -634,21 +632,18 @@ mod execution { match manager.strategy { ExecutionStrategyWithHandler::Both(on_consensus_failure) => self .execute_call_with_both_strategy(native_call.take(), on_consensus_failure), - ExecutionStrategyWithHandler::NativeElseWasm => { - self.execute_call_with_native_else_wasm_strategy(native_call.take()) - } + ExecutionStrategyWithHandler::NativeElseWasm => + self.execute_call_with_native_else_wasm_strategy(native_call.take()), ExecutionStrategyWithHandler::AlwaysWasm(trust_level) => { let _abort_guard = match trust_level { BackendTrustLevel::Trusted => None, - BackendTrustLevel::Untrusted => { - Some(sp_panic_handler::AbortGuard::never_abort()) - } + BackendTrustLevel::Untrusted => + Some(sp_panic_handler::AbortGuard::never_abort()), }; self.execute_aux(false, native_call).0 - } - ExecutionStrategyWithHandler::NativeWhenPossible => { - self.execute_aux(true, native_call).0 - } + }, + ExecutionStrategyWithHandler::NativeWhenPossible => + self.execute_aux(true, native_call).0, } }; @@ -1167,7 +1162,9 @@ mod tests { ); assert_eq!( - state_machine.execute(ExecutionStrategy::NativeWhenPossible.in_consensus()).unwrap(), + state_machine + .execute(ExecutionStrategy::NativeWhenPossible.in_consensus()) + .unwrap(), vec![66], ); } diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index 92d99814dee3f..2cd8b7320a6e2 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -24,7 +24,7 @@ pub mod client_ext; pub use self::client_ext::{ClientBlockImportExt, ClientExt}; pub use sc_client_api::{ execution_extensions::{ExecutionConfigs, ExecutionExtensions}, - ForkBlocks, BadBlocks, + BadBlocks, ForkBlocks, }; pub use sc_client_db::{self, Backend}; pub use sc_executor::{self, NativeExecutor, WasmExecutionMethod}; @@ -35,7 +35,7 @@ pub use sp_keyring::{ }; pub use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr}; pub use sp_runtime::{Storage, StorageChild}; -pub use sp_state_machine::{ExecutionStrategy, ExecutionConfig}; +pub use sp_state_machine::{ExecutionConfig, ExecutionStrategy}; use futures::{ future::{Future, FutureExt}, diff --git a/test-utils/test-runner/src/utils.rs b/test-utils/test-runner/src/utils.rs index 1f547b1ec149b..eaacaadca73f6 100644 --- a/test-utils/test-runner/src/utils.rs +++ b/test-utils/test-runner/src/utils.rs @@ -17,6 +17,7 @@ // along with this program. If not, see . use futures::FutureExt; +use sc_client_api::execution_extensions::ExecutionConfigs; use sc_executor::WasmExecutionMethod; use sc_informant::OutputFormat; use sc_network::{ @@ -28,7 +29,6 @@ use sc_service::{ TaskExecutor, TaskType, TransactionStorageMode, }; use sp_keyring::sr25519::Keyring::Alice; -use sc_client_api::execution_extensions::ExecutionConfigs; use tokio::runtime::Handle; pub use sc_cli::build_runtime; @@ -85,11 +85,21 @@ pub fn default_config( chain_spec, wasm_method: WasmExecutionMethod::Interpreted, execution_configs: ExecutionConfigs { - syncing: sc_client_api::ExecutionConfig::new_offchain(sc_client_api::ExecutionStrategy::AlwaysWasm), - importing: sc_client_api::ExecutionConfig::new_offchain(sc_client_api::ExecutionStrategy::AlwaysWasm), - block_construction: sc_client_api::ExecutionConfig::new_offchain(sc_client_api::ExecutionStrategy::AlwaysWasm), - offchain_worker: sc_client_api::ExecutionConfig::new_offchain(sc_client_api::ExecutionStrategy::AlwaysWasm), - other: sc_client_api::ExecutionConfig::new_offchain(sc_client_api::ExecutionStrategy::AlwaysWasm), + syncing: sc_client_api::ExecutionConfig::new_offchain( + sc_client_api::ExecutionStrategy::AlwaysWasm, + ), + importing: sc_client_api::ExecutionConfig::new_offchain( + sc_client_api::ExecutionStrategy::AlwaysWasm, + ), + block_construction: sc_client_api::ExecutionConfig::new_offchain( + sc_client_api::ExecutionStrategy::AlwaysWasm, + ), + offchain_worker: sc_client_api::ExecutionConfig::new_offchain( + sc_client_api::ExecutionStrategy::AlwaysWasm, + ), + other: sc_client_api::ExecutionConfig::new_offchain( + sc_client_api::ExecutionStrategy::AlwaysWasm, + ), }, rpc_http: None, rpc_ws: None, diff --git a/utils/frame/try-runtime/cli/src/lib.rs b/utils/frame/try-runtime/cli/src/lib.rs index 0c68200196117..320cecb60bd22 100644 --- a/utils/frame/try-runtime/cli/src/lib.rs +++ b/utils/frame/try-runtime/cli/src/lib.rs @@ -191,8 +191,7 @@ where let mut changes = Default::default(); let max_runtime_instances = config.max_runtime_instances; - let executor = - NativeExecutor::::new(wasm_method.into(), max_runtime_instances); + let executor = NativeExecutor::::new(wasm_method.into(), max_runtime_instances); let ext = { let builder = match command.state { @@ -265,8 +264,7 @@ where let mut changes = Default::default(); let max_runtime_instances = config.max_runtime_instances; - let executor = - NativeExecutor::::new(wasm_method.into(), max_runtime_instances); + let executor = NativeExecutor::::new(wasm_method.into(), max_runtime_instances); let mode = match command.state { State::Live { snapshot_path, modules } => { @@ -347,8 +345,7 @@ where let mut changes = Default::default(); let max_runtime_instances = config.max_runtime_instances; - let executor = - NativeExecutor::::new(wasm_method.into(), max_runtime_instances); + let executor = NativeExecutor::::new(wasm_method.into(), max_runtime_instances); let block_hash = shared.block_at::()?; let block: Block = rpc_api::get_block::(shared.url.clone(), block_hash).await?;