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

try-runtime pre-post hooks not executed if defined within the pallet #13681

@kianenigma

Description

@kianenigma

we have been increasingly using external migrations and have seemingly introduced a regression in how we define migrations, namely pre-post migration hooks within a pallet. Here's the most simple way to reproduce this:

Apply this:

diff --git a/Cargo.lock b/Cargo.lock
index 29ddf38328..d3200668fd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6506,6 +6506,7 @@ dependencies = [
  "sp-core",
  "sp-io",
  "sp-runtime",
+ "sp-std",
 ]
 
 [[package]]
diff --git a/bin/node-template/pallets/template/Cargo.toml b/bin/node-template/pallets/template/Cargo.toml
index f6607b25c4..6db219734d 100644
--- a/bin/node-template/pallets/template/Cargo.toml
+++ b/bin/node-template/pallets/template/Cargo.toml
@@ -20,6 +20,7 @@ scale-info = { version = "2.1.1", default-features = false, features = ["derive"
 frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../../../../frame/benchmarking" }
 frame-support = { version = "4.0.0-dev", default-features = false, path = "../../../../frame/support" }
 frame-system = { version = "4.0.0-dev", default-features = false, path = "../../../../frame/system" }
+sp-std = { path = "../../../../primitives/std", default-features = false }
 
 [dev-dependencies]
 sp-core = { version = "7.0.0", path = "../../../../primitives/core" }
diff --git a/bin/node-template/pallets/template/src/lib.rs b/bin/node-template/pallets/template/src/lib.rs
index 4630e344ad..3b760e06d6 100644
--- a/bin/node-template/pallets/template/src/lib.rs
+++ b/bin/node-template/pallets/template/src/lib.rs
@@ -30,6 +30,25 @@ pub mod pallet {
 		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
 	}
 
+	#[pallet::hooks]
+	impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {
+		fn on_runtime_upgrade() -> Weight {
+			Default::default()
+		}
+
+		#[cfg(feature = "try-runtime")]
+		fn pre_upgrade() -> Result<sp_std::prelude::Vec<u8>, &'static str> {
+			frame_support::log::info!(target: "pallet-template", "pre_upgrade");
+			Err("BOOOO")
+		}
+
+		#[cfg(feature = "try-runtime")]
+		fn post_upgrade(_state: sp_std::prelude::Vec<u8>) -> Result<(), &'static str> {
+			frame_support::log::info!(target: "pallet-template", "post_upgrade");
+			Ok(())
+		}
+	}
+
 	// The pallet's runtime storage items.
 	// https://docs.substrate.io/main-docs/build/runtime-storage/
 	#[pallet::storage]

Then:

# Run a base node: 
cargo remote run --  --release -p node-template -- --dev

# Build a try-runtime CLI and runtime 
cargo remote build --  --release --features try-runtime -p node-template
cargo remote build --  --release --features try-runtime -p node-template-runtime 

# Execute
RUST_LOG=pallet-template=trace,info ./target/release/node-template try-runtime --runtime ./target/release/wbuild/node-template-runtime/node_template_runtime.wasm on-runtime-upgrade live -u ws://localhost:9944

Logs won't be emitted, the error that I return in pre_upgrade is also not caught. Seems like the code is not really executed.

Initially tested against polkadot-9.38 branch. Probably present in master as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T1-runtimeThis PR/Issue is related to the topic “runtime”.U1-asapNo need to stop dead in your tracks, however issue should be addressed as soon as possible.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions