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 statemine_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
250
pub fn evm_account() -> H160 {
54
250
	H160::from_str("1000000000000000000000000000000000000001").unwrap()
55
250
}
56

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

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

            
78
decl_test_parachain! {
79
	pub struct ParaA {
80
		Runtime = parachain::Runtime,
81
		XcmpMessageHandler = parachain::MsgQueue,
82
		DmpMessageHandler = parachain::MsgQueue,
83
		new_ext = para_ext(1),
84
	}
85
}
86

            
87
decl_test_parachain! {
88
	pub struct ParaB {
89
		Runtime = parachain::Runtime,
90
		XcmpMessageHandler = parachain::MsgQueue,
91
		DmpMessageHandler = parachain::MsgQueue,
92
		new_ext = para_ext(2),
93
	}
94
}
95

            
96
decl_test_parachain! {
97
	pub struct ParaC {
98
		Runtime = parachain::Runtime,
99
		XcmpMessageHandler = parachain::MsgQueue,
100
		DmpMessageHandler = parachain::MsgQueue,
101
		new_ext = para_ext(3),
102
	}
103
}
104

            
105
decl_test_parachain! {
106
	pub struct Statemine {
107
		Runtime = statemine_like::Runtime,
108
		XcmpMessageHandler = statemine_like::MsgQueue,
109
		DmpMessageHandler = statemine_like::MsgQueue,
110
		new_ext = statemine_ext(1000),
111
	}
112
}
113

            
114
decl_test_relay_chain! {
115
	pub struct Relay {
116
		Runtime = relay_chain::Runtime,
117
		RuntimeCall = relay_chain::RuntimeCall,
118
		RuntimeEvent = relay_chain::RuntimeEvent,
119
		XcmConfig = relay_chain::XcmConfig,
120
		MessageQueue = relay_chain::MessageQueue,
121
		System = relay_chain::System,
122
		new_ext = relay_ext(vec![1, 2, 3, 1000]),
123
	}
124
}
125

            
126
decl_test_network! {
127
	pub struct MockNet {
128
		relay_chain = Relay,
129
		parachains = vec![
130
			(1, ParaA),
131
			(2, ParaB),
132
			(3, ParaC),
133
			(1000, Statemine),
134
		],
135
	}
136
}
137

            
138
pub const INITIAL_BALANCE: u128 = 10_000_000_000_000_000;
139

            
140
pub const INITIAL_EVM_BALANCE: u128 = 0;
141
pub const INITIAL_EVM_NONCE: u32 = 1;
142

            
143
246
pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
144
	use parachain::{MsgQueue, Runtime, System};
145

            
146
246
	let mut t = frame_system::GenesisConfig::<Runtime>::default()
147
246
		.build_storage()
148
246
		.unwrap();
149
246

            
150
246
	pallet_balances::GenesisConfig::<Runtime> {
151
246
		balances: vec![(PARAALICE.into(), INITIAL_BALANCE)],
152
246
		dev_accounts: None,
153
246
	}
154
246
	.assimilate_storage(&mut t)
155
246
	.unwrap();
156
246

            
157
246
	pallet_xcm_transactor::GenesisConfig::<Runtime> {
158
246
		// match relay runtime construct_runtime order in xcm_mock::relay_chain
159
246
		relay_indices: RelayChainIndices {
160
246
			hrmp: 6u8,
161
246
			init_open_channel: 0u8,
162
246
			accept_open_channel: 1u8,
163
246
			close_channel: 2u8,
164
246
			cancel_open_request: 6u8,
165
246
			..Default::default()
166
246
		},
167
246
		..Default::default()
168
246
	}
169
246
	.assimilate_storage(&mut t)
170
246
	.unwrap();
171
246

            
172
246
	// EVM accounts are self-sufficient.
173
246
	let mut evm_accounts = BTreeMap::new();
174
246
	evm_accounts.insert(
175
246
		evm_account(),
176
246
		fp_evm::GenesisAccount {
177
246
			nonce: U256::from(INITIAL_EVM_NONCE),
178
246
			balance: U256::from(INITIAL_EVM_BALANCE),
179
246
			storage: Default::default(),
180
246
			code: vec![
181
246
				0x00, // STOP
182
246
			],
183
246
		},
184
246
	);
185
246

            
186
246
	let genesis_config = pallet_evm::GenesisConfig::<Runtime> {
187
246
		accounts: evm_accounts,
188
246
		..Default::default()
189
246
	};
190
246
	genesis_config.assimilate_storage(&mut t).unwrap();
191
246

            
192
246
	let mut ext = sp_io::TestExternalities::new(t);
193
246
	ext.execute_with(|| {
194
246
		System::set_block_number(1);
195
246
		MsgQueue::set_para_id(para_id.into());
196
246
	});
197
246
	ext
198
246
}
199

            
200
82
pub fn statemine_ext(para_id: u32) -> sp_io::TestExternalities {
201
	use statemine_like::{MsgQueue, Runtime, System};
202

            
203
82
	let mut t = frame_system::GenesisConfig::<Runtime>::default()
204
82
		.build_storage()
205
82
		.unwrap();
206
82

            
207
82
	pallet_balances::GenesisConfig::<Runtime> {
208
82
		balances: vec![
209
82
			(RELAYALICE.into(), INITIAL_BALANCE),
210
82
			(RELAYBOB.into(), INITIAL_BALANCE),
211
82
		],
212
82
		dev_accounts: None,
213
82
	}
214
82
	.assimilate_storage(&mut t)
215
82
	.unwrap();
216
82

            
217
82
	let mut ext = sp_io::TestExternalities::new(t);
218
82
	ext.execute_with(|| {
219
82
		System::set_block_number(1);
220
82
		MsgQueue::set_para_id(para_id.into());
221
82
	});
222
82
	ext
223
82
}
224

            
225
82
pub fn relay_ext(paras: Vec<u32>) -> sp_io::TestExternalities {
226
	use relay_chain::{Runtime, System};
227

            
228
82
	let mut t = frame_system::GenesisConfig::<Runtime>::default()
229
82
		.build_storage()
230
82
		.unwrap();
231
82

            
232
82
	pallet_balances::GenesisConfig::<Runtime> {
233
82
		balances: vec![(RELAYALICE, INITIAL_BALANCE)],
234
82
		dev_accounts: None,
235
82
	}
236
82
	.assimilate_storage(&mut t)
237
82
	.unwrap();
238
82

            
239
82
	let para_genesis: Vec<(ParaId, ParaGenesisArgs)> = paras
240
82
		.iter()
241
328
		.map(|&para_id| (para_id.into(), mock_para_genesis_info()))
242
82
		.collect();
243
82

            
244
82
	let genesis_config = ConfigurationGenesisConfig::<Runtime> {
245
82
		config: mock_relay_config(),
246
82
	};
247
82
	genesis_config.assimilate_storage(&mut t).unwrap();
248
82

            
249
82
	let genesis_config = ParasGenesisConfig::<Runtime> {
250
82
		paras: para_genesis,
251
82
		..Default::default()
252
82
	};
253
82
	genesis_config.assimilate_storage(&mut t).unwrap();
254
82

            
255
82
	let mut ext = sp_io::TestExternalities::new(t);
256
82
	ext.execute_with(|| {
257
82
		System::set_block_number(1);
258
82
	});
259
82
	ext
260
82
}
261

            
262
pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
263
pub type Hrmp = polkadot_runtime_parachains::hrmp::Pallet<relay_chain::Runtime>;
264

            
265
pub type StatemineBalances = pallet_balances::Pallet<statemine_like::Runtime>;
266
pub type StatemineChainPalletXcm = pallet_xcm::Pallet<statemine_like::Runtime>;
267
pub type StatemineAssets = pallet_assets::Pallet<statemine_like::Runtime>;
268

            
269
pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;
270

            
271
pub type RelayBalances = pallet_balances::Pallet<relay_chain::Runtime>;
272
pub type ParaBalances = pallet_balances::Pallet<parachain::Runtime>;
273
pub type XcmTransactor = pallet_xcm_transactor::Pallet<parachain::Runtime>;