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
use cumulus_primitives_core::ParaId;
21
use pallet_xcm_transactor::chain_indices::*;
22
use sp_runtime::traits::AccountIdConversion;
23
use sp_runtime::{AccountId32, BuildStorage};
24
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt};
25

            
26
use parachain::MockTransactors;
27

            
28
use polkadot_runtime_parachains::configuration::{
29
	GenesisConfig as ConfigurationGenesisConfig, HostConfiguration,
30
};
31
use polkadot_runtime_parachains::paras::{
32
	GenesisConfig as ParasGenesisConfig, ParaGenesisArgs, ParaKind,
33
};
34

            
35
use sp_core::{H160, U256};
36
use std::{collections::BTreeMap, str::FromStr};
37

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

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

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

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

            
55
250
pub fn evm_account() -> H160 {
56
250
	H160::from_str("1000000000000000000000000000000000000001").unwrap()
57
250
}
58

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

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

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

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

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

            
107
decl_test_parachain! {
108
	pub struct Statemint {
109
		Runtime = statemint_like::Runtime,
110
		XcmpMessageHandler = statemint_like::MsgQueue,
111
		DmpMessageHandler = statemint_like::MsgQueue,
112
		new_ext = statemint_ext(1000),
113
	}
114
}
115

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

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

            
140
pub const INITIAL_BALANCE: u128 = 10_000_000_000_000_000;
141

            
142
pub const INITIAL_EVM_BALANCE: u128 = 0;
143
pub const INITIAL_EVM_NONCE: u32 = 1;
144

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

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

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

            
159
246
	pallet_xcm_transactor::GenesisConfig::<Runtime> {
160
246
		// match relay runtime construct_runtime order in xcm_mock::relay_chain
161
246
		chain_indices_map: vec![(
162
246
			MockTransactors::Relay,
163
246
			pallet_xcm_transactor::chain_indices::ChainIndices::Relay(RelayChainIndices {
164
246
				hrmp: 6u8,
165
246
				init_open_channel: 0u8,
166
246
				accept_open_channel: 1u8,
167
246
				close_channel: 2u8,
168
246
				cancel_open_request: 6u8,
169
246
				..Default::default()
170
246
			}),
171
246
		)],
172
246
		..Default::default()
173
246
	}
174
246
	.assimilate_storage(&mut t)
175
246
	.unwrap();
176
246

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

            
191
246
	let genesis_config = pallet_evm::GenesisConfig::<Runtime> {
192
246
		accounts: evm_accounts,
193
246
		..Default::default()
194
246
	};
195
246
	genesis_config.assimilate_storage(&mut t).unwrap();
196
246

            
197
246
	let mut ext = sp_io::TestExternalities::new(t);
198
246
	ext.execute_with(|| {
199
246
		System::set_block_number(1);
200
246
		MsgQueue::set_para_id(para_id.into());
201
246
	});
202
246
	ext
203
246
}
204

            
205
82
pub fn statemint_ext(para_id: u32) -> sp_io::TestExternalities {
206
	use statemint_like::{MsgQueue, Runtime, System};
207

            
208
82
	let mut t = frame_system::GenesisConfig::<Runtime>::default()
209
82
		.build_storage()
210
82
		.unwrap();
211
82

            
212
82
	pallet_balances::GenesisConfig::<Runtime> {
213
82
		balances: vec![
214
82
			(RELAYALICE.into(), INITIAL_BALANCE),
215
82
			(RELAYBOB.into(), INITIAL_BALANCE),
216
82
		],
217
82
		dev_accounts: None,
218
82
	}
219
82
	.assimilate_storage(&mut t)
220
82
	.unwrap();
221
82

            
222
82
	let mut ext = sp_io::TestExternalities::new(t);
223
82
	ext.execute_with(|| {
224
82
		System::set_block_number(1);
225
82
		MsgQueue::set_para_id(para_id.into());
226
82
	});
227
82
	ext
228
82
}
229

            
230
82
pub fn relay_ext(paras: Vec<u32>) -> sp_io::TestExternalities {
231
	use relay_chain::{Runtime, System};
232

            
233
82
	let mut t = frame_system::GenesisConfig::<Runtime>::default()
234
82
		.build_storage()
235
82
		.unwrap();
236
82

            
237
82
	pallet_balances::GenesisConfig::<Runtime> {
238
82
		balances: vec![(RELAYALICE, INITIAL_BALANCE)],
239
82
		dev_accounts: None,
240
82
	}
241
82
	.assimilate_storage(&mut t)
242
82
	.unwrap();
243
82

            
244
82
	let para_genesis: Vec<(ParaId, ParaGenesisArgs)> = paras
245
82
		.iter()
246
328
		.map(|&para_id| (para_id.into(), mock_para_genesis_info()))
247
82
		.collect();
248
82

            
249
82
	let genesis_config = ConfigurationGenesisConfig::<Runtime> {
250
82
		config: mock_relay_config(),
251
82
	};
252
82
	genesis_config.assimilate_storage(&mut t).unwrap();
253
82

            
254
82
	let genesis_config = ParasGenesisConfig::<Runtime> {
255
82
		paras: para_genesis,
256
82
		..Default::default()
257
82
	};
258
82
	genesis_config.assimilate_storage(&mut t).unwrap();
259
82

            
260
82
	let mut ext = sp_io::TestExternalities::new(t);
261
82
	ext.execute_with(|| {
262
82
		System::set_block_number(1);
263
82
	});
264
82
	ext
265
82
}
266
pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
267
pub type Hrmp = polkadot_runtime_parachains::hrmp::Pallet<relay_chain::Runtime>;
268

            
269
pub type StatemintBalances = pallet_balances::Pallet<statemint_like::Runtime>;
270
pub type StatemintChainPalletXcm = pallet_xcm::Pallet<statemint_like::Runtime>;
271
pub type StatemintAssets = pallet_assets::Pallet<statemint_like::Runtime>;
272

            
273
pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;
274

            
275
pub type RelayBalances = pallet_balances::Pallet<relay_chain::Runtime>;
276
pub type ParaBalances = pallet_balances::Pallet<parachain::Runtime>;
277
pub type XcmTransactor = pallet_xcm_transactor::Pallet<parachain::Runtime>;