1
// Copyright 2019-2022 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! A minimal runtime including the moonbeam-orbiters pallet
18

            
19
use crate as pallet_moonbeam_orbiters;
20
use frame_support::{
21
	construct_runtime, pallet_prelude::*, parameter_types, traits::Everything, weights::Weight,
22
};
23
use frame_system::{pallet_prelude::BlockNumberFor, EnsureRoot};
24
use nimbus_primitives::{AccountLookup, NimbusId};
25
use sp_core::H256;
26
use sp_runtime::{
27
	traits::{BlakeTwo256, IdentityLookup},
28
	BuildStorage, Perbill,
29
};
30

            
31
pub type AccountId = u64;
32
pub type Balance = u128;
33
pub type BlockNumber = BlockNumberFor<Test>;
34

            
35
type Block = frame_system::mocking::MockBlockU32<Test>;
36

            
37
// Configure a mock runtime to test the pallet.
38
775
construct_runtime!(
39
	pub enum Test
40
	{
41
		System: frame_system,
42
		Balances: pallet_balances,
43
		MoonbeamOrbiters: pallet_moonbeam_orbiters,
44
	}
45
1202
);
46

            
47
// Pallet system configuration
48

            
49
parameter_types! {
50
	pub const BlockHashCount: u32 = 250;
51
	pub const MaximumBlockWeight: Weight = Weight::from_parts(1024, 1);
52
	pub const MaximumBlockLength: u32 = 2 * 1024;
53
	pub const AvailableBlockRatio: Perbill = Perbill::one();
54
	pub const SS58Prefix: u8 = 42;
55
}
56

            
57
impl frame_system::Config for Test {
58
	type BaseCallFilter = Everything;
59
	type DbWeight = ();
60
	type RuntimeOrigin = RuntimeOrigin;
61
	type RuntimeTask = RuntimeTask;
62
	type Nonce = u64;
63
	type Block = Block;
64
	type RuntimeCall = RuntimeCall;
65
	type Hash = H256;
66
	type Hashing = BlakeTwo256;
67
	type AccountId = AccountId;
68
	type Lookup = IdentityLookup<Self::AccountId>;
69
	type RuntimeEvent = RuntimeEvent;
70
	type BlockHashCount = BlockHashCount;
71
	type Version = ();
72
	type PalletInfo = PalletInfo;
73
	type AccountData = pallet_balances::AccountData<Balance>;
74
	type OnNewAccount = ();
75
	type OnKilledAccount = ();
76
	type SystemWeightInfo = ();
77
	type BlockWeights = ();
78
	type BlockLength = ();
79
	type SS58Prefix = SS58Prefix;
80
	type OnSetCode = ();
81
	type MaxConsumers = frame_support::traits::ConstU32<16>;
82
	type SingleBlockMigrations = ();
83
	type MultiBlockMigrator = ();
84
	type PreInherents = ();
85
	type PostInherents = ();
86
	type PostTransactions = ();
87
}
88

            
89
// Pallet balances configuration
90

            
91
parameter_types! {
92
	pub const ExistentialDeposit: u128 = 0;
93
}
94

            
95
impl pallet_balances::Config for Test {
96
	type MaxReserves = ConstU32<2>;
97
	type ReserveIdentifier = [u8; 4];
98
	type MaxLocks = ();
99
	type Balance = Balance;
100
	type RuntimeEvent = RuntimeEvent;
101
	type DustRemoval = ();
102
	type ExistentialDeposit = ExistentialDeposit;
103
	type AccountStore = System;
104
	type WeightInfo = ();
105
	type RuntimeHoldReason = ();
106
	type FreezeIdentifier = ();
107
	type MaxFreezes = ();
108
	type RuntimeFreezeReason = ();
109
}
110

            
111
// Pallet moonbeam-orbiters configuration
112

            
113
parameter_types! {
114
	pub OrbiterReserveIdentifier: [u8; 4] = [b'o', b'r', b'b', b'i'];
115
}
116

            
117
pub struct MockAccountLookup;
118
impl AccountLookup<AccountId> for MockAccountLookup {
119
	fn lookup_account(nimbus_id: &NimbusId) -> Option<AccountId> {
120
		let nimbus_id_bytes: &[u8] = nimbus_id.as_ref();
121

            
122
		if nimbus_id_bytes[0] % 3 == 0 {
123
			Some(nimbus_id_bytes[0] as AccountId)
124
		} else {
125
			None
126
		}
127
	}
128
}
129

            
130
impl pallet_moonbeam_orbiters::Config for Test {
131
	type RuntimeEvent = RuntimeEvent;
132
	type AccountLookup = MockAccountLookup;
133
	type AddCollatorOrigin = EnsureRoot<AccountId>;
134
	type Currency = Balances;
135
	type DelCollatorOrigin = EnsureRoot<AccountId>;
136
	/// Maximum number of orbiters per collator
137
	type MaxPoolSize = ConstU32<2>;
138
	/// Maximum number of round to keep on storage
139
	type MaxRoundArchive = ConstU32<4>;
140
	type OrbiterReserveIdentifier = OrbiterReserveIdentifier;
141
	type RotatePeriod = ConstU32<2>;
142
	/// Round index type.
143
	type RoundIndex = u32;
144
	type WeightInfo = ();
145
}
146

            
147
pub(crate) struct ExtBuilder {
148
	// endowed accounts with balances
149
	balances: Vec<(AccountId, Balance)>,
150
	min_orbiter_deposit: Balance,
151
}
152

            
153
impl Default for ExtBuilder {
154
8
	fn default() -> ExtBuilder {
155
8
		ExtBuilder {
156
8
			balances: vec![],
157
8
			min_orbiter_deposit: 10_000,
158
8
		}
159
8
	}
160
}
161

            
162
impl ExtBuilder {
163
6
	pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
164
6
		self.balances = balances;
165
6
		self
166
6
	}
167
7
	pub(crate) fn with_min_orbiter_deposit(mut self, min_orbiter_deposit: Balance) -> Self {
168
7
		self.min_orbiter_deposit = min_orbiter_deposit;
169
7
		self
170
7
	}
171
8
	pub(crate) fn build(self) -> sp_io::TestExternalities {
172
8
		let mut t = frame_system::GenesisConfig::<Test>::default()
173
8
			.build_storage()
174
8
			.expect("Frame system builds valid default genesis config");
175
8

            
176
8
		pallet_balances::GenesisConfig::<Test> {
177
8
			balances: self.balances,
178
8
		}
179
8
		.assimilate_storage(&mut t)
180
8
		.expect("Pallet balances storage can be assimilated");
181
8

            
182
8
		pallet_moonbeam_orbiters::GenesisConfig::<Test> {
183
8
			min_orbiter_deposit: self.min_orbiter_deposit,
184
8
		}
185
8
		.assimilate_storage(&mut t)
186
8
		.expect("Pallet moonbeam-orbiters storage can be assimilated");
187
8

            
188
8
		let mut ext = sp_io::TestExternalities::new(t);
189
8
		ext.execute_with(|| System::set_block_number(1));
190
8
		ext
191
8
	}
192
}
193

            
194
/// Rolls to the desired block. Returns the number of blocks played.
195
3
pub(crate) fn roll_to(n: BlockNumber) -> BlockNumber {
196
3
	let mut num_blocks = 0;
197
3
	let mut block = System::block_number();
198
14
	while block < n {
199
11
		block = roll_one_block();
200
11
		num_blocks += 1;
201
11
	}
202
3
	num_blocks
203
3
}
204

            
205
// Rolls forward one block. Returns the new block number.
206
11
fn roll_one_block() -> BlockNumber {
207
11
	MoonbeamOrbiters::on_finalize(System::block_number());
208
11
	Balances::on_finalize(System::block_number());
209
11
	System::on_finalize(System::block_number());
210
11
	System::set_block_number(System::block_number() + 1);
211
11
	System::reset_events();
212
11
	System::on_initialize(System::block_number());
213
11
	Balances::on_initialize(System::block_number());
214
11
	MoonbeamOrbiters::on_initialize(System::block_number());
215
11
	// Trigger a new round each two blocks
216
11
	let block_number = System::block_number();
217
11
	if block_number % 2 == 0 {
218
6
		MoonbeamOrbiters::on_new_round(block_number as u32 / 2);
219
6
	}
220
11
	System::block_number()
221
11
}