Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add freeze & unfreeze extrinsics #1947

Merged
merged 20 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
62 changes: 57 additions & 5 deletions libs/types/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ pub struct TrancheInvestorInfo<TrancheId> {
is_frozen: bool,
lemunozm marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Encode, Decode, TypeInfo, Debug, Clone, Eq, PartialEq, MaxEncodedLen)]
pub struct FrozenTrancheInvestorInfo<TrancheId> {
tranche_id: TrancheId,
}

#[derive(Encode, Decode, TypeInfo, Debug, Clone, Eq, PartialEq, MaxEncodedLen)]
pub struct PermissionedCurrencyHolders<Now, MinDelay> {
info: Option<PermissionedCurrencyHolderInfo>,
Expand Down Expand Up @@ -685,3 +680,60 @@ mod tests {
assert!(!roles.exists(Role::PoolRole(PoolRole::PODReadAccess)));
}
}

pub mod v0 {
use super::*;

#[derive(Encode, Decode, TypeInfo, Clone, Eq, PartialEq, Debug, MaxEncodedLen)]
pub struct PermissionRoles<Now, MinDelay, TrancheId, MaxTranches: Get<u32>> {
pool_admin: PoolAdminRoles,
currency_admin: CurrencyAdminRoles,
permissioned_asset_holder: PermissionedCurrencyHolders<Now, MinDelay>,
tranche_investor: TrancheInvestors<Now, MinDelay, TrancheId, MaxTranches>,
}

#[derive(Encode, Decode, TypeInfo, Debug, Clone, Eq, PartialEq, MaxEncodedLen)]
pub struct TrancheInvestors<Now, MinDelay, TrancheId, MaxTranches: Get<u32>> {
info: BoundedVec<TrancheInvestorInfo<TrancheId>, MaxTranches>,
_phantom: PhantomData<(Now, MinDelay)>,
}

#[derive(Encode, Decode, TypeInfo, Debug, Clone, Eq, PartialEq, MaxEncodedLen)]
pub struct TrancheInvestorInfo<TrancheId> {
tranche_id: TrancheId,
permissioned_till: Seconds,
}

impl<Now, MinDelay, TrancheId, MaxTranches: Get<u32>>
TrancheInvestors<Now, MinDelay, TrancheId, MaxTranches>
{
fn migrate(self) -> super::TrancheInvestors<Now, MinDelay, TrancheId, MaxTranches> {
super::TrancheInvestors::<Now, MinDelay, TrancheId, MaxTranches> {
info: BoundedVec::truncate_from(
self.info
.into_iter()
.map(|info| super::TrancheInvestorInfo {
tranche_id: info.tranche_id,
permissioned_till: info.permissioned_till,
is_frozen: false,
})
.collect(),
),
_phantom: self._phantom,
}
}
}

impl<Now, MinDelay, TrancheId: Clone, MaxTranches: Get<u32>>
PermissionRoles<Now, MinDelay, TrancheId, MaxTranches>
{
pub fn migrate(self) -> super::PermissionRoles<Now, MinDelay, TrancheId, MaxTranches> {
super::PermissionRoles {
pool_admin: self.pool_admin,
currency_admin: self.currency_admin,
permissioned_asset_holder: self.permissioned_asset_holder,
tranche_investor: self.tranche_investor.migrate(),
}
}
}
}
3 changes: 3 additions & 0 deletions pallets/permissions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ pub mod pallet {
type WeightInfo: WeightInfo;
}

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::storage]
Expand Down
18 changes: 18 additions & 0 deletions runtime/altair/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,22 @@ pub type UpgradeAltair1401 = (
1,
2,
>,
// Migrate TrancheInvestor permission role and storage version from v0 to v1
frame_support::migrations::VersionedMigration<
0,
1,
frame_support::migrations::VersionedMigration<
0,
1,
runtime_common::migrations::permissions_v1::Migration<
Runtime,
crate::MinDelay,
crate::MaxTranches,
>,
pallet_permissions::Pallet<Runtime>,
<Runtime as frame_system::Config>::DbWeight,
>,
pallet_permissions::Pallet<Runtime>,
<Runtime as frame_system::Config>::DbWeight,
>,
wischli marked this conversation as resolved.
Show resolved Hide resolved
);
12 changes: 12 additions & 0 deletions runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ pub type UpgradeCentrifuge1401 = (
1,
2,
>,
// Migrate TrancheInvestor permission role and storage version from v0 to v1
frame_support::migrations::VersionedMigration<
0,
1,
runtime_common::migrations::permissions_v1::Migration<
Runtime,
crate::MinDelay,
crate::MaxTranches,
>,
pallet_permissions::Pallet<Runtime>,
<Runtime as frame_system::Config>::DbWeight,
>,
);
1 change: 1 addition & 0 deletions runtime/common/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod foreign_investments_v2;
pub mod increase_storage_version;
pub mod liquidity_pools_gateway;
pub mod nuke;
pub mod permissions_v1;
pub mod precompile_account_codes;
pub mod restricted_location;
pub mod technical_comittee;
Expand Down
112 changes: 112 additions & 0 deletions runtime/common/src/migrations/permissions_v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2024 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_primitives::{PoolId, TrancheId};
use cfg_types::{
permissions::{v0::PermissionRoles as PermissionRolesV0, PermissionRoles, PermissionScope},
time::TimeProvider,
tokens::CurrencyId,
};
#[cfg(feature = "try-runtime")]
use frame_support::pallet_prelude::{Decode, Encode};
use frame_support::{
storage_alias,
traits::{Get, OnRuntimeUpgrade},
weights::Weight,
Blake2_128Concat,
};
use sp_arithmetic::traits::SaturatedConversion;
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;

mod v0 {
use super::*;

#[storage_alias]
pub type Permission<T: pallet_permissions::Config + frame_system::Config, MinD, MaxT> =
StorageDoubleMap<
pallet_permissions::Pallet<T>,
Blake2_128Concat,
<T as frame_system::Config>::AccountId,
Blake2_128Concat,
PermissionScope<PoolId, CurrencyId>,
PermissionRolesV0<TimeProvider<pallet_timestamp::Pallet<T>>, MinD, TrancheId, MaxT>,
>;
}

const LOG_PREFIX: &str = "PermssionsV1";

pub struct Migration<T, MinDelay, MaxTranches>(
sp_std::marker::PhantomData<(T, MinDelay, MaxTranches)>,
);

impl<T, MinDelay, MaxTranches> OnRuntimeUpgrade for Migration<T, MinDelay, MaxTranches>
where
T: pallet_permissions::Config<
Storage = PermissionRoles<
TimeProvider<pallet_timestamp::Pallet<T>>,
MinDelay,
TrancheId,
MaxTranches,
>,
> + frame_system::Config
+ pallet_timestamp::Config,
MinDelay: 'static,
MaxTranches: Get<u32> + 'static,
{
fn on_runtime_upgrade() -> Weight {
let writes = v0::Permission::<T, MinDelay, MaxTranches>::iter_keys()
.count()
.saturated_into();

pallet_permissions::Permission::<T>::translate_values::<
PermissionRolesV0<
TimeProvider<pallet_timestamp::Pallet<T>>,
MinDelay,
TrancheId,
MaxTranches,
>,
_,
>(|role| Some(role.migrate().into()));

log::info!("{LOG_PREFIX}: Migrated {writes} permissions!");
T::DbWeight::get().reads_writes(1, writes)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
let count: u64 = v0::Permission::<T, MinDelay, MaxTranches>::iter_keys()
.count()
.saturated_into();

log::info!("{LOG_PREFIX}: Pre checks done!");

Ok(count.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(pre_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
let pre_count: u64 = Decode::decode(&mut pre_state.as_slice())
.expect("pre_upgrade provides a valid state; qed");
let post_count: u64 = pallet_permissions::Permission::<T>::iter_keys()
.count()
.saturated_into();
assert_eq!(
pre_count, post_count,
"{LOG_PREFIX}: Mismatching number of permission roles after migration!"
);

log::info!("{LOG_PREFIX}: Post checks done!");

Ok(())
}
}
18 changes: 18 additions & 0 deletions runtime/development/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,22 @@ pub type UpgradeDevelopment1401 = (
1,
2,
>,
// Migrate TrancheInvestor permission role and storage version from v0 to v1
frame_support::migrations::VersionedMigration<
0,
1,
frame_support::migrations::VersionedMigration<
wischli marked this conversation as resolved.
Show resolved Hide resolved
0,
1,
runtime_common::migrations::permissions_v1::Migration<
Runtime,
crate::MinDelay,
crate::MaxTranches,
>,
pallet_permissions::Pallet<Runtime>,
<Runtime as frame_system::Config>::DbWeight,
>,
pallet_permissions::Pallet<Runtime>,
<Runtime as frame_system::Config>::DbWeight,
>,
);