Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to enumerated types #50

Merged
merged 5 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 32 additions & 4 deletions core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ pub enum State {
Live(LiveState),
}

/// Options for [`to_ext`]
///
/// Whether to check that the runtime was compiled with try-runtime feature
#[derive(PartialEq, PartialOrd)]
pub enum TryRuntimeFeatureCheck {
/// Check the runtime was compiled with try-runtime feature
Check,
/// Don't check if the runtime was compiled with try-runtime feature
Skip,
}
/// Options for [`to_ext`]
///
/// Whether to check if the new runtime `spec_version` is greater than the previous runtime
/// `spec_version`
#[derive(PartialEq, PartialOrd)]
pub enum SpecVersionCheck {
/// Check that the new runtime `spec_version` is greater than the previous runtime
/// `spec_version`
Check,
/// Don't check that the new runtime `spec_version` is greater than the previous runtime
/// `spec_version`
Skip,
}

impl State {
/// Create the [`RemoteExternalities`].
///
Expand All @@ -107,8 +131,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 +280,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 +291,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