Skip to content

Commit

Permalink
[substrate-apply] pallet-*: BREAKING - Try-runtime: Use proper error …
Browse files Browse the repository at this point in the history
…types (#13993)
  • Loading branch information
dmitrylavrenov committed Oct 28, 2024
1 parent 62c5ad6 commit f2f1d7a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,12 +149,12 @@ pub mod pallet {
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
upgrade_init::pre_upgrade()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
fn post_upgrade(state: Vec<u8>) -> Result<(), TryRuntimeError> {
upgrade_init::post_upgrade::<T>(state)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -55,7 +57,7 @@ pub fn on_runtime_upgrade<T: Config>() -> Weight {
///
/// Panics if anything goes wrong.
#[cfg(feature = "try-runtime")]
pub fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
pub fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
// Do nothing.
Ok(Vec::new())
}
Expand All @@ -64,16 +66,19 @@ pub fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
///
/// Panics if anything goes wrong.
#[cfg(feature = "try-runtime")]
pub fn post_upgrade<T: Config>(_state: Vec<u8>) -> Result<(), &'static str> {
pub fn post_upgrade<T: Config>(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
use frame_support::{storage_root, StateVersion};

let storage_root_before = storage_root(StateVersion::V1);

if !Pallet::<T>::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!(
<Pallet<T>>::on_chain_storage_version(),
Expand Down
10 changes: 7 additions & 3 deletions crates/pallet-dummy-precompiles-code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -118,13 +120,13 @@ pub mod pallet {
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
// Do nothing.
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
use sp_std::vec::Vec;

let mut not_created_precompiles = Vec::new();
Expand All @@ -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!(
Expand Down
18 changes: 11 additions & 7 deletions crates/pallet-erc20-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ type BalanceOf<T, I> = <<T as Config<I>>::Currency as Currency<AccountIdOf<T, I>
#[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::*;
Expand Down Expand Up @@ -120,15 +123,16 @@ pub mod pallet {
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
Ok(vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
assert_eq!(
<Pallet<T, I>>::on_chain_storage_version(),
<Pallet<T, I>>::current_storage_version()
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
ensure!(
<Pallet<T, I>>::on_chain_storage_version()
== <Pallet<T, I>>::current_storage_version(),
"the current storage version and onchain storage version should be the same"
);
Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions crates/pallet-humanode-session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -168,12 +170,12 @@ pub mod pallet {
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
Ok(migrations::v1::pre_migrate::<T>())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
fn post_upgrade(state: Vec<u8>) -> Result<(), TryRuntimeError> {
migrations::v1::post_migrate::<T>(state);
Ok(())
}
Expand Down

0 comments on commit f2f1d7a

Please sign in to comment.