Skip to content

Commit

Permalink
Stable build and Refactor to enumerated types
Browse files Browse the repository at this point in the history
Boolean options replaced with an enumerated types
to improve readability of the code.
  • Loading branch information
simonsso committed Nov 1, 2023
1 parent 466c616 commit 9dfd152
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 22 deletions.
10 changes: 8 additions & 2 deletions core/src/commands/create_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use substrate_rpc_client::{ws_client, StateApi};

use crate::{
build_executor,
state::{LiveState, State},
state::{LiveState, SpecVersionCheck, State, TryRuntimeFeatureCheck},
SharedParams, LOG_TARGET,
};

Expand Down Expand Up @@ -76,7 +76,13 @@ where

let executor = build_executor::<HostFns>(&shared);
let _ = State::Live(command.from)
.to_ext::<Block, HostFns>(&shared, &executor, Some(path.into()), false, false)
.to_ext::<Block, HostFns>(
&shared,
&executor,
Some(path.into()),
TryRuntimeFeatureCheck::Skip,
SpecVersionCheck::Skip,
)
.await?;

Ok(())
Expand Down
10 changes: 8 additions & 2 deletions core/src/commands/execute_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use substrate_rpc_client::{ws_client, ChainApi};

use crate::{
build_executor, full_extensions, rpc_err_handler,
state::{LiveState, State},
state::{LiveState, SpecVersionCheck, State, TryRuntimeFeatureCheck},
state_machine_call_with_proof, SharedParams, LOG_TARGET,
};

Expand Down Expand Up @@ -97,7 +97,13 @@ where
let executor = build_executor::<HostFns>(&shared);
let ext = command
.state
.to_ext::<Block, HostFns>(&shared, &executor, None, true, false)
.to_ext::<Block, HostFns>(
&shared,
&executor,
None,
TryRuntimeFeatureCheck::Check,
SpecVersionCheck::Skip,
)
.await?;

// get the block number associated with this block.
Expand Down
10 changes: 8 additions & 2 deletions core/src/commands/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
build_executor, full_extensions,
inherent_provider::{Chain, InherentProvider},
rpc_err_handler,
state::{LiveState, State},
state::{LiveState, SpecVersionCheck, State, TryRuntimeFeatureCheck},
state_machine_call, state_machine_call_with_proof, BlockT, SharedParams,
};

Expand Down Expand Up @@ -234,7 +234,13 @@ where
let executor = build_executor::<HostFns>(&shared);
let ext = command
.state
.to_ext::<Block, HostFns>(&shared, &executor, None, true, false)
.to_ext::<Block, HostFns>(
&shared,
&executor,
None,
TryRuntimeFeatureCheck::Check,
SpecVersionCheck::Skip,
)
.await?;

if command.run_migrations {
Expand Down
10 changes: 8 additions & 2 deletions core/src/commands/follow_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use substrate_rpc_client::{ws_client, ChainApi, FinalizedHeaders, Subscription,

use crate::{
build_executor, full_extensions, parse, rpc_err_handler,
state::{LiveState, State},
state::{LiveState, SpecVersionCheck, State, TryRuntimeFeatureCheck},
state_machine_call_with_proof, SharedParams, LOG_TARGET,
};

Expand Down Expand Up @@ -143,7 +143,13 @@ where
hashed_prefixes: vec![],
});
let ext = state
.to_ext::<Block, HostFns>(&shared, &executor, None, true, false)
.to_ext::<Block, HostFns>(
&shared,
&executor,
None,
TryRuntimeFeatureCheck::Check,
SpecVersionCheck::Skip,
)
.await?;
maybe_state_ext = Some(ext);
}
Expand Down
10 changes: 8 additions & 2 deletions core/src/commands/offchain_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
build_executor,
commands::execute_block::next_hash_of,
full_extensions, parse, rpc_err_handler,
state::{LiveState, State},
state::{LiveState, SpecVersionCheck, State, TryRuntimeFeatureCheck},
state_machine_call, SharedParams, LOG_TARGET,
};

Expand Down Expand Up @@ -78,7 +78,13 @@ where
// we first build the externalities with the remote code.
let ext = command
.state
.to_ext::<Block, HostFns>(&shared, &executor, None, true, false)
.to_ext::<Block, HostFns>(
&shared,
&executor,
None,
TryRuntimeFeatureCheck::Check,
SpecVersionCheck::Skip,
)
.await?;

let header_ws_uri = command.header_ws_uri();
Expand Down
13 changes: 10 additions & 3 deletions core/src/commands/on_runtime_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ use sp_runtime::traits::{Block as BlockT, NumberFor};
use sp_state_machine::{CompactProof, StorageProof};

use crate::{
build_executor, state::State, state_machine_call_with_proof, RefTimeInfo, SharedParams,
LOG_TARGET,
build_executor,
state::{SpecVersionCheck, State, TryRuntimeFeatureCheck},
state_machine_call_with_proof, RefTimeInfo, SharedParams, LOG_TARGET,
};

/// Configuration for [`run`].
Expand Down Expand Up @@ -76,7 +77,13 @@ where
let executor = build_executor(&shared);
let ext = command
.state
.to_ext::<Block, HostFns>(&shared, &executor, None, true, true)
.to_ext::<Block, HostFns>(
&shared,
&executor,
None,
TryRuntimeFeatureCheck::Check,
SpecVersionCheck::Check,
)
.await?;

if let State::Live(_) = command.state {
Expand Down
11 changes: 6 additions & 5 deletions core/src/shared_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

use std::{path::PathBuf, str::FromStr};

use sc_cli::{WasmExecutionMethod, WasmtimeInstantiationStrategy};
use sp_runtime::StateVersion;
use {
crate::parse,
sc_cli::{DEFAULT_WASMTIME_INSTANTIATION_STRATEGY, DEFAULT_WASM_EXECUTION_METHOD},
use sc_cli::{
WasmExecutionMethod, WasmtimeInstantiationStrategy, DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
DEFAULT_WASM_EXECUTION_METHOD,
};
use sp_runtime::StateVersion;

use crate::parse;

/// Shared parameters of the `try-runtime` commands
#[derive(Debug, Clone, clap::Parser)]
Expand Down
31 changes: 27 additions & 4 deletions core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ pub enum State {
Live(LiveState),
}

/// Options for [`to_ext`]
///
/// Check if runtime is compiled with try-runtime option or not
#[derive(PartialEq, PartialOrd)]
pub enum TryRuntimeFeatureCheck {
Check,
Skip,
}
/// Options for [`to_ext`]
///
/// Enable of disable check wether new runtime spec version is not greater
/// than the on-chain runtime spec version in [`to_ext`]
#[derive(PartialEq, PartialOrd)]
pub enum SpecVersionCheck {
Check,
Skip,
}

impl State {
/// Create the [`RemoteExternalities`].
///
Expand All @@ -107,8 +126,8 @@ impl State {
shared: &SharedParams,
executor: &WasmExecutor<HostFns>,
state_snapshot: Option<SnapshotConfig>,
try_runtime_check: bool,
spec_version_check: bool,
try_runtime_check: TryRuntimeFeatureCheck,
spec_version_check: SpecVersionCheck,
) -> sc_cli::Result<RemoteExternalities<Block>>
where
Block::Header: DeserializeOwned,
Expand Down Expand Up @@ -256,7 +275,9 @@ impl State {
return Err("Spec names must match.".into());
}

if spec_version_check && new_version.spec_version <= old_version.spec_version {
if spec_version_check == SpecVersionCheck::Check
&& new_version.spec_version <= old_version.spec_version
{
log::warn!(
target: LOG_TARGET,
"New runtime spec version is not greater than the on-chain runtime spec version. Don't forget to increment the spec version if you intend to use the new code in a runtime upgrade."
Expand All @@ -265,7 +286,9 @@ impl State {
}

// whatever runtime we have in store now must have been compiled with try-runtime feature.
if try_runtime_check && !ensure_try_runtime::<Block, HostFns>(executor, &mut ext) {
if try_runtime_check == TryRuntimeFeatureCheck::Check
&& !ensure_try_runtime::<Block, HostFns>(executor, &mut ext)
{
return Err("given runtime is NOT compiled with try-runtime feature!".into());
}

Expand Down

0 comments on commit 9dfd152

Please sign in to comment.