Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
2 changes: 2 additions & 0 deletions client/cli/src/arg_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ arg_enum! {
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.
Expand Down
5 changes: 2 additions & 3 deletions client/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ macro_rules! substrate_cli_subcommands {
}
}

fn execution_strategies(&self, is_dev: bool)
fn execution_strategies(&self, is_dev: bool, is_validator: bool)
-> $crate::Result<::sc_client_api::execution_extensions::ExecutionStrategies> {
match self {
$($enum::$variant(cmd) => cmd.execution_strategies(is_dev)),*
$($enum::$variant(cmd) => cmd.execution_strategies(is_dev, is_validator)),*
}
}

Expand Down Expand Up @@ -409,4 +409,3 @@ macro_rules! substrate_cli_subcommands {
substrate_cli_subcommands!(
Subcommand => BuildSpec, ExportBlocks, ImportBlocks, CheckBlock, Revert, PurgeChain, ExportState
);

14 changes: 10 additions & 4 deletions client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,14 @@ pub trait CliConfiguration: Sized {
///
/// By default this is retrieved from `ImportParams` if it is available. Otherwise its
/// `ExecutionStrategies::default()`.
fn execution_strategies(&self, is_dev: bool) -> Result<ExecutionStrategies> {
Ok(self.import_params()
.map(|x| x.execution_strategies(is_dev))
fn execution_strategies(
&self,
is_dev: bool,
is_validator: bool,
) -> Result<ExecutionStrategies> {
Ok(self
.import_params()
.map(|x| x.execution_strategies(is_dev, is_validator))
.unwrap_or(Default::default()))
}

Expand Down Expand Up @@ -419,6 +424,7 @@ pub trait CliConfiguration: Sized {
let node_key = self.node_key(&net_config_dir)?;
let role = self.role(is_dev)?;
let max_runtime_instances = self.max_runtime_instances()?.unwrap_or(8);
let is_validator = role.is_network_authority();

let unsafe_pruning = self
.import_params()
Expand All @@ -444,7 +450,7 @@ pub trait CliConfiguration: Sized {
state_cache_child_ratio: self.state_cache_child_ratio()?,
pruning: self.pruning(unsafe_pruning, &role)?,
wasm_method: self.wasm_method()?,
execution_strategies: self.execution_strategies(is_dev)?,
execution_strategies: self.execution_strategies(is_dev, is_validator)?,
rpc_http: self.rpc_http()?,
rpc_ws: self.rpc_ws()?,
rpc_methods: self.rpc_methods()?,
Expand Down
48 changes: 25 additions & 23 deletions client/cli/src/params/import_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::arg_enums::{
ExecutionStrategy, TracingReceiver, WasmExecutionMethod,
DEFAULT_EXECUTION_BLOCK_CONSTRUCTION, DEFAULT_EXECUTION_IMPORT_BLOCK,
ExecutionStrategy, TracingReceiver, WasmExecutionMethod, DEFAULT_EXECUTION_BLOCK_CONSTRUCTION,
DEFAULT_EXECUTION_IMPORT_BLOCK, DEFAULT_EXECUTION_IMPORT_BLOCK_VALIDATOR,
DEFAULT_EXECUTION_OFFCHAIN_WORKER, DEFAULT_EXECUTION_OTHER, DEFAULT_EXECUTION_SYNCING,
};
use crate::params::DatabaseParams;
Expand Down Expand Up @@ -104,22 +104,27 @@ impl ImportParams {
}

/// Get execution strategies for the parameters
pub fn execution_strategies(
&self,
is_dev: bool,
) -> ExecutionStrategies {
pub fn execution_strategies(&self, is_dev: bool, is_validator: bool) -> ExecutionStrategies {
let exec = &self.execution_strategies;
let exec_all_or = |strat: ExecutionStrategy, default: ExecutionStrategy| {
exec.execution.unwrap_or(if strat == default && is_dev {
let exec_all_or = |strat: Option<ExecutionStrategy>, default: ExecutionStrategy| {
let default = if is_dev {
ExecutionStrategy::Native
} else {
strat
}).into()
default
};

exec.execution.unwrap_or(strat.unwrap_or(default)).into()
};

let default_execution_import_block = if is_validator {
DEFAULT_EXECUTION_IMPORT_BLOCK_VALIDATOR
} else {
DEFAULT_EXECUTION_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),
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:
Expand All @@ -132,55 +137,52 @@ impl ImportParams {
/// Execution strategies parameters.
#[derive(Debug, StructOpt, Clone)]
pub struct ExecutionStrategiesParams {
/// The means of execution used when calling into the runtime while syncing blocks.
/// The means of execution used when calling into the runtime for importing blocks as
/// part of an initial sync.
#[structopt(
long = "execution-syncing",
value_name = "STRATEGY",
possible_values = &ExecutionStrategy::variants(),
case_insensitive = true,
default_value = DEFAULT_EXECUTION_SYNCING.as_str(),
)]
pub execution_syncing: ExecutionStrategy,
pub execution_syncing: Option<ExecutionStrategy>,

/// The means of execution used when calling into the runtime while importing blocks.
/// The means of execution used when calling into the runtime for general block import
/// (including locally authored blocks).
#[structopt(
long = "execution-import-block",
value_name = "STRATEGY",
possible_values = &ExecutionStrategy::variants(),
case_insensitive = true,
default_value = DEFAULT_EXECUTION_IMPORT_BLOCK.as_str(),
)]
pub execution_import_block: ExecutionStrategy,
pub execution_import_block: Option<ExecutionStrategy>,

/// The means of execution used when calling into the runtime while constructing blocks.
#[structopt(
long = "execution-block-construction",
value_name = "STRATEGY",
possible_values = &ExecutionStrategy::variants(),
case_insensitive = true,
default_value = DEFAULT_EXECUTION_BLOCK_CONSTRUCTION.as_str(),
)]
pub execution_block_construction: ExecutionStrategy,
pub execution_block_construction: Option<ExecutionStrategy>,

/// The means of execution used when calling into the runtime while using an off-chain worker.
#[structopt(
long = "execution-offchain-worker",
value_name = "STRATEGY",
possible_values = &ExecutionStrategy::variants(),
case_insensitive = true,
default_value = DEFAULT_EXECUTION_OFFCHAIN_WORKER.as_str(),
)]
pub execution_offchain_worker: ExecutionStrategy,
pub execution_offchain_worker: Option<ExecutionStrategy>,

/// The means of execution used when calling into the runtime while not syncing, importing or constructing blocks.
#[structopt(
long = "execution-other",
value_name = "STRATEGY",
possible_values = &ExecutionStrategy::variants(),
case_insensitive = true,
default_value = DEFAULT_EXECUTION_OTHER.as_str(),
)]
pub execution_other: ExecutionStrategy,
pub execution_other: Option<ExecutionStrategy>,

/// The execution strategy that should be used by all execution contexts.
#[structopt(
Expand Down
18 changes: 10 additions & 8 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ use parking_lot::{Mutex, RwLock};
use codec::{Encode, Decode};
use hash_db::Prefix;
use sp_core::{
ChangesTrieConfiguration, convert_hash, NativeOrEncoded,
storage::{StorageKey, PrefixedStorageKey, StorageData, well_known_keys, ChildInfo},
convert_hash,
storage::{well_known_keys, ChildInfo, PrefixedStorageKey, StorageData, StorageKey},
ChangesTrieConfiguration, ExecutionContext, NativeOrEncoded,
};
use sc_telemetry::{telemetry, SUBSTRATE_INFO};
use sp_runtime::{
Expand Down Expand Up @@ -754,11 +755,6 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
) = storage_changes.into_inner();

if self.config.offchain_indexing_api {
// if let Some(mut offchain_storage) = self.backend.offchain_storage() {
// offchain_sc.iter().for_each(|(k,v)| {
// offchain_storage.set(b"block-import-info", k,v)
// });
// }
operation.op.update_offchain_storage(offchain_sc)?;
}

Expand Down Expand Up @@ -865,9 +861,15 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
// block.
(true, ref mut storage_changes @ None, Some(ref body)) => {
let runtime_api = self.runtime_api();
let execution_context = if import_block.origin == BlockOrigin::NetworkInitialSync {
ExecutionContext::Syncing
} else {
ExecutionContext::Importing
};

runtime_api.execute_block(
runtime_api.execute_block_with_context(
&at,
execution_context,
Block::new(import_block.header.clone(), body.clone()),
)?;

Expand Down
11 changes: 9 additions & 2 deletions primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,16 @@ pub use sp_std;

/// Context for executing a call into the runtime.
pub enum ExecutionContext {
/// Context for general importing (including own blocks).
/// Context used for general block import (including locally authored blocks).
Importing,
/// Context used when syncing the blockchain.
/// Context used for importing blocks as part of an initial sync of the blockchain.
///
/// We distinguish between major sync and import so that validators who are running
/// their initial sync (or catching up after some time offline) can use the faster
/// native runtime (since we can reasonably assume the network as a whole has already
/// come to a broad conensus on the block and it probably hasn't been crafted
/// specifically to attack this node), but when importing blocks at the head of the
/// chain in normal operation they can use the safer Wasm version.
Syncing,
/// Context used for block construction.
BlockConstruction,
Expand Down