1
// Copyright 2019-2025 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
820
construct_runtime!(
39
239
	pub enum Test
40
239
	{
41
239
		System: frame_system,
42
239
		Balances: pallet_balances,
43
239
		MoonbeamOrbiters: pallet_moonbeam_orbiters,
44
239
	}
45
857
);
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
	type ExtensionsWeightInfo = ();
88
}
89

            
90
// Pallet balances configuration
91

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

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

            
113
// Pallet moonbeam-orbiters configuration
114

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

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

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

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

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

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

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

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

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

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

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

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