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::relay_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 polkadot_runtime_parachains::configuration::{
27
	GenesisConfig as ConfigurationGenesisConfig, HostConfiguration,
28
};
29
use polkadot_runtime_parachains::paras::{
30
	GenesisConfig as ParasGenesisConfig, ParaGenesisArgs, ParaKind,
31
};
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 RELAYALICE: AccountId32 = AccountId32::new([0u8; 32]);
38
pub const RELAYBOB: AccountId32 = AccountId32::new([2u8; 32]);
39

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

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

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

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

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

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

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

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

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

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

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

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

            
137
pub const INITIAL_BALANCE: u128 = 10_000_000_000_000_000;
138

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

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

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

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

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

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

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

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

            
198
82
pub fn statemint_ext(para_id: u32) -> sp_io::TestExternalities {
199
	use statemint_like::{MsgQueue, Runtime, System};
200

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

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

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

            
222
82
pub fn relay_ext(paras: Vec<u32>) -> sp_io::TestExternalities {
223
	use relay_chain::{Runtime, System};
224

            
225
82
	let mut t = frame_system::GenesisConfig::<Runtime>::default()
226
82
		.build_storage()
227
82
		.unwrap();
228
82

            
229
82
	pallet_balances::GenesisConfig::<Runtime> {
230
82
		balances: vec![(RELAYALICE, INITIAL_BALANCE)],
231
82
	}
232
82
	.assimilate_storage(&mut t)
233
82
	.unwrap();
234
82

            
235
82
	let para_genesis: Vec<(ParaId, ParaGenesisArgs)> = paras
236
82
		.iter()
237
328
		.map(|&para_id| (para_id.into(), mock_para_genesis_info()))
238
82
		.collect();
239
82

            
240
82
	let genesis_config = ConfigurationGenesisConfig::<Runtime> {
241
82
		config: mock_relay_config(),
242
82
	};
243
82
	genesis_config.assimilate_storage(&mut t).unwrap();
244
82

            
245
82
	let genesis_config = ParasGenesisConfig::<Runtime> {
246
82
		paras: para_genesis,
247
82
		..Default::default()
248
82
	};
249
82
	genesis_config.assimilate_storage(&mut t).unwrap();
250
82

            
251
82
	let mut ext = sp_io::TestExternalities::new(t);
252
82
	ext.execute_with(|| {
253
82
		System::set_block_number(1);
254
82
	});
255
82
	ext
256
82
}
257
pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
258
pub type Hrmp = polkadot_runtime_parachains::hrmp::Pallet<relay_chain::Runtime>;
259

            
260
pub type StatemintBalances = pallet_balances::Pallet<statemint_like::Runtime>;
261
pub type StatemintChainPalletXcm = pallet_xcm::Pallet<statemint_like::Runtime>;
262
pub type StatemintAssets = pallet_assets::Pallet<statemint_like::Runtime>;
263

            
264
pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;
265
pub type Assets = pallet_assets::Pallet<parachain::Runtime, parachain::ForeignAssetInstance>;
266

            
267
pub type Treasury = pallet_treasury::Pallet<parachain::Runtime>;
268
pub type AssetManager = pallet_asset_manager::Pallet<parachain::Runtime>;
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>;
272
pub type XcmWeightTrader = pallet_xcm_weight_trader::Pallet<parachain::Runtime>;