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
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 Statemint {
107
		Runtime = statemint_like::Runtime,
108
		XcmpMessageHandler = statemint_like::MsgQueue,
109
		DmpMessageHandler = statemint_like::MsgQueue,
110
		new_ext = statemint_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, Statemint),
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
234
pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
144
	use parachain::{MsgQueue, Runtime, System};
145

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

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

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

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

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

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

            
200
78
pub fn statemint_ext(para_id: u32) -> sp_io::TestExternalities {
201
	use statemint_like::{MsgQueue, Runtime, System};
202

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

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

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

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

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

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

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

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

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

            
255
78
	let mut ext = sp_io::TestExternalities::new(t);
256
78
	ext.execute_with(|| {
257
78
		System::set_block_number(1);
258
78
	});
259
78
	ext
260
78
}
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 StatemintBalances = pallet_balances::Pallet<statemint_like::Runtime>;
266
pub type StatemintChainPalletXcm = pallet_xcm::Pallet<statemint_like::Runtime>;
267
pub type StatemintAssets = pallet_assets::Pallet<statemint_like::Runtime>;
268

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