mod.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. pub use primitives::{Blake2Hasher, H256};
  10. pub use sr_primitives::{
  11. testing::{Digest, DigestItem, Header, UintAuthorityId},
  12. traits::{BlakeTwo256, Convert, IdentityLookup, OnFinalize, Zero},
  13. weights::Weight,
  14. BuildStorage, DispatchError, Perbill,
  15. };
  16. use srml_support::{impl_outer_event, impl_outer_origin, parameter_types};
  17. pub use system;
  18. mod balance_manager;
  19. pub(crate) mod proposals;
  20. mod stakes;
  21. use balance_manager::*;
  22. pub use proposals::*;
  23. pub use stakes::*;
  24. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
  25. #[derive(Clone, PartialEq, Eq, Debug)]
  26. pub struct Test;
  27. impl_outer_origin! {
  28. pub enum Origin for Test {}
  29. }
  30. mod engine {
  31. pub use crate::Event;
  32. }
  33. mod membership_mod {
  34. pub use membership::members::Event;
  35. }
  36. mod council_mod {
  37. pub use governance::council::Event;
  38. }
  39. // impl_outer_dispatch! {
  40. // pub enum Call for Test where origin: Origin {
  41. // engine::ProposalsEngine,
  42. // }
  43. // }
  44. impl_outer_event! {
  45. pub enum TestEvent for Test {
  46. balances<T>,
  47. engine<T>,
  48. membership_mod<T>,
  49. council_mod<T>,
  50. }
  51. }
  52. parameter_types! {
  53. pub const ExistentialDeposit: u32 = 0;
  54. pub const TransferFee: u32 = 0;
  55. pub const CreationFee: u32 = 0;
  56. }
  57. impl balances::Trait for Test {
  58. /// The type for recording an account's balance.
  59. type Balance = u64;
  60. /// What to do if an account's free balance gets zeroed.
  61. type OnFreeBalanceZero = ();
  62. /// What to do if a new account is created.
  63. type OnNewAccount = ();
  64. type TransferPayment = ();
  65. type DustRemoval = ();
  66. type Event = TestEvent;
  67. type ExistentialDeposit = ExistentialDeposit;
  68. type TransferFee = TransferFee;
  69. type CreationFee = CreationFee;
  70. }
  71. impl common::currency::GovernanceCurrency for Test {
  72. type Currency = balances::Module<Self>;
  73. }
  74. impl governance::council::Trait for Test {
  75. type Event = TestEvent;
  76. type CouncilTermEnded = ();
  77. }
  78. impl proposals::Trait for Test {}
  79. impl stake::Trait for Test {
  80. type Currency = Balances;
  81. type StakePoolId = StakePoolId;
  82. type StakingEventsHandler = BalanceManagerStakingEventsHandler;
  83. type StakeId = u64;
  84. type SlashId = u64;
  85. }
  86. parameter_types! {
  87. pub const CancellationFee: u64 = 5;
  88. pub const RejectionFee: u64 = 3;
  89. pub const TitleMaxLength: u32 = 100;
  90. pub const DescriptionMaxLength: u32 = 10000;
  91. pub const MaxActiveProposalLimit: u32 = 100;
  92. }
  93. impl membership::members::Trait for Test {
  94. type Event = TestEvent;
  95. type MemberId = u64;
  96. type PaidTermId = u64;
  97. type SubscriptionId = u64;
  98. type ActorId = u64;
  99. type InitialMembersBalance = ();
  100. }
  101. impl crate::Trait for Test {
  102. type Event = TestEvent;
  103. type ProposerOriginValidator = ();
  104. type VoterOriginValidator = ();
  105. type TotalVotersCounter = ();
  106. type ProposalId = u32;
  107. type StakeHandlerProvider = stakes::TestStakeHandlerProvider;
  108. type CancellationFee = CancellationFee;
  109. type RejectionFee = RejectionFee;
  110. type TitleMaxLength = TitleMaxLength;
  111. type DescriptionMaxLength = DescriptionMaxLength;
  112. type MaxActiveProposalLimit = MaxActiveProposalLimit;
  113. type ProposalCode = proposals::Call<Test>;
  114. }
  115. impl Default for proposals::Call<Test> {
  116. fn default() -> Self {
  117. panic!("shouldn't call default for Call");
  118. }
  119. }
  120. impl common::origin_validator::ActorOriginValidator<Origin, u64, u64> for () {
  121. fn ensure_actor_origin(origin: Origin, _account_id: u64) -> Result<u64, &'static str> {
  122. let signed_account_id = system::ensure_signed(origin)?;
  123. Ok(signed_account_id)
  124. }
  125. }
  126. // If changing count is required, we can upgrade the implementation as shown here:
  127. // https://substrate.dev/recipes/3-entrees/testing/externalities.html
  128. impl crate::VotersParameters for () {
  129. fn total_voters_count() -> u32 {
  130. 4
  131. }
  132. }
  133. parameter_types! {
  134. pub const BlockHashCount: u64 = 250;
  135. pub const MaximumBlockWeight: u32 = 1024;
  136. pub const MaximumBlockLength: u32 = 2 * 1024;
  137. pub const AvailableBlockRatio: Perbill = Perbill::one();
  138. pub const MinimumPeriod: u64 = 5;
  139. pub const StakePoolId: [u8; 8] = *b"joystake";
  140. }
  141. impl system::Trait for Test {
  142. type Origin = Origin;
  143. type Call = ();
  144. type Index = u64;
  145. type BlockNumber = u64;
  146. type Hash = H256;
  147. type Hashing = BlakeTwo256;
  148. type AccountId = u64;
  149. type Lookup = IdentityLookup<Self::AccountId>;
  150. type Header = Header;
  151. type Event = TestEvent;
  152. type BlockHashCount = BlockHashCount;
  153. type MaximumBlockWeight = MaximumBlockWeight;
  154. type MaximumBlockLength = MaximumBlockLength;
  155. type AvailableBlockRatio = AvailableBlockRatio;
  156. type Version = ();
  157. }
  158. impl timestamp::Trait for Test {
  159. type Moment = u64;
  160. type OnTimestampSet = ();
  161. type MinimumPeriod = MinimumPeriod;
  162. }
  163. pub fn initial_test_ext() -> runtime_io::TestExternalities {
  164. let t = system::GenesisConfig::default()
  165. .build_storage::<Test>()
  166. .unwrap();
  167. t.into()
  168. }
  169. pub type ProposalsEngine = crate::Module<Test>;
  170. pub type System = system::Module<Test>;
  171. pub type Balances = balances::Module<Test>;