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

Commit

Permalink
Migration testing runtime API/Bot (#8038)
Browse files Browse the repository at this point in the history
* A clean new attempt

* Checkpoint to move remote.

* A lot of dependency wiring to make it feature gated.

* bad macro, bad macro.

* Undo the DB mess.

* Update frame/support/src/traits.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Apply suggestions from code review

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* unbreak the build

* Update frame/try-runtime/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update utils/frame/try-runtime/cli/Cargo.toml

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/try-runtime/Cargo.toml

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Address most review grumbles.

* Fix build

* Add some comments

* Remove allowing one pallet at a time.

* More grumbles.

* relocate remote-ext

* Fix build

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
  • Loading branch information
4 people authored Feb 19, 2021
1 parent 9029ab0 commit 394c52e
Show file tree
Hide file tree
Showing 24 changed files with 991 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ rls*.log
**/hfuzz_workspace/
.cargo/
.cargo-remote.toml
*.bin
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ members = [
"frame/contracts/rpc",
"frame/contracts/rpc/runtime-api",
"frame/democracy",
"frame/try-runtime",
"frame/elections",
"frame/example",
"frame/example-offchain-worker",
Expand Down Expand Up @@ -185,7 +186,9 @@ members = [
"utils/build-script-utils",
"utils/fork-tree",
"utils/frame/benchmarking-cli",
"utils/frame/remote-externalities",
"utils/frame/frame-utilities-cli",
"utils/frame/try-runtime/cli",
"utils/frame/rpc/support",
"utils/frame/rpc/system",
"utils/prometheus",
Expand Down
9 changes: 9 additions & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ node-executor = { version = "2.0.0", path = "../executor" }
sc-cli = { version = "0.9.0", optional = true, path = "../../../client/cli" }
frame-benchmarking-cli = { version = "3.0.0", optional = true, path = "../../../utils/frame/benchmarking-cli" }
node-inspect = { version = "0.8.0", optional = true, path = "../inspect" }
try-runtime-cli = { version = "0.9.0", optional = true, path = "../../../utils/frame/try-runtime/cli" }

# WASM-specific dependencies
wasm-bindgen = { version = "0.2.57", optional = true }
Expand Down Expand Up @@ -133,6 +134,7 @@ node-inspect = { version = "0.8.0", optional = true, path = "../inspect" }
frame-benchmarking-cli = { version = "3.0.0", optional = true, path = "../../../utils/frame/benchmarking-cli" }
substrate-build-script-utils = { version = "3.0.0", optional = true, path = "../../../utils/build-script-utils" }
substrate-frame-cli = { version = "3.0.0", optional = true, path = "../../../utils/frame/frame-utilities-cli" }
try-runtime-cli = { version = "0.9.0", optional = true, path = "../../../utils/frame/try-runtime/cli" }

[build-dependencies.sc-cli]
version = "0.9.0"
Expand All @@ -157,8 +159,15 @@ cli = [
"sc-finality-grandpa-warp-sync",
"structopt",
"substrate-build-script-utils",
"try-runtime-cli",
]
runtime-benchmarks = [
"node-runtime/runtime-benchmarks",
"frame-benchmarking-cli",
]
# Enable features that allow the runtime to be tried and debugged. Name might be subject to change
# in the near future.
try-runtime = [
"node-runtime/try-runtime",
"try-runtime-cli",
]
5 changes: 5 additions & 0 deletions bin/node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub enum Subcommand {
#[structopt(name = "benchmark", about = "Benchmark runtime pallets.")]
Benchmark(frame_benchmarking_cli::BenchmarkCmd),

/// Try some experimental command on the runtime. This includes migration and runtime-upgrade
/// testing.
#[cfg(feature = "try-runtime")]
TryRuntime(try_runtime_cli::TryRuntimeCmd),

/// Verify a signature for a message, provided on STDIN, with a given (public or secret) key.
Verify(VerifyCmd),

Expand Down
15 changes: 15 additions & 0 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,20 @@ pub fn run() -> Result<()> {
Ok((cmd.run(client, backend), task_manager))
})
},
#[cfg(feature = "try-runtime")]
Some(Subcommand::TryRuntime(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
// we don't need any of the components of new_partial, just a runtime, or a task
// manager to do `async_run`.
let registry = config.prometheus_config.as_ref().map(|cfg| &cfg.registry);
let task_manager = sc_service::TaskManager::new(
config.task_executor.clone(),
registry,
).unwrap();

Ok((cmd.run::<Block, Executor>(config), task_manager))
})
}
}
}
5 changes: 5 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ frame-support = { version = "3.0.0", default-features = false, path = "../../../
frame-system = { version = "3.0.0", default-features = false, path = "../../../frame/system" }
frame-system-benchmarking = { version = "3.0.0", default-features = false, path = "../../../frame/system/benchmarking", optional = true }
frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/system/rpc/runtime-api/" }
frame-try-runtime = { version = "0.9.0", default-features = false, path = "../../../frame/try-runtime", optional = true }
pallet-assets = { version = "3.0.0", default-features = false, path = "../../../frame/assets" }
pallet-authority-discovery = { version = "3.0.0", default-features = false, path = "../../../frame/authority-discovery" }
pallet-authorship = { version = "3.0.0", default-features = false, path = "../../../frame/authorship" }
Expand Down Expand Up @@ -186,3 +187,7 @@ runtime-benchmarks = [
"frame-system-benchmarking",
"hex-literal",
]
try-runtime = [
"frame-executive/try-runtime",
"frame-try-runtime",
]
24 changes: 20 additions & 4 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,14 @@ pub type SignedPayload = generic::SignedPayload<Call, SignedExtra>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExtra>;
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<Runtime, Block, frame_system::ChainContext<Runtime>, Runtime, AllModules>;
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllModules,
(),
>;

/// MMR helper types.
mod mmr {
Expand Down Expand Up @@ -1325,15 +1332,24 @@ impl_runtime_apis! {
}
}

#[cfg(feature = "try-runtime")]
impl frame_try_runtime::TryRuntime<Block> for Runtime {
fn on_runtime_upgrade() -> Result<(Weight, Weight), sp_runtime::RuntimeString> {
frame_support::debug::RuntimeLogger::init();
let weight = Executive::try_runtime_upgrade()?;
Ok((weight, RuntimeBlockWeights::get().max_block))
}
}

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn dispatch_benchmark(
config: frame_benchmarking::BenchmarkConfig
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};
// Trying to add benchmarks directly to the Session Pallet caused cyclic dependency issues.
// To get around that, we separated the Session benchmarks into its own crate, which is why
// we need these two lines below.
// Trying to add benchmarks directly to the Session Pallet caused cyclic dependency
// issues. To get around that, we separated the Session benchmarks into its own crate,
// which is why we need these two lines below.
use pallet_session_benchmarking::Module as SessionBench;
use pallet_offences_benchmarking::Module as OffencesBench;
use frame_system_benchmarking::Module as SystemBench;
Expand Down
2 changes: 1 addition & 1 deletion client/service/src/task_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub struct TaskManager {
impl TaskManager {
/// If a Prometheus registry is passed, it will be used to report statistics about the
/// service tasks.
pub(super) fn new(
pub fn new(
executor: TaskExecutor,
prometheus_registry: Option<&Registry>,
) -> Result<Self, PrometheusError> {
Expand Down
8 changes: 4 additions & 4 deletions client/state-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
//! Canonicalization window tracks a tree of blocks identified by header hash. The in-memory
//! overlay allows to get any node that was inserted in any of the blocks within the window.
//! The tree is journaled to the backing database and rebuilt on startup.
//! Canonicalization function selects one root from the top of the tree and discards all other roots and
//! their subtrees.
//! Canonicalization function selects one root from the top of the tree and discards all other roots
//! and their subtrees.
//!
//! # Pruning.
//! See `RefWindow` for pruning algorithm details. `StateDb` prunes on each canonicalization until pruning
//! constraints are satisfied.
//! See `RefWindow` for pruning algorithm details. `StateDb` prunes on each canonicalization until
//! pruning constraints are satisfied.
mod noncanonical;
mod pruning;
Expand Down
3 changes: 3 additions & 0 deletions frame/executive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ std = [
"sp-tracing/std",
"sp-std/std",
]
try-runtime = [
"frame-support/try-runtime"
]
72 changes: 51 additions & 21 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
//!
//! ## Usage
//!
//! The default Substrate node template declares the [`Executive`](./struct.Executive.html) type in its library.
//! The default Substrate node template declares the [`Executive`](./struct.Executive.html) type in
//! its library.
//!
//! ### Example
//!
Expand Down Expand Up @@ -185,26 +186,58 @@ where
}

impl<
System: frame_system::Config,
Block: traits::Block<Header=System::Header, Hash=System::Hash>,
Context: Default,
UnsignedValidator,
AllModules:
OnRuntimeUpgrade +
OnInitialize<System::BlockNumber> +
OnFinalize<System::BlockNumber> +
OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
> Executive<System, Block, Context, UnsignedValidator, AllModules, COnRuntimeUpgrade>
System: frame_system::Config,
Block: traits::Block<Header = System::Header, Hash = System::Hash>,
Context: Default,
UnsignedValidator,
AllModules: OnRuntimeUpgrade
+ OnInitialize<System::BlockNumber>
+ OnFinalize<System::BlockNumber>
+ OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
> Executive<System, Block, Context, UnsignedValidator, AllModules, COnRuntimeUpgrade>
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>:
Applyable +
GetDispatchInfo,
CallOf<Block::Extrinsic, Context>: Dispatchable<Info=DispatchInfo, PostInfo=PostDispatchInfo>,
CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
CallOf<Block::Extrinsic, Context>:
Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
OriginOf<Block::Extrinsic, Context>: From<Option<System::AccountId>>,
UnsignedValidator: ValidateUnsigned<Call=CallOf<Block::Extrinsic, Context>>,
UnsignedValidator: ValidateUnsigned<Call = CallOf<Block::Extrinsic, Context>>,
{
/// Execute all `OnRuntimeUpgrade` of this runtime, and return the aggregate weight.
pub fn execute_on_runtime_upgrade() -> frame_support::weights::Weight {
let mut weight = 0;
weight = weight.saturating_add(
<frame_system::Module<System> as OnRuntimeUpgrade>::on_runtime_upgrade(),
);
weight = weight.saturating_add(COnRuntimeUpgrade::on_runtime_upgrade());
weight = weight.saturating_add(<AllModules as OnRuntimeUpgrade>::on_runtime_upgrade());

weight
}

/// Execute all `OnRuntimeUpgrade` of this runtime, including the pre and post migration checks.
///
/// This should only be used for testing.
#[cfg(feature = "try-runtime")]
pub fn try_runtime_upgrade() -> Result<frame_support::weights::Weight, &'static str> {
<
(frame_system::Module::<System>, COnRuntimeUpgrade, AllModules)
as
OnRuntimeUpgrade
>::pre_upgrade()?;

let weight = Self::execute_on_runtime_upgrade();

<
(frame_system::Module::<System>, COnRuntimeUpgrade, AllModules)
as
OnRuntimeUpgrade
>::post_upgrade()?;

Ok(weight)
}

/// Start the execution of a particular block.
pub fn initialize_block(header: &System::Header) {
sp_io::init_tracing();
Expand Down Expand Up @@ -234,10 +267,7 @@ where
) {
let mut weight = 0;
if Self::runtime_upgraded() {
// System is not part of `AllModules`, so we need to call this manually.
weight = weight.saturating_add(<frame_system::Module::<System> as OnRuntimeUpgrade>::on_runtime_upgrade());
weight = weight.saturating_add(COnRuntimeUpgrade::on_runtime_upgrade());
weight = weight.saturating_add(<AllModules as OnRuntimeUpgrade>::on_runtime_upgrade());
weight = weight.saturating_add(Self::execute_on_runtime_upgrade());
}
<frame_system::Module<System>>::initialize(
block_number,
Expand Down
1 change: 1 addition & 0 deletions frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ std = [
nightly = []
strict = []
runtime-benchmarks = []
try-runtime = []
Loading

0 comments on commit 394c52e

Please sign in to comment.