Skip to content

Commit

Permalink
Merge pull request #182 from Abstracted-Labs/francisco-fix_pallets_fu…
Browse files Browse the repository at this point in the history
…ngible_migration

Fix: Migration locks to fungible freeze
  • Loading branch information
arrudagates authored Dec 30, 2024
2 parents 264a2bd + 942ef6b commit e2d1e45
Show file tree
Hide file tree
Showing 10 changed files with 2,897 additions and 2,873 deletions.
5,507 changes: 2,687 additions & 2,820 deletions invarch/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion invarch/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ runtime-benchmarks = [
"polkadot-cli/runtime-benchmarks",
]

try-runtime = ["invarch-runtime/try-runtime", "try-runtime-cli"]
try-runtime = ["invarch-runtime/try-runtime", "try-runtime-cli", "sp-runtime/try-runtime"]
36 changes: 36 additions & 0 deletions invarch/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,23 @@ std = [
"cumulus-primitives-core/std",
"cumulus-primitives-timestamp/std",
"cumulus-primitives-utility/std",
"frame-benchmarking?/std",
"frame-executive/std",
"frame-metadata-hash-extension/std",
"frame-support/std",
"frame-system-benchmarking?/std",
"frame-system-rpc-runtime-api/std",
"frame-system/std",
"frame-try-runtime?/std",
"log/std",
"new-modified-construct-runtime/std",
"orml-asset-registry/std",
"orml-currencies/std",
"orml-tokens/std",
"orml-tokens2/std",
"orml-traits2/std",
"orml-vesting/std",
"orml-xcm-support/std",
"orml-xcm/std",
"orml-xtokens/std",
"pallet-aura/std",
Expand All @@ -142,6 +147,7 @@ std = [
"pallet-dao-staking/std",
"pallet-identity/std",
"pallet-insecure-randomness-collective-flip/std",
"pallet-message-queue/std",
"pallet-session/std",
"pallet-sudo/std",
"pallet-timestamp/std",
Expand All @@ -159,6 +165,7 @@ std = [
"sp-block-builder/std",
"sp-consensus-aura/std",
"sp-core/std",
"sp-genesis-builder/std",
"sp-inherents/std",
"sp-offchain/std",
"sp-runtime/std",
Expand All @@ -174,6 +181,7 @@ std = [
runtime-benchmarks = [
"cumulus-pallet-session-benchmarking/runtime-benchmarks",
"cumulus-pallet-xcmp-queue/runtime-benchmarks",
"cumulus-primitives-core/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system-benchmarking/runtime-benchmarks",
Expand All @@ -183,10 +191,18 @@ runtime-benchmarks = [
"pallet-collator-selection/runtime-benchmarks",
"pallet-dao-manager/runtime-benchmarks",
"pallet-dao-staking/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"polkadot-parachain/runtime-benchmarks",
"polkadot-runtime-common/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"xcm-executor/runtime-benchmarks",
]

try-runtime = [
Expand All @@ -196,16 +212,36 @@ try-runtime = [
"cumulus-pallet-xcm/try-runtime",
"cumulus-pallet-xcmp-queue/try-runtime",
"frame-executive/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame-try-runtime/try-runtime",
"orml-asset-registry/try-runtime",
"orml-currencies/try-runtime",
"orml-tokens/try-runtime",
"orml-tokens2/try-runtime",
"orml-vesting/try-runtime",
"orml-xcm/try-runtime",
"orml-xtokens/try-runtime",
"pallet-aura/try-runtime",
"pallet-authorship/try-runtime",
"pallet-balances/try-runtime",
"pallet-checked-inflation/try-runtime",
"pallet-collator-selection/try-runtime",
"pallet-contracts/try-runtime",
"pallet-dao-manager/try-runtime",
"pallet-dao-staking/try-runtime",
"pallet-identity/try-runtime",
"pallet-insecure-randomness-collective-flip/try-runtime",
"pallet-message-queue/try-runtime",
"pallet-session/try-runtime",
"pallet-sudo/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-transaction-payment/try-runtime",
"pallet-treasury/try-runtime",
"pallet-tx-pause/try-runtime",
"pallet-utility/try-runtime",
"pallet-xcm/try-runtime",
"parachain-info/try-runtime",
"polkadot-runtime-common/try-runtime",
"sp-runtime/try-runtime",
]
3 changes: 2 additions & 1 deletion invarch/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
pallet_dao_staking::migrations::v1::MigrateToV1<Runtime>,
>;

/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included into the
Expand Down Expand Up @@ -164,7 +165,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("invarch"),
impl_name: create_runtime_str!("invarch"),
authoring_version: 1,
spec_version: 10,
spec_version: 11,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
4 changes: 3 additions & 1 deletion pallets/pallet-checked-inflation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
try-runtime = ["frame-support/try-runtime", "sp-runtime/try-runtime"]
1 change: 1 addition & 0 deletions pallets/pallet-dao-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ std = [
"sp-runtime/std",
"sp-std/std",
"xcm/std",
"log/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
Expand Down
6 changes: 5 additions & 1 deletion pallets/pallet-dao-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { workspace = true }
log = { workspace = true }
scale-info = { workspace = true }
serde = { workspace = true, optional = true }

Expand Down Expand Up @@ -65,12 +66,15 @@ std = [
"sp-staking/std",
"sp-std/std",
"xcm/std",
"log/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-dao-manager/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
try-runtime = ["frame-support/try-runtime", "pallet-dao-manager/try-runtime", "sp-runtime/try-runtime"]
8 changes: 6 additions & 2 deletions pallets/pallet-dao-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ mod benchmarking;
mod testing;
pub mod weights;

pub mod migrations;

pub use weights::WeightInfo;

/// Staking lock identifier.
Expand Down Expand Up @@ -111,7 +113,10 @@ pub mod pallet {
type NegativeImbalanceOf<T> =
Credit<<T as frame_system::Config>::AccountId, <T as Config>::Currency>;

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(PhantomData<T>);

/// The dao metadata type of this pallet.
Expand Down Expand Up @@ -1163,10 +1168,9 @@ pub mod pallet {
) -> DispatchResult {
if ledger.is_empty() {
Ledger::<T>::remove(staker);
<T as pallet::Config>::OldCurrency::remove_lock(LOCK_ID, staker);
let _ = <T as pallet::Config>::Currency::thaw(&LOCK_ID, staker);
} else {
<T as pallet::Config>::Currency::extend_freeze(&LOCK_ID, staker, ledger.locked)?;
<T as pallet::Config>::Currency::set_freeze(&LOCK_ID, staker, ledger.locked)?;
Ledger::<T>::insert(staker, ledger);
}
Ok(())
Expand Down
113 changes: 113 additions & 0 deletions pallets/pallet-dao-staking/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use super::*;
use frame_support::{
pallet_prelude::GetStorageVersion,
traits::{Get, OnRuntimeUpgrade},
weights::Weight,
};
use log::{info, warn};

pub mod v1 {

use super::*;

/// This will check all the info on the ledger and remove the old lock while reapplying the new lock based on the
/// value of the ledger, so the wrogly locked tokens will be unlocked.
pub fn migrate_locks_to_freeze<T: Config>() -> Weight {
let mut weight = Weight::zero();
let mut count: u32 = 0;

Ledger::<T>::iter().for_each(|(account, ledger)| {
if ledger.locked > Zero::zero() {
<T as pallet::Config>::OldCurrency::remove_lock(LOCK_ID, &account);

let set_freeze_result =
<T as pallet::Config>::Currency::set_freeze(&LOCK_ID, &account, ledger.locked);

if set_freeze_result.is_err() {
warn!("set_freeze_result {:?}", set_freeze_result);
}

weight.saturating_accrue(T::DbWeight::get().reads_writes(3, 2));
count += 1;
}
});

info!("Migrated {} locks", count);
weight
}

/// This will just remove all the holds on the enabling staking on a dao to apply the new one.
pub fn migrate_holds<T: Config>() -> Weight {
let mut count: u32 = 0;
let mut weight = Weight::zero();

RegisteredCore::<T>::iter().for_each(|(_dao_id, dao_info)| {
let dao_account = dao_info.account;
let dao_reserved = <T as Config>::OldCurrency::reserved_balance(&dao_account);

<T as Config>::OldCurrency::unreserve(&dao_account, dao_reserved);

let set_on_hold_result = <T as Config>::Currency::set_on_hold(
&HoldReason::DaoStaking.into(),
&dao_account,
dao_reserved,
);

if set_on_hold_result.is_err() {
warn!("set_on_hold_result {:?}", set_on_hold_result);
}

count += 1;
weight.saturating_accrue(T::DbWeight::get().reads_writes(3, 2));
});

info!("Migrated {} daos", count);
weight
}

pub struct MigrateToV1<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_runtime::Vec<u8>, sp_runtime::DispatchError> {
frame_support::ensure!(
Pallet::<T>::on_chain_storage_version() <= Pallet::<T>::in_code_storage_version(),
"Required v0 before upgrading to v1"
);

Ok(Default::default())
}

fn on_runtime_upgrade() -> Weight {
let mut weight = Weight::zero();
let current = Pallet::<T>::in_code_storage_version();

let chain_version = Pallet::<T>::on_chain_storage_version();

weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 0));

if current > chain_version {
weight.saturating_accrue(migrate_locks_to_freeze::<T>());

weight.saturating_accrue(migrate_holds::<T>());

current.put::<Pallet<T>>();

info!("v1 applied successfully");
T::DbWeight::get().reads_writes(0, 1)
} else {
warn!("Skipping v1, should be removed");
T::DbWeight::get().reads(1)
}
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: sp_std::vec::Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
frame_support::ensure!(
Pallet::<T>::on_chain_storage_version() == 1,
"v1 not applied"
);

Ok(())
}
}
}
Loading

0 comments on commit e2d1e45

Please sign in to comment.