mock.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. use crate::{BalanceOf, Module, NegativeImbalance, Trait};
  2. use common::constraints::InputValidationLengthConstraint;
  3. use primitives::H256;
  4. use sr_primitives::{
  5. testing::Header,
  6. traits::{BlakeTwo256, IdentityLookup},
  7. Perbill,
  8. };
  9. use srml_support::{
  10. impl_outer_event, impl_outer_origin, parameter_types, StorageLinkedMap, StorageMap,
  11. };
  12. use std::marker::PhantomData;
  13. impl_outer_origin! {
  14. pub enum Origin for Test {}
  15. }
  16. mod working_group {
  17. pub use super::TestWorkingGroupInstance;
  18. pub use crate::Event;
  19. }
  20. mod membership_mod {
  21. pub use membership::Event;
  22. }
  23. impl_outer_event! {
  24. pub enum TestEvent for Test {
  25. balances<T>,
  26. working_group TestWorkingGroupInstance <T>,
  27. membership_mod<T>,
  28. }
  29. }
  30. parameter_types! {
  31. pub const BlockHashCount: u64 = 250;
  32. pub const MaximumBlockWeight: u32 = 1024;
  33. pub const MaximumBlockLength: u32 = 2 * 1024;
  34. pub const AvailableBlockRatio: Perbill = Perbill::one();
  35. pub const MinimumPeriod: u64 = 5;
  36. pub const StakePoolId: [u8; 8] = *b"joystake";
  37. pub const ExistentialDeposit: u32 = 0;
  38. pub const TransferFee: u32 = 0;
  39. pub const CreationFee: 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 Origin = Origin;
  46. type Call = ();
  47. type Index = u64;
  48. type BlockNumber = u64;
  49. type Hash = H256;
  50. type Hashing = BlakeTwo256;
  51. type AccountId = u64;
  52. type Lookup = IdentityLookup<Self::AccountId>;
  53. type Header = Header;
  54. type Event = TestEvent;
  55. type BlockHashCount = BlockHashCount;
  56. type MaximumBlockWeight = MaximumBlockWeight;
  57. type MaximumBlockLength = MaximumBlockLength;
  58. type AvailableBlockRatio = AvailableBlockRatio;
  59. type Version = ();
  60. }
  61. impl hiring::Trait for Test {
  62. type OpeningId = u64;
  63. type ApplicationId = u64;
  64. type ApplicationDeactivatedHandler = ();
  65. type StakeHandlerProvider = hiring::Module<Self>;
  66. }
  67. impl minting::Trait for Test {
  68. type Currency = Balances;
  69. type MintId = u64;
  70. }
  71. impl stake::Trait for Test {
  72. type Currency = Balances;
  73. type StakePoolId = StakePoolId;
  74. type StakingEventsHandler = StakingEventsHandler<Test>;
  75. type StakeId = u64;
  76. type SlashId = u64;
  77. }
  78. impl membership::Trait for Test {
  79. type Event = TestEvent;
  80. type MemberId = u64;
  81. type PaidTermId = u64;
  82. type SubscriptionId = u64;
  83. type ActorId = u64;
  84. }
  85. impl common::currency::GovernanceCurrency for Test {
  86. type Currency = Balances;
  87. }
  88. impl timestamp::Trait for Test {
  89. type Moment = u64;
  90. type OnTimestampSet = ();
  91. type MinimumPeriod = MinimumPeriod;
  92. }
  93. impl balances::Trait for Test {
  94. type Balance = u64;
  95. type OnFreeBalanceZero = ();
  96. type OnNewAccount = ();
  97. type TransferPayment = ();
  98. type DustRemoval = ();
  99. type Event = TestEvent;
  100. type ExistentialDeposit = ExistentialDeposit;
  101. type TransferFee = TransferFee;
  102. type CreationFee = CreationFee;
  103. }
  104. impl recurringrewards::Trait for Test {
  105. type PayoutStatusHandler = ();
  106. type RecipientId = u64;
  107. type RewardRelationshipId = u64;
  108. }
  109. pub type Balances = balances::Module<Test>;
  110. pub type System = system::Module<Test>;
  111. parameter_types! {
  112. pub const MaxWorkerNumberLimit: u32 = 3;
  113. }
  114. impl Trait<TestWorkingGroupInstance> for Test {
  115. type Event = TestEvent;
  116. type MaxWorkerNumberLimit = MaxWorkerNumberLimit;
  117. }
  118. pub type Membership = membership::Module<Test>;
  119. pub type TestWorkingGroupInstance = crate::Instance1;
  120. pub type TestWorkingGroup = Module<Test, TestWorkingGroupInstance>;
  121. pub(crate) const WORKING_GROUP_MINT_CAPACITY: u64 = 40000;
  122. pub(crate) const WORKING_GROUP_CONSTRAINT_MIN: u16 = 1;
  123. pub(crate) const WORKING_GROUP_CONSTRAINT_DIFF: u16 = 40;
  124. pub fn build_test_externalities() -> runtime_io::TestExternalities {
  125. let mut t = system::GenesisConfig::default()
  126. .build_storage::<Test>()
  127. .unwrap();
  128. crate::GenesisConfig::<Test, TestWorkingGroupInstance> {
  129. phantom: Default::default(),
  130. storage_working_group_mint_capacity: WORKING_GROUP_MINT_CAPACITY,
  131. opening_human_readable_text_constraint: InputValidationLengthConstraint::new(
  132. WORKING_GROUP_CONSTRAINT_MIN,
  133. WORKING_GROUP_CONSTRAINT_DIFF,
  134. ),
  135. worker_application_human_readable_text_constraint: InputValidationLengthConstraint::new(
  136. WORKING_GROUP_CONSTRAINT_MIN,
  137. WORKING_GROUP_CONSTRAINT_DIFF,
  138. ),
  139. worker_exit_rationale_text_constraint: InputValidationLengthConstraint::new(
  140. WORKING_GROUP_CONSTRAINT_MIN,
  141. WORKING_GROUP_CONSTRAINT_DIFF,
  142. ),
  143. }
  144. .assimilate_storage(&mut t)
  145. .unwrap();
  146. t.into()
  147. }
  148. pub struct StakingEventsHandler<T> {
  149. pub marker: PhantomData<T>,
  150. }
  151. impl<T: stake::Trait + crate::Trait<TestWorkingGroupInstance>> stake::StakingEventsHandler<T>
  152. for StakingEventsHandler<T>
  153. {
  154. /// Unstake remaining sum back to the source_account_id
  155. fn unstaked(
  156. stake_id: &<T as stake::Trait>::StakeId,
  157. _unstaked_amount: BalanceOf<T>,
  158. remaining_imbalance: NegativeImbalance<T>,
  159. ) -> NegativeImbalance<T> {
  160. // Stake not related to a staked role managed by the hiring module.
  161. if !hiring::ApplicationIdByStakingId::<T>::exists(*stake_id) {
  162. return remaining_imbalance;
  163. }
  164. let hiring_application_id = hiring::ApplicationIdByStakingId::<T>::get(*stake_id);
  165. if crate::MemberIdByHiringApplicationId::<T, TestWorkingGroupInstance>::exists(
  166. hiring_application_id,
  167. ) {
  168. return <crate::Module<T, TestWorkingGroupInstance>>::refund_working_group_stake(
  169. *stake_id,
  170. remaining_imbalance,
  171. );
  172. }
  173. remaining_imbalance
  174. }
  175. /// Empty handler for slashing
  176. fn slashed(
  177. _: &<T as stake::Trait>::StakeId,
  178. _: Option<<T as stake::Trait>::SlashId>,
  179. _: BalanceOf<T>,
  180. _: BalanceOf<T>,
  181. remaining_imbalance: NegativeImbalance<T>,
  182. ) -> NegativeImbalance<T> {
  183. remaining_imbalance
  184. }
  185. }