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

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

            
64
78
pub fn mock_relay_config() -> HostConfiguration<relay_chain::BlockNumber> {
65
78
	HostConfiguration::<relay_chain::BlockNumber> {
66
78
		hrmp_channel_max_capacity: u32::MAX,
67
78
		hrmp_channel_max_total_size: u32::MAX,
68
78
		hrmp_max_parachain_inbound_channels: 10,
69
78
		hrmp_max_parachain_outbound_channels: 10,
70
78
		hrmp_channel_max_message_size: u32::MAX,
71
78
		// Changed to avoid aritmetic errors within hrmp_close
72
78
		max_downward_message_size: 100_000u32,
73
78
		..Default::default()
74
78
	}
75
78
}
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
234
pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
143
	use parachain::{MsgQueue, Runtime, System};
144

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
251
78
	let mut ext = sp_io::TestExternalities::new(t);
252
78
	ext.execute_with(|| {
253
78
		System::set_block_number(1);
254
78
	});
255
78
	ext
256
78
}
257

            
258
pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
259
pub type Hrmp = polkadot_runtime_parachains::hrmp::Pallet<relay_chain::Runtime>;
260

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

            
265
pub type Assets = pallet_assets::Pallet<parachain::Runtime, parachain::ForeignAssetInstance>;
266
pub type Treasury = pallet_treasury::Pallet<parachain::Runtime>;
267
pub type AssetManager = pallet_asset_manager::Pallet<parachain::Runtime>;
268
pub type RelayBalances = pallet_balances::Pallet<relay_chain::Runtime>;
269
pub type ParaBalances = pallet_balances::Pallet<parachain::Runtime>;
270
pub type XcmTransactor = pallet_xcm_transactor::Pallet<parachain::Runtime>;
271
pub type XcmWeightTrader = pallet_xcm_weight_trader::Pallet<parachain::Runtime>;