Skip to content

Commit

Permalink
upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed Oct 1, 2024
1 parent fcbe8c5 commit f95bd72
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
73 changes: 73 additions & 0 deletions pallets/parachain-staking/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,76 @@

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};

use crate::*;

#[derive(Clone, PartialEq, Eq, parity_scale_codec::Decode, sp_runtime::RuntimeDebug)]
/// Reserve information { account, percent_of_inflation }
pub struct OldParachainBondConfig<AccountId> {
/// Account which receives funds intended for parachain bond
pub account: AccountId,
/// Percent of inflation set aside for parachain bond account
pub percent: sp_runtime::Percent,
}

pub struct MigrateParachainBondConfig<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateParachainBondConfig<T> {
fn on_runtime_upgrade() -> Weight {
let (account, percent) = if let Some(config) =
frame_support::storage::migration::get_storage_value::<
OldParachainBondConfig<T::AccountId>,
>(b"ParachainStaking", b"ParachainBondInfo", &[])
{
(config.account, config.percent)
} else {
return Weight::default();
};

let pbr = InflationDistributionAccount { account, percent };
let treasury = InflationDistributionAccount::<T::AccountId>::default();
let configs: InflationDistributionConfig<T::AccountId> = [pbr, treasury].into();

//***** Start mutate storage *****//

InflationDistributionInfo::<T>::put(configs);

// Remove storage value AssetManager::SupportedFeePaymentAssets
frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
b"ParachainStaking",
b"ParachainBondInfo",
));

Weight::default()
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
let state = frame_support::storage::migration::get_storage_value::<
OldParachainBondConfig<T::AccountId>,
>(b"ParachainStaking", b"ParachainBondInfo", &[]);

ensure!(state.is_some(), "State not found");

Ok(state.unwrap().encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(&self, state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
let old_state: OldParachainBondConfig<T::AccountId> =
parity_scale_codec::Decode::decode(&mut &state[..])
.map_err(|_| sp_runtime::DispatchError::Other("Failed to decode old state"))?;

let new_state = InflationDistributionInfo::<T>::get();

let pbr = InflationDistributionAccount {
account: old_state.account,
percent: old_state.percent,
};
let treasury = InflationDistributionAccount::<T::AccountId>::default();
let expected_new_state: InflationDistributionConfig<T::AccountId> = [pbr, treasury].into();

ensure!(new_state == expected_new_state, "State migration failed");
}
}
29 changes: 29 additions & 0 deletions runtime/common/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@ where
}
}

pub struct MigrateStakingParachainBondConfig<Runtime>(PhantomData<Runtime>);
impl<Runtime> Migration for MigrateStakingParachainBondConfig<Runtime>
where
Runtime: pallet_parachain_staking::Config,
{
fn friendly_name(&self) -> &str {
"MM_MigrateStakingParachainBondConfig"
}

fn migrate(&self, _available_weight: Weight) -> Weight {
pallet_parachain_staking::migrations::MigrateParachainBondConfig::<Runtime>::on_runtime_upgrade()
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
pallet_parachain_staking::migrations::MigrateParachainBondConfig::<Runtime>::pre_upgrade()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(&self, state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
pallet_parachain_staking::migrations::MigrateParachainBondConfig::<Runtime>::post_upgrade(
state,
)
}
}

pub struct CommonMigrations<Runtime>(PhantomData<Runtime>);

impl<Runtime> GetMigrations for CommonMigrations<Runtime>
Expand Down Expand Up @@ -303,6 +329,9 @@ where
Box::new(MigrateXcmFeesAssetsMeatdata::<Runtime>(Default::default())),
// permanent migrations
Box::new(MigrateToLatestXcmVersion::<Runtime>(Default::default())),
Box::new(MigrateStakingParachainBondConfig::<Runtime>(
Default::default(),
)),
]
}
}

0 comments on commit f95bd72

Please sign in to comment.