From c590ad6428ccca53cdf00844574204492ace596c Mon Sep 17 00:00:00 2001 From: Tsvetomir Dimitrov Date: Wed, 4 Jan 2023 11:46:04 +0200 Subject: [PATCH 1/4] Add `try-runtime` to Rococo runtime --- Cargo.lock | 1 + runtime/rococo/Cargo.toml | 3 +++ runtime/rococo/src/lib.rs | 55 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e9bd9b74286d..57ef6419d940 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8050,6 +8050,7 @@ dependencies = [ "frame-system", "frame-system-benchmarking", "frame-system-rpc-runtime-api", + "frame-try-runtime", "hex-literal", "log", "pallet-authority-discovery", diff --git a/runtime/rococo/Cargo.toml b/runtime/rococo/Cargo.toml index aaf6710b7c1a..c5cd5f741503 100644 --- a/runtime/rococo/Cargo.toml +++ b/runtime/rococo/Cargo.toml @@ -77,6 +77,7 @@ pallet-xcm = { path = "../../xcm/pallet-xcm", default-features = false } pallet-xcm-benchmarks = { path = "../../xcm/pallet-xcm-benchmarks", default-features = false, optional = true } frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } +frame-try-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } frame-system-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } hex-literal = { version = "0.3.4" } @@ -168,6 +169,7 @@ std = [ "sp-session/std", "runtime-common/std", "runtime-parachains/std", + "frame-try-runtime/std", "beefy-primitives/std", "rococo-runtime-constants/std", "xcm/std", @@ -214,6 +216,7 @@ runtime-benchmarks = [ ] try-runtime = [ "frame-executive/try-runtime", + "frame-try-runtime", "frame-system/try-runtime", "pallet-authority-discovery/try-runtime", "pallet-authorship/try-runtime", diff --git a/runtime/rococo/src/lib.rs b/runtime/rococo/src/lib.rs index 91a095cf23f5..69f281236265 100644 --- a/runtime/rococo/src/lib.rs +++ b/runtime/rococo/src/lib.rs @@ -1926,6 +1926,26 @@ sp_api::impl_runtime_apis! { } } + #[cfg(feature = "try-runtime")] + impl frame_try_runtime::TryRuntime for Runtime { + fn on_runtime_upgrade(checks: bool) -> (Weight, Weight) { + log::info!("try-runtime::on_runtime_upgrade rococo."); + let weight = Executive::try_runtime_upgrade(checks).unwrap(); + (weight, BlockWeights::get().max_block) + } + + 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, signature_check, select).unwrap() + } + } + #[cfg(feature = "runtime-benchmarks")] impl frame_benchmarking::Benchmark for Runtime { fn benchmark_metadata(extra: bool) -> ( @@ -2047,3 +2067,38 @@ sp_api::impl_runtime_apis! { } } } + +#[cfg(all(test, feature = "try-runtime"))] +mod remote_tests { + use super::*; + use frame_try_runtime::runtime_decl_for_TryRuntime::TryRuntime; + use remote_externalities::{ + Builder, Mode, OfflineConfig, OnlineConfig, SnapshotConfig, Transport, + }; + use std::env::var; + + #[tokio::test] + async fn run_migrations() { + sp_tracing::try_init_simple(); + let transport: Transport = + var("WS").unwrap_or("wss://rococo-rpc.polkadot.io:443".to_string()).into(); + let maybe_state_snapshot: Option = var("SNAP").map(|s| s.into()).ok(); + let mut ext = Builder::::default() + .mode(if let Some(state_snapshot) = maybe_state_snapshot { + Mode::OfflineOrElseOnline( + OfflineConfig { state_snapshot: state_snapshot.clone() }, + OnlineConfig { + transport, + state_snapshot: Some(state_snapshot), + ..Default::default() + }, + ) + } else { + Mode::Online(OnlineConfig { transport, ..Default::default() }) + }) + .build() + .await + .unwrap(); + ext.execute_with(|| Runtime::on_runtime_upgrade(true)); + } +} From 1d4181a0b0c05267d093aaa6d712573a142a2a5d Mon Sep 17 00:00:00 2001 From: Tsvetomir Dimitrov Date: Wed, 4 Jan 2023 13:49:32 +0200 Subject: [PATCH 2/4] Enable try-runtime in all pallets --- runtime/rococo/Cargo.toml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/runtime/rococo/Cargo.toml b/runtime/rococo/Cargo.toml index c5cd5f741503..09c431b189bf 100644 --- a/runtime/rococo/Cargo.toml +++ b/runtime/rococo/Cargo.toml @@ -221,6 +221,9 @@ try-runtime = [ "pallet-authority-discovery/try-runtime", "pallet-authorship/try-runtime", "pallet-balances/try-runtime", + "pallet-babe/try-runtime", + "pallet-beefy/try-runtime", + "pallet-beefy-mmr/try-runtime", "pallet-bounties/try-runtime", "pallet-child-bounties/try-runtime", "pallet-transaction-payment/try-runtime", @@ -232,7 +235,9 @@ try-runtime = [ "pallet-im-online/try-runtime", "pallet-indices/try-runtime", "pallet-membership/try-runtime", + "pallet-mmr/try-runtime", "pallet-multisig/try-runtime", + "pallet-nis/try-runtime", "pallet-offences/try-runtime", "pallet-preimage/try-runtime", "pallet-proxy/try-runtime", @@ -242,13 +247,15 @@ try-runtime = [ "pallet-society/try-runtime", "pallet-sudo/try-runtime", "pallet-staking/try-runtime", + "pallet-state-trie-migration/try-runtime", "pallet-timestamp/try-runtime", "pallet-tips/try-runtime", "pallet-treasury/try-runtime", "pallet-utility/try-runtime", "pallet-vesting/try-runtime", - "pallet-babe/try-runtime", + "pallet-xcm/try-runtime", "runtime-common/try-runtime", + "runtime-parachains/try-runtime", ] # When enabled, the runtime API will not be build. # From 7ab5cfcd2c1ee2bf881fb745c8beebb3241468c2 Mon Sep 17 00:00:00 2001 From: Tsvetomir Dimitrov Date: Wed, 4 Jan 2023 15:35:29 +0200 Subject: [PATCH 3/4] Update runtime/rococo/src/lib.rs Co-authored-by: Oliver Tale-Yazdi --- runtime/rococo/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/rococo/src/lib.rs b/runtime/rococo/src/lib.rs index 69f281236265..65ad8223e5d8 100644 --- a/runtime/rococo/src/lib.rs +++ b/runtime/rococo/src/lib.rs @@ -1928,7 +1928,7 @@ sp_api::impl_runtime_apis! { #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { - fn on_runtime_upgrade(checks: bool) -> (Weight, Weight) { + fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) { log::info!("try-runtime::on_runtime_upgrade rococo."); let weight = Executive::try_runtime_upgrade(checks).unwrap(); (weight, BlockWeights::get().max_block) From 4715dba40b09c71c4e2d57fdf671c9645e21e7c6 Mon Sep 17 00:00:00 2001 From: Tsvetomir Dimitrov Date: Wed, 4 Jan 2023 16:01:42 +0200 Subject: [PATCH 4/4] Add dev-dependencies for the try-runtime test --- Cargo.lock | 3 +++ runtime/rococo/Cargo.toml | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 57ef6419d940..c748b5d956ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8046,6 +8046,7 @@ dependencies = [ "beefy-merkle-tree", "frame-benchmarking", "frame-executive", + "frame-remote-externalities", "frame-support", "frame-system", "frame-system-benchmarking", @@ -8118,12 +8119,14 @@ dependencies = [ "sp-session", "sp-staking", "sp-std", + "sp-tracing", "sp-transaction-pool", "sp-trie", "sp-version", "static_assertions", "substrate-wasm-builder", "tiny-keccak", + "tokio", "xcm", "xcm-builder", "xcm-executor", diff --git a/runtime/rococo/Cargo.toml b/runtime/rococo/Cargo.toml index 09c431b189bf..dda042a6e5ce 100644 --- a/runtime/rococo/Cargo.toml +++ b/runtime/rococo/Cargo.toml @@ -93,9 +93,12 @@ xcm-builder = { package = "xcm-builder", path = "../../xcm/xcm-builder", default [dev-dependencies] tiny-keccak = { version = "2.0.2", features = ["keccak"] } keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substrate", branch = "master" } +remote-externalities = { git = "https://github.com/paritytech/substrate", branch = "master", package = "frame-remote-externalities" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } separator = "0.4.1" serde_json = "1.0.81" +sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } +tokio = { version = "1.22.0", features = ["macros"] } [build-dependencies] substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }