瀏覽代碼

runtime: staking-handler: Add mocks

Shamil Gadelshin 4 年之前
父節點
當前提交
46280db4d4

+ 6 - 0
Cargo.lock

@@ -7375,8 +7375,14 @@ dependencies = [
  "frame-support",
  "frame-system",
  "pallet-balances",
+ "pallet-common",
  "pallet-membership",
+ "pallet-timestamp",
+ "parity-scale-codec",
  "sp-arithmetic",
+ "sp-core",
+ "sp-io",
+ "sp-runtime",
  "sp-std",
 ]
 

+ 8 - 0
runtime-modules/staking-handler/Cargo.toml

@@ -12,6 +12,14 @@ sp-arithmetic = { package = 'sp-arithmetic', default-features = false, git = 'ht
 pallet-balances = { package = 'pallet-balances', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 membership = { package = 'pallet-membership', default-features = false, path = '../membership'}
 
+[dev-dependencies]
+sp-io = { package = 'sp-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
+sp-core = { package = 'sp-core', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
+codec = { package = 'parity-scale-codec', version = '1.3.1', default-features = false, features = ['derive'] }
+sp-runtime = { package = 'sp-runtime', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
+common = { package = 'pallet-common', default-features = false, path = '../common'}
+pallet-timestamp = { package = 'pallet-timestamp', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
+
 [features]
 default = ['std']
 std = [

+ 4 - 1
runtime-modules/staking-handler/src/lib.rs

@@ -1,7 +1,7 @@
 //! Staking handler module.
 //! Contains StakingHandler trait and its implementation - StakingManager.
 //! StakingHandler is responsible for staking logic in the Joystream runtime:
-//! https://joystream.gitbook.io/joystream-handbook/key-concepts/staking
+//! https://joystream.gitbook.io/joystream-handbook/key-concepts/stakingmock.rs
 
 // Ensure we're `no_std` when compiling for Wasm.
 #![cfg_attr(not(feature = "std"), no_std)]
@@ -11,6 +11,9 @@ use frame_support::traits::{Currency, Get, LockIdentifier, LockableCurrency, Wit
 use sp_arithmetic::traits::Zero;
 use sp_std::marker::PhantomData;
 
+#[cfg(test)]
+mod mock;
+
 /// Type alias for member id.
 pub type MemberId<T> = <T as membership::Trait>::MemberId;
 

+ 97 - 0
runtime-modules/staking-handler/src/mock.rs

@@ -0,0 +1,97 @@
+use frame_support::{impl_outer_origin, parameter_types};
+use frame_system;
+use sp_core::H256;
+use sp_runtime::{
+    testing::Header,
+    traits::{BlakeTwo256, IdentityLookup},
+    Perbill,
+};
+
+impl_outer_origin! {
+    pub enum Origin for Test {}
+}
+
+mod membership_mod {
+    pub use membership::Event;
+}
+
+parameter_types! {
+    pub const BlockHashCount: u64 = 250;
+    pub const MaximumBlockWeight: u32 = 1024;
+    pub const MaximumBlockLength: u32 = 2 * 1024;
+    pub const AvailableBlockRatio: Perbill = Perbill::one();
+    pub const MinimumPeriod: u64 = 5;
+    pub const StakePoolId: [u8; 8] = *b"joystake";
+    pub const ExistentialDeposit: u32 = 0;
+}
+
+// Workaround for https://github.com/rust-lang/rust/issues/26925 - remove when sorted.
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct Test;
+
+impl frame_system::Trait for Test {
+    type BaseCallFilter = ();
+    type Origin = Origin;
+    type Call = ();
+    type Index = u64;
+    type BlockNumber = u64;
+    type Hash = H256;
+    type Hashing = BlakeTwo256;
+    type AccountId = u64;
+    type Lookup = IdentityLookup<Self::AccountId>;
+    type Header = Header;
+    type Event = ();
+    type BlockHashCount = BlockHashCount;
+    type MaximumBlockWeight = MaximumBlockWeight;
+    type DbWeight = ();
+    type BlockExecutionWeight = ();
+    type ExtrinsicBaseWeight = ();
+    type MaximumExtrinsicWeight = ();
+    type MaximumBlockLength = MaximumBlockLength;
+    type AvailableBlockRatio = AvailableBlockRatio;
+    type Version = ();
+    type PalletInfo = ();
+    type AccountData = pallet_balances::AccountData<u64>;
+    type OnNewAccount = ();
+    type OnKilledAccount = ();
+    type SystemWeightInfo = ();
+}
+
+impl pallet_balances::Trait for Test {
+    type Balance = u64;
+    type DustRemoval = ();
+    type Event = ();
+    type ExistentialDeposit = ExistentialDeposit;
+    type AccountStore = System;
+    type WeightInfo = ();
+    type MaxLocks = ();
+}
+
+impl membership::Trait for Test {
+    type Event = ();
+    type MemberId = u64;
+    type PaidTermId = u64;
+    type SubscriptionId = u64;
+    type ActorId = u64;
+}
+
+impl common::currency::GovernanceCurrency for Test {
+    type Currency = Balances;
+}
+
+impl pallet_timestamp::Trait for Test {
+    type Moment = u64;
+    type OnTimestampSet = ();
+    type MinimumPeriod = MinimumPeriod;
+    type WeightInfo = ();
+}
+
+pub type Balances = pallet_balances::Module<Test>;
+pub type System = frame_system::Module<Test>;
+
+parameter_types! {
+    pub const RewardPeriod: u32 = 2;
+    pub const MaxWorkerNumberLimit: u32 = 3;
+    pub const MinUnstakingPeriodLimit: u64 = 3;
+    pub const LockId: [u8; 8] = [1; 8];
+}