Skip to content

Commit

Permalink
Add UpgradeInit for pallet-balanced.. at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Nov 7, 2024
1 parent 0e5974d commit 8d53e99
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
54 changes: 54 additions & 0 deletions crates/humanode-runtime/src/init_storage_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use core::marker::PhantomData;

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

/// Pallet storage version initializer.
pub struct InitStorageVersion<P, R>(PhantomData<(P, R)>);

impl<P, R> OnRuntimeUpgrade for InitStorageVersion<P, R>
where
P: GetStorageVersion + PalletInfoAccess,
R: frame_system::Config,
{
fn on_runtime_upgrade() -> Weight {
// Properly manage default on chain storage version as the pallet was added after genesis
// with initial storage version != 0.
//
// <https://github.com/paritytech/substrate/pull/14641>
let current_storage_version = P::current_storage_version();
let onchain_storage_version = P::on_chain_storage_version();

let mut weight = R::DbWeight::get().reads(1);

if onchain_storage_version == 0 && current_storage_version != 0 {
// Set new storage version.
current_storage_version.put::<P>();

// Write the onchain storage version.
weight = weight.saturating_add(R::DbWeight::get().writes(1));
} else {
info!(target: "runtime", message = "Nothing to do. This runtime upgrade probably should be removed");
}

weight
}

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

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
assert_eq!(P::on_chain_storage_version(), P::current_storage_version());

Ok(())
}
}
1 change: 1 addition & 0 deletions crates/humanode-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ pub type Executive = frame_executive::Executive<
AllPalletsWithSystem,
(
pallet_dummy_precompiles_code::UpgradeInit<Runtime>,
pallet_balanced_currency_swap_bridges_initializer::UpgradeInit<Runtime>,
pallet_erc20_support::MigrationToV1<Runtime>,
pallet_humanode_session::MigrationToV1<Runtime>,
),
Expand Down

0 comments on commit 8d53e99

Please sign in to comment.