Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/kiz-revamp-try-runtime-stuff' in…
Browse files Browse the repository at this point in the history
…to lexnv/kiz-revamp-try-runtime-stuff
  • Loading branch information
lexnv committed Dec 13, 2022
2 parents dcaf580 + 59a11d6 commit b7c0219
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 262 deletions.
5 changes: 2 additions & 3 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,13 @@ impl_runtime_apis! {
fn execute_block(
block: Block,
state_root_check: bool,
signature_check: bool,
select: frame_try_runtime::TryStateSelect
) -> Weight {
// NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
// have a backtrace here.
Executive::try_execute_block(block, state_root_check, true, select).expect("execute-block failed")
Executive::try_execute_block(block, state_root_check, signature_check, select).expect("execute-block failed")
}

fn ping() {}
}
}

Expand Down
3 changes: 2 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2169,11 +2169,12 @@ impl_runtime_apis! {
fn execute_block(
block: Block,
state_root_check: bool,
signature_check: bool,
select: frame_try_runtime::TryStateSelect
) -> Weight {
// NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
// have a backtrace here.
Executive::try_execute_block(block, state_root_check, false, select).unwrap()
Executive::try_execute_block(block, state_root_check, signature_check, select).unwrap()
}
}

Expand Down
9 changes: 7 additions & 2 deletions frame/try-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ sp_api::decl_runtime_apis! {
/// tracking is likely inaccurate.
fn on_runtime_upgrade(checks: bool) -> (Weight, Weight);

/// Execute the given block, but optionally disable state-root checks.
/// Execute the given block, but optionally disable state-root and signature checks.
///
/// Optionally, a number of `try_state` hooks can also be executed after the block
/// execution.
fn execute_block(block: Block, state_root_check: bool, try_state: TryStateSelect) -> Weight;
fn execute_block(
block: Block,
state_root_check: bool,
signature_check: bool,
try_state: TryStateSelect,
) -> Weight;
}
}
43 changes: 18 additions & 25 deletions utils/frame/try-runtime/cli/src/commands/create_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,70 +15,63 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{hash_of, LiveState, LOG_TARGET};
use remote_externalities::{Builder, Mode, OnlineConfig, SnapshotConfig};
use crate::{build_executor, LiveState, SharedParams, State, LOG_TARGET};
use sc_executor::sp_wasm_interface::HostFunctions;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::{fmt::Debug, str::FromStr};
use substrate_rpc_client::{ws_client, StateApi};

/// Configurations of the [`Command::CreateSnapshot`].
/// Configurations of the [`crate::Command::CreateSnapshot`].
#[derive(Debug, Clone, clap::Parser)]
pub struct CreateSnapshotCmd {
/// The source of the snapshot. Must be a remote node.
#[clap(flatten)]
from: LiveState,
pub from: LiveState,

/// The snapshot path to write to.
///
/// If not provided `<spec-name>-<spec-version>@<block-hash>.snap` will be used.
snapshot_path: Option<String>,
pub snapshot_path: Option<String>,
}

/// inner command for `Command::CreateSnapshot`.
pub(crate) async fn create_snapshot<Block>(command: CreateSnapshotCmd) -> sc_cli::Result<()>
pub(crate) async fn create_snapshot<Block, HostFns>(
shared: SharedParams,
command: CreateSnapshotCmd,
) -> sc_cli::Result<()>
where
Block: BlockT + serde::de::DeserializeOwned,
Block::Hash: FromStr + serde::de::DeserializeOwned,
Block::Header: serde::de::DeserializeOwned,
<Block::Hash as FromStr>::Err: Debug,
NumberFor<Block>: FromStr,
<NumberFor<Block> as FromStr>::Err: Debug,
HostFns: HostFunctions,
{
let snapshot_path = command.snapshot_path;
let command = command.from;

let at = match command.at {
Some(ref at_str) => Some(hash_of::<Block>(at_str)?),
None => None,
};
if !matches!(shared.runtime, crate::Runtime::Existing) {
return Err("creating a snapshot is only possible with --runtime existing.".into())
}

let path = match snapshot_path {
Some(path) => path,
None => {
let rpc = ws_client(&command.uri).await.unwrap();
let rpc = ws_client(&command.from.uri).await.unwrap();
let remote_spec = StateApi::<Block::Hash>::runtime_version(&rpc, None).await.unwrap();
let path_str = format!(
"{}-{}@{}.snap",
remote_spec.spec_name.to_lowercase(),
remote_spec.spec_version,
command.at.clone().unwrap_or("latest".to_owned())
command.from.at.clone().unwrap_or("latest".to_owned())
);
log::info!(target: LOG_TARGET, "snapshot path not provided (-s), using '{}'", path_str);
path_str.into()
},
};

let _ = Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig {
at,
state_snapshot: Some(SnapshotConfig::new(path)),
pallets: command.pallet,
transport: command.uri.into(),
child_trie: command.child_tree,
hashed_prefixes: vec![],
hashed_keys: vec![],
}))
.build()
let executor = build_executor::<HostFns>(&shared);
let _ = State::Live(command.from)
.into_ext::<Block, HostFns>(&shared, &executor, Some(path.into()))
.await?;

Ok(())
Expand Down
17 changes: 10 additions & 7 deletions utils/frame/try-runtime/cli/src/commands/execute_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use sp_runtime::{
use std::{fmt::Debug, str::FromStr};
use substrate_rpc_client::{ws_client, ChainApi};

/// Configurations of the [`Command::ExecuteBlock`].
/// Configurations of the [`crate::Command::ExecuteBlock`].
///
/// This will always call into `TryRuntime_execute_block`, which can optionally skip the state-root
/// check (useful for trying a unreleased runtime), and can execute runtime sanity checks as well.
Expand All @@ -45,24 +45,24 @@ pub struct ExecuteBlockCmd {
/// - `rr-[x]` where `[x]` is a number. Then, the given number of pallets are checked in a
/// round-robin fashion.
#[arg(long, default_value = "all")]
try_state: frame_try_runtime::TryStateSelect,
pub try_state: frame_try_runtime::TryStateSelect,

/// The ws uri from which to fetch the block.
///
/// This will always fetch the next block of whatever `state` is referring to, because this is
/// the only sensible combination. In other words, if you have the state of block `n`, you
/// should execute block `n+1` on top of it.
///
/// If `state` is `Live`, this can be ignored.
/// If `state` is `Live`, this can be ignored and the same uri is used for both.
#[arg(
long,
value_parser = crate::parse::url
)]
block_ws_uri: Option<String>,
pub block_ws_uri: Option<String>,

/// The state type to use.
#[command(subcommand)]
state: State,
pub state: State,
}

impl ExecuteBlockCmd {
Expand Down Expand Up @@ -99,7 +99,7 @@ where
HostFns: HostFunctions,
{
let executor = build_executor::<HostFns>(&shared);
let ext = command.state.into_ext::<Block, HostFns>(&shared, &executor).await?;
let ext = command.state.into_ext::<Block, HostFns>(&shared, &executor, None).await?;

// get the block number associated with this block.
let block_ws_uri = command.block_ws_uri::<Block>();
Expand All @@ -122,8 +122,11 @@ where
let (mut header, extrinsics) = block.deconstruct();
header.digest_mut().pop();
let block = Block::new(header, extrinsics);

// for now, hardcoded for the sake of simplicity. We might customize them one day.
let state_root_check = false;
let payload = (block.clone(), state_root_check, command.try_state).encode();
let signature_check = false;
let payload = (block.clone(), state_root_check, signature_check, command.try_state).encode();

let _ = state_machine_call_with_proof::<Block, HostFns>(
&ext,
Expand Down
12 changes: 6 additions & 6 deletions utils/frame/try-runtime/cli/src/commands/follow_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ use substrate_rpc_client::{ws_client, ChainApi, FinalizedHeaders, Subscription,
const SUB: &str = "chain_subscribeFinalizedHeads";
const UN_SUB: &str = "chain_unsubscribeFinalizedHeads";

/// Configurations of the [`Command::FollowChain`].
/// Configurations of the [`crate::Command::FollowChain`].
#[derive(Debug, Clone, clap::Parser)]
pub struct FollowChainCmd {
/// The url to connect to.
#[arg(short, long, value_parser = parse::url)]
uri: String,
pub uri: String,

/// If set, then the state root check is enabled.
#[arg(long)]
state_root_check: bool,
pub state_root_check: bool,

/// Which try-state targets to execute when running this command.
///
Expand All @@ -54,11 +54,11 @@ pub struct FollowChainCmd {
/// - `rr-[x]` where `[x]` is a number. Then, the given number of pallets are checked in a
/// round-robin fashion.
#[arg(long, default_value = "all")]
try_state: frame_try_runtime::TryStateSelect,
pub try_state: frame_try_runtime::TryStateSelect,

/// If present, a single connection to a node will be kept and reused for fetching blocks.
#[arg(long)]
keep_connection: bool,
pub keep_connection: bool,
}

/// Start listening for with `SUB` at `url`.
Expand Down Expand Up @@ -137,7 +137,7 @@ where
pallet: vec![],
child_tree: true,
});
let ext = state.into_ext::<Block, HostFns>(&shared, &executor).await?;
let ext = state.into_ext::<Block, HostFns>(&shared, &executor, None).await?;
maybe_state_ext = Some(ext);
}

Expand Down
10 changes: 5 additions & 5 deletions utils/frame/try-runtime/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub(crate) mod create_snapshot;
pub(crate) mod execute_block;
pub(crate) mod follow_chain;
pub(crate) mod offchain_worker;
pub(crate) mod on_runtime_upgrade;
pub mod create_snapshot;
pub mod execute_block;
pub mod follow_chain;
pub mod offchain_worker;
pub mod on_runtime_upgrade;
6 changes: 3 additions & 3 deletions utils/frame/try-runtime/cli/src/commands/offchain_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::{fmt::Debug, str::FromStr};
use substrate_rpc_client::{ws_client, ChainApi};

/// Configurations of the [`Command::OffchainWorker`].
/// Configurations of the [`crate::Command::OffchainWorker`].
#[derive(Debug, Clone, clap::Parser)]
pub struct OffchainWorkerCmd {
/// The ws uri from which to fetch the header.
Expand All @@ -36,7 +36,7 @@ pub struct OffchainWorkerCmd {
long,
value_parser = parse::url
)]
header_ws_uri: Option<String>,
pub header_ws_uri: Option<String>,

/// The state type to use.
#[command(subcommand)]
Expand Down Expand Up @@ -78,7 +78,7 @@ where
{
let executor = build_executor(&shared);
// we first build the externalities with the remote code.
let ext = command.state.into_ext::<Block, HostFns>(&shared, &executor).await?;
let ext = command.state.into_ext::<Block, HostFns>(&shared, &executor, None).await?;

let header_ws_uri = command.header_ws_uri::<Block>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sp_runtime::traits::{Block as BlockT, NumberFor};
use sp_weights::Weight;
use std::{fmt::Debug, str::FromStr};

/// Configurations of the [`Command::OnRuntimeUpgrade`].
/// Configurations of the [`crate::Command::OnRuntimeUpgrade`].
#[derive(Debug, Clone, clap::Parser)]
pub struct OnRuntimeUpgradeCmd {
/// The state type to use.
Expand Down Expand Up @@ -51,7 +51,7 @@ where
HostFns: HostFunctions,
{
let executor = build_executor(&shared);
let ext = command.state.into_ext::<Block, HostFns>(&shared, &executor).await?;
let ext = command.state.into_ext::<Block, HostFns>(&shared, &executor, None).await?;

let (_, encoded_result) = state_machine_call_with_proof::<Block, HostFns>(
&ext,
Expand Down
Loading

0 comments on commit b7c0219

Please sign in to comment.