From f2f1d7aa4d32671db9647437e2426195011045de Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Mon, 18 Mar 2024 16:53:49 +0300 Subject: [PATCH] [substrate-apply] pallet-*: BREAKING - Try-runtime: Use proper error types (#13993) --- .../src/lib.rs | 6 ++++-- .../src/upgrade_init.rs | 13 +++++++++---- .../pallet-dummy-precompiles-code/src/lib.rs | 10 +++++++--- crates/pallet-erc20-support/src/lib.rs | 18 +++++++++++------- crates/pallet-humanode-session/src/lib.rs | 6 ++++-- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs index 9bfb210c6..ec67377cc 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs @@ -47,6 +47,8 @@ pub const CURRENT_BRIDGES_INITIALIZER_VERSION: u16 = 1; #[allow(clippy::missing_docs_in_private_items)] #[frame_support::pallet] pub mod pallet { + #[cfg(feature = "try-runtime")] + use frame_support::sp_runtime::TryRuntimeError; use frame_support::{pallet_prelude::*, sp_runtime::traits::MaybeDisplay}; use frame_system::pallet_prelude::*; use sp_std::fmt::Debug; @@ -147,12 +149,12 @@ pub mod pallet { } #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { + fn pre_upgrade() -> Result, TryRuntimeError> { upgrade_init::pre_upgrade() } #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), &'static str> { + fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { upgrade_init::post_upgrade::(state) } } diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/upgrade_init.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/upgrade_init.rs index 365159627..43851e72d 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/upgrade_init.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/upgrade_init.rs @@ -2,6 +2,8 @@ use frame_support::pallet_prelude::*; #[cfg(feature = "try-runtime")] +use frame_support::sp_runtime::TryRuntimeError; +#[cfg(feature = "try-runtime")] use sp_std::vec::Vec; use crate::{ @@ -55,7 +57,7 @@ pub fn on_runtime_upgrade() -> Weight { /// /// Panics if anything goes wrong. #[cfg(feature = "try-runtime")] -pub fn pre_upgrade() -> Result, &'static str> { +pub fn pre_upgrade() -> Result, TryRuntimeError> { // Do nothing. Ok(Vec::new()) } @@ -64,16 +66,19 @@ pub fn pre_upgrade() -> Result, &'static str> { /// /// Panics if anything goes wrong. #[cfg(feature = "try-runtime")] -pub fn post_upgrade(_state: Vec) -> Result<(), &'static str> { +pub fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { use frame_support::{storage_root, StateVersion}; let storage_root_before = storage_root(StateVersion::V1); if !Pallet::::is_balanced()? { - return Err("currencies are not balanced"); + return Err(TryRuntimeError::Other("currencies are not balanced")); } - assert_eq!(storage_root_before, storage_root(StateVersion::V1)); + ensure!( + storage_root_before == storage_root(StateVersion::V1), + "expect V1 state version at storage root" + ); assert_eq!( >::on_chain_storage_version(), diff --git a/crates/pallet-dummy-precompiles-code/src/lib.rs b/crates/pallet-dummy-precompiles-code/src/lib.rs index 797cc56a8..de61631cf 100644 --- a/crates/pallet-dummy-precompiles-code/src/lib.rs +++ b/crates/pallet-dummy-precompiles-code/src/lib.rs @@ -32,6 +32,8 @@ pub const DUMMY_CODE: &[u8] = &[0x5F, 0x5F, 0xFD]; #[allow(clippy::missing_docs_in_private_items)] #[frame_support::pallet] pub mod pallet { + #[cfg(feature = "try-runtime")] + use frame_support::sp_runtime::TryRuntimeError; use frame_support::{pallet_prelude::*, sp_std::vec::Vec}; use frame_system::pallet_prelude::*; @@ -118,13 +120,13 @@ pub mod pallet { } #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { + fn pre_upgrade() -> Result, TryRuntimeError> { // Do nothing. Ok(Vec::new()) } #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { use sp_std::vec::Vec; let mut not_created_precompiles = Vec::new(); @@ -137,7 +139,9 @@ pub mod pallet { } if !not_created_precompiles.is_empty() { - return Err("precompiles not created properly: {:not_created_precompiles}"); + return Err(TryRuntimeError::Other( + "precompiles not created properly: {:not_created_precompiles}", + )); } assert_eq!( diff --git a/crates/pallet-erc20-support/src/lib.rs b/crates/pallet-erc20-support/src/lib.rs index c2ee7b744..a42adb7e5 100644 --- a/crates/pallet-erc20-support/src/lib.rs +++ b/crates/pallet-erc20-support/src/lib.rs @@ -45,9 +45,12 @@ type BalanceOf = <>::Currency as Currency #[frame_support::pallet] pub mod pallet { - #[cfg(feature = "try-runtime")] - use frame_support::sp_std::{vec, vec::Vec}; use frame_support::{pallet_prelude::*, sp_runtime::traits::MaybeDisplay, sp_std::fmt::Debug}; + #[cfg(feature = "try-runtime")] + use frame_support::{ + sp_runtime::TryRuntimeError, + sp_std::{vec, vec::Vec}, + }; use frame_system::pallet_prelude::*; use super::*; @@ -120,15 +123,16 @@ pub mod pallet { } #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { + fn pre_upgrade() -> Result, TryRuntimeError> { Ok(vec![]) } #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), &'static str> { - assert_eq!( - >::on_chain_storage_version(), - >::current_storage_version() + fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { + ensure!( + >::on_chain_storage_version() + == >::current_storage_version(), + "the current storage version and onchain storage version should be the same" ); Ok(()) } diff --git a/crates/pallet-humanode-session/src/lib.rs b/crates/pallet-humanode-session/src/lib.rs index f9bdc7438..987d6ea75 100644 --- a/crates/pallet-humanode-session/src/lib.rs +++ b/crates/pallet-humanode-session/src/lib.rs @@ -34,6 +34,8 @@ pub mod pallet { use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; use sp_runtime::BoundedBTreeSet; + #[cfg(feature = "try-runtime")] + use sp_runtime::TryRuntimeError; use super::*; @@ -168,12 +170,12 @@ pub mod pallet { } #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { + fn pre_upgrade() -> Result, TryRuntimeError> { Ok(migrations::v1::pre_migrate::()) } #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), &'static str> { + fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { migrations::v1::post_migrate::(state); Ok(()) }