Newer
Older
//! Mocks for the tokens module.
#![cfg(test)]
use frame_support::{
impl_outer_event, impl_outer_origin, parameter_types,
traits::{ChangeMembers, Contains, ContainsLengthBound, SaturatingCurrencyToVote},
use sp_runtime::{testing::Header, traits::IdentityLookup, ModuleId, Perbill, Percent, Permill};
use std::collections::HashMap;
use super::*;
impl_outer_origin! {
pub enum Origin for Runtime {}
}
mod tokens {
pub use crate::Event;
}
impl_outer_event! {
pub enum TestEvent for Runtime {
pallet_treasury<T>,
pallet_elections_phragmen<T>,
}
}
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Runtime;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: u32 = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
type Call = ();
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = TestEvent;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type DbWeight = ();
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ();
}
pub type System = system::Module<Runtime>;
type CurrencyId = u32;
pub type Balance = u64;
thread_local! {
pub static ACCUMULATED_RECEIVED: RefCell<HashMap<(AccountId, CurrencyId), Balance>> = RefCell::new(HashMap::new());
}
pub struct MockOnReceived;
impl OnReceived<AccountId, CurrencyId, Balance> for MockOnReceived {
fn on_received(who: &AccountId, currency_id: CurrencyId, amount: Balance) {
ACCUMULATED_RECEIVED.with(|v| {
let mut old_map = v.borrow().clone();
if let Some(before) = old_map.get_mut(&(*who, currency_id)) {
old_map.insert((*who, currency_id), amount);
};
*v.borrow_mut() = old_map;
});
}
}
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
thread_local! {
static TEN_TO_FOURTEEN: RefCell<Vec<u64>> = RefCell::new(vec![10,11,12,13,14]);
}
pub struct TenToFourteen;
impl Contains<u64> for TenToFourteen {
fn sorted_members() -> Vec<u64> {
TEN_TO_FOURTEEN.with(|v| v.borrow().clone())
}
#[cfg(feature = "runtime-benchmarks")]
fn add(new: &u64) {
TEN_TO_FOURTEEN.with(|v| {
let mut members = v.borrow_mut();
members.push(*new);
members.sort();
})
}
}
impl ContainsLengthBound for TenToFourteen {
fn max_len() -> usize {
TEN_TO_FOURTEEN.with(|v| v.borrow().len())
}
fn min_len() -> usize {
0
}
}
parameter_types! {
pub const ProposalBond: Permill = Permill::from_percent(5);
pub const ProposalBondMinimum: u64 = 1;
pub const TipCountdown: u64 = 1;
pub const TipFindersFee: Percent = Percent::from_percent(20);
pub const TipReportDepositBase: u64 = 1;
pub const SpendPeriod: u64 = 2;
pub const Burn: Permill = Permill::from_percent(50);
pub const TreasuryModuleId: ModuleId = ModuleId(*b"py/trsry");
pub const GetTokenId: CurrencyId = TEST_TOKEN_ID;
}
impl pallet_treasury::Trait for Runtime {
type ModuleId = TreasuryModuleId;
type Currency = CurrencyAdapter<Runtime, GetTokenId>;
type ApproveOrigin = frame_system::EnsureRoot<u64>;
type RejectOrigin = frame_system::EnsureRoot<u64>;
type Tippers = TenToFourteen;
type TipCountdown = TipCountdown;
type TipFindersFee = TipFindersFee;
type TipReportDepositBase = TipReportDepositBase;
type DataDepositPerByte = DataDepositPerByte;
type ProposalBond = ProposalBond;
type ProposalBondMinimum = ProposalBondMinimum;
type SpendPeriod = SpendPeriod;
type Burn = Burn;
type BurnDestination = (); // Just gets burned.
type BountyDepositBase = ();
type BountyDepositPayoutDelay = ();
type BountyUpdatePeriod = ();
type BountyCuratorDeposit = ();
type BountyValueMinimum = ();
type MaximumReasonLength = ();
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
type WeightInfo = ();
}
parameter_types! {
pub const CandidacyBond: u64 = 3;
}
thread_local! {
static VOTING_BOND: RefCell<u64> = RefCell::new(2);
static DESIRED_MEMBERS: RefCell<u32> = RefCell::new(2);
static DESIRED_RUNNERS_UP: RefCell<u32> = RefCell::new(2);
static TERM_DURATION: RefCell<u64> = RefCell::new(5);
}
pub struct VotingBond;
impl Get<u64> for VotingBond {
fn get() -> u64 {
VOTING_BOND.with(|v| *v.borrow())
}
}
pub struct DesiredMembers;
impl Get<u32> for DesiredMembers {
fn get() -> u32 {
DESIRED_MEMBERS.with(|v| *v.borrow())
}
}
pub struct DesiredRunnersUp;
impl Get<u32> for DesiredRunnersUp {
fn get() -> u32 {
DESIRED_RUNNERS_UP.with(|v| *v.borrow())
}
}
pub struct TermDuration;
impl Get<u64> for TermDuration {
fn get() -> u64 {
TERM_DURATION.with(|v| *v.borrow())
}
}
thread_local! {
pub static MEMBERS: RefCell<Vec<u64>> = RefCell::new(vec![]);
pub static PRIME: RefCell<Option<u64>> = RefCell::new(None);
}
pub struct TestChangeMembers;
impl ChangeMembers<u64> for TestChangeMembers {
fn change_members_sorted(incoming: &[u64], outgoing: &[u64], new: &[u64]) {
// new, incoming, outgoing must be sorted.
let mut new_sorted = new.to_vec();
new_sorted.sort();
assert_eq!(new, &new_sorted[..]);
let mut incoming_sorted = incoming.to_vec();
incoming_sorted.sort();
assert_eq!(incoming, &incoming_sorted[..]);
let mut outgoing_sorted = outgoing.to_vec();
outgoing_sorted.sort();
assert_eq!(outgoing, &outgoing_sorted[..]);
// incoming and outgoing must be disjoint
for x in incoming.iter() {
assert!(outgoing.binary_search(x).is_err());
}
let mut old_plus_incoming = MEMBERS.with(|m| m.borrow().to_vec());
old_plus_incoming.extend_from_slice(incoming);
old_plus_incoming.sort();
let mut new_plus_outgoing = new.to_vec();
new_plus_outgoing.extend_from_slice(outgoing);
new_plus_outgoing.sort();
assert_eq!(
old_plus_incoming, new_plus_outgoing,
"change members call is incorrect!"
);
MEMBERS.with(|m| *m.borrow_mut() = new.to_vec());
PRIME.with(|p| *p.borrow_mut() = None);
}
fn set_prime(who: Option<u64>) {
PRIME.with(|p| *p.borrow_mut() = who);
}
}
parameter_types! {
pub const ElectionsPhragmenModuleId: LockIdentifier = *b"phrelect";
}
impl pallet_elections_phragmen::Trait for Runtime {
type ModuleId = ElectionsPhragmenModuleId;
type Event = TestEvent;
type Currency = CurrencyAdapter<Runtime, GetTokenId>;
type ChangeMembers = TestChangeMembers;
type InitializeMembers = ();
type CandidacyBond = CandidacyBond;
type VotingBond = VotingBond;
type TermDuration = TermDuration;
type DesiredMembers = DesiredMembers;
type DesiredRunnersUp = DesiredRunnersUp;
type LoserCandidate = ();
type KickedMember = ();
type BadReport = ();
type WeightInfo = ();
}
impl Trait for Runtime {
type Event = TestEvent;
type Balance = Balance;
}
pub type Tokens = Module<Runtime>;
pub type TreasuryCurrencyAdapter = <Runtime as pallet_treasury::Trait>::Currency;
pub const TEST_TOKEN_ID: CurrencyId = 1;
pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
pub const ID_1: LockIdentifier = *b"1 ";
pub const ID_2: LockIdentifier = *b"2 ";
endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>,
}
impl Default for ExtBuilder {
fn default() -> Self {
Self {
endowed_accounts: vec![],
pub fn balances(mut self, endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>) -> Self {
self.endowed_accounts = endowed_accounts;
self
}
pub fn one_hundred_for_alice_n_bob(self) -> Self {
self.balances(vec![(ALICE, TEST_TOKEN_ID, 100), (BOB, TEST_TOKEN_ID, 100)])
pub fn one_hundred_for_treasury_account(mut self) -> Self {
self.treasury_genesis = true;
self.balances(vec![(TREASURY_ACCOUNT, TEST_TOKEN_ID, 100)])
}
.build_storage::<Runtime>()
.unwrap();
GenesisConfig::<Runtime> {
endowed_accounts: self.endowed_accounts,
}
.assimilate_storage(&mut t)
.unwrap();
if self.treasury_genesis {
pallet_treasury::GenesisConfig::default()
.unwrap();
pallet_elections_phragmen::GenesisConfig::<Runtime> {
members: vec![(TREASURY_ACCOUNT, 10)],
}
.assimilate_storage(&mut t)
.unwrap();
}
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext