difftreelog
feat switch `collator-selection` from `Currency` trait to `fungible::*` traits
in: master
4 files changed
pallets/collator-selection/src/benchmarking.rsdiffbeforeafterboth40use frame_support::{40use frame_support::{41 assert_ok,41 assert_ok,42 codec::Decode,42 codec::Decode,43 traits::{Currency, EnsureOrigin, Get},43 traits::{44 EnsureOrigin,45 fungible::{Inspect, Mutate},46 Get,47 },44};48};45use frame_system::{EventRecord, RawOrigin};49use frame_system::{EventRecord, RawOrigin};78) -> T::AccountId {82) -> T::AccountId {79 let user = account(string, n, SEED);83 let user = account(string, n, SEED);80 let balance = balance_unit::<T>() * balance_factor.into();84 let balance = balance_unit::<T>() * balance_factor.into();81 let _ = T::Currency::make_free_balance_be(&user, balance);85 let _ = T::Currency::set_balance(&user, balance);82 user86 user83}87}8488137 );141 );138142139 for who in candidates {143 for who in candidates {140 T::Currency::make_free_balance_be(&who, <LicenseBond<T>>::get() * 2u32.into());144 T::Currency::set_balance(&who, <LicenseBond<T>>::get() * 2u32.into());141 <CollatorSelection<T>>::get_license(RawOrigin::Signed(who.clone()).into()).unwrap();145 <CollatorSelection<T>>::get_license(RawOrigin::Signed(who.clone()).into()).unwrap();142 <CollatorSelection<T>>::onboard(RawOrigin::Signed(who).into()).unwrap();146 <CollatorSelection<T>>::onboard(RawOrigin::Signed(who).into()).unwrap();143 }147 }153 );157 );154158155 for who in candidates {159 for who in candidates {156 T::Currency::make_free_balance_be(&who, <LicenseBond<T>>::get() * 2u32.into());160 T::Currency::set_balance(&who, <LicenseBond<T>>::get() * 2u32.into());157 <CollatorSelection<T>>::get_license(RawOrigin::Signed(who.clone()).into()).unwrap();161 <CollatorSelection<T>>::get_license(RawOrigin::Signed(who.clone()).into()).unwrap();158 }162 }159}163}160164161/// `Currency::minimum_balance` was used originally, but in unique-chain, we have165/// `Currency::minimum_balance` was used originally, but in unique-chain, we have162/// zero existential deposit, thus triggering zero bond assertion.166/// zero existential deposit, thus triggering zero bond assertion.163fn balance_unit<T: Config>() -> <T::Currency as Currency<T::AccountId>>::Balance {167fn balance_unit<T: Config>() -> BalanceOf<T> {164 200u32.into()168 200u32.into()165}169}166170182188183 let new_invulnerable: T::AccountId = whitelisted_caller();189 let new_invulnerable: T::AccountId = whitelisted_caller();184 let bond: BalanceOf<T> = balance_unit::<T>() * 2u32.into();190 let bond: BalanceOf<T> = balance_unit::<T>() * 2u32.into();185 T::Currency::make_free_balance_be(&new_invulnerable, bond.clone());191 T::Currency::set_balance(&new_invulnerable, bond.clone());186192187 <session::Pallet<T>>::set_keys(193 <session::Pallet<T>>::set_keys(188 RawOrigin::Signed(new_invulnerable.clone()).into(),194 RawOrigin::Signed(new_invulnerable.clone()).into(),227233228 let caller: T::AccountId = whitelisted_caller();234 let caller: T::AccountId = whitelisted_caller();229 let bond: BalanceOf<T> = balance_unit::<T>() * 2u32.into();235 let bond: BalanceOf<T> = balance_unit::<T>() * 2u32.into();230 T::Currency::make_free_balance_be(&caller, bond.clone());236 T::Currency::set_balance(&caller, bond.clone());231237232 <session::Pallet<T>>::set_keys(238 <session::Pallet<T>>::set_keys(233 RawOrigin::Signed(caller.clone()).into(),239 RawOrigin::Signed(caller.clone()).into(),253259254 let caller: T::AccountId = whitelisted_caller();260 let caller: T::AccountId = whitelisted_caller();255 let bond: BalanceOf<T> = balance_unit::<T>() * 2u32.into();261 let bond: BalanceOf<T> = balance_unit::<T>() * 2u32.into();256 T::Currency::make_free_balance_be(&caller, bond.clone());262 T::Currency::set_balance(&caller, bond.clone());257263258 let origin = RawOrigin::Signed(caller.clone());264 let origin = RawOrigin::Signed(caller.clone());259265329 // worst case is paying a non-existing candidate account.335 // worst case is paying a non-existing candidate account.330 note_author {336 note_author {331 <LicenseBond<T>>::put(balance_unit::<T>());337 <LicenseBond<T>>::put(balance_unit::<T>());332 T::Currency::make_free_balance_be(338 T::Currency::set_balance(333 &<CollatorSelection<T>>::account_id(),339 &<CollatorSelection<T>>::account_id(),334 balance_unit::<T>() * 4u32.into(),340 balance_unit::<T>() * 4u32.into(),335 );341 );336 let author = account("author", 0, SEED);342 let author = account("author", 0, SEED);337 let new_block: T::BlockNumber = 10u32.into();343 let new_block: T::BlockNumber = 10u32.into();338344339 frame_system::Pallet::<T>::set_block_number(new_block);345 frame_system::Pallet::<T>::set_block_number(new_block);340 assert!(T::Currency::free_balance(&author) == 0u32.into());346 assert!(T::Currency::balance(&author) == 0u32.into());341 }: {347 }: {342 <CollatorSelection<T> as EventHandler<_, _>>::note_author(author.clone())348 <CollatorSelection<T> as EventHandler<_, _>>::note_author(author.clone())343 } verify {349 } verify {344 assert!(T::Currency::free_balance(&author) > 0u32.into());350 assert!(T::Currency::balance(&author) > 0u32.into());345 assert_eq!(frame_system::Pallet::<T>::block_number(), new_block);351 assert_eq!(frame_system::Pallet::<T>::block_number(), new_block);346 }352 }347353pallets/collator-selection/src/lib.rsdiffbeforeafterboth--- a/pallets/collator-selection/src/lib.rs
+++ b/pallets/collator-selection/src/lib.rs
@@ -92,6 +92,7 @@
#[frame_support::pallet]
pub mod pallet {
+ use super::*;
pub use crate::weights::WeightInfo;
use core::ops::Div;
use frame_support::{
@@ -100,8 +101,10 @@
pallet_prelude::*,
sp_runtime::traits::{AccountIdConversion, CheckedSub, Saturating, Zero},
traits::{
- Currency, EnsureOrigin, ExistenceRequirement::KeepAlive, ReservableCurrency,
+ EnsureOrigin,
+ fungible::{Balanced, BalancedHold, Inspect, InspectHold, Mutate, MutateHold},
ValidatorRegistration,
+ tokens::{Precision, Preservation},
},
BoundedVec, PalletId,
};
@@ -158,6 +161,9 @@
/// The weight information of this pallet.
type WeightInfo: WeightInfo;
+
+ #[pallet::constant]
+ type LicenceBondIdentifier: Get<<<Self as pallet_configuration::Config>::Currency as InspectHold<Self::AccountId>>::Reason>;
}
#[pallet::pallet]
@@ -361,7 +367,7 @@
let deposit = <LicenseBond<T>>::get();
- T::Currency::reserve(&who, deposit)?;
+ T::Currency::hold(&T::LicenceBondIdentifier::get(), &who, deposit)?;
LicenseDepositOf::<T>::insert(who.clone(), deposit);
Self::deposit_event(Event::LicenseObtained {
@@ -523,17 +529,24 @@
let slashed = T::SlashRatio::get() * deposit;
let remaining = deposit - slashed;
- let (imbalance, _) = T::Currency::slash_reserved(who, slashed);
+ let (imbalance, _) =
+ T::Currency::slash(&T::LicenceBondIdentifier::get(), who, slashed);
//T::Currency::unreserve(who, remaining);
deposit_returned = remaining;
- T::Currency::resolve_creating(&T::TreasuryAccountId::get(), imbalance);
+ T::Currency::resolve(&T::TreasuryAccountId::get(), imbalance)
+ .map_err(|_| DispatchError::Other("Failed to deposit imbalance"))?;
} else {
//T::Currency::unreserve(who, deposit);
deposit_returned = deposit;
}
- T::Currency::unreserve(who, deposit_returned);
+ T::Currency::release(
+ &T::LicenceBondIdentifier::get(),
+ who,
+ deposit_returned,
+ Precision::Exact,
+ )?;
Ok(())
} else {
Err(Error::<T>::NoLicense.into())
@@ -594,12 +607,12 @@
fn note_author(author: T::AccountId) {
let pot = Self::account_id();
// assumes an ED will be sent to pot.
- let reward = T::Currency::free_balance(&pot)
+ let reward = T::Currency::balance(&pot)
.checked_sub(&T::Currency::minimum_balance())
.unwrap_or_else(Zero::zero)
.div(2u32.into());
// `reward` is half of pot account minus ED, this should never fail.
- let _success = T::Currency::transfer(&pot, &author, reward, KeepAlive);
+ let _success = T::Currency::transfer(&pot, &author, reward, Preservation::Preserve);
debug_assert!(_success.is_ok());
<LastAuthoredBlock<T>>::insert(author, frame_system::Pallet::<T>::block_number());
pallets/collator-selection/src/tests.rsdiffbeforeafterboth--- a/pallets/collator-selection/src/tests.rs
+++ b/pallets/collator-selection/src/tests.rs
@@ -417,7 +417,10 @@
fn authorship_event_handler() {
new_test_ext().execute_with(|| {
// put 100 in the pot + 5 for ED
- Balances::make_free_balance_be(&CollatorSelection::account_id(), 105);
+ <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::set_balance(
+ &CollatorSelection::account_id(),
+ 105,
+ );
// 4 is the default author.
assert_eq!(Balances::free_balance(4), 100);
@@ -441,7 +444,10 @@
// Nothing panics, no reward when no ED in balance
Authorship::on_initialize(1);
// put some money into the pot at ED
- Balances::make_free_balance_be(&CollatorSelection::account_id(), 5);
+ <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::set_balance(
+ &CollatorSelection::account_id(),
+ 5,
+ );
// 4 is the default author.
assert_eq!(Balances::free_balance(4), 100);
get_license_and_onboard(4);
pallets/configuration/src/lib.rsdiffbeforeafterboth--- a/pallets/configuration/src/lib.rs
+++ b/pallets/configuration/src/lib.rs
@@ -49,21 +49,20 @@
use frame_system::{pallet_prelude::OriginFor, ensure_root, Config as SystemConfig};
pub use crate::weights::WeightInfo;
- pub type BalanceOf<T> =
- <<T as Config>::Balances as fungible::Inspect<<T as SystemConfig>::AccountId>>::Balance;
+ pub type BalanceOf<T> =
+ <<T as Config>::Currency as fungible::Inspect<<T as SystemConfig>::AccountId>>::Balance;
#[pallet::config]
pub trait Config: frame_system::Config {
/// Overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
- type Balances:
- fungible::Inspect<Self::AccountId>
- + fungible::Mutate::<Self::AccountId>
- + fungible::MutateFreeze::<Self::AccountId>
- + fungible::InspectHold::<Self::AccountId>
- + fungible::MutateHold::<Self::AccountId>
- + fungible::BalancedHold::<Self::AccountId>;
+ type Currency: fungible::Inspect<Self::AccountId>
+ + fungible::Mutate<Self::AccountId>
+ + fungible::MutateFreeze<Self::AccountId>
+ + fungible::InspectHold<Self::AccountId>
+ + fungible::MutateHold<Self::AccountId>
+ + fungible::BalancedHold<Self::AccountId>;
#[pallet::constant]
type DefaultWeightToFeeCoefficient: Get<u64>;