mock.rs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. use frame_support::storage::StorageMap;
  2. use frame_support::traits::{OnFinalize, OnInitialize};
  3. use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
  4. use sp_core::H256;
  5. use sp_runtime::{
  6. testing::Header,
  7. traits::{BlakeTwo256, IdentityLookup},
  8. Perbill,
  9. };
  10. use std::marker::PhantomData;
  11. use system;
  12. use crate::{BalanceOf, Module, NegativeImbalance, Trait};
  13. use common::constraints::InputValidationLengthConstraint;
  14. impl_outer_origin! {
  15. pub enum Origin for Test {}
  16. }
  17. mod working_group {
  18. pub use super::TestWorkingGroupInstance;
  19. pub use crate::Event;
  20. }
  21. mod membership_mod {
  22. pub use membership::Event;
  23. }
  24. impl_outer_event! {
  25. pub enum TestEvent for Test {
  26. balances<T>,
  27. working_group TestWorkingGroupInstance <T>,
  28. membership_mod<T>,
  29. system<T>,
  30. }
  31. }
  32. parameter_types! {
  33. pub const BlockHashCount: u64 = 250;
  34. pub const MaximumBlockWeight: u32 = 1024;
  35. pub const MaximumBlockLength: u32 = 2 * 1024;
  36. pub const AvailableBlockRatio: Perbill = Perbill::one();
  37. pub const MinimumPeriod: u64 = 5;
  38. pub const StakePoolId: [u8; 8] = *b"joystake";
  39. pub const ExistentialDeposit: u32 = 0;
  40. }
  41. // Workaround for https://github.com/rust-lang/rust/issues/26925 - remove when sorted.
  42. #[derive(Clone, PartialEq, Eq, Debug)]
  43. pub struct Test;
  44. impl system::Trait for Test {
  45. type BaseCallFilter = ();
  46. type Origin = Origin;
  47. type Call = ();
  48. type Index = u64;
  49. type BlockNumber = u64;
  50. type Hash = H256;
  51. type Hashing = BlakeTwo256;
  52. type AccountId = u64;
  53. type Lookup = IdentityLookup<Self::AccountId>;
  54. type Header = Header;
  55. type Event = TestEvent;
  56. type BlockHashCount = BlockHashCount;
  57. type MaximumBlockWeight = MaximumBlockWeight;
  58. type DbWeight = ();
  59. type BlockExecutionWeight = ();
  60. type ExtrinsicBaseWeight = ();
  61. type MaximumExtrinsicWeight = ();
  62. type MaximumBlockLength = MaximumBlockLength;
  63. type AvailableBlockRatio = AvailableBlockRatio;
  64. type Version = ();
  65. type ModuleToIndex = ();
  66. type AccountData = balances::AccountData<u64>;
  67. type OnNewAccount = ();
  68. type OnKilledAccount = ();
  69. }
  70. impl hiring::Trait for Test {
  71. type OpeningId = u64;
  72. type ApplicationId = u64;
  73. type ApplicationDeactivatedHandler = ();
  74. type StakeHandlerProvider = hiring::Module<Self>;
  75. }
  76. impl minting::Trait for Test {
  77. type Currency = Balances;
  78. type MintId = u64;
  79. }
  80. impl stake::Trait for Test {
  81. type Currency = Balances;
  82. type StakePoolId = StakePoolId;
  83. type StakingEventsHandler = StakingEventsHandler<Test>;
  84. type StakeId = u64;
  85. type SlashId = u64;
  86. }
  87. impl membership::Trait for Test {
  88. type Event = TestEvent;
  89. type MemberId = u64;
  90. type PaidTermId = u64;
  91. type SubscriptionId = u64;
  92. type ActorId = u64;
  93. }
  94. impl common::currency::GovernanceCurrency for Test {
  95. type Currency = Balances;
  96. }
  97. impl pallet_timestamp::Trait for Test {
  98. type Moment = u64;
  99. type OnTimestampSet = ();
  100. type MinimumPeriod = MinimumPeriod;
  101. }
  102. impl balances::Trait for Test {
  103. type Balance = u64;
  104. type DustRemoval = ();
  105. type Event = TestEvent;
  106. type ExistentialDeposit = ExistentialDeposit;
  107. type AccountStore = System;
  108. }
  109. impl recurringrewards::Trait for Test {
  110. type PayoutStatusHandler = ();
  111. type RecipientId = u64;
  112. type RewardRelationshipId = u64;
  113. }
  114. pub type Balances = balances::Module<Test>;
  115. pub type System = system::Module<Test>;
  116. parameter_types! {
  117. pub const MaxWorkerNumberLimit: u32 = 3;
  118. }
  119. impl Trait<TestWorkingGroupInstance> for Test {
  120. type Event = TestEvent;
  121. type MaxWorkerNumberLimit = MaxWorkerNumberLimit;
  122. }
  123. pub type Membership = membership::Module<Test>;
  124. pub type TestWorkingGroupInstance = crate::Instance1;
  125. pub type TestWorkingGroup = Module<Test, TestWorkingGroupInstance>;
  126. pub(crate) const WORKING_GROUP_MINT_CAPACITY: u64 = 40000;
  127. pub(crate) const WORKING_GROUP_CONSTRAINT_MIN: u16 = 1;
  128. pub(crate) const WORKING_GROUP_CONSTRAINT_DIFF: u16 = 40;
  129. pub fn build_test_externalities() -> sp_io::TestExternalities {
  130. let mut t = system::GenesisConfig::default()
  131. .build_storage::<Test>()
  132. .unwrap();
  133. crate::GenesisConfig::<Test, TestWorkingGroupInstance> {
  134. phantom: Default::default(),
  135. working_group_mint_capacity: WORKING_GROUP_MINT_CAPACITY,
  136. opening_human_readable_text_constraint: InputValidationLengthConstraint::new(
  137. WORKING_GROUP_CONSTRAINT_MIN,
  138. WORKING_GROUP_CONSTRAINT_DIFF,
  139. ),
  140. worker_application_human_readable_text_constraint: InputValidationLengthConstraint::new(
  141. WORKING_GROUP_CONSTRAINT_MIN,
  142. WORKING_GROUP_CONSTRAINT_DIFF,
  143. ),
  144. worker_exit_rationale_text_constraint: InputValidationLengthConstraint::new(
  145. WORKING_GROUP_CONSTRAINT_MIN,
  146. WORKING_GROUP_CONSTRAINT_DIFF,
  147. ),
  148. worker_storage_size_constraint: crate::default_storage_size_constraint(),
  149. }
  150. .assimilate_storage(&mut t)
  151. .unwrap();
  152. t.into()
  153. }
  154. pub struct StakingEventsHandler<T> {
  155. pub marker: PhantomData<T>,
  156. }
  157. impl<T: stake::Trait + crate::Trait<TestWorkingGroupInstance>> stake::StakingEventsHandler<T>
  158. for StakingEventsHandler<T>
  159. {
  160. /// Unstake remaining sum back to the source_account_id
  161. fn unstaked(
  162. stake_id: &<T as stake::Trait>::StakeId,
  163. _unstaked_amount: BalanceOf<T>,
  164. remaining_imbalance: NegativeImbalance<T>,
  165. ) -> NegativeImbalance<T> {
  166. // Stake not related to a staked role managed by the hiring module.
  167. if !hiring::ApplicationIdByStakingId::<T>::contains_key(*stake_id) {
  168. return remaining_imbalance;
  169. }
  170. let hiring_application_id = hiring::ApplicationIdByStakingId::<T>::get(*stake_id);
  171. if crate::MemberIdByHiringApplicationId::<T, TestWorkingGroupInstance>::contains_key(
  172. hiring_application_id,
  173. ) {
  174. return <crate::Module<T, TestWorkingGroupInstance>>::refund_working_group_stake(
  175. *stake_id,
  176. remaining_imbalance,
  177. );
  178. }
  179. remaining_imbalance
  180. }
  181. /// Empty handler for slashing
  182. fn slashed(
  183. _: &<T as stake::Trait>::StakeId,
  184. _: Option<<T as stake::Trait>::SlashId>,
  185. _: BalanceOf<T>,
  186. _: BalanceOf<T>,
  187. remaining_imbalance: NegativeImbalance<T>,
  188. ) -> NegativeImbalance<T> {
  189. remaining_imbalance
  190. }
  191. }
  192. // Recommendation from Parity on testing on_finalize
  193. // https://substrate.dev/docs/en/next/development/module/tests
  194. pub fn run_to_block(n: u64) {
  195. while System::block_number() < n {
  196. <System as OnFinalize<u64>>::on_finalize(System::block_number());
  197. <TestWorkingGroup as OnFinalize<u64>>::on_finalize(System::block_number());
  198. System::set_block_number(System::block_number() + 1);
  199. <System as OnInitialize<u64>>::on_initialize(System::block_number());
  200. <TestWorkingGroup as OnInitialize<u64>>::on_initialize(System::block_number());
  201. }
  202. }