git.delta.rocks / unique-network / refs/commits / 5c283c1e8412

difftreelog

fix rust tests

Grigoriy Simonov2023-05-24parent: #e1a1068.patch.diff
in: master

8 files changed

modifiedpallets/collator-selection/Cargo.tomldiffbeforeafterboth
--- a/pallets/collator-selection/Cargo.toml
+++ b/pallets/collator-selection/Cargo.toml
@@ -55,9 +55,12 @@
 	"frame-system/std",
 	"log/std",
 	"pallet-authorship/std",
+	'pallet-aura/std',
+	'pallet-balances/std',
 	"pallet-session/std",
 	"rand/std",
 	"scale-info/std",
+	"sp-consensus-aura/std",
 	"sp-runtime/std",
 	"sp-staking/std",
 	"sp-std/std",
modifiedpallets/collator-selection/src/mock.rsdiffbeforeafterboth
--- a/pallets/collator-selection/src/mock.rs
+++ b/pallets/collator-selection/src/mock.rs
@@ -102,6 +102,8 @@
 parameter_types! {
 	pub const ExistentialDeposit: u64 = 5;
 	pub const MaxReserves: u32 = 50;
+	pub const MaxHolds: u32 = 2;
+	pub const MaxFreezes: u32 = 2;
 }
 
 impl pallet_balances::Config for Test {
@@ -114,6 +116,10 @@
 	type MaxLocks = ();
 	type MaxReserves = MaxReserves;
 	type ReserveIdentifier = [u8; 8];
+	type HoldIdentifier = [u8; 16];
+	type FreezeIdentifier = [u8; 16];
+	type MaxHolds = MaxHolds;
+	type MaxFreezes = MaxFreezes;
 }
 
 pub struct Author4;
@@ -209,6 +215,7 @@
 	pub const MaxXcmAllowedLocations: u32 = 16;
 	pub AppPromotionDailyRate: Perbill = Perbill::from_rational(5u32, 10_000);
 	pub const DayRelayBlocks: u32 = 1;
+	pub const LicenceBondIdentifier: [u8; 16] = *b"licenceidentifie";
 }
 
 impl pallet_configuration::Config for Test {
@@ -257,6 +264,7 @@
 	type ValidatorId = <Self as frame_system::Config>::AccountId;
 	type ValidatorIdOf = IdentityCollator;
 	type ValidatorRegistration = IsRegistered;
+	type LicenceBondIdentifier = LicenceBondIdentifier;
 	type WeightInfo = ();
 }
 
@@ -267,7 +275,9 @@
 		.unwrap();
 	let invulnerables = vec![1, 2];
 
-	let balances = vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100)];
+	let ed = <Test as pallet_balances::Config>::ExistentialDeposit::get();
+
+	let balances = vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100), (33, ed)];
 	let keys = balances
 		.iter()
 		.map(|&(i, _)| {
modifiedpallets/collator-selection/src/tests.rsdiffbeforeafterboth
--- a/pallets/collator-selection/src/tests.rs
+++ b/pallets/collator-selection/src/tests.rs
@@ -34,16 +34,16 @@
 use crate::{mock::*, Error};
 use frame_support::{
 	assert_noop, assert_ok,
-	traits::{Currency, GenesisBuild, OnInitialize},
+	traits::{fungible, GenesisBuild, OnInitialize},
 };
 use frame_system::RawOrigin;
-use pallet_balances::Error as BalancesError;
-use sp_runtime::traits::BadOrigin;
+use sp_runtime::{traits::BadOrigin, TokenError};
 use pallet_configuration::{
 	CollatorSelectionDesiredCollatorsOverride as DesiredCollators,
 	CollatorSelectionKickThresholdOverride as KickThreshold,
 	CollatorSelectionLicenseBondOverride as LicenseBond,
 };
+use scale_info::prelude::*;
 
 fn get_license_and_onboard(account_id: <Test as frame_system::Config>::AccountId) {
 	assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(
@@ -226,8 +226,9 @@
 #[test]
 fn cannot_obtain_license_if_poor() {
 	new_test_ext().execute_with(|| {
+		let ed = <Test as pallet_balances::Config>::ExistentialDeposit::get();
 		assert_eq!(Balances::free_balance(&3), 100);
-		assert_eq!(Balances::free_balance(&33), 0);
+		assert_eq!(Balances::free_balance(&33), ed);
 
 		// works
 		assert_ok!(CollatorSelection::get_license(RuntimeOrigin::signed(3)));
@@ -235,7 +236,7 @@
 		// poor
 		assert_noop!(
 			CollatorSelection::get_license(RuntimeOrigin::signed(33)),
-			BalancesError::<Test>::InsufficientBalance,
+			TokenError::FundsUnavailable,
 		);
 	});
 }
@@ -417,10 +418,7 @@
 fn authorship_event_handler() {
 	new_test_ext().execute_with(|| {
 		// put 100 in the pot + 5 for ED
-		<pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::set_balance(
-			&CollatorSelection::account_id(),
-			105,
-		);
+		<Balances as fungible::Mutate<_>>::set_balance(&CollatorSelection::account_id(), 105);
 
 		// 4 is the default author.
 		assert_eq!(Balances::free_balance(4), 100);
@@ -444,10 +442,7 @@
 		// Nothing panics, no reward when no ED in balance
 		Authorship::on_initialize(1);
 		// put some money into the pot at ED
-		<pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::set_balance(
-			&CollatorSelection::account_id(),
-			5,
-		);
+		<Balances as fungible::Mutate<_>>::set_balance(&CollatorSelection::account_id(), 5);
 		// 4 is the default author.
 		assert_eq!(Balances::free_balance(4), 100);
 		get_license_and_onboard(4);
modifiedpallets/foreign-assets/Cargo.tomldiffbeforeafterboth
--- a/pallets/foreign-assets/Cargo.toml
+++ b/pallets/foreign-assets/Cargo.toml
@@ -42,5 +42,6 @@
 	"sp-runtime/std",
 	"sp-std/std",
 	"up-data-structs/std",
+	"xcm-executor/std"
 ]
 try-runtime = ["frame-support/try-runtime"]
modifiedpallets/identity/Cargo.tomldiffbeforeafterboth
--- a/pallets/identity/Cargo.toml
+++ b/pallets/identity/Cargo.toml
@@ -46,5 +46,6 @@
 	"sp-io/std",
 	"sp-runtime/std",
 	"sp-std/std",
+	"pallet-balances/std",
 ]
 try-runtime = ["frame-support/try-runtime"]
modifiedpallets/identity/src/tests.rsdiffbeforeafterboth
--- a/pallets/identity/src/tests.rs
+++ b/pallets/identity/src/tests.rs
@@ -106,6 +106,10 @@
 	type MaxReserves = ();
 	type ReserveIdentifier = [u8; 8];
 	type WeightInfo = ();
+	type HoldIdentifier = ();
+	type FreezeIdentifier = ();
+	type MaxHolds = ();
+	type MaxFreezes = ();
 }
 
 parameter_types! {
modifiedpallets/inflation/src/lib.rsdiffbeforeafterboth
before · pallets/inflation/src/lib.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! # Inflation18//!19//! The inflation pallet is designed to increase the number of tokens at certain intervals.20//! With each iteration, increases the `total_issuance` value for the native token.21//! Executing an `on_initialize` hook at the beginning of each block, causing inflation to begin.22//!23//! ## Interface24//!25//! ### Dispatchable Functions26//!27//! * `start_inflation` - This method sets the inflation start date. Can be only called once.28//! Inflation start block can be backdated and will catch up. The method will create Treasury29//!	account if it does not exist and perform the first inflation deposit.3031// #![recursion_limit = "1024"]32#![cfg_attr(not(feature = "std"), no_std)]3334#[cfg(feature = "runtime-benchmarks")]35mod benchmarking;3637#[cfg(test)]38mod tests;3940use frame_support::{41	dispatch::{DispatchResult},42	traits::{43		fungible::{Balanced, Inspect, Mutate},44		Get, Imbalance,45		tokens::Precision,46	},47};48pub use pallet::*;49use sp_runtime::{50	Perbill,51	traits::{BlockNumberProvider, Zero},52};5354use sp_std::convert::TryInto;5556type BalanceOf<T> =57	<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;5859pub const YEAR: u32 = 5_259_600; // 6-second block60								 // pub const YEAR: u32 = 2_629_800; // 12-second block61pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;62pub const START_INFLATION_PERCENT: u32 = 10;63pub const END_INFLATION_PERCENT: u32 = 4;6465#[frame_support::pallet]66pub mod pallet {67	use super::*;68	use frame_support::pallet_prelude::*;69	use frame_system::pallet_prelude::*;7071	#[pallet::config]72	pub trait Config: frame_system::Config {73		type Currency: Balanced<Self::AccountId>74			+ Inspect<Self::AccountId>75			+ Mutate<Self::AccountId>;76		type TreasuryAccountId: Get<Self::AccountId>;7778		// The block number provider79		type BlockNumberProvider: BlockNumberProvider<BlockNumber = Self::BlockNumber>;8081		/// Number of blocks that pass between treasury balance updates due to inflation82		#[pallet::constant]83		type InflationBlockInterval: Get<Self::BlockNumber>;84	}8586	#[pallet::pallet]87	pub struct Pallet<T>(_);8889	/// starting year total issuance90	#[pallet::storage]91	pub type StartingYearTotalIssuance<T: Config> =92		StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;9394	/// Current inflation for `InflationBlockInterval` number of blocks95	#[pallet::storage]96	pub type BlockInflation<T: Config> = StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;9798	/// Next target (relay) block when inflation will be applied99	#[pallet::storage]100	pub type NextInflationBlock<T: Config> =101		StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;102103	/// Next target (relay) block when inflation is recalculated104	#[pallet::storage]105	pub type NextRecalculationBlock<T: Config> =106		StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;107108	/// Relay block when inflation has started109	#[pallet::storage]110	pub type StartBlock<T: Config> = StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;111112	#[pallet::hooks]113	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {114		fn on_initialize(_: T::BlockNumber) -> Weight115		where116			<T as frame_system::Config>::BlockNumber: From<u32>,117		{118			let mut consumed_weight = Weight::zero();119			let mut add_weight = |reads, writes, weight| {120				consumed_weight += T::DbWeight::get().reads_writes(reads, writes);121				consumed_weight += weight;122			};123124			let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);125			let current_relay_block = T::BlockNumberProvider::current_block_number();126			let next_inflation: T::BlockNumber = <NextInflationBlock<T>>::get();127			add_weight(1, 0, Weight::from_parts(5_000_000, 0));128129			// Apply inflation every InflationBlockInterval blocks130			// If next_inflation == 0, this means inflation wasn't yet initialized131			if (next_inflation != 0u32.into()) && (current_relay_block >= next_inflation) {132				// Recalculate inflation on the first block of the year (or if it is not initialized yet)133				// Do the "current_relay_block >= next_recalculation" check in the "current_relay_block >= next_inflation"134				// block because it saves InflationBlockInterval DB reads for NextRecalculationBlock.135				let next_recalculation: T::BlockNumber = <NextRecalculationBlock<T>>::get();136				add_weight(1, 0, Weight::zero());137				if current_relay_block >= next_recalculation {138					Self::recalculate_inflation(next_recalculation);139					add_weight(0, 4, Weight::from_parts(5_000_000, 0));140				}141142				T::Currency::mint_into(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get())143					.ok();144145				// Update inflation block146				<NextInflationBlock<T>>::set(next_inflation + block_interval.into());147148				add_weight(3, 3, Weight::from_parts(10_000_000, 0));149			}150151			consumed_weight152		}153	}154155	#[pallet::call]156	impl<T: Config> Pallet<T> {157		/// This method sets the inflation start date. Can be only called once.158		/// Inflation start block can be backdated and will catch up. The method will create Treasury159		/// account if it does not exist and perform the first inflation deposit.160		///161		/// # Permissions162		///163		/// * Root164		///165		/// # Arguments166		///167		/// * inflation_start_relay_block: The relay chain block at which inflation should start168		#[pallet::call_index(0)]169		#[pallet::weight(0)]170		pub fn start_inflation(171			origin: OriginFor<T>,172			inflation_start_relay_block: T::BlockNumber,173		) -> DispatchResult174		where175			<T as frame_system::Config>::BlockNumber: From<u32>,176		{177			ensure_root(origin)?;178179			// Start inflation if it has not been yet initialized180			if <StartBlock<T>>::get() == 0u32.into() {181				// Set inflation global start block182				<StartBlock<T>>::set(inflation_start_relay_block);183184				// Recalculate inflation. This can be backdated and will catch up.185				Self::recalculate_inflation(inflation_start_relay_block);186				let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);187				<NextInflationBlock<T>>::set(inflation_start_relay_block + block_interval.into());188189				// First time deposit - create Treasury account so that we can call deposit_into_existing everywhere else190				let imbalance = T::Currency::deposit(191					&T::TreasuryAccountId::get(),192					<BlockInflation<T>>::get(),193					Precision::Exact,194				)?;195				debug_assert!(imbalance.peek().is_zero());196			}197198			Ok(())199		}200	}201}202203impl<T: Config> Pallet<T> {204	pub fn recalculate_inflation(recalculation_block: T::BlockNumber) {205		let current_year: u32 = ((recalculation_block - <StartBlock<T>>::get())206			/ T::BlockNumber::from(YEAR))207		.try_into()208		.unwrap_or(0);209		let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);210211		let one_percent = Perbill::from_percent(1);212213		if current_year <= TOTAL_YEARS_UNTIL_FLAT {214			let amount: BalanceOf<T> = Perbill::from_rational(215				block_interval216					* (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT217						- current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),218				YEAR * TOTAL_YEARS_UNTIL_FLAT,219			) * (one_percent * T::Currency::total_issuance());220			<BlockInflation<T>>::put(amount);221		} else {222			let amount: BalanceOf<T> =223				Perbill::from_rational(block_interval * END_INFLATION_PERCENT, YEAR)224					* (one_percent * T::Currency::total_issuance());225			<BlockInflation<T>>::put(amount);226		}227		<StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());228229		// Update recalculation and inflation blocks230		<NextRecalculationBlock<T>>::set(recalculation_block + YEAR.into());231	}232}
modifiedpallets/inflation/src/tests.rsdiffbeforeafterboth
--- a/pallets/inflation/src/tests.rs
+++ b/pallets/inflation/src/tests.rs
@@ -20,7 +20,7 @@
 
 use frame_support::{
 	assert_ok, parameter_types,
-	traits::{Currency, OnInitialize, Everything, ConstU32},
+	traits::{fungible::{Balanced, Inspect}, OnInitialize, Everything, ConstU32, tokens::Precision},
 	weights::Weight,
 };
 use frame_system::RawOrigin;
@@ -53,6 +53,10 @@
 	type MaxLocks = MaxLocks;
 	type MaxReserves = ();
 	type ReserveIdentifier = ();
+	type HoldIdentifier = ();
+	type FreezeIdentifier = ();
+	type MaxHolds = ();
+	type MaxFreezes = ();
 }
 
 frame_support::construct_runtime!(
@@ -141,7 +145,7 @@
 fn uninitialized_inflation() {
 	new_test_ext().execute_with(|| {
 		let initial_issuance: u64 = 1_000_000_000;
-		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
 		assert_eq!(Balances::free_balance(1234), initial_issuance);
 
 		// BlockInflation should be set after inflation is started
@@ -157,7 +161,7 @@
 	new_test_ext().execute_with(|| {
 		// Total issuance = 1_000_000_000
 		let initial_issuance: u64 = 1_000_000_000;
-		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
 		assert_eq!(Balances::free_balance(1234), initial_issuance);
 
 		// BlockInflation should be set after inflation is started
@@ -187,7 +191,7 @@
 	new_test_ext().execute_with(|| {
 		// Total issuance = 1_000_000_000
 		let initial_issuance: u64 = 1_000_000_000;
-		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
 		assert_eq!(Balances::free_balance(1234), initial_issuance);
 		MockBlockNumberProvider::set(1);
 
@@ -218,7 +222,7 @@
 	new_test_ext().execute_with(|| {
 		// Total issuance = 1_000_000_000
 		let initial_issuance: u64 = 1_000_000_000;
-		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
 		assert_eq!(Balances::free_balance(1234), initial_issuance);
 		MockBlockNumberProvider::set(1);
 
@@ -234,7 +238,7 @@
 		}
 		assert_eq!(
 			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),
-			<Balances as Currency<_>>::total_issuance()
+			<Balances as Inspect<_>>::total_issuance()
 		);
 
 		MockBlockNumberProvider::set(YEAR + 1);
@@ -254,7 +258,7 @@
 		// Total issuance = 1_000_000_000
 		let initial_issuance: u64 = 1_000_000_000;
 		let start_block: u64 = 10457457;
-		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
 		assert_eq!(Balances::free_balance(1234), initial_issuance);
 		MockBlockNumberProvider::set(start_block);
 
@@ -273,7 +277,7 @@
 		}
 		assert_eq!(
 			initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),
-			<Balances as Currency<_>>::total_issuance()
+			<Balances as Inspect<_>>::total_issuance()
 		);
 
 		MockBlockNumberProvider::set(start_block + YEAR + 1);
@@ -292,7 +296,7 @@
 	new_test_ext().execute_with(|| {
 		// Total issuance = 1_000_000_000
 		let initial_issuance: u64 = 1_000_000_000;
-		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
 		assert_eq!(Balances::free_balance(1234), initial_issuance);
 		MockBlockNumberProvider::set(YEAR * 9 + 1);
 
@@ -327,7 +331,7 @@
 
 		// For accuracy total issuance = payout0 * payouts * 10;
 		let initial_issuance: u64 = payout_by_year[0] * payouts * 10;
-		let _ = <Balances as Currency<_>>::deposit_creating(&1234, initial_issuance);
+		let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
 		assert_eq!(Balances::free_balance(1234), initial_issuance);
 
 		// Start inflation as sudo