From f9e3e0d3a0732dc5210e5c46053b99ad05cfcb94 Mon Sep 17 00:00:00 2001 From: Ankan Date: Tue, 3 Oct 2023 15:24:19 +0200 Subject: [PATCH 01/11] trait bound balance --- .../runtime/common/src/assigned_slots/mod.rs | 5 +- polkadot/runtime/common/src/auctions.rs | 8 ++- polkadot/runtime/common/src/slots/mod.rs | 70 +++++++++++++------ polkadot/runtime/common/src/traits.rs | 14 ++-- 4 files changed, 67 insertions(+), 30 deletions(-) diff --git a/polkadot/runtime/common/src/assigned_slots/mod.rs b/polkadot/runtime/common/src/assigned_slots/mod.rs index cc8ec339c1184..a02a6029cce96 100644 --- a/polkadot/runtime/common/src/assigned_slots/mod.rs +++ b/polkadot/runtime/common/src/assigned_slots/mod.rs @@ -30,7 +30,7 @@ use crate::{ slots::{self, Pallet as Slots, WeightInfo as SlotsWeightInfo}, traits::{LeaseError, Leaser, Registrar}, }; -use frame_support::{pallet_prelude::*, traits::Currency}; +use frame_support::{pallet_prelude::*, traits::{Currency, fungible::Inspect as FunInspect}}; use frame_system::pallet_prelude::*; pub use pallet::*; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; @@ -98,9 +98,10 @@ impl WeightInfo for TestWeightInfo { } } -type BalanceOf = <<::Leaser as Leaser>>::Currency as Currency< +type BalanceOf = <<::Leaser as Leaser>>::Currency as FunInspect< ::AccountId, >>::Balance; + type LeasePeriodOf = <::Leaser as Leaser>>::LeasePeriod; #[frame_support::pallet] diff --git a/polkadot/runtime/common/src/auctions.rs b/polkadot/runtime/common/src/auctions.rs index e35303912fa1d..5d5b7bef19df9 100644 --- a/polkadot/runtime/common/src/auctions.rs +++ b/polkadot/runtime/common/src/auctions.rs @@ -25,7 +25,7 @@ use crate::{ use frame_support::{ dispatch::DispatchResult, ensure, - traits::{Currency, Get, Randomness, ReservableCurrency}, + traits::{{Currency, Get, Randomness, ReservableCurrency}, fungible::Inspect as FunInspect}, weights::Weight, }; use frame_system::pallet_prelude::BlockNumberFor; @@ -35,8 +35,8 @@ use primitives::Id as ParaId; use sp_runtime::traits::{CheckedSub, One, Saturating, Zero}; use sp_std::{mem::swap, prelude::*}; -type CurrencyOf = <::Leaser as Leaser>>::Currency; -type BalanceOf = <<::Leaser as Leaser>>::Currency as Currency< +type CurrencyOf = ::Currency; +type BalanceOf = <<::Leaser as Leaser>>::Currency as FunInspect< ::AccountId, >>::Balance; @@ -98,6 +98,8 @@ pub mod pallet { LeasePeriod = BlockNumberFor, >; + type Currency: ReservableCurrency>>::Currency as FunInspect>::Balance>; + /// The parachain registrar type. type Registrar: Registrar; diff --git a/polkadot/runtime/common/src/slots/mod.rs b/polkadot/runtime/common/src/slots/mod.rs index a3efd5bfa30a6..c6276fcfe1b27 100644 --- a/polkadot/runtime/common/src/slots/mod.rs +++ b/polkadot/runtime/common/src/slots/mod.rs @@ -26,8 +26,12 @@ pub mod migration; use crate::traits::{LeaseError, Leaser, Registrar}; use frame_support::{ + defensive_assert, pallet_prelude::*, - traits::{Currency, ReservableCurrency}, + traits::fungible::{ + hold::{Inspect as FunHoldInspect, Mutate as FunHoldMutate}, + Inspect as FunInspect, Mutate as FunMutate, + }, weights::Weight, }; use frame_system::pallet_prelude::*; @@ -37,7 +41,8 @@ use sp_runtime::traits::{CheckedConversion, CheckedSub, Saturating, Zero}; use sp_std::prelude::*; type BalanceOf = - <::Currency as Currency<::AccountId>>::Balance; + <::Currency as FunInspect<::AccountId>>::Balance; + type LeasePeriodOf = BlockNumberFor; pub trait WeightInfo { @@ -77,7 +82,10 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The currency type used for bidding. - type Currency: ReservableCurrency; + type Currency: FunHoldMutate; + + /// The hold reason when reserving funds for the lease. + type RuntimeHoldReason: From; /// The parachain registrar type. type Registrar: Registrar; @@ -144,6 +152,14 @@ pub mod pallet { LeaseError, } + /// A reason for the pallet placing a hold on funds. + #[pallet::composite_enum] + pub enum HoldReason { + /// Funds are held for the current or upcoming leases. + #[codec(index = 0)] + LeaseDeposit, + } + #[pallet::hooks] impl Hooks> for Pallet { fn on_initialize(n: BlockNumberFor) -> Weight { @@ -192,8 +208,7 @@ pub mod pallet { // Refund any deposits for these leases for (who, deposit) in deposits { - let err_amount = T::Currency::unreserve(&who, deposit); - debug_assert!(err_amount.is_zero()); + Self::release(&who, deposit); } Leases::::remove(para); @@ -247,9 +262,9 @@ impl Pallet { // // `para` is now just an on-demand parachain. // - // Unreserve whatever is left. + // Release whatever is left. if let Some((who, value)) = &lease_periods[0] { - T::Currency::unreserve(&who, *value); + Self::release(&who, *value); } // Remove the now-empty lease list. @@ -270,9 +285,9 @@ impl Pallet { let now_held = Self::deposit_held(para, &ended_lease.0); // If this is less than what we were holding for this leaser's now-ended lease, - // then unreserve it. + // then release it. if let Some(rebate) = ended_lease.1.checked_sub(&now_held) { - T::Currency::unreserve(&ended_lease.0, rebate); + Self::release(&ended_lease.0, rebate); } } @@ -308,7 +323,7 @@ impl Pallet { // Return a vector of (user, balance) for all deposits for a parachain. // Useful when trying to clean up a parachain leases, as this would tell - // you all the balances you need to unreserve. + // you all the balances you need to release. fn all_deposits_held(para: ParaId) -> Vec<(T::AccountId, BalanceOf)> { let mut tracker = sp_std::collections::btree_map::BTreeMap::new(); Leases::::get(para).into_iter().for_each(|lease| match lease { @@ -326,6 +341,21 @@ impl Pallet { tracker.into_iter().collect() } + + fn hold(leaser: &T::AccountId, amount: BalanceOf) -> DispatchResult { + T::Currency::hold(&HoldReason::LeaseDeposit.into(), leaser, amount) + } + + fn release(leaser: &T::AccountId, amount: BalanceOf) { + let amount = T::Currency::release( + &HoldReason::LeaseDeposit.into(), + &leaser, + amount, + frame_support::traits::tokens::Precision::BestEffort, + ); + + defensive_assert!(amount.is_ok() && amount.unwrap() == amount); + } } impl crate::traits::OnSwap for Pallet { @@ -342,7 +372,7 @@ impl Leaser> for Pallet { fn lease_out( para: ParaId, leaser: &Self::AccountId, - amount: >::Balance, + amount: BalanceOf, period_begin: Self::LeasePeriod, period_count: Self::LeasePeriod, ) -> Result<(), LeaseError> { @@ -397,8 +427,7 @@ impl Leaser> for Pallet { // `para_id`. If so, then we can deduct those from the amount that we need to reserve. let maybe_additional = amount.checked_sub(&Self::deposit_held(para, &leaser)); if let Some(ref additional) = maybe_additional { - T::Currency::reserve(&leaser, *additional) - .map_err(|_| LeaseError::ReserveFailed)?; + Self::hold(&leaser, *additional).map_err(|_| LeaseError::ReserveFailed)?; } let reserved = maybe_additional.unwrap_or_default(); @@ -424,10 +453,7 @@ impl Leaser> for Pallet { }) } - fn deposit_held( - para: ParaId, - leaser: &Self::AccountId, - ) -> >::Balance { + fn deposit_held(para: ParaId, leaser: &Self::AccountId) -> BalanceOf { Leases::::get(para) .into_iter() .map(|lease| match lease { @@ -584,6 +610,7 @@ mod tests { impl Config for Test { type RuntimeEvent = RuntimeEvent; type Currency = Balances; + type RuntimeHoldReason = RuntimeHoldReason; type Registrar = TestRegistrar; type LeasePeriod = LeasePeriod; type LeaseOffset = LeaseOffset; @@ -1008,7 +1035,7 @@ mod benchmarking { fn register_a_parathread(i: u32) -> (ParaId, T::AccountId) { let para = ParaId::from(i); let leaser: T::AccountId = account("leaser", i, 0); - T::Currency::make_free_balance_be(&leaser, BalanceOf::::max_value()); + T::Currency::set_balance(&leaser, BalanceOf::::max_value()); let worst_head_data = T::Registrar::worst_head_data(); let worst_validation_code = T::Registrar::worst_validation_code(); @@ -1036,7 +1063,7 @@ mod benchmarking { frame_system::Pallet::::set_block_number(T::LeaseOffset::get() + One::one()); let para = ParaId::from(1337); let leaser: T::AccountId = account("leaser", 0, 0); - T::Currency::make_free_balance_be(&leaser, BalanceOf::::max_value()); + T::Currency::set_balance(&leaser, BalanceOf::::max_value()); let amount = T::Currency::minimum_balance(); let period_begin = 69u32.into(); let period_count = 3u32.into(); @@ -1122,7 +1149,7 @@ mod benchmarking { for i in 0 .. max_people { let leaser = account("lease_deposit", i, 0); let amount = T::Currency::minimum_balance(); - T::Currency::make_free_balance_be(&leaser, BalanceOf::::max_value()); + T::Currency::set_balance(&leaser, BalanceOf::::max_value()); // Average slot has 4 lease periods. let period_count: LeasePeriodOf = 4u32.into(); @@ -1134,7 +1161,7 @@ mod benchmarking { for i in 0 .. max_people { let leaser = account("lease_deposit", i, 0); - assert_eq!(T::Currency::reserved_balance(&leaser), T::Currency::minimum_balance()); + assert_eq!(T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into, &leaser), T::Currency::minimum_balance()); } let origin = @@ -1143,6 +1170,7 @@ mod benchmarking { verify { for i in 0 .. max_people { let leaser = account("lease_deposit", i, 0); + assert_eq!(T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into, &leaser), 0u32.into()); assert_eq!(T::Currency::reserved_balance(&leaser), 0u32.into()); } } diff --git a/polkadot/runtime/common/src/traits.rs b/polkadot/runtime/common/src/traits.rs index 8f75bf5c2fd83..8fa23fcdf53c1 100644 --- a/polkadot/runtime/common/src/traits.rs +++ b/polkadot/runtime/common/src/traits.rs @@ -18,7 +18,13 @@ use frame_support::{ dispatch::DispatchResult, - traits::{Currency, ReservableCurrency}, + traits::{ + fungible::{ + hold::{Inspect as FunHoldInspect, Mutate as FunHoldMutate}, + Inspect as FunInspect, Mutate as FunMutate, + }, + Currency, ReservableCurrency, + }, }; use primitives::{HeadData, Id as ParaId, ValidationCode}; use sp_std::vec::*; @@ -109,7 +115,7 @@ pub trait Leaser { type LeasePeriod; /// The currency type in which the lease is taken. - type Currency: ReservableCurrency; + type Currency: FunHoldMutate; /// Lease a new parachain slot for `para`. /// @@ -126,7 +132,7 @@ pub trait Leaser { fn lease_out( para: ParaId, leaser: &Self::AccountId, - amount: >::Balance, + amount: >::Balance, period_begin: Self::LeasePeriod, period_count: Self::LeasePeriod, ) -> Result<(), LeaseError>; @@ -136,7 +142,7 @@ pub trait Leaser { fn deposit_held( para: ParaId, leaser: &Self::AccountId, - ) -> >::Balance; + ) -> >::Balance; /// The length of a lease period, and any offset which may be introduced. /// This is only used in benchmarking to automate certain calls. From 624c3f4aa9106989c9e6cd6daf2817a301fcb907 Mon Sep 17 00:00:00 2001 From: Ankan Date: Wed, 4 Oct 2023 04:39:41 +0200 Subject: [PATCH 02/11] everything compiles --- polkadot/runtime/common/src/assigned_slots/mod.rs | 3 ++- polkadot/runtime/common/src/auctions.rs | 1 + polkadot/runtime/common/src/integration_tests.rs | 4 +++- polkadot/runtime/common/src/slots/mod.rs | 6 +++--- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/polkadot/runtime/common/src/assigned_slots/mod.rs b/polkadot/runtime/common/src/assigned_slots/mod.rs index a02a6029cce96..bd1bdda1afde6 100644 --- a/polkadot/runtime/common/src/assigned_slots/mod.rs +++ b/polkadot/runtime/common/src/assigned_slots/mod.rs @@ -664,7 +664,7 @@ mod tests { Configuration: parachains_configuration::{Pallet, Call, Storage, Config}, ParasShared: parachains_shared::{Pallet, Call, Storage}, Parachains: parachains_paras::{Pallet, Call, Storage, Config, Event}, - Slots: slots::{Pallet, Call, Storage, Event}, + Slots: slots::{Pallet, Call, Storage, Event, HoldReason}, AssignedSlots: assigned_slots::{Pallet, Call, Storage, Event}, } ); @@ -754,6 +754,7 @@ mod tests { impl slots::Config for Test { type RuntimeEvent = RuntimeEvent; type Currency = Balances; + type RuntimeHoldReason = RuntimeHoldReason; type Registrar = TestRegistrar; type LeasePeriod = LeasePeriod; type LeaseOffset = LeaseOffset; diff --git a/polkadot/runtime/common/src/auctions.rs b/polkadot/runtime/common/src/auctions.rs index 5d5b7bef19df9..2a937c7322e21 100644 --- a/polkadot/runtime/common/src/auctions.rs +++ b/polkadot/runtime/common/src/auctions.rs @@ -875,6 +875,7 @@ mod tests { impl Config for Test { type RuntimeEvent = RuntimeEvent; type Leaser = TestLeaser; + type Currency = Balances; type Registrar = TestRegistrar; type EndingPeriod = EndingPeriod; type SampleLength = SampleLength; diff --git a/polkadot/runtime/common/src/integration_tests.rs b/polkadot/runtime/common/src/integration_tests.rs index f14db68267d5b..3560f3481a01d 100644 --- a/polkadot/runtime/common/src/integration_tests.rs +++ b/polkadot/runtime/common/src/integration_tests.rs @@ -87,7 +87,7 @@ frame_support::construct_runtime!( Registrar: paras_registrar::{Pallet, Call, Storage, Event}, Auctions: auctions::{Pallet, Call, Storage, Event}, Crowdloan: crowdloan::{Pallet, Call, Storage, Event}, - Slots: slots::{Pallet, Call, Storage, Event}, + Slots: slots::{Pallet, Call, Storage, Event, HoldReason}, } ); @@ -230,6 +230,7 @@ parameter_types! { impl auctions::Config for Test { type RuntimeEvent = RuntimeEvent; type Leaser = Slots; + type Currency = Balances; type Registrar = Registrar; type EndingPeriod = EndingPeriod; type SampleLength = SampleLength; @@ -246,6 +247,7 @@ parameter_types! { impl slots::Config for Test { type RuntimeEvent = RuntimeEvent; type Currency = Balances; + type RuntimeHoldReason = RuntimeHoldReason; type Registrar = Registrar; type LeasePeriod = LeasePeriod; type LeaseOffset = LeaseOffset; diff --git a/polkadot/runtime/common/src/slots/mod.rs b/polkadot/runtime/common/src/slots/mod.rs index c6276fcfe1b27..09b67c1239ac8 100644 --- a/polkadot/runtime/common/src/slots/mod.rs +++ b/polkadot/runtime/common/src/slots/mod.rs @@ -347,14 +347,14 @@ impl Pallet { } fn release(leaser: &T::AccountId, amount: BalanceOf) { - let amount = T::Currency::release( + let released_amount = T::Currency::release( &HoldReason::LeaseDeposit.into(), &leaser, amount, frame_support::traits::tokens::Precision::BestEffort, ); - defensive_assert!(amount.is_ok() && amount.unwrap() == amount); + defensive_assert!(released_amount.is_ok() && released_amount.unwrap() == amount); } } @@ -548,7 +548,7 @@ mod tests { { System: frame_system::{Pallet, Call, Config, Storage, Event}, Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, - Slots: slots::{Pallet, Call, Storage, Event}, + Slots: slots::{Pallet, Call, Storage, Event, HoldReason}, } ); From f812bf272141fad6adcedcf9c49a289bf7601edf Mon Sep 17 00:00:00 2001 From: Ankan Date: Wed, 4 Oct 2023 05:27:01 +0200 Subject: [PATCH 03/11] all test pass --- .../runtime/common/src/assigned_slots/mod.rs | 11 ++++++----- polkadot/runtime/common/src/auctions.rs | 16 +++++++++++----- polkadot/runtime/common/src/integration_tests.rs | 2 +- polkadot/runtime/common/src/slots/mod.rs | 5 +---- polkadot/runtime/common/src/traits.rs | 5 +---- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/polkadot/runtime/common/src/assigned_slots/mod.rs b/polkadot/runtime/common/src/assigned_slots/mod.rs index bd1bdda1afde6..f7c2889809e24 100644 --- a/polkadot/runtime/common/src/assigned_slots/mod.rs +++ b/polkadot/runtime/common/src/assigned_slots/mod.rs @@ -30,7 +30,7 @@ use crate::{ slots::{self, Pallet as Slots, WeightInfo as SlotsWeightInfo}, traits::{LeaseError, Leaser, Registrar}, }; -use frame_support::{pallet_prelude::*, traits::{Currency, fungible::Inspect as FunInspect}}; +use frame_support::{pallet_prelude::*, traits::fungible::Inspect as FunInspect}; use frame_system::pallet_prelude::*; pub use pallet::*; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; @@ -98,9 +98,10 @@ impl WeightInfo for TestWeightInfo { } } -type BalanceOf = <<::Leaser as Leaser>>::Currency as FunInspect< - ::AccountId, ->>::Balance; +type BalanceOf = + <<::Leaser as Leaser>>::Currency as FunInspect< + ::AccountId, + >>::Balance; type LeasePeriodOf = <::Leaser as Leaser>>::LeasePeriod; @@ -783,7 +784,7 @@ mod tests { pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); pallet_balances::GenesisConfig:: { - balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)], + balances: vec![(0, 5), (1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)], } .assimilate_storage(&mut t) .unwrap(); diff --git a/polkadot/runtime/common/src/auctions.rs b/polkadot/runtime/common/src/auctions.rs index 2a937c7322e21..4ec6dcfb854e3 100644 --- a/polkadot/runtime/common/src/auctions.rs +++ b/polkadot/runtime/common/src/auctions.rs @@ -25,7 +25,7 @@ use crate::{ use frame_support::{ dispatch::DispatchResult, ensure, - traits::{{Currency, Get, Randomness, ReservableCurrency}, fungible::Inspect as FunInspect}, + traits::{fungible::Inspect as FunInspect, Get, Randomness, ReservableCurrency}, weights::Weight, }; use frame_system::pallet_prelude::BlockNumberFor; @@ -36,9 +36,10 @@ use sp_runtime::traits::{CheckedSub, One, Saturating, Zero}; use sp_std::{mem::swap, prelude::*}; type CurrencyOf = ::Currency; -type BalanceOf = <<::Leaser as Leaser>>::Currency as FunInspect< - ::AccountId, ->>::Balance; +type BalanceOf = + <<::Leaser as Leaser>>::Currency as FunInspect< + ::AccountId, + >>::Balance; pub trait WeightInfo { fn new_auction() -> Weight; @@ -98,7 +99,12 @@ pub mod pallet { LeasePeriod = BlockNumberFor, >; - type Currency: ReservableCurrency>>::Currency as FunInspect>::Balance>; + type Currency: ReservableCurrency< + Self::AccountId, + Balance = <>>::Currency as FunInspect< + Self::AccountId, + >>::Balance, + >; /// The parachain registrar type. type Registrar: Registrar; diff --git a/polkadot/runtime/common/src/integration_tests.rs b/polkadot/runtime/common/src/integration_tests.rs index 3560f3481a01d..87d595a386ff4 100644 --- a/polkadot/runtime/common/src/integration_tests.rs +++ b/polkadot/runtime/common/src/integration_tests.rs @@ -182,7 +182,7 @@ impl pallet_balances::Config for Test { type ReserveIdentifier = [u8; 8]; type RuntimeHoldReason = RuntimeHoldReason; type FreezeIdentifier = (); - type MaxHolds = ConstU32<0>; + type MaxHolds = ConstU32<1>; type MaxFreezes = ConstU32<0>; } diff --git a/polkadot/runtime/common/src/slots/mod.rs b/polkadot/runtime/common/src/slots/mod.rs index 09b67c1239ac8..0e8f62464d5d5 100644 --- a/polkadot/runtime/common/src/slots/mod.rs +++ b/polkadot/runtime/common/src/slots/mod.rs @@ -28,10 +28,7 @@ use crate::traits::{LeaseError, Leaser, Registrar}; use frame_support::{ defensive_assert, pallet_prelude::*, - traits::fungible::{ - hold::{Inspect as FunHoldInspect, Mutate as FunHoldMutate}, - Inspect as FunInspect, Mutate as FunMutate, - }, + traits::fungible::{hold::Mutate as FunHoldMutate, Inspect as FunInspect}, weights::Weight, }; use frame_system::pallet_prelude::*; diff --git a/polkadot/runtime/common/src/traits.rs b/polkadot/runtime/common/src/traits.rs index 8fa23fcdf53c1..68256d7234a62 100644 --- a/polkadot/runtime/common/src/traits.rs +++ b/polkadot/runtime/common/src/traits.rs @@ -19,10 +19,7 @@ use frame_support::{ dispatch::DispatchResult, traits::{ - fungible::{ - hold::{Inspect as FunHoldInspect, Mutate as FunHoldMutate}, - Inspect as FunInspect, Mutate as FunMutate, - }, + fungible::{hold::Mutate as FunHoldMutate, Inspect as FunInspect}, Currency, ReservableCurrency, }, }; From b5dc2051c96774ef4d5038f47c3646373ebc03e1 Mon Sep 17 00:00:00 2001 From: Ankan Date: Wed, 4 Oct 2023 15:33:08 +0200 Subject: [PATCH 04/11] add runtime upgrade --- .../runtime/common/src/slots/migration.rs | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/polkadot/runtime/common/src/slots/migration.rs b/polkadot/runtime/common/src/slots/migration.rs index 4b499ca7c7bd8..5bb876f2c64ba 100644 --- a/polkadot/runtime/common/src/slots/migration.rs +++ b/polkadot/runtime/common/src/slots/migration.rs @@ -16,8 +16,115 @@ use super::*; use crate::crowdloan; +use frame_support::traits::OnRuntimeUpgrade; use sp_runtime::traits::AccountIdConversion; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; + +pub mod versioned { + use super::*; + + /// Wrapper over `MigrateToV1` with convenience version checks. + /// + /// This migration would move reserves into Named holds. + pub type ToV1 = frame_support::migrations::VersionedMigration< + 0, + 1, + v1::MigrateToV1, + Pallet, + ::DbWeight, + >; +} + +mod v1 { + use super::*; + use frame_support::traits::ReservableCurrency; + + pub type OldBalanceOf = ::AccountId, + >>::Balance; + + #[frame_support::storage_alias] + pub type Leases = + StorageMap, Twox64Concat, ParaId, Vec::AccountId, OldBalanceOf)>>, ValueQuery>; + + /// This migration would move funds for lease from reserved to hold. + pub struct MigrateToV1( + sp_std::marker::PhantomData<(T, OldCurrency)>, + ); + + impl OnRuntimeUpgrade for MigrateToV1 + where + T: Config, + OldCurrency: ReservableCurrency<::AccountId>, + BalanceOf: From, + { + fn on_runtime_upgrade() -> Weight { + for (_, lease_periods) in Leases::::iter() { + let mut max_deposits: sp_std::collections::btree_map::BTreeMap< + T::AccountId, + OldBalanceOf, + > = sp_std::collections::btree_map::BTreeMap::new(); + + lease_periods.iter().for_each(|lease| { + if let Some((who, amount)) = lease { + max_deposits + .entry(who.clone()) + .and_modify(|deposit| *deposit = *amount.max(deposit)) + .or_insert(*amount); + } + }); + + max_deposits.iter().for_each(|(leaser, deposit)| { + OldCurrency::unreserve(leaser, *deposit); + let hold_result = Pallet::::hold(leaser, BalanceOf::::from(*deposit)); + defensive_assert!( + hold_result.is_ok(), + "hold should not fail, since we just unreserved the same amount; qed" + ); + }) + } + + // weight: all active lease periods * rw + todo!("weights") + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_data: Vec) -> Result<(), TryRuntimeError> { + // let mut para_leasers = sp_std::collections::btree_set::BTreeSet::<(ParaId, + // T::AccountId)>::new(); for (para, lease_periods) in Leases::::iter() { + // lease_periods.into_iter().for_each(|maybe_lease| { + // if let Some((who, _)) = maybe_lease { + // para_leasers.insert((para, who)); + // } + // }); + // } + // + // // for each pair assert ReservedAmount is what we expect + // para_leasers.iter().try_for_each(|(para, who)| -> Result<(), TryRuntimeError> { + // let migrated_entry = ReservedAmounts::::get(para, who) + // .expect("Migration should have inserted this entry"); + // let expected = Pallet::::required_deposit(*para, who); + // let reserved_balance = T::Currency::reserved_balance(who); + // + // ensure!( + // migrated_entry == expected, + // "ReservedAmount value not same as required deposit" + // ); + // // fixme(ank4n) if there is another reserve on the account, this might be possible. + // ensure!( + // migrated_entry == reserved_balance, + // "ReservedAmount value not same as actual reserved balance" + // ); + // + // Ok(()) + // }) + todo!() + } + } +} + /// Migrations for using fund index to create fund accounts instead of para ID. pub mod slots_crowdloan_index_migration { use super::*; From 710e5f9bf0acb66c5098f3674ad91b56aaec52d5 Mon Sep 17 00:00:00 2001 From: Ankan Date: Wed, 4 Oct 2023 15:33:32 +0200 Subject: [PATCH 05/11] doc comments --- polkadot/runtime/common/src/slots/migration.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/polkadot/runtime/common/src/slots/migration.rs b/polkadot/runtime/common/src/slots/migration.rs index 5bb876f2c64ba..5c1a7327dbcc0 100644 --- a/polkadot/runtime/common/src/slots/migration.rs +++ b/polkadot/runtime/common/src/slots/migration.rs @@ -41,10 +41,12 @@ mod v1 { use super::*; use frame_support::traits::ReservableCurrency; + /// Balance type of OldCurrency. pub type OldBalanceOf = ::AccountId, >>::Balance; + /// Alias to leases storage map with old currency. #[frame_support::storage_alias] pub type Leases = StorageMap, Twox64Concat, ParaId, Vec::AccountId, OldBalanceOf)>>, ValueQuery>; From a7eff680b947710a89ad0fc28d324173dd72fb88 Mon Sep 17 00:00:00 2001 From: Ankan Date: Wed, 4 Oct 2023 15:51:38 +0200 Subject: [PATCH 06/11] add migration for reserve to fungibles --- polkadot/runtime/common/src/auctions.rs | 2 +- .../runtime/common/src/slots/migration.rs | 88 ++++++++++--------- 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/polkadot/runtime/common/src/auctions.rs b/polkadot/runtime/common/src/auctions.rs index 4ec6dcfb854e3..2e62bb791ac7c 100644 --- a/polkadot/runtime/common/src/auctions.rs +++ b/polkadot/runtime/common/src/auctions.rs @@ -686,7 +686,7 @@ mod tests { use ::test_helpers::{dummy_hash, dummy_head_data, dummy_validation_code}; use frame_support::{ assert_noop, assert_ok, assert_storage_noop, ord_parameter_types, parameter_types, - traits::{ConstU32, EitherOfDiverse, OnFinalize, OnInitialize}, + traits::{ConstU32, EitherOfDiverse, OnFinalize, OnInitialize, Currency}, }; use frame_system::{EnsureRoot, EnsureSignedBy}; use pallet_balances; diff --git a/polkadot/runtime/common/src/slots/migration.rs b/polkadot/runtime/common/src/slots/migration.rs index 5c1a7327dbcc0..55f213c7918ff 100644 --- a/polkadot/runtime/common/src/slots/migration.rs +++ b/polkadot/runtime/common/src/slots/migration.rs @@ -40,6 +40,7 @@ pub mod versioned { mod v1 { use super::*; use frame_support::traits::ReservableCurrency; + use sp_std::collections::btree_map::BTreeMap; /// Balance type of OldCurrency. pub type OldBalanceOf = = - StorageMap, Twox64Concat, ParaId, Vec::AccountId, OldBalanceOf)>>, ValueQuery>; + pub type OldLeases = StorageMap< + Pallet, + Twox64Concat, + ParaId, + Vec::AccountId, OldBalanceOf)>>, + ValueQuery, + >; /// This migration would move funds for lease from reserved to hold. - pub struct MigrateToV1( - sp_std::marker::PhantomData<(T, OldCurrency)>, - ); + pub struct MigrateToV1(sp_std::marker::PhantomData<(T, OldCurrency)>); impl OnRuntimeUpgrade for MigrateToV1 where @@ -63,66 +67,64 @@ mod v1 { BalanceOf: From, { fn on_runtime_upgrade() -> Weight { - for (_, lease_periods) in Leases::::iter() { - let mut max_deposits: sp_std::collections::btree_map::BTreeMap< - T::AccountId, - OldBalanceOf, - > = sp_std::collections::btree_map::BTreeMap::new(); + let mut migrated = 0u32; + for (_, lease_periods) in OldLeases::::iter() { + let mut deposit_held: BTreeMap> = + BTreeMap::new(); + // go through each lease and find the max lease deposit required for each leaser. lease_periods.iter().for_each(|lease| { if let Some((who, amount)) = lease { - max_deposits + deposit_held .entry(who.clone()) .and_modify(|deposit| *deposit = *amount.max(deposit)) .or_insert(*amount); } }); - max_deposits.iter().for_each(|(leaser, deposit)| { + deposit_held.iter().for_each(|(leaser, deposit)| { OldCurrency::unreserve(leaser, *deposit); let hold_result = Pallet::::hold(leaser, BalanceOf::::from(*deposit)); defensive_assert!( hold_result.is_ok(), - "hold should not fail, since we just unreserved the same amount; qed" + "hold should not fail, since we just unreserved the same amount" ); + migrated += 1; }) } // weight: all active lease periods * rw - todo!("weights") + T::DbWeight::get().reads_writes(migrated.saturating_mul(2).saturating_add(2).into(), 2) } #[cfg(feature = "try-runtime")] fn post_upgrade(_data: Vec) -> Result<(), TryRuntimeError> { - // let mut para_leasers = sp_std::collections::btree_set::BTreeSet::<(ParaId, - // T::AccountId)>::new(); for (para, lease_periods) in Leases::::iter() { - // lease_periods.into_iter().for_each(|maybe_lease| { - // if let Some((who, _)) = maybe_lease { - // para_leasers.insert((para, who)); - // } - // }); - // } - // - // // for each pair assert ReservedAmount is what we expect - // para_leasers.iter().try_for_each(|(para, who)| -> Result<(), TryRuntimeError> { - // let migrated_entry = ReservedAmounts::::get(para, who) - // .expect("Migration should have inserted this entry"); - // let expected = Pallet::::required_deposit(*para, who); - // let reserved_balance = T::Currency::reserved_balance(who); - // - // ensure!( - // migrated_entry == expected, - // "ReservedAmount value not same as required deposit" - // ); - // // fixme(ank4n) if there is another reserve on the account, this might be possible. - // ensure!( - // migrated_entry == reserved_balance, - // "ReservedAmount value not same as actual reserved balance" - // ); - // - // Ok(()) - // }) - todo!() + // Build a set of pairs of (para, who) that have a lease. + let mut para_leasers = sp_std::collections::btree_set::BTreeSet::<(ParaId, + T::AccountId)>::new(); + for (para, lease_periods) in Leases::::iter() { + lease_periods.into_iter().for_each(|maybe_lease| { + if let Some((who, _)) = maybe_lease { + para_leasers.insert((para, who)); + } + }); + } + + // for each pair assert hold amount is what we expect + para_leasers.iter().try_for_each(|(para, who)| -> Result<(), TryRuntimeError> { + // fixme(ank4n) there is a case where an account has a hold for multiple para-ids.. + // TODO(ank4n) Add integrity check.. + let actual_hold = T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into(), who) + .expect("Migration should have inserted this entry"); + let expected_hold = Pallet::::deposit_held(*para, who); + + ensure!( + actual_hold == expected_hold, + "ReservedAmount value not same as actual reserved balance" + ); + + Ok(()) + }) } } } From 52a12bdcc7e706812ae025a4aee31722c0372442 Mon Sep 17 00:00:00 2001 From: Ankan Date: Thu, 5 Oct 2023 09:47:04 +0200 Subject: [PATCH 07/11] add migration to westend runtime --- .../runtime/common/src/slots/migration.rs | 21 +++++++++++++------ polkadot/runtime/common/src/slots/mod.rs | 1 + polkadot/runtime/westend/src/lib.rs | 5 ++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/polkadot/runtime/common/src/slots/migration.rs b/polkadot/runtime/common/src/slots/migration.rs index 55f213c7918ff..092eb05ae4ce3 100644 --- a/polkadot/runtime/common/src/slots/migration.rs +++ b/polkadot/runtime/common/src/slots/migration.rs @@ -22,12 +22,15 @@ use sp_runtime::traits::AccountIdConversion; #[cfg(feature = "try-runtime")] use sp_runtime::TryRuntimeError; +/// The current storage version. +pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); + pub mod versioned { use super::*; /// Wrapper over `MigrateToV1` with convenience version checks. /// - /// This migration would move reserves into Named holds. + /// This migration would move lease reserves into named holds. pub type ToV1 = frame_support::migrations::VersionedMigration< 0, 1, @@ -67,12 +70,16 @@ mod v1 { BalanceOf: From, { fn on_runtime_upgrade() -> Weight { - let mut migrated = 0u32; + // useful for calculating weights later + let mut migrated = 0u64; + let mut leases_count = 0u64; + for (_, lease_periods) in OldLeases::::iter() { + leases_count += 1; let mut deposit_held: BTreeMap> = BTreeMap::new(); - // go through each lease and find the max lease deposit required for each leaser. + // go through each lease and find the lease deposit required for each leaser. lease_periods.iter().for_each(|lease| { if let Some((who, amount)) = lease { deposit_held @@ -93,8 +100,11 @@ mod v1 { }) } - // weight: all active lease periods * rw - T::DbWeight::get().reads_writes(migrated.saturating_mul(2).saturating_add(2).into(), 2) + T::DbWeight::get().reads_writes( + // reads: leases_count + leases_count, + // writes = migrated * (unreserve + hold) + migrated.saturating_mul(2)) } #[cfg(feature = "try-runtime")] @@ -113,7 +123,6 @@ mod v1 { // for each pair assert hold amount is what we expect para_leasers.iter().try_for_each(|(para, who)| -> Result<(), TryRuntimeError> { // fixme(ank4n) there is a case where an account has a hold for multiple para-ids.. - // TODO(ank4n) Add integrity check.. let actual_hold = T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into(), who) .expect("Migration should have inserted this entry"); let expected_hold = Pallet::::deposit_held(*para, who); diff --git a/polkadot/runtime/common/src/slots/mod.rs b/polkadot/runtime/common/src/slots/mod.rs index 0e8f62464d5d5..0f1a2fe37d2d2 100644 --- a/polkadot/runtime/common/src/slots/mod.rs +++ b/polkadot/runtime/common/src/slots/mod.rs @@ -70,6 +70,7 @@ pub mod pallet { use super::*; #[pallet::pallet] + #[pallet::storage_version(migration::STORAGE_VERSION)] #[pallet::without_storage_info] pub struct Pallet(_); diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 6085b6e37455b..e11e796ec7a62 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1255,6 +1255,7 @@ parameter_types! { impl slots::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Currency = Balances; + type RuntimeHoldReason = RuntimeHoldReason; type Registrar = Registrar; type LeasePeriod = LeasePeriod; type LeaseOffset = (); @@ -1294,6 +1295,7 @@ parameter_types! { impl auctions::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Leaser = Slots; + type Currency = Balances; type Registrar = Registrar; type EndingPeriod = EndingPeriod; type SampleLength = SampleLength; @@ -1432,7 +1434,7 @@ construct_runtime! { // Parachain Onboarding Pallets. Start indices at 60 to leave room. Registrar: paras_registrar::{Pallet, Call, Storage, Event, Config} = 60, - Slots: slots::{Pallet, Call, Storage, Event} = 61, + Slots: slots::{Pallet, Call, Storage, Event, HoldReason} = 61, ParasSudoWrapper: paras_sudo_wrapper::{Pallet, Call} = 62, Auctions: auctions::{Pallet, Call, Storage, Event} = 63, Crowdloan: crowdloan::{Pallet, Call, Storage, Event} = 64, @@ -1509,6 +1511,7 @@ pub mod migrations { pallet_nomination_pools::migration::versioned_migrations::V5toV6, pallet_referenda::migration::v1::MigrateV0ToV1, pallet_nomination_pools::migration::versioned_migrations::V6ToV7, + slots::migration::versioned::ToV1, ); } From c01e72bda301aa8f026c0c653faf5dea82a7636b Mon Sep 17 00:00:00 2001 From: Ankan Date: Sun, 8 Oct 2023 22:40:10 +0200 Subject: [PATCH 08/11] compile things --- polkadot/runtime/rococo/src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 4bdcc1237394e..cf615082429aa 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -997,6 +997,7 @@ parameter_types! { impl slots::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Currency = Balances; + type RuntimeHoldReason = RuntimeHoldReason; type Registrar = Registrar; type LeasePeriod = LeasePeriod; type LeaseOffset = (); @@ -1036,6 +1037,7 @@ parameter_types! { impl auctions::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Leaser = Slots; + type Currency = Balances; type Registrar = Registrar; type EndingPeriod = EndingPeriod; type SampleLength = SampleLength; @@ -1061,7 +1063,7 @@ impl pallet_balances::Config for Runtime { type WeightInfo = weights::pallet_balances_nis_counterpart_balances::WeightInfo; type RuntimeHoldReason = RuntimeHoldReason; type FreezeIdentifier = (); - type MaxHolds = ConstU32<0>; + type MaxHolds = ConstU32<1>; type MaxFreezes = ConstU32<0>; } @@ -1309,7 +1311,7 @@ construct_runtime! { // Parachain Onboarding Pallets. Start indices at 70 to leave room. Registrar: paras_registrar::{Pallet, Call, Storage, Event, Config} = 70, - Slots: slots::{Pallet, Call, Storage, Event} = 71, + Slots: slots::{Pallet, Call, Storage, Event, HoldReason} = 71, Auctions: auctions::{Pallet, Call, Storage, Event} = 72, Crowdloan: crowdloan::{Pallet, Call, Storage, Event} = 73, From e0fa97a1c8ec7dd98da9fb645328778940c6fe39 Mon Sep 17 00:00:00 2001 From: Ankan Date: Mon, 9 Oct 2023 00:35:11 +0200 Subject: [PATCH 09/11] fix benchmark compile --- .../common/src/assigned_slots/benchmarking.rs | 7 +++---- polkadot/runtime/common/src/auctions.rs | 8 +++----- polkadot/runtime/common/src/slots/migration.rs | 6 ++++-- polkadot/runtime/common/src/slots/mod.rs | 13 ++++++++----- polkadot/runtime/common/src/traits.rs | 4 ++-- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/polkadot/runtime/common/src/assigned_slots/benchmarking.rs b/polkadot/runtime/common/src/assigned_slots/benchmarking.rs index 61638fe6cabfc..16df7f947bace 100644 --- a/polkadot/runtime/common/src/assigned_slots/benchmarking.rs +++ b/polkadot/runtime/common/src/assigned_slots/benchmarking.rs @@ -20,19 +20,18 @@ use super::*; use frame_benchmarking::v2::*; -use frame_support::assert_ok; use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin}; use primitives::Id as ParaId; use sp_runtime::traits::Bounded; type CurrencyOf = <::Leaser as Leaser>>::Currency; -type BalanceOf = <<::Leaser as Leaser>>::Currency as Currency< +type BalanceOf = <<::Leaser as Leaser>>::Currency as FunInspect< ::AccountId, >>::Balance; #[benchmarks(where T: Config)] mod benchmarks { use super::*; - + use frame_support::{assert_ok, traits::fungible::Mutate}; use crate::assigned_slots::Pallet as AssignedSlots; fn register_parachain(para_id: ParaId) { @@ -40,7 +39,7 @@ mod benchmarks { let worst_validation_code = T::Registrar::worst_validation_code(); let worst_head_data = T::Registrar::worst_head_data(); - CurrencyOf::::make_free_balance_be(&who, BalanceOf::::max_value()); + CurrencyOf::::set_balance(&who, BalanceOf::::max_value()); assert_ok!(T::Registrar::register( who, diff --git a/polkadot/runtime/common/src/auctions.rs b/polkadot/runtime/common/src/auctions.rs index 2e62bb791ac7c..49c499b74fac6 100644 --- a/polkadot/runtime/common/src/auctions.rs +++ b/polkadot/runtime/common/src/auctions.rs @@ -25,7 +25,7 @@ use crate::{ use frame_support::{ dispatch::DispatchResult, ensure, - traits::{fungible::Inspect as FunInspect, Get, Randomness, ReservableCurrency}, + traits::{fungible::Inspect as FunInspect, Currency, Get, Randomness, ReservableCurrency}, weights::Weight, }; use frame_system::pallet_prelude::BlockNumberFor; @@ -37,9 +37,7 @@ use sp_std::{mem::swap, prelude::*}; type CurrencyOf = ::Currency; type BalanceOf = - <<::Leaser as Leaser>>::Currency as FunInspect< - ::AccountId, - >>::Balance; + <::Currency as Currency<::AccountId>>::Balance; pub trait WeightInfo { fn new_auction() -> Weight; @@ -686,7 +684,7 @@ mod tests { use ::test_helpers::{dummy_hash, dummy_head_data, dummy_validation_code}; use frame_support::{ assert_noop, assert_ok, assert_storage_noop, ord_parameter_types, parameter_types, - traits::{ConstU32, EitherOfDiverse, OnFinalize, OnInitialize, Currency}, + traits::{ConstU32, Currency, EitherOfDiverse, OnFinalize, OnInitialize}, }; use frame_system::{EnsureRoot, EnsureSignedBy}; use pallet_balances; diff --git a/polkadot/runtime/common/src/slots/migration.rs b/polkadot/runtime/common/src/slots/migration.rs index 092eb05ae4ce3..49b49ffda64e7 100644 --- a/polkadot/runtime/common/src/slots/migration.rs +++ b/polkadot/runtime/common/src/slots/migration.rs @@ -45,6 +45,9 @@ mod v1 { use frame_support::traits::ReservableCurrency; use sp_std::collections::btree_map::BTreeMap; + #[cfg(feature = "try-runtime")] + use frame_support::traits::fungible::InspectHold; + /// Balance type of OldCurrency. pub type OldBalanceOf = ::AccountId, @@ -123,8 +126,7 @@ mod v1 { // for each pair assert hold amount is what we expect para_leasers.iter().try_for_each(|(para, who)| -> Result<(), TryRuntimeError> { // fixme(ank4n) there is a case where an account has a hold for multiple para-ids.. - let actual_hold = T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into(), who) - .expect("Migration should have inserted this entry"); + let actual_hold = T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into(), who); let expected_hold = Pallet::::deposit_held(*para, who); ensure!( diff --git a/polkadot/runtime/common/src/slots/mod.rs b/polkadot/runtime/common/src/slots/mod.rs index 0f1a2fe37d2d2..f3a619ec28a41 100644 --- a/polkadot/runtime/common/src/slots/mod.rs +++ b/polkadot/runtime/common/src/slots/mod.rs @@ -28,7 +28,10 @@ use crate::traits::{LeaseError, Leaser, Registrar}; use frame_support::{ defensive_assert, pallet_prelude::*, - traits::fungible::{hold::Mutate as FunHoldMutate, Inspect as FunInspect}, + traits::fungible::{ + hold::{Inspect as FunHoldInspect, Mutate as FunHoldMutate}, + Inspect as FunInspect, Mutate as FunMutate, + }, weights::Weight, }; use frame_system::pallet_prelude::*; @@ -80,7 +83,8 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The currency type used for bidding. - type Currency: FunHoldMutate; + type Currency: FunHoldMutate + + FunMutate; /// The hold reason when reserving funds for the lease. type RuntimeHoldReason: From; @@ -1159,7 +1163,7 @@ mod benchmarking { for i in 0 .. max_people { let leaser = account("lease_deposit", i, 0); - assert_eq!(T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into, &leaser), T::Currency::minimum_balance()); + assert_eq!(T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into(), &leaser), T::Currency::minimum_balance()); } let origin = @@ -1168,8 +1172,7 @@ mod benchmarking { verify { for i in 0 .. max_people { let leaser = account("lease_deposit", i, 0); - assert_eq!(T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into, &leaser), 0u32.into()); - assert_eq!(T::Currency::reserved_balance(&leaser), 0u32.into()); + assert_eq!(T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into(), &leaser), 0u32.into()); } } diff --git a/polkadot/runtime/common/src/traits.rs b/polkadot/runtime/common/src/traits.rs index 68256d7234a62..759b8c027c6e8 100644 --- a/polkadot/runtime/common/src/traits.rs +++ b/polkadot/runtime/common/src/traits.rs @@ -19,7 +19,7 @@ use frame_support::{ dispatch::DispatchResult, traits::{ - fungible::{hold::Mutate as FunHoldMutate, Inspect as FunInspect}, + fungible::{hold::Mutate as FunHoldMutate, Inspect as FunInspect, Mutate as FunMutate}, Currency, ReservableCurrency, }, }; @@ -112,7 +112,7 @@ pub trait Leaser { type LeasePeriod; /// The currency type in which the lease is taken. - type Currency: FunHoldMutate; + type Currency: FunHoldMutate + FunMutate; /// Lease a new parachain slot for `para`. /// From 716ab455cbbd5c1c60378e9d173e48f89e9a4abd Mon Sep 17 00:00:00 2001 From: Ankan Date: Mon, 9 Oct 2023 02:58:46 +0200 Subject: [PATCH 10/11] fmt --- .../runtime/common/src/assigned_slots/benchmarking.rs | 9 +++++---- polkadot/runtime/common/src/slots/migration.rs | 10 ++++++---- polkadot/runtime/common/src/slots/mod.rs | 7 ++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/polkadot/runtime/common/src/assigned_slots/benchmarking.rs b/polkadot/runtime/common/src/assigned_slots/benchmarking.rs index 16df7f947bace..affe8ef417887 100644 --- a/polkadot/runtime/common/src/assigned_slots/benchmarking.rs +++ b/polkadot/runtime/common/src/assigned_slots/benchmarking.rs @@ -25,14 +25,15 @@ use primitives::Id as ParaId; use sp_runtime::traits::Bounded; type CurrencyOf = <::Leaser as Leaser>>::Currency; -type BalanceOf = <<::Leaser as Leaser>>::Currency as FunInspect< - ::AccountId, ->>::Balance; +type BalanceOf = + <<::Leaser as Leaser>>::Currency as FunInspect< + ::AccountId, + >>::Balance; #[benchmarks(where T: Config)] mod benchmarks { use super::*; - use frame_support::{assert_ok, traits::fungible::Mutate}; use crate::assigned_slots::Pallet as AssignedSlots; + use frame_support::{assert_ok, traits::fungible::Mutate}; fn register_parachain(para_id: ParaId) { let who: T::AccountId = whitelisted_caller(); diff --git a/polkadot/runtime/common/src/slots/migration.rs b/polkadot/runtime/common/src/slots/migration.rs index 49b49ffda64e7..6fc928ad5038e 100644 --- a/polkadot/runtime/common/src/slots/migration.rs +++ b/polkadot/runtime/common/src/slots/migration.rs @@ -107,14 +107,15 @@ mod v1 { // reads: leases_count leases_count, // writes = migrated * (unreserve + hold) - migrated.saturating_mul(2)) + migrated.saturating_mul(2), + ) } #[cfg(feature = "try-runtime")] fn post_upgrade(_data: Vec) -> Result<(), TryRuntimeError> { // Build a set of pairs of (para, who) that have a lease. - let mut para_leasers = sp_std::collections::btree_set::BTreeSet::<(ParaId, - T::AccountId)>::new(); + let mut para_leasers = + sp_std::collections::btree_set::BTreeSet::<(ParaId, T::AccountId)>::new(); for (para, lease_periods) in Leases::::iter() { lease_periods.into_iter().for_each(|maybe_lease| { if let Some((who, _)) = maybe_lease { @@ -126,7 +127,8 @@ mod v1 { // for each pair assert hold amount is what we expect para_leasers.iter().try_for_each(|(para, who)| -> Result<(), TryRuntimeError> { // fixme(ank4n) there is a case where an account has a hold for multiple para-ids.. - let actual_hold = T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into(), who); + let actual_hold = + T::Currency::balance_on_hold(&HoldReason::LeaseDeposit.into(), who); let expected_hold = Pallet::::deposit_held(*para, who); ensure!( diff --git a/polkadot/runtime/common/src/slots/mod.rs b/polkadot/runtime/common/src/slots/mod.rs index f3a619ec28a41..3641cd5edfde2 100644 --- a/polkadot/runtime/common/src/slots/mod.rs +++ b/polkadot/runtime/common/src/slots/mod.rs @@ -28,10 +28,7 @@ use crate::traits::{LeaseError, Leaser, Registrar}; use frame_support::{ defensive_assert, pallet_prelude::*, - traits::fungible::{ - hold::{Inspect as FunHoldInspect, Mutate as FunHoldMutate}, - Inspect as FunInspect, Mutate as FunMutate, - }, + traits::fungible::{hold::Mutate as FunHoldMutate, Inspect as FunInspect, Mutate as FunMutate}, weights::Weight, }; use frame_system::pallet_prelude::*; @@ -1016,7 +1013,7 @@ mod tests { #[cfg(feature = "runtime-benchmarks")] mod benchmarking { use super::*; - use frame_support::assert_ok; + use frame_support::{assert_ok, traits::fungible::hold::Inspect}; use frame_system::RawOrigin; use runtime_parachains::paras; use sp_runtime::traits::{Bounded, One}; From 1f7c6ab80a827eb4e05bba19969b78101ad099af Mon Sep 17 00:00:00 2001 From: Ankan Date: Mon, 9 Oct 2023 03:14:04 +0200 Subject: [PATCH 11/11] fix storage version --- polkadot/runtime/common/src/slots/migration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polkadot/runtime/common/src/slots/migration.rs b/polkadot/runtime/common/src/slots/migration.rs index 6fc928ad5038e..8d586df07a59f 100644 --- a/polkadot/runtime/common/src/slots/migration.rs +++ b/polkadot/runtime/common/src/slots/migration.rs @@ -23,7 +23,7 @@ use sp_runtime::traits::AccountIdConversion; use sp_runtime::TryRuntimeError; /// The current storage version. -pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); +pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); pub mod versioned { use super::*;