mock.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #![cfg(test)]
  2. use frame_support::traits::LockIdentifier;
  3. use frame_support::{impl_outer_dispatch, impl_outer_origin, parameter_types, weights::Weight};
  4. pub use frame_system;
  5. use sp_core::H256;
  6. use sp_runtime::curve::PiecewiseLinear;
  7. use sp_runtime::{
  8. testing::Header,
  9. traits::{BlakeTwo256, IdentityLookup},
  10. Perbill,
  11. };
  12. use sp_staking::SessionIndex;
  13. use crate::{ProposalDetailsOf, ProposalEncoder, ProposalParameters};
  14. use proposals_engine::VotersParameters;
  15. use sp_runtime::testing::TestXt;
  16. impl_outer_origin! {
  17. pub enum Origin for Test {}
  18. }
  19. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
  20. #[derive(Clone, PartialEq, Eq, Debug)]
  21. pub struct Test;
  22. parameter_types! {
  23. pub const BlockHashCount: u64 = 250;
  24. pub const MaximumBlockWeight: u32 = 1024;
  25. pub const MaximumBlockLength: u32 = 2 * 1024;
  26. pub const AvailableBlockRatio: Perbill = Perbill::one();
  27. pub const MinimumPeriod: u64 = 5;
  28. }
  29. impl_outer_dispatch! {
  30. pub enum Call for Test where origin: Origin {
  31. codex::ProposalCodex,
  32. proposals::ProposalsEngine,
  33. staking::Staking,
  34. frame_system::System,
  35. }
  36. }
  37. impl common::currency::GovernanceCurrency for Test {
  38. type Currency = balances::Module<Self>;
  39. }
  40. impl membership::Trait for Test {
  41. type Event = ();
  42. type MemberId = u64;
  43. type PaidTermId = u64;
  44. type SubscriptionId = u64;
  45. type ActorId = u64;
  46. }
  47. parameter_types! {
  48. pub const ExistentialDeposit: u32 = 0;
  49. }
  50. impl balances::Trait for Test {
  51. type Balance = u64;
  52. type DustRemoval = ();
  53. type Event = ();
  54. type ExistentialDeposit = ExistentialDeposit;
  55. type AccountStore = System;
  56. type WeightInfo = ();
  57. type MaxLocks = ();
  58. }
  59. parameter_types! {
  60. pub const CancellationFee: u64 = 5;
  61. pub const RejectionFee: u64 = 3;
  62. pub const TitleMaxLength: u32 = 100;
  63. pub const DescriptionMaxLength: u32 = 10000;
  64. pub const MaxActiveProposalLimit: u32 = 100;
  65. pub const LockId: LockIdentifier = [2; 8];
  66. }
  67. pub struct MockProposalsEngineWeight;
  68. impl proposals_engine::Trait for Test {
  69. type Event = ();
  70. type ProposerOriginValidator = ();
  71. type VoterOriginValidator = ();
  72. type TotalVotersCounter = MockVotersParameters;
  73. type ProposalId = u32;
  74. type StakingHandler = staking_handler::StakingManager<Test, LockId>;
  75. type CancellationFee = CancellationFee;
  76. type RejectionFee = RejectionFee;
  77. type TitleMaxLength = TitleMaxLength;
  78. type DescriptionMaxLength = DescriptionMaxLength;
  79. type MaxActiveProposalLimit = MaxActiveProposalLimit;
  80. type DispatchableCallCode = crate::Call<Test>;
  81. type ProposalObserver = crate::Module<Test>;
  82. type WeightInfo = MockProposalsEngineWeight;
  83. }
  84. impl proposals_engine::WeightInfo for MockProposalsEngineWeight {
  85. fn vote(_: u32) -> Weight {
  86. 0
  87. }
  88. fn cancel_proposal(_: u32) -> Weight {
  89. 0
  90. }
  91. fn veto_proposal() -> Weight {
  92. 0
  93. }
  94. fn on_initialize_immediate_execution_decode_fails(_: u32) -> Weight {
  95. 0
  96. }
  97. fn on_initialize_pending_execution_decode_fails(_: u32) -> Weight {
  98. 0
  99. }
  100. fn on_initialize_approved_pending_constitutionality(_: u32) -> Weight {
  101. 0
  102. }
  103. fn on_initialize_rejected(_: u32) -> Weight {
  104. 0
  105. }
  106. fn on_initialize_slashed(_: u32) -> Weight {
  107. 0
  108. }
  109. }
  110. impl Default for crate::Call<Test> {
  111. fn default() -> Self {
  112. panic!("shouldn't call default for Call");
  113. }
  114. }
  115. impl minting::Trait for Test {
  116. type Currency = Balances;
  117. type MintId = u64;
  118. }
  119. impl governance::council::Trait for Test {
  120. type Event = ();
  121. type CouncilTermEnded = ();
  122. }
  123. impl common::origin::ActorOriginValidator<Origin, u64, u64> for () {
  124. fn ensure_actor_origin(origin: Origin, _: u64) -> Result<u64, &'static str> {
  125. let account_id = frame_system::ensure_signed(origin)?;
  126. Ok(account_id)
  127. }
  128. }
  129. parameter_types! {
  130. pub const ThreadTitleLengthLimit: u32 = 200;
  131. pub const PostLengthLimit: u32 = 2000;
  132. pub const MaxWhiteListSize: u32 = 20;
  133. }
  134. pub struct MockProposalsDiscussionWeight;
  135. impl proposals_discussion::Trait for Test {
  136. type Event = ();
  137. type AuthorOriginValidator = ();
  138. type CouncilOriginValidator = ();
  139. type ThreadId = u64;
  140. type PostId = u64;
  141. type MaxWhiteListSize = MaxWhiteListSize;
  142. type WeightInfo = MockProposalsDiscussionWeight;
  143. }
  144. impl proposals_discussion::WeightInfo for MockProposalsDiscussionWeight {
  145. fn add_post(_: u32) -> Weight {
  146. 0
  147. }
  148. fn update_post() -> Weight {
  149. 0
  150. }
  151. fn change_thread_mode(_: u32) -> Weight {
  152. 0
  153. }
  154. }
  155. pub struct MockVotersParameters;
  156. impl VotersParameters for MockVotersParameters {
  157. fn total_voters_count() -> u32 {
  158. 4
  159. }
  160. }
  161. parameter_types! {
  162. pub const TextProposalMaxLength: u32 = 20_000;
  163. pub const RuntimeUpgradeWasmProposalMaxLength: u32 = 20_000;
  164. }
  165. impl governance::election::Trait for Test {
  166. type Event = ();
  167. type CouncilElected = ();
  168. }
  169. // The content directory working group instance alias.
  170. pub type ContentDirectoryWorkingGroupInstance = working_group::Instance3;
  171. // The storage working group instance alias.
  172. pub type StorageWorkingGroupInstance = working_group::Instance2;
  173. parameter_types! {
  174. pub const MaxWorkerNumberLimit: u32 = 100;
  175. pub const LockId1: [u8; 8] = [1; 8];
  176. pub const LockId2: [u8; 8] = [2; 8];
  177. }
  178. impl working_group::Trait<ContentDirectoryWorkingGroupInstance> for Test {
  179. type Event = ();
  180. type MaxWorkerNumberLimit = MaxWorkerNumberLimit;
  181. type StakingHandler = staking_handler::StakingManager<Self, LockId1>;
  182. type MemberOriginValidator = ();
  183. type MinUnstakingPeriodLimit = ();
  184. type RewardPeriod = ();
  185. }
  186. impl working_group::Trait<StorageWorkingGroupInstance> for Test {
  187. type Event = ();
  188. type MaxWorkerNumberLimit = MaxWorkerNumberLimit;
  189. type StakingHandler = staking_handler::StakingManager<Self, LockId2>;
  190. type MemberOriginValidator = ();
  191. type MinUnstakingPeriodLimit = ();
  192. type RewardPeriod = ();
  193. }
  194. impl recurring_rewards::Trait for Test {
  195. type PayoutStatusHandler = ();
  196. type RecipientId = u64;
  197. type RewardRelationshipId = u64;
  198. }
  199. pallet_staking_reward_curve::build! {
  200. const I_NPOS: PiecewiseLinear<'static> = curve!(
  201. min_inflation: 0_025_000,
  202. max_inflation: 0_100_000,
  203. ideal_stake: 0_500_000,
  204. falloff: 0_050_000,
  205. max_piece_count: 40,
  206. test_precision: 0_005_000,
  207. );
  208. }
  209. parameter_types! {
  210. pub const SessionsPerEra: SessionIndex = 3;
  211. pub const BondingDuration: staking::EraIndex = 3;
  212. pub const RewardCurve: &'static PiecewiseLinear<'static> = &I_NPOS;
  213. }
  214. impl staking::Trait for Test {
  215. type Currency = Balances;
  216. type UnixTime = Timestamp;
  217. type CurrencyToVote = ();
  218. type RewardRemainder = ();
  219. type Event = ();
  220. type Slash = ();
  221. type Reward = ();
  222. type SessionsPerEra = SessionsPerEra;
  223. type BondingDuration = BondingDuration;
  224. type SlashDeferDuration = ();
  225. type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
  226. type SessionInterface = Self;
  227. type RewardCurve = RewardCurve;
  228. type NextNewSession = ();
  229. type ElectionLookahead = ();
  230. type Call = Call;
  231. type MaxIterations = ();
  232. type MinSolutionScoreBump = ();
  233. type MaxNominatorRewardedPerValidator = ();
  234. type UnsignedPriority = ();
  235. type WeightInfo = ();
  236. }
  237. impl<LocalCall> frame_system::offchain::SendTransactionTypes<LocalCall> for Test
  238. where
  239. Call: From<LocalCall>,
  240. {
  241. type OverarchingCall = Call;
  242. type Extrinsic = Extrinsic;
  243. }
  244. pub type Extrinsic = TestXt<Call, ()>;
  245. impl staking::SessionInterface<u64> for Test {
  246. fn disable_validator(_: &u64) -> Result<bool, ()> {
  247. unimplemented!()
  248. }
  249. fn validators() -> Vec<u64> {
  250. unimplemented!()
  251. }
  252. fn prune_historical_up_to(_: u32) {
  253. unimplemented!()
  254. }
  255. }
  256. parameter_types! {
  257. pub DefaultProposalParameters: ProposalParameters<u64, u64> = default_proposal_parameters();
  258. }
  259. pub(crate) fn default_proposal_parameters() -> ProposalParameters<u64, u64> {
  260. ProposalParameters {
  261. voting_period: 43200,
  262. grace_period: 0,
  263. approval_quorum_percentage: 66,
  264. approval_threshold_percentage: 80,
  265. slashing_quorum_percentage: 60,
  266. slashing_threshold_percentage: 80,
  267. required_stake: Some(100_000),
  268. constitutionality: 1,
  269. }
  270. }
  271. impl crate::Trait for Test {
  272. type TextProposalMaxLength = TextProposalMaxLength;
  273. type RuntimeUpgradeWasmProposalMaxLength = RuntimeUpgradeWasmProposalMaxLength;
  274. type MembershipOriginValidator = ();
  275. type ProposalEncoder = ();
  276. type SetValidatorCountProposalParameters = DefaultProposalParameters;
  277. type RuntimeUpgradeProposalParameters = DefaultProposalParameters;
  278. type TextProposalParameters = DefaultProposalParameters;
  279. type SpendingProposalParameters = DefaultProposalParameters;
  280. type AddWorkingGroupOpeningProposalParameters = DefaultProposalParameters;
  281. type BeginReviewWorkingGroupApplicationsProposalParameters = DefaultProposalParameters;
  282. type FillWorkingGroupOpeningProposalParameters = DefaultProposalParameters;
  283. type SetWorkingGroupBudgetCapacityProposalParameters = DefaultProposalParameters;
  284. type DecreaseWorkingGroupLeaderStakeProposalParameters = DefaultProposalParameters;
  285. type SlashWorkingGroupLeaderStakeProposalParameters = DefaultProposalParameters;
  286. type SetWorkingGroupLeaderRewardProposalParameters = DefaultProposalParameters;
  287. type TerminateWorkingGroupLeaderRoleProposalParameters = DefaultProposalParameters;
  288. type AmendConstitutionProposalParameters = DefaultProposalParameters;
  289. }
  290. impl ProposalEncoder<Test> for () {
  291. fn encode_proposal(_proposal_details: ProposalDetailsOf<Test>) -> Vec<u8> {
  292. Vec::new()
  293. }
  294. }
  295. impl frame_system::Trait for Test {
  296. type BaseCallFilter = ();
  297. type Origin = Origin;
  298. type Call = Call;
  299. type Index = u64;
  300. type BlockNumber = u64;
  301. type Hash = H256;
  302. type Hashing = BlakeTwo256;
  303. type AccountId = u64;
  304. type Lookup = IdentityLookup<Self::AccountId>;
  305. type Header = Header;
  306. type Event = ();
  307. type BlockHashCount = BlockHashCount;
  308. type MaximumBlockWeight = MaximumBlockWeight;
  309. type DbWeight = ();
  310. type BlockExecutionWeight = ();
  311. type ExtrinsicBaseWeight = ();
  312. type MaximumExtrinsicWeight = ();
  313. type MaximumBlockLength = MaximumBlockLength;
  314. type AvailableBlockRatio = AvailableBlockRatio;
  315. type Version = ();
  316. type PalletInfo = ();
  317. type AccountData = balances::AccountData<u64>;
  318. type OnNewAccount = ();
  319. type OnKilledAccount = ();
  320. type SystemWeightInfo = ();
  321. }
  322. impl pallet_timestamp::Trait for Test {
  323. type Moment = u64;
  324. type OnTimestampSet = ();
  325. type MinimumPeriod = MinimumPeriod;
  326. type WeightInfo = ();
  327. }
  328. pub fn initial_test_ext() -> sp_io::TestExternalities {
  329. let t = frame_system::GenesisConfig::default()
  330. .build_storage::<Test>()
  331. .unwrap();
  332. t.into()
  333. }
  334. pub type Staking = staking::Module<Test>;
  335. pub type ProposalCodex = crate::Module<Test>;
  336. pub type ProposalsEngine = proposals_engine::Module<Test>;
  337. pub type Balances = balances::Module<Test>;
  338. pub type Timestamp = pallet_timestamp::Module<Test>;
  339. pub type System = frame_system::Module<Test>;