Skip to content

Commit

Permalink
Use fully qualify syntax for try-runtime usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Dec 4, 2024
1 parent 47ff153 commit 76005cf
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 41 deletions.
14 changes: 6 additions & 8 deletions crates/humanode-runtime/src/storage_version_initializer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use core::marker::PhantomData;

#[cfg(feature = "try-runtime")]
use frame_support::{ensure, sp_runtime::TryRuntimeError};
use frame_support::{
log::info,
traits::{Get, GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess, StorageVersion},
weights::Weight,
};
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;

/// Pallet storage version initializer.
pub struct StorageVersionInitializer<P, R>(PhantomData<(P, R)>);
Expand Down Expand Up @@ -51,14 +47,16 @@ where
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, frame_support::sp_runtime::TryRuntimeError> {
// Do nothing.
Ok(Vec::new())
Ok(sp_std::vec::Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
ensure!(
fn post_upgrade(
_state: sp_std::vec::Vec<u8>,
) -> Result<(), frame_support::sp_runtime::TryRuntimeError> {
frame_support::ensure!(
P::on_chain_storage_version() == P::current_storage_version(),
"the current storage version and onchain storage version should be the same"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use frame_support::{
};
pub use pallet::*;
use sp_std::cmp::Ordering;
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;
pub use weights::*;

pub mod weights;
Expand Down Expand Up @@ -47,8 +45,6 @@ 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 @@ -149,12 +145,15 @@ pub mod pallet {
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, frame_support::sp_runtime::TryRuntimeError>
{
upgrade_init::pre_upgrade()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), TryRuntimeError> {
fn post_upgrade(
state: sp_std::vec::Vec<u8>,
) -> Result<(), frame_support::sp_runtime::TryRuntimeError> {
upgrade_init::post_upgrade::<T>(state)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
//! Initialization of the bridge pot accounts on runtime upgrade.
#[cfg(feature = "try-runtime")]
use frame_support::sp_runtime::TryRuntimeError;
use frame_support::{log, pallet_prelude::*};
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;

use crate::{
Config, LastForceRebalanceAskCounter, LastInitializerVersion, Pallet,
Expand Down Expand Up @@ -40,22 +36,26 @@ pub fn on_runtime_upgrade<T: Config>() -> Weight {
///
/// Panics if anything goes wrong.
#[cfg(feature = "try-runtime")]
pub fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
pub fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, frame_support::sp_runtime::TryRuntimeError> {
// Do nothing.
Ok(Vec::new())
Ok(sp_std::vec::Vec::new())
}

/// Check the state after the bridges initialization.
///
/// Panics if anything goes wrong.
#[cfg(feature = "try-runtime")]
pub fn post_upgrade<T: Config>(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
pub fn post_upgrade<T: Config>(
_state: sp_std::vec::Vec<u8>,
) -> Result<(), frame_support::sp_runtime::TryRuntimeError> {
use frame_support::{storage_root, StateVersion};

let storage_root_before = storage_root(StateVersion::V1);

if !Pallet::<T>::is_balanced()? {
return Err(TryRuntimeError::Other("currencies are not balanced"));
return Err(frame_support::sp_runtime::TryRuntimeError::Other(
"currencies are not balanced",
));
}

ensure!(
Expand Down
10 changes: 3 additions & 7 deletions crates/pallet-dummy-precompiles-code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ 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 @@ -103,15 +101,13 @@ pub mod pallet {
}

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

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

fn post_upgrade(_state: Vec<u8>) -> Result<(), frame_support::sp_runtime::TryRuntimeError> {
let mut not_created_precompiles = Vec::new();

for precompile_address in &T::PrecompilesAddresses::get() {
Expand All @@ -122,7 +118,7 @@ pub mod pallet {
}

if !not_created_precompiles.is_empty() {
return Err(TryRuntimeError::Other(
return Err(frame_support::sp_runtime::TryRuntimeError::Other(
"precompiles not created properly: {:not_created_precompiles}",
));
}
Expand Down
15 changes: 7 additions & 8 deletions crates/pallet-erc20-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ type BalanceOf<T, I> = <<T as Config<I>>::Currency as Currency<AccountIdOf<T, I>
pub mod pallet {

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 @@ -123,12 +118,16 @@ pub mod pallet {
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
Ok(vec![])
fn pre_upgrade(
) -> Result<frame_support::sp_std::vec::Vec<u8>, frame_support::sp_runtime::TryRuntimeError>
{
Ok(frame_support::sp_std::vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
fn post_upgrade(
_state: frame_support::sp_std::vec::Vec<u8>,
) -> Result<(), frame_support::sp_runtime::TryRuntimeError> {
ensure!(
<Pallet<T, I>>::on_chain_storage_version()
== <Pallet<T, I>>::current_storage_version(),
Expand Down
6 changes: 2 additions & 4 deletions crates/pallet-humanode-session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ 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 @@ -170,12 +168,12 @@ pub mod pallet {
}

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

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

0 comments on commit 76005cf

Please sign in to comment.