1
// Copyright 2021 Parity Technologies (UK) Ltd.
2
// This file is part of Polkadot.
3

            
4
// Polkadot 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
// Polkadot 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 Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
pub mod parachain;
18
pub mod relay_chain;
19
pub mod statemint_like;
20

            
21
use cumulus_primitives_core::ParaId;
22
use pallet_xcm_transactor::relay_indices::*;
23
use sp_runtime::traits::AccountIdConversion;
24
use sp_runtime::{AccountId32, BuildStorage};
25
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt};
26

            
27
use polkadot_runtime_parachains::configuration::{
28
	GenesisConfig as ConfigurationGenesisConfig, HostConfiguration,
29
};
30
use polkadot_runtime_parachains::paras::{
31
	GenesisConfig as ParasGenesisConfig, ParaGenesisArgs, ParaKind,
32
};
33
use sp_core::{H160, U256};
34
use std::{collections::BTreeMap, str::FromStr};
35

            
36
pub const PARAALICE: [u8; 20] = [1u8; 20];
37
pub const PARABOB: [u8; 20] = [2u8; 20];
38
pub const RELAYALICE: AccountId32 = AccountId32::new([0u8; 32]);
39
pub const RELAYBOB: AccountId32 = AccountId32::new([2u8; 32]);
40

            
41
39
pub fn para_a_account() -> AccountId32 {
42
39
	ParaId::from(1).into_account_truncating()
43
39
}
44

            
45
1
pub fn para_b_account() -> AccountId32 {
46
1
	ParaId::from(2).into_account_truncating()
47
1
}
48

            
49
9
pub fn para_a_account_20() -> parachain::AccountId {
50
9
	ParaId::from(1).into_account_truncating()
51
9
}
52

            
53
238
pub fn evm_account() -> H160 {
54
238
	H160::from_str("1000000000000000000000000000000000000001").unwrap()
55
238
}
56

            
57
312
pub fn mock_para_genesis_info() -> ParaGenesisArgs {
58
312
	ParaGenesisArgs {
59
312
		genesis_head: vec![1u8].into(),
60
312
		validation_code: vec![1u8].into(),
61
312
		para_kind: ParaKind::Parachain,
62
312
	}
63
312
}
64

            
65
78
pub fn mock_relay_config() -> HostConfiguration<relay_chain::BlockNumber> {
66
78
	HostConfiguration::<relay_chain::BlockNumber> {
67
78
		hrmp_channel_max_capacity: u32::MAX,
68
78
		hrmp_channel_max_total_size: u32::MAX,
69
78
		hrmp_max_parachain_inbound_channels: 10,
70
78
		hrmp_max_parachain_outbound_channels: 10,
71
78
		hrmp_channel_max_message_size: u32::MAX,
72
78
		// Changed to avoid arithmetic errors within hrmp_close
73
78
		max_downward_message_size: 100_000u32,
74
78
		..Default::default()
75
78
	}
76
78
}
77

            
78
234
pub fn mock_xcm_transactor_storage() -> RelayChainIndices {
79
234
	RelayChainIndices {
80
234
		staking: 0u8,
81
234
		utility: 5u8,
82
234
		hrmp: 6u8,
83
234
		bond: 0u8,
84
234
		bond_extra: 1u8,
85
234
		unbond: 2u8,
86
234
		withdraw_unbonded: 3u8,
87
234
		validate: 4u8,
88
234
		nominate: 5u8,
89
234
		chill: 6u8,
90
234
		set_payee: 7u8,
91
234
		set_controller: 8u8,
92
234
		rebond: 19u8,
93
234
		as_derivative: 1u8,
94
234
		init_open_channel: 0u8,
95
234
		accept_open_channel: 1u8,
96
234
		close_channel: 2u8,
97
234
		cancel_open_request: 6u8,
98
234
	}
99
234
}
100

            
101
decl_test_parachain! {
102
	pub struct ParaA {
103
		Runtime = parachain::Runtime,
104
		XcmpMessageHandler = parachain::MsgQueue,
105
		DmpMessageHandler = parachain::MsgQueue,
106
		new_ext = para_ext(1),
107
	}
108
}
109

            
110
decl_test_parachain! {
111
	pub struct ParaB {
112
		Runtime = parachain::Runtime,
113
		XcmpMessageHandler = parachain::MsgQueue,
114
		DmpMessageHandler = parachain::MsgQueue,
115
		new_ext = para_ext(2),
116
	}
117
}
118

            
119
decl_test_parachain! {
120
	pub struct ParaC {
121
		Runtime = parachain::Runtime,
122
		XcmpMessageHandler = parachain::MsgQueue,
123
		DmpMessageHandler = parachain::MsgQueue,
124
		new_ext = para_ext(3),
125
	}
126
}
127

            
128
decl_test_parachain! {
129
	pub struct Statemint {
130
		Runtime = statemint_like::Runtime,
131
		XcmpMessageHandler = statemint_like::MsgQueue,
132
		DmpMessageHandler = statemint_like::MsgQueue,
133
		new_ext = statemint_ext(1000),
134
	}
135
}
136

            
137
decl_test_relay_chain! {
138
	pub struct Relay {
139
		Runtime = relay_chain::Runtime,
140
		RuntimeCall = relay_chain::RuntimeCall,
141
		RuntimeEvent = relay_chain::RuntimeEvent,
142
		XcmConfig = relay_chain::XcmConfig,
143
		MessageQueue = relay_chain::MessageQueue,
144
		System = relay_chain::System,
145
		new_ext = relay_ext(vec![1, 2, 3, 1000]),
146
	}
147
}
148

            
149
decl_test_network! {
150
	pub struct MockNet {
151
		relay_chain = Relay,
152
		parachains = vec![
153
			(1, ParaA),
154
			(2, ParaB),
155
			(3, ParaC),
156
			(1000, Statemint),
157
		],
158
	}
159
}
160

            
161
pub const INITIAL_BALANCE: u128 = 10_000_000_000_000_000;
162

            
163
pub const INITIAL_EVM_BALANCE: u128 = 0;
164
pub const INITIAL_EVM_NONCE: u32 = 1;
165

            
166
234
pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
167
	use parachain::{MsgQueue, Runtime, System};
168

            
169
234
	let mut t = frame_system::GenesisConfig::<Runtime>::default()
170
234
		.build_storage()
171
234
		.unwrap();
172
234

            
173
234
	pallet_balances::GenesisConfig::<Runtime> {
174
234
		balances: vec![(PARAALICE.into(), INITIAL_BALANCE)],
175
234
		dev_accounts: None,
176
234
	}
177
234
	.assimilate_storage(&mut t)
178
234
	.unwrap();
179
234

            
180
234
	pallet_xcm_transactor::GenesisConfig::<Runtime> {
181
234
		relay_indices: mock_xcm_transactor_storage(),
182
234
		..Default::default()
183
234
	}
184
234
	.assimilate_storage(&mut t)
185
234
	.unwrap();
186
234

            
187
234
	// EVM accounts are self-sufficient.
188
234
	let mut evm_accounts = BTreeMap::new();
189
234
	evm_accounts.insert(
190
234
		evm_account(),
191
234
		fp_evm::GenesisAccount {
192
234
			nonce: U256::from(INITIAL_EVM_NONCE),
193
234
			balance: U256::from(INITIAL_EVM_BALANCE),
194
234
			storage: Default::default(),
195
234
			code: vec![
196
234
				0x00, // STOP
197
234
			],
198
234
		},
199
234
	);
200
234

            
201
234
	let genesis_config = pallet_evm::GenesisConfig::<Runtime> {
202
234
		accounts: evm_accounts,
203
234
		..Default::default()
204
234
	};
205
234
	genesis_config.assimilate_storage(&mut t).unwrap();
206
234

            
207
234
	let mut ext = sp_io::TestExternalities::new(t);
208
234
	ext.execute_with(|| {
209
234
		System::set_block_number(1);
210
234
		MsgQueue::set_para_id(para_id.into());
211
234
	});
212
234
	ext
213
234
}
214

            
215
78
pub fn statemint_ext(para_id: u32) -> sp_io::TestExternalities {
216
	use statemint_like::{MsgQueue, Runtime, System};
217

            
218
78
	let mut t = frame_system::GenesisConfig::<Runtime>::default()
219
78
		.build_storage()
220
78
		.unwrap();
221
78

            
222
78
	pallet_balances::GenesisConfig::<Runtime> {
223
78
		balances: vec![
224
78
			(RELAYALICE.into(), INITIAL_BALANCE),
225
78
			(RELAYBOB.into(), INITIAL_BALANCE),
226
78
		],
227
78
		dev_accounts: None,
228
78
	}
229
78
	.assimilate_storage(&mut t)
230
78
	.unwrap();
231
78

            
232
78
	let mut ext = sp_io::TestExternalities::new(t);
233
78
	ext.execute_with(|| {
234
78
		System::set_block_number(1);
235
78
		MsgQueue::set_para_id(para_id.into());
236
78
	});
237
78
	ext
238
78
}
239

            
240
78
pub fn relay_ext(paras: Vec<u32>) -> sp_io::TestExternalities {
241
	use relay_chain::{Runtime, System};
242

            
243
78
	let mut t = frame_system::GenesisConfig::<Runtime>::default()
244
78
		.build_storage()
245
78
		.unwrap();
246
78

            
247
78
	pallet_balances::GenesisConfig::<Runtime> {
248
78
		balances: vec![(RELAYALICE, INITIAL_BALANCE)],
249
78
		dev_accounts: None,
250
78
	}
251
78
	.assimilate_storage(&mut t)
252
78
	.unwrap();
253
78

            
254
78
	let para_genesis: Vec<(ParaId, ParaGenesisArgs)> = paras
255
78
		.iter()
256
312
		.map(|&para_id| (para_id.into(), mock_para_genesis_info()))
257
78
		.collect();
258
78

            
259
78
	let genesis_config = ConfigurationGenesisConfig::<Runtime> {
260
78
		config: mock_relay_config(),
261
78
	};
262
78
	genesis_config.assimilate_storage(&mut t).unwrap();
263
78

            
264
78
	let genesis_config = ParasGenesisConfig::<Runtime> {
265
78
		paras: para_genesis,
266
78
		..Default::default()
267
78
	};
268
78
	genesis_config.assimilate_storage(&mut t).unwrap();
269
78

            
270
78
	let mut ext = sp_io::TestExternalities::new(t);
271
78
	ext.execute_with(|| {
272
78
		System::set_block_number(1);
273
78
	});
274
78
	ext
275
78
}
276

            
277
pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
278
pub type Hrmp = polkadot_runtime_parachains::hrmp::Pallet<relay_chain::Runtime>;
279

            
280
pub type StatemintBalances = pallet_balances::Pallet<statemint_like::Runtime>;
281
pub type StatemintChainPalletXcm = pallet_xcm::Pallet<statemint_like::Runtime>;
282
pub type StatemintAssets = pallet_assets::Pallet<statemint_like::Runtime>;
283

            
284
pub type RelayBalances = pallet_balances::Pallet<relay_chain::Runtime>;
285
pub type ParaBalances = pallet_balances::Pallet<parachain::Runtime>;
286
pub type XcmTransactor = pallet_xcm_transactor::Pallet<parachain::Runtime>;