mod.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. impl_outer_event! {
  37. pub enum TestEvent for Test {
  38. balances<T>,
  39. engine<T>,
  40. membership_mod<T>,
  41. }
  42. }
  43. parameter_types! {
  44. pub const ExistentialDeposit: u32 = 0;
  45. pub const TransferFee: u32 = 0;
  46. pub const CreationFee: u32 = 0;
  47. }
  48. impl balances::Trait for Test {
  49. /// The type for recording an account's balance.
  50. type Balance = u64;
  51. /// What to do if an account's free balance gets zeroed.
  52. type OnFreeBalanceZero = ();
  53. /// What to do if a new account is created.
  54. type OnNewAccount = ();
  55. type TransferPayment = ();
  56. type DustRemoval = ();
  57. type Event = TestEvent;
  58. type ExistentialDeposit = ExistentialDeposit;
  59. type TransferFee = TransferFee;
  60. type CreationFee = CreationFee;
  61. }
  62. impl common::currency::GovernanceCurrency for Test {
  63. type Currency = balances::Module<Self>;
  64. }
  65. impl proposals::Trait for Test {}
  66. impl stake::Trait for Test {
  67. type Currency = Balances;
  68. type StakePoolId = StakePoolId;
  69. type StakingEventsHandler = BalanceManagerStakingEventsHandler;
  70. type StakeId = u64;
  71. type SlashId = u64;
  72. }
  73. parameter_types! {
  74. pub const CancellationFee: u64 = 5;
  75. pub const RejectionFee: u64 = 3;
  76. pub const TitleMaxLength: u32 = 100;
  77. pub const DescriptionMaxLength: u32 = 10000;
  78. pub const MaxActiveProposalLimit: u32 = 100;
  79. }
  80. impl membership::members::Trait for Test {
  81. type Event = TestEvent;
  82. type MemberId = u64;
  83. type PaidTermId = u64;
  84. type SubscriptionId = u64;
  85. type ActorId = u64;
  86. type InitialMembersBalance = ();
  87. }
  88. impl crate::Trait for Test {
  89. type Event = TestEvent;
  90. type ProposerOriginValidator = ();
  91. type VoterOriginValidator = ();
  92. type TotalVotersCounter = ();
  93. type ProposalId = u32;
  94. type StakeHandlerProvider = stakes::TestStakeHandlerProvider;
  95. type CancellationFee = CancellationFee;
  96. type RejectionFee = RejectionFee;
  97. type TitleMaxLength = TitleMaxLength;
  98. type DescriptionMaxLength = DescriptionMaxLength;
  99. type MaxActiveProposalLimit = MaxActiveProposalLimit;
  100. type DispatchableCallCode = proposals::Call<Test>;
  101. }
  102. impl Default for proposals::Call<Test> {
  103. fn default() -> Self {
  104. panic!("shouldn't call default for Call");
  105. }
  106. }
  107. impl common::origin_validator::ActorOriginValidator<Origin, u64, u64> for () {
  108. fn ensure_actor_origin(origin: Origin, _account_id: u64) -> Result<u64, &'static str> {
  109. let signed_account_id = system::ensure_signed(origin)?;
  110. Ok(signed_account_id)
  111. }
  112. }
  113. // If changing count is required, we can upgrade the implementation as shown here:
  114. // https://substrate.dev/recipes/3-entrees/testing/externalities.html
  115. impl crate::VotersParameters for () {
  116. fn total_voters_count() -> u32 {
  117. 4
  118. }
  119. }
  120. parameter_types! {
  121. pub const BlockHashCount: u64 = 250;
  122. pub const MaximumBlockWeight: u32 = 1024;
  123. pub const MaximumBlockLength: u32 = 2 * 1024;
  124. pub const AvailableBlockRatio: Perbill = Perbill::one();
  125. pub const MinimumPeriod: u64 = 5;
  126. pub const StakePoolId: [u8; 8] = *b"joystake";
  127. }
  128. impl system::Trait for Test {
  129. type Origin = Origin;
  130. type Call = ();
  131. type Index = u64;
  132. type BlockNumber = u64;
  133. type Hash = H256;
  134. type Hashing = BlakeTwo256;
  135. type AccountId = u64;
  136. type Lookup = IdentityLookup<Self::AccountId>;
  137. type Header = Header;
  138. type Event = TestEvent;
  139. type BlockHashCount = BlockHashCount;
  140. type MaximumBlockWeight = MaximumBlockWeight;
  141. type MaximumBlockLength = MaximumBlockLength;
  142. type AvailableBlockRatio = AvailableBlockRatio;
  143. type Version = ();
  144. }
  145. impl timestamp::Trait for Test {
  146. type Moment = u64;
  147. type OnTimestampSet = ();
  148. type MinimumPeriod = MinimumPeriod;
  149. }
  150. pub fn initial_test_ext() -> runtime_io::TestExternalities {
  151. let t = system::GenesisConfig::default()
  152. .build_storage::<Test>()
  153. .unwrap();
  154. t.into()
  155. }
  156. pub type ProposalsEngine = crate::Module<Test>;
  157. pub type System = system::Module<Test>;
  158. pub type Balances = balances::Module<Test>;