Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9940e78
update interfaces of OnRuntimeUpgrade & Hooks
NingLin-P Sep 4, 2022
df6c7d3
remove try-runtime for PreStateDigest
NingLin-P Sep 4, 2022
958f645
remove the Default bound of PreStateDigest
NingLin-P Sep 4, 2022
4cbfda0
remove try-runtime for PreStateDigest & pre_upgrade
NingLin-P Sep 4, 2022
6367791
remove tmp storage between upgrade hooks
NingLin-P Sep 4, 2022
a0c953b
Merge branch 'master' of github.com:paritytech/substrate into upgrade…
NingLin-P Sep 4, 2022
ca1c9e3
ensure hooks are storage noop
NingLin-P Sep 4, 2022
46e47c0
remove OnRuntimeUpgradeHelpersExt
NingLin-P Sep 4, 2022
e80c7fb
cargo check & fmt
NingLin-P Sep 5, 2022
49f487c
rename PreStateDigest to PreUpgradeState
NingLin-P Sep 6, 2022
c95eed3
replace associate type with codec & vec
NingLin-P Sep 6, 2022
44226fc
add helper strcut to help encode/decode tuple
NingLin-P Sep 6, 2022
88e4da5
update comment
NingLin-P Sep 6, 2022
33a4227
fix
NingLin-P Sep 6, 2022
ffea862
add test
NingLin-P Sep 7, 2022
4e19a3f
address comment
NingLin-P Sep 8, 2022
313bc4e
fix doc
NingLin-P Sep 8, 2022
f71a5c2
Merge branch 'master' of github.com:paritytech/substrate into upgrade…
NingLin-P Sep 8, 2022
af17cbf
fix ci
NingLin-P Sep 9, 2022
90108ec
address comment
NingLin-P Sep 13, 2022
eb0cb8b
add more test cases
NingLin-P Sep 13, 2022
2beb905
merge master
NingLin-P Sep 13, 2022
96f3a21
make clippy happy
NingLin-P Sep 13, 2022
c623720
fmt
NingLin-P Sep 13, 2022
7df0f56
update comment
NingLin-P Sep 16, 2022
8714113
fmt
NingLin-P Sep 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
replace associate type with codec & vec
Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P committed Sep 6, 2022
commit c95eed3832277cd1f7870e66c95119bee1492d3f
6 changes: 0 additions & 6 deletions frame/alliance/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,9 @@ pub fn migrate<T: Config<I>, I: 'static>() -> Weight {
pub struct Migration<T, I = ()>(PhantomData<(T, I)>);

impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for Migration<T, I> {
type PreUpgradeState = ();

fn on_runtime_upgrade() -> Weight {
migrate::<T, I>()
}

fn pre_upgrade() -> Result<(), &'static str> {
Ok(())
}
}

/// v0_to_v1: `UpForKicking` is replaced by a retirement period.
Expand Down
18 changes: 9 additions & 9 deletions frame/bags-list/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ use frame_support::{ensure, traits::OnRuntimeUpgrade};
/// 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)>);
impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for CheckCounterPrefix<T, I> {
type PreUpgradeState = ();

fn on_runtime_upgrade() -> frame_support::weights::Weight {
frame_support::weights::Weight::zero()
}

fn pre_upgrade() -> Result<(), &'static str> {
#[cfg(feature = "try-runtime")]
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> =
Expand All @@ -49,7 +48,7 @@ impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for CheckCounterPrefix<T,
crate::ListNodes::<T, I>::count()
);

Ok(())
Ok(vec![])
}
}

Expand Down Expand Up @@ -83,17 +82,16 @@ mod old {
/// 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> {
type PreUpgradeState = u32;

fn pre_upgrade() -> Result<u32, &'static str> {
#[cfg(feature = "try-runtime")]
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.
let iter_node_count: u32 = old::ListNodes::<T, I>::iter().count() as u32;
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.");
Ok(iter_node_count)
Ok(iter_node_count.encode())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
Expand All @@ -116,7 +114,9 @@ impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for AddScore<T, I> {
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(node_count_before: u32) -> Result<(), &'static str> {
fn post_upgrade(node_count_before: Vec<u8>) -> Result<(), &'static str> {
let node_count_before = Decode::decode(&mut node_count_before.as_slice())
.expect("the state parameter should be something that 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();
Expand Down
6 changes: 0 additions & 6 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,18 +865,12 @@ mod tests {

struct CustomOnRuntimeUpgrade;
impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
type PreUpgradeState = ();

fn on_runtime_upgrade() -> Weight {
sp_io::storage::set(TEST_KEY, "custom_upgrade".as_bytes());
sp_io::storage::set(CUSTOM_ON_RUNTIME_KEY, &true.encode());
System::deposit_event(frame_system::Event::CodeUpdated);
Weight::from_ref_time(100)
}

fn pre_upgrade() -> Result<(), &'static str> {
Ok(())
}
}

type Executive = super::Executive<
Expand Down
17 changes: 5 additions & 12 deletions frame/nomination-pools/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ pub mod v1 {
/// Note: The depositor is not optional since he can never change.
pub struct MigrateToV1<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
type PreUpgradeState = ();

fn on_runtime_upgrade() -> Weight {
let current = Pallet::<T>::current_storage_version();
let onchain = Pallet::<T>::on_chain_storage_version();
Expand Down Expand Up @@ -98,12 +96,8 @@ pub mod v1 {
}
}

fn pre_upgrade() -> Result<(), &'static str> {
Ok(())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: ()) -> Result<(), &'static str> {
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
// new version must be set.
assert_eq!(Pallet::<T>::on_chain_storage_version(), 1);
Ok(())
Expand Down Expand Up @@ -332,8 +326,6 @@ pub mod v2 {
}

impl<T: Config> OnRuntimeUpgrade for MigrateToV2<T> {
type PreUpgradeState = ();

fn on_runtime_upgrade() -> Weight {
let current = Pallet::<T>::current_storage_version();
let onchain = Pallet::<T>::on_chain_storage_version();
Expand All @@ -353,7 +345,8 @@ pub mod v2 {
}
}

fn pre_upgrade() -> Result<(), &'static str> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
// all reward accounts must have more than ED.
RewardPools::<T>::iter().for_each(|(id, _)| {
assert!(
Expand All @@ -362,11 +355,11 @@ pub mod v2 {
)
});

Ok(())
Ok(vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: ()) -> Result<(), &'static str> {
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
// new version must be set.
assert_eq!(Pallet::<T>::on_chain_storage_version(), 2);

Expand Down
18 changes: 7 additions & 11 deletions frame/staking/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ pub mod v10 {
/// prevent us from iterating over an arbitrary large number of keys `on_runtime_upgrade`.
pub struct MigrateToV10<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV10<T> {
type PreUpgradeState = ();

fn on_runtime_upgrade() -> frame_support::weights::Weight {
if StorageVersion::<T>::get() == Releases::V9_0_0 {
let pending_slashes = <Pallet<T> as Store>::UnappliedSlashes::iter().take(512);
Expand All @@ -58,23 +56,18 @@ pub mod v10 {
T::DbWeight::get().reads(1)
}
}

fn pre_upgrade() -> Result<(), &'static str> {
Ok(())
}
}
}

pub mod v9 {
use super::*;
use frame_support::codec::{Decode, Encode};

/// Migration implementation that injects all validators into sorted list.
///
/// This is only useful for chains that started their `VoterList` just based on nominators.
pub struct InjectValidatorsIntoVoterList<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for InjectValidatorsIntoVoterList<T> {
type PreUpgradeState = u32;

fn on_runtime_upgrade() -> Weight {
if StorageVersion::<T>::get() == Releases::V8_0_0 {
let prev_count = T::VoterList::count();
Expand Down Expand Up @@ -106,18 +99,21 @@ pub mod v9 {
}
}

fn pre_upgrade() -> Result<u32, &'static str> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
frame_support::ensure!(
StorageVersion::<T>::get() == crate::Releases::V8_0_0,
"must upgrade linearly"
);

let prev_count = T::VoterList::count();
Ok(prev_count)
Ok(prev_count.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(prev_count: u32) -> Result<(), &'static str> {
fn post_upgrade(prev_count: Vec<u8>) -> Result<(), &'static str> {
let prev_count = Decode::decode(&mut prev_count)
.expect("the state parameter should be something that generated by pre_upgrade");
let post_count = T::VoterList::count();
let validators = Validators::<T>::count();
assert!(post_count == prev_count + validators);
Expand Down
23 changes: 9 additions & 14 deletions frame/support/procedural/src/pallet/expand/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use crate::pallet::Def;

/// * implement the individual traits using the Hooks trait
pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
let (where_clause, span, has_runtime_upgrade, pre_upgrade_state) = match def.hooks.as_ref()
{
let (where_clause, span, has_runtime_upgrade, pre_upgrade_state) = match def.hooks.as_ref() {
Some(hooks) => {
let where_clause = hooks.where_clause.clone();
let span = hooks.attr_span;
Expand Down Expand Up @@ -147,8 +146,6 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
#frame_support::traits::OnRuntimeUpgrade
for #pallet_ident<#type_use_gen> #where_clause
{
type PreUpgradeState = #pre_upgrade_state;

fn on_runtime_upgrade() -> #frame_support::weights::Weight {
#frame_support::sp_tracing::enter_span!(
#frame_support::sp_tracing::trace_span!("on_runtime_update")
Expand All @@ -170,29 +167,27 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
>::on_runtime_upgrade()
}

#[cfg(not(feature = "try-runtime"))]
fn pre_upgrade() -> Result<Self::PreUpgradeState, &'static str> {
Ok(Self::PreUpgradeState::default())
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Self::PreUpgradeState, &'static str> {
<
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
use #frame_support::codec::Encode as _;
Ok(<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber,
#pre_upgrade_state
>
>::pre_upgrade()
>::pre_upgrade().encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(digest: Self::PreUpgradeState) -> Result<(), &'static str> {
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
let state: #pre_upgrade_state = #frame_support::codec::Decode::decode(&mut state.as_slice())
.expect("the state parameter should be something that generated by pre_upgrade");
<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber,
#pre_upgrade_state
>
>::post_upgrade(digest)
>::post_upgrade(state)
}
}

Expand Down
16 changes: 2 additions & 14 deletions frame/support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,8 +1588,6 @@ macro_rules! decl_module {
$crate::traits::OnRuntimeUpgrade
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
type PreUpgradeState = ();

fn on_runtime_upgrade() -> $return {
$crate::sp_tracing::enter_span!($crate::sp_tracing::trace_span!("on_runtime_upgrade"));
let pallet_name = <<
Expand All @@ -1610,12 +1608,8 @@ macro_rules! decl_module {
{ $( $impl )* }
}

fn pre_upgrade() -> Result<(), &'static str> {
Ok(())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: ()) -> Result<(), &'static str> {
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
Ok(())
}
}
Expand All @@ -1630,8 +1624,6 @@ macro_rules! decl_module {
$crate::traits::OnRuntimeUpgrade
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
type PreUpgradeState = ();

fn on_runtime_upgrade() -> $crate::dispatch::Weight {
$crate::sp_tracing::enter_span!($crate::sp_tracing::trace_span!("on_runtime_upgrade"));
let pallet_name = <<
Expand All @@ -1649,12 +1641,8 @@ macro_rules! decl_module {
$crate::dispatch::Weight::zero()
}

fn pre_upgrade() -> Result<(), &'static str> {
Ok(())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: ()) -> Result<(), &'static str> {
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
Ok(())
}
}
Expand Down
Loading