support upgrade hooks to directly pass data (#12185)

* update interfaces of OnRuntimeUpgrade & Hooks

Signed-off-by: linning <linningde25@gmail.com>

* remove try-runtime for PreStateDigest

Signed-off-by: linning <linningde25@gmail.com>

* remove the Default bound of PreStateDigest

Signed-off-by: linning <linningde25@gmail.com>

* remove try-runtime for PreStateDigest & pre_upgrade

Signed-off-by: linning <linningde25@gmail.com>

* remove tmp storage between upgrade hooks

Signed-off-by: linning <linningde25@gmail.com>

* ensure hooks are storage noop

Signed-off-by: linning <linningde25@gmail.com>

* remove OnRuntimeUpgradeHelpersExt

Signed-off-by: linning <linningde25@gmail.com>

* cargo check & fmt

Signed-off-by: linning <linningde25@gmail.com>

* rename PreStateDigest to PreUpgradeState

Signed-off-by: linning <linningde25@gmail.com>

* replace associate type with codec & vec

Signed-off-by: linning <linningde25@gmail.com>

* add helper strcut to help encode/decode tuple

Signed-off-by: linning <linningde25@gmail.com>

* update comment

Signed-off-by: linning <linningde25@gmail.com>

* fix

Signed-off-by: linning <linningde25@gmail.com>

* add test

Signed-off-by: linning <linningde25@gmail.com>

* address comment

Signed-off-by: linning <linningde25@gmail.com>

* fix doc

Signed-off-by: linning <linningde25@gmail.com>

* fix ci

Signed-off-by: linning <linningde25@gmail.com>

* address comment

Signed-off-by: linning <linningde25@gmail.com>

* add more test cases

Signed-off-by: linning <linningde25@gmail.com>

* make clippy happy

Signed-off-by: linning <linningde25@gmail.com>

* fmt

Signed-off-by: linning <linningde25@gmail.com>

* update comment

Signed-off-by: linning <linningde25@gmail.com>

* fmt

Signed-off-by: linning <linningde25@gmail.com>

Signed-off-by: linning <linningde25@gmail.com>
This commit is contained in:
NingLin-P
2022-09-19 18:52:55 +08:00
committed by GitHub
parent 63c2e288bf
commit f7ac2cd20f
12 changed files with 182 additions and 99 deletions
+10 -12
View File
@@ -24,6 +24,8 @@ use frame_support::traits::OnRuntimeUpgrade;
#[cfg(feature = "try-runtime")]
use frame_support::ensure;
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;
/// A struct that does not migration, but only checks that the counter prefix exists and is correct.
pub struct CheckCounterPrefix<T: crate::Config<I>, I: 'static>(sp_std::marker::PhantomData<(T, I)>);
@@ -33,7 +35,7 @@ impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for CheckCounterPrefix<T,
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
// The old explicit storage item.
#[frame_support::storage_alias]
type CounterForListNodes<T: crate::Config<I>, I: 'static> =
@@ -51,7 +53,7 @@ impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for CheckCounterPrefix<T,
crate::ListNodes::<T, I>::count()
);
Ok(())
Ok(Vec::new())
}
}
@@ -80,17 +82,13 @@ mod old {
#[frame_support::storage_alias]
pub type CounterForListNodes<T: crate::Config<I>, I: 'static> =
StorageValue<crate::Pallet<T, I>, u32, ValueQuery>;
#[frame_support::storage_alias]
pub type TempStorage<T: crate::Config<I>, I: 'static> =
StorageValue<crate::Pallet<T, I>, u32, ValueQuery>;
}
/// A struct that migrates all bags lists to contain a score value.
pub struct AddScore<T: crate::Config<I>, I: 'static = ()>(sp_std::marker::PhantomData<(T, I)>);
impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for AddScore<T, I> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
// The list node data should be corrupt at this point, so this is zero.
ensure!(crate::ListNodes::<T, I>::iter().count() == 0, "list node data is not corrupt");
// We can use the helper `old::ListNode` to get the existing data.
@@ -98,8 +96,7 @@ impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for AddScore<T, I> {
let tracked_node_count: u32 = old::CounterForListNodes::<T, I>::get();
crate::log!(info, "number of nodes before: {:?} {:?}", iter_node_count, tracked_node_count);
ensure!(iter_node_count == tracked_node_count, "Node count is wrong.");
old::TempStorage::<T, I>::put(iter_node_count);
Ok(())
Ok(iter_node_count.encode())
}
fn on_runtime_upgrade() -> frame_support::weights::Weight {
@@ -122,9 +119,10 @@ impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for AddScore<T, I> {
}
#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
let node_count_before = old::TempStorage::<T, I>::take();
// Now, the list node data is not corrupt anymore.
fn post_upgrade(node_count_before: Vec<u8>) -> Result<(), &'static str> {
let node_count_before: u32 = Decode::decode(&mut node_count_before.as_slice())
.expect("the state parameter should be something that was generated by pre_upgrade");
// Now the list node data is not corrupt anymore.
let iter_node_count_after: u32 = crate::ListNodes::<T, I>::iter().count() as u32;
let tracked_node_count_after: u32 = crate::ListNodes::<T, I>::count();
crate::log!(