git.delta.rocks / unique-network / refs/commits / 3539f20b1874

difftreelog

feat switch `collator-selection` from `Currency` trait to `fungible::*` traits

Grigoriy Simonov2023-05-22parent: #fcb9d1c.patch.diff
in: master

4 files changed

modifiedpallets/collator-selection/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/collator-selection/src/benchmarking.rs
+++ b/pallets/collator-selection/src/benchmarking.rs
@@ -40,7 +40,11 @@
 use frame_support::{
 	assert_ok,
 	codec::Decode,
-	traits::{Currency, EnsureOrigin, Get},
+	traits::{
+		EnsureOrigin,
+		fungible::{Inspect, Mutate},
+		Get,
+	},
 };
 use frame_system::{EventRecord, RawOrigin};
 use pallet_authorship::EventHandler;
@@ -78,7 +82,7 @@
 ) -> T::AccountId {
 	let user = account(string, n, SEED);
 	let balance = balance_unit::<T>() * balance_factor.into();
-	let _ = T::Currency::make_free_balance_be(&user, balance);
+	let _ = T::Currency::set_balance(&user, balance);
 	user
 }
 
@@ -137,7 +141,7 @@
 	);
 
 	for who in candidates {
-		T::Currency::make_free_balance_be(&who, <LicenseBond<T>>::get() * 2u32.into());
+		T::Currency::set_balance(&who, <LicenseBond<T>>::get() * 2u32.into());
 		<CollatorSelection<T>>::get_license(RawOrigin::Signed(who.clone()).into()).unwrap();
 		<CollatorSelection<T>>::onboard(RawOrigin::Signed(who).into()).unwrap();
 	}
@@ -153,14 +157,14 @@
 	);
 
 	for who in candidates {
-		T::Currency::make_free_balance_be(&who, <LicenseBond<T>>::get() * 2u32.into());
+		T::Currency::set_balance(&who, <LicenseBond<T>>::get() * 2u32.into());
 		<CollatorSelection<T>>::get_license(RawOrigin::Signed(who.clone()).into()).unwrap();
 	}
 }
 
 /// `Currency::minimum_balance` was used originally, but in unique-chain, we have
 /// zero existential deposit, thus triggering zero bond assertion.
-fn balance_unit<T: Config>() -> <T::Currency as Currency<T::AccountId>>::Balance {
+fn balance_unit<T: Config>() -> BalanceOf<T> {
 	200u32.into()
 }
 
@@ -168,7 +172,9 @@
 const INITIAL_INVULNERABLES: u32 = 2;
 
 benchmarks! {
-	where_clause { where T: pallet_authorship::Config + session::Config + configuration::Config }
+	where_clause { where
+		T: pallet_authorship::Config + session::Config + configuration::Config
+	}
 
 	// todo:collator this and all the following do not work for some reason, going all the way up to 10 in length
 	// Both invulnerables and candidates count together against MaxCollators.
@@ -182,7 +188,7 @@
 
 		let new_invulnerable: T::AccountId = whitelisted_caller();
 		let bond: BalanceOf<T> = balance_unit::<T>() * 2u32.into();
-		T::Currency::make_free_balance_be(&new_invulnerable, bond.clone());
+		T::Currency::set_balance(&new_invulnerable, bond.clone());
 
 		<session::Pallet<T>>::set_keys(
 			RawOrigin::Signed(new_invulnerable.clone()).into(),
@@ -227,7 +233,7 @@
 
 		let caller: T::AccountId = whitelisted_caller();
 		let bond: BalanceOf<T> = balance_unit::<T>() * 2u32.into();
-		T::Currency::make_free_balance_be(&caller, bond.clone());
+		T::Currency::set_balance(&caller, bond.clone());
 
 		<session::Pallet<T>>::set_keys(
 			RawOrigin::Signed(caller.clone()).into(),
@@ -253,7 +259,7 @@
 
 		let caller: T::AccountId = whitelisted_caller();
 		let bond: BalanceOf<T> = balance_unit::<T>() * 2u32.into();
-		T::Currency::make_free_balance_be(&caller, bond.clone());
+		T::Currency::set_balance(&caller, bond.clone());
 
 		let origin = RawOrigin::Signed(caller.clone());
 
@@ -329,7 +335,7 @@
 	// worst case is paying a non-existing candidate account.
 	note_author {
 		<LicenseBond<T>>::put(balance_unit::<T>());
-		T::Currency::make_free_balance_be(
+		T::Currency::set_balance(
 			&<CollatorSelection<T>>::account_id(),
 			balance_unit::<T>() * 4u32.into(),
 		);
@@ -337,11 +343,11 @@
 		let new_block: T::BlockNumber = 10u32.into();
 
 		frame_system::Pallet::<T>::set_block_number(new_block);
-		assert!(T::Currency::free_balance(&author) == 0u32.into());
+		assert!(T::Currency::balance(&author) == 0u32.into());
 	}: {
 		<CollatorSelection<T> as EventHandler<_, _>>::note_author(author.clone())
 	} verify {
-		assert!(T::Currency::free_balance(&author) > 0u32.into());
+		assert!(T::Currency::balance(&author) > 0u32.into());
 		assert_eq!(frame_system::Pallet::<T>::block_number(), new_block);
 	}
 
modifiedpallets/collator-selection/src/lib.rsdiffbeforeafterboth
9292
93#[frame_support::pallet]93#[frame_support::pallet]
94pub mod pallet {94pub mod pallet {
95 use super::*;
95 pub use crate::weights::WeightInfo;96 pub use crate::weights::WeightInfo;
96 use core::ops::Div;97 use core::ops::Div;
97 use frame_support::{98 use frame_support::{
100 pallet_prelude::*,101 pallet_prelude::*,
101 sp_runtime::traits::{AccountIdConversion, CheckedSub, Saturating, Zero},102 sp_runtime::traits::{AccountIdConversion, CheckedSub, Saturating, Zero},
102 traits::{103 traits::{
103 Currency, EnsureOrigin, ExistenceRequirement::KeepAlive, ReservableCurrency,104 EnsureOrigin,
105 fungible::{Balanced, BalancedHold, Inspect, InspectHold, Mutate, MutateHold},
104 ValidatorRegistration,106 ValidatorRegistration,
107 tokens::{Precision, Preservation},
105 },108 },
106 BoundedVec, PalletId,109 BoundedVec, PalletId,
107 };110 };
159 /// The weight information of this pallet.162 /// The weight information of this pallet.
160 type WeightInfo: WeightInfo;163 type WeightInfo: WeightInfo;
164
165 #[pallet::constant]
166 type LicenceBondIdentifier: Get<<<Self as pallet_configuration::Config>::Currency as InspectHold<Self::AccountId>>::Reason>;
161 }167 }
162168
163 #[pallet::pallet]169 #[pallet::pallet]
361367
362 let deposit = <LicenseBond<T>>::get();368 let deposit = <LicenseBond<T>>::get();
363369
364 T::Currency::reserve(&who, deposit)?;370 T::Currency::hold(&T::LicenceBondIdentifier::get(), &who, deposit)?;
365 LicenseDepositOf::<T>::insert(who.clone(), deposit);371 LicenseDepositOf::<T>::insert(who.clone(), deposit);
366372
367 Self::deposit_event(Event::LicenseObtained {373 Self::deposit_event(Event::LicenseObtained {
523 let slashed = T::SlashRatio::get() * deposit;529 let slashed = T::SlashRatio::get() * deposit;
524 let remaining = deposit - slashed;530 let remaining = deposit - slashed;
525531
526 let (imbalance, _) = T::Currency::slash_reserved(who, slashed);532 let (imbalance, _) =
533 T::Currency::slash(&T::LicenceBondIdentifier::get(), who, slashed);
527 //T::Currency::unreserve(who, remaining);534 //T::Currency::unreserve(who, remaining);
528 deposit_returned = remaining;535 deposit_returned = remaining;
529536
530 T::Currency::resolve_creating(&T::TreasuryAccountId::get(), imbalance);537 T::Currency::resolve(&T::TreasuryAccountId::get(), imbalance)
538 .map_err(|_| DispatchError::Other("Failed to deposit imbalance"))?;
531 } else {539 } else {
532 //T::Currency::unreserve(who, deposit);540 //T::Currency::unreserve(who, deposit);
533 deposit_returned = deposit;541 deposit_returned = deposit;
534 }542 }
535543
536 T::Currency::unreserve(who, deposit_returned);544 T::Currency::release(
545 &T::LicenceBondIdentifier::get(),
546 who,
547 deposit_returned,
548 Precision::Exact,
549 )?;
537 Ok(())550 Ok(())
538 } else {551 } else {
539 Err(Error::<T>::NoLicense.into())552 Err(Error::<T>::NoLicense.into())
594 fn note_author(author: T::AccountId) {607 fn note_author(author: T::AccountId) {
595 let pot = Self::account_id();608 let pot = Self::account_id();
596 // assumes an ED will be sent to pot.609 // assumes an ED will be sent to pot.
597 let reward = T::Currency::free_balance(&pot)610 let reward = T::Currency::balance(&pot)
598 .checked_sub(&T::Currency::minimum_balance())611 .checked_sub(&T::Currency::minimum_balance())
599 .unwrap_or_else(Zero::zero)612 .unwrap_or_else(Zero::zero)
600 .div(2u32.into());613 .div(2u32.into());
601 // `reward` is half of pot account minus ED, this should never fail.614 // `reward` is half of pot account minus ED, this should never fail.
602 let _success = T::Currency::transfer(&pot, &author, reward, KeepAlive);615 let _success = T::Currency::transfer(&pot, &author, reward, Preservation::Preserve);
603 debug_assert!(_success.is_ok());616 debug_assert!(_success.is_ok());
604 <LastAuthoredBlock<T>>::insert(author, frame_system::Pallet::<T>::block_number());617 <LastAuthoredBlock<T>>::insert(author, frame_system::Pallet::<T>::block_number());
605618
modifiedpallets/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);
modifiedpallets/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>;