-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UpgradeInit for pallet-balanced.. at runtime
- Loading branch information
1 parent
0e5974d
commit 8d53e99
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters