mod.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //! Mock runtime for the module testing.
  2. //!
  3. //! Submodules:
  4. //! - stakes: contains support for mocking external 'stake' module
  5. //! - balance_restorator: restores balances after unstaking
  6. //! - proposals: provides types for proposal execution tests
  7. //!
  8. #![cfg(test)]
  9. use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
  10. use sp_core::H256;
  11. use sp_runtime::{
  12. testing::Header,
  13. traits::{BlakeTwo256, IdentityLookup},
  14. Perbill,
  15. };
  16. pub use system;
  17. mod balance_manager;
  18. pub(crate) mod proposals;
  19. mod stakes;
  20. use balance_manager::*;
  21. pub use proposals::*;
  22. pub use stakes::*;
  23. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
  24. #[derive(Clone, PartialEq, Eq, Debug)]
  25. pub struct Test;
  26. impl_outer_origin! {
  27. pub enum Origin for Test {}
  28. }
  29. mod engine {
  30. pub use crate::Event;
  31. }
  32. mod membership_mod {
  33. pub use membership::Event;
  34. }
  35. impl_outer_event! {
  36. pub enum TestEvent for Test {
  37. balances<T>,
  38. engine<T>,
  39. membership_mod<T>,
  40. system<T>,
  41. }
  42. }
  43. parameter_types! {
  44. pub const ExistentialDeposit: u32 = 0;
  45. }
  46. impl balances::Trait for Test {
  47. type Balance = u64;
  48. type DustRemoval = ();
  49. type Event = TestEvent;
  50. type ExistentialDeposit = ExistentialDeposit;
  51. type AccountStore = System;
  52. }
  53. impl common::currency::GovernanceCurrency for Test {
  54. type Currency = balances::Module<Self>;
  55. }
  56. impl proposals::Trait for Test {}
  57. impl stake::Trait for Test {
  58. type Currency = Balances;
  59. type StakePoolId = StakePoolId;
  60. type StakingEventsHandler = BalanceManagerStakingEventsHandler;
  61. type StakeId = u64;
  62. type SlashId = u64;
  63. }
  64. parameter_types! {
  65. pub const CancellationFee: u64 = 5;
  66. pub const RejectionFee: u64 = 3;
  67. pub const TitleMaxLength: u32 = 100;
  68. pub const DescriptionMaxLength: u32 = 10000;
  69. pub const MaxActiveProposalLimit: u32 = 100;
  70. }
  71. impl membership::Trait for Test {
  72. type Event = TestEvent;
  73. type MemberId = u64;
  74. type PaidTermId = u64;
  75. type SubscriptionId = u64;
  76. type ActorId = u64;
  77. }
  78. impl crate::Trait for Test {
  79. type Event = TestEvent;
  80. type ProposerOriginValidator = ();
  81. type VoterOriginValidator = ();
  82. type TotalVotersCounter = ();
  83. type ProposalId = u32;
  84. type StakeHandlerProvider = stakes::TestStakeHandlerProvider;
  85. type CancellationFee = CancellationFee;
  86. type RejectionFee = RejectionFee;
  87. type TitleMaxLength = TitleMaxLength;
  88. type DescriptionMaxLength = DescriptionMaxLength;
  89. type MaxActiveProposalLimit = MaxActiveProposalLimit;
  90. type DispatchableCallCode = proposals::Call<Test>;
  91. }
  92. impl Default for proposals::Call<Test> {
  93. fn default() -> Self {
  94. panic!("shouldn't call default for Call");
  95. }
  96. }
  97. impl common::origin::ActorOriginValidator<Origin, u64, u64> for () {
  98. fn ensure_actor_origin(origin: Origin, _account_id: u64) -> Result<u64, &'static str> {
  99. let signed_account_id = system::ensure_signed(origin)?;
  100. Ok(signed_account_id)
  101. }
  102. }
  103. // If changing count is required, we can upgrade the implementation as shown here:
  104. // https://substrate.dev/recipes/3-entrees/testing/externalities.html
  105. impl crate::VotersParameters for () {
  106. fn total_voters_count() -> u32 {
  107. 4
  108. }
  109. }
  110. parameter_types! {
  111. pub const BlockHashCount: u64 = 250;
  112. pub const MaximumBlockWeight: u32 = 1024;
  113. pub const MaximumBlockLength: u32 = 2 * 1024;
  114. pub const AvailableBlockRatio: Perbill = Perbill::one();
  115. pub const MinimumPeriod: u64 = 5;
  116. pub const StakePoolId: [u8; 8] = *b"joystake";
  117. }
  118. impl system::Trait for Test {
  119. type BaseCallFilter = ();
  120. type Origin = Origin;
  121. type Call = ();
  122. type Index = u64;
  123. type BlockNumber = u64;
  124. type Hash = H256;
  125. type Hashing = BlakeTwo256;
  126. type AccountId = u64;
  127. type Lookup = IdentityLookup<Self::AccountId>;
  128. type Header = Header;
  129. type Event = TestEvent;
  130. type BlockHashCount = BlockHashCount;
  131. type MaximumBlockWeight = MaximumBlockWeight;
  132. type DbWeight = ();
  133. type BlockExecutionWeight = ();
  134. type ExtrinsicBaseWeight = ();
  135. type MaximumExtrinsicWeight = ();
  136. type MaximumBlockLength = MaximumBlockLength;
  137. type AvailableBlockRatio = AvailableBlockRatio;
  138. type Version = ();
  139. type ModuleToIndex = ();
  140. type AccountData = balances::AccountData<u64>;
  141. type OnNewAccount = ();
  142. type OnKilledAccount = ();
  143. }
  144. impl pallet_timestamp::Trait for Test {
  145. type Moment = u64;
  146. type OnTimestampSet = ();
  147. type MinimumPeriod = MinimumPeriod;
  148. }
  149. pub fn initial_test_ext() -> sp_io::TestExternalities {
  150. let t = system::GenesisConfig::default()
  151. .build_storage::<Test>()
  152. .unwrap();
  153. t.into()
  154. }
  155. pub type ProposalsEngine = crate::Module<Test>;
  156. pub type System = system::Module<Test>;
  157. pub type Balances = balances::Module<Test>;