-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathmock.rs
More file actions
164 lines (144 loc) · 4.51 KB
/
Copy pathmock.rs
File metadata and controls
164 lines (144 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate as payment;
use crate::PaymentDetail;
use frame_support::{
derive_impl,
dispatch::DispatchClass,
parameter_types,
traits::{ConstU32, Contains, Hooks, OnFinalize},
};
use frame_system as system;
use orml_traits::parameter_type_with_key;
use sp_runtime::{traits::IdentityLookup, BuildStorage, Percent};
type Block = frame_system::mocking::MockBlock<Test>;
pub type Balance = u128;
pub type AccountId = u8;
pub const PAYMENT_CREATOR: AccountId = 10;
pub const PAYMENT_RECIPENT: AccountId = 11;
pub const PAYMENT_CREATOR_TWO: AccountId = 30;
pub const PAYMENT_RECIPENT_TWO: AccountId = 31;
pub const CURRENCY_ID: u32 = 1;
pub const RESOLVER_ACCOUNT: AccountId = 12;
pub const FEE_RECIPIENT_ACCOUNT: AccountId = 20;
pub const PAYMENT_RECIPENT_FEE_CHARGED: AccountId = 21;
pub const INCENTIVE_PERCENTAGE: u8 = 10;
pub const MARKETPLACE_FEE_PERCENTAGE: u8 = 10;
pub const CANCEL_BLOCK_BUFFER: u64 = 600;
frame_support::construct_runtime!(
pub enum Test {
System: frame_system,
Tokens: orml_tokens,
Payment: payment,
}
);
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl system::Config for Test {
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
}
parameter_type_with_key! {
pub ExistentialDeposits: |_currency_id: u32| -> Balance {
0u128
};
}
parameter_types! {
pub const MaxLocks: u32 = 50;
}
pub type ReserveIdentifier = [u8; 8];
pub struct MockDustRemovalWhitelist;
impl Contains<AccountId> for MockDustRemovalWhitelist {
fn contains(_a: &AccountId) -> bool {
false
}
}
impl orml_tokens::Config for Test {
type Amount = i64;
type Balance = Balance;
type CurrencyId = u32;
type ExistentialDeposits = ExistentialDeposits;
type CurrencyHooks = ();
type WeightInfo = ();
type MaxLocks = MaxLocks;
type DustRemovalWhitelist = MockDustRemovalWhitelist;
type MaxReserves = ConstU32<2>;
type ReserveIdentifier = ReserveIdentifier;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
pub struct MockDisputeResolver;
impl crate::types::DisputeResolver<AccountId> for MockDisputeResolver {
fn get_resolver_account() -> AccountId {
RESOLVER_ACCOUNT
}
}
pub struct MockFeeHandler;
impl crate::types::FeeHandler<Test> for MockFeeHandler {
fn apply_fees(
_from: &AccountId,
to: &AccountId,
_detail: &PaymentDetail<Test>,
_remark: Option<&[u8]>,
) -> (AccountId, Percent) {
match to {
&PAYMENT_RECIPENT_FEE_CHARGED => (FEE_RECIPIENT_ACCOUNT, Percent::from_percent(MARKETPLACE_FEE_PERCENTAGE)),
_ => (FEE_RECIPIENT_ACCOUNT, Percent::from_percent(0)),
}
}
}
parameter_types! {
pub const IncentivePercentage: Percent = Percent::from_percent(INCENTIVE_PERCENTAGE);
pub const MaxRemarkLength: u32 = 50;
pub const CancelBufferBlockLength: u64 = CANCEL_BLOCK_BUFFER;
pub const MaxScheduledTaskListLength : u32 = 5;
}
impl payment::Config for Test {
type Asset = Tokens;
type DisputeResolver = MockDisputeResolver;
type IncentivePercentage = IncentivePercentage;
type FeeHandler = MockFeeHandler;
type MaxRemarkLength = MaxRemarkLength;
type CancelBufferBlockLength = CancelBufferBlockLength;
type MaxScheduledTaskListLength = MaxScheduledTaskListLength;
type WeightInfo = ();
}
// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap();
orml_tokens::GenesisConfig::<Test> {
balances: vec![
(PAYMENT_CREATOR, CURRENCY_ID, 100),
(PAYMENT_CREATOR_TWO, CURRENCY_ID, 100),
],
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext: sp_io::TestExternalities = t.into();
// need to set block number to 1 to test events
ext.execute_with(|| System::set_block_number(1));
ext
}
pub fn run_n_blocks(n: u64) -> u64 {
const IDLE_WEIGHT: u64 = 10_000_000_000;
const BUSY_WEIGHT: u64 = IDLE_WEIGHT / 1000;
let start_block = System::block_number();
for block_number in (0..=n).map(|n| n + start_block) {
System::set_block_number(block_number);
// Odd blocks gets busy
let idle_weight = if block_number % 2 == 0 {
IDLE_WEIGHT
} else {
BUSY_WEIGHT
};
// ensure the on_idle is executed
<frame_system::Pallet<Test>>::register_extra_weight_unchecked(
Payment::on_idle(block_number, frame_support::weights::Weight::from_parts(idle_weight, 0)),
DispatchClass::Mandatory,
);
<frame_system::Pallet<Test> as OnFinalize<u64>>::on_finalize(block_number);
}
System::block_number()
}