This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathintegration_test.rs
More file actions
408 lines (360 loc) · 11.4 KB
/
integration_test.rs
File metadata and controls
408 lines (360 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
// TODO: this is necessary for the jsonrpsee macro used
#![allow(unused_variables, dead_code)]
use assert_cmd::cargo::cargo_bin;
use async_std::{net, task::sleep};
use codec::Encode;
use futures::{future::FutureExt, join, pin_mut, select};
use jsonrpsee::{raw::RawClient, transport::http::HttpTransportClient};
use polkadot_primitives::parachain::{Info, Scheduling};
use polkadot_primitives::Hash as PHash;
use polkadot_runtime::{Header, OnlyStakingAndClaims, Runtime, SignedExtra, SignedPayload};
use polkadot_runtime_common::{parachains, registrar, BlockHashCount};
use serde_json::Value;
use sp_arithmetic::traits::SaturatedConversion;
use sp_runtime::generic;
use sp_version::RuntimeVersion;
use std::{
collections::HashSet,
env, fs,
io::Read,
path::PathBuf,
process::{Child, Command, Stdio},
time::Duration,
};
use substrate_test_runtime_client::AccountKeyring::Alice;
use tempfile::tempdir;
static POLKADOT_ARGS: &[&str] = &["polkadot", "--chain=res/polkadot_chainspec.json"];
static INTEGRATION_TEST_ALLOWED_TIME: Option<&str> = option_env!("INTEGRATION_TEST_ALLOWED_TIME");
jsonrpsee::rpc_api! {
Author {
#[rpc(method = "author_submitExtrinsic", positional_params)]
fn submit_extrinsic(extrinsic: String) -> PHash;
}
Chain {
#[rpc(method = "chain_getFinalizedHead")]
fn current_block_hash() -> PHash;
#[rpc(method = "chain_getHeader", positional_params)]
fn header(hash: PHash) -> Option<Header>;
#[rpc(method = "chain_getBlockHash", positional_params)]
fn block_hash(hash: Option<u64>) -> Option<PHash>;
}
State {
#[rpc(method = "state_getRuntimeVersion")]
fn runtime_version() -> RuntimeVersion;
}
System {
#[rpc(method = "system_networkState")]
fn network_state() -> Value;
}
}
// Adapted from
// https://github.com/rust-lang/cargo/blob/485670b3983b52289a2f353d589c57fae2f60f82/tests/testsuite/support/mod.rs#L507
fn target_dir() -> PathBuf {
env::current_exe()
.ok()
.map(|mut path| {
path.pop();
if path.ends_with("deps") {
path.pop();
}
path
})
.unwrap()
}
struct ChildHelper<'a> {
name: String,
child: &'a mut Child,
}
impl<'a> Drop for ChildHelper<'a> {
fn drop(&mut self) {
let name = self.name.clone();
self.terminate();
let mut stdout = String::new();
if let Some(reader) = self.child.stdout.as_mut() {
let _ = reader.read_to_string(&mut stdout);
}
eprintln!("process '{}' stdout:\n{}\n", name, stdout,);
let mut stderr = String::new();
if let Some(reader) = self.child.stderr.as_mut() {
let _ = reader.read_to_string(&mut stderr);
}
eprintln!("process '{}' stderr:\n{}\n", name, stderr,);
}
}
impl<'a> ChildHelper<'a> {
fn new(name: &str, child: &'a mut Child) -> ChildHelper<'a> {
ChildHelper {
name: name.to_string(),
child,
}
}
fn terminate(&mut self) {
match self.child.try_wait() {
Ok(Some(_)) => return,
Ok(None) => {}
Err(err) => {
eprintln!("could not wait for child process to finish: {}", err);
let _ = self.child.kill();
let _ = self.child.wait();
return;
}
}
let _ = self.child.kill();
let _ = self.child.wait();
}
}
async fn wait_for_tcp<A: net::ToSocketAddrs + std::fmt::Display>(address: A) {
while let Err(err) = net::TcpStream::connect(&address).await {
eprintln!("Waiting for {} to be up ({})...", address, err);
sleep(Duration::from_secs(2)).await;
}
}
/// wait for parachain blocks to be produced
async fn wait_for_blocks(number_of_blocks: usize, mut client: &mut RawClient<HttpTransportClient>) {
let mut previous_blocks = HashSet::with_capacity(number_of_blocks);
loop {
let current_block_hash = Chain::block_hash(&mut client, None).await.unwrap().unwrap();
if previous_blocks.insert(current_block_hash) {
eprintln!("new parachain block: {}", current_block_hash);
if previous_blocks.len() == number_of_blocks {
break;
}
}
sleep(Duration::from_secs(2)).await;
}
}
#[async_std::test]
#[ignore]
async fn integration_test() {
assert!(
!net::TcpStream::connect("127.0.0.1:27015").await.is_ok(),
"tcp port is already open 127.0.0.1:27015, this test cannot be run",
);
assert!(
!net::TcpStream::connect("127.0.0.1:27016").await.is_ok(),
"tcp port is already open 127.0.0.1:27016, this test cannot be run",
);
let t1 = sleep(Duration::from_secs(
INTEGRATION_TEST_ALLOWED_TIME
.and_then(|x| x.parse().ok())
.unwrap_or(600),
))
.fuse();
let t2 = async {
// start alice
let polkadot_alice_dir = tempdir().unwrap();
let mut polkadot_alice = Command::new(cargo_bin("cumulus-test-parachain-collator"))
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.args(POLKADOT_ARGS)
.arg("--base-path")
.arg(polkadot_alice_dir.path())
.arg("--alice")
.arg("--unsafe-rpc-expose")
.arg("--rpc-port=27015")
.arg("--port=27115")
.spawn()
.unwrap();
let polkadot_alice_helper = ChildHelper::new("alice", &mut polkadot_alice);
// start bob
let polkadot_bob_dir = tempdir().unwrap();
let mut polkadot_bob = Command::new(cargo_bin("cumulus-test-parachain-collator"))
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.args(POLKADOT_ARGS)
.arg("--base-path")
.arg(polkadot_bob_dir.path())
.arg("--bob")
.arg("--unsafe-rpc-expose")
.arg("--rpc-port=27016")
.arg("--port=27116")
.spawn()
.unwrap();
let polkadot_bob_helper = ChildHelper::new("bob", &mut polkadot_bob);
// wait for both nodes to be up and running
join!(
wait_for_tcp("127.0.0.1:27015"),
wait_for_tcp("127.0.0.1:27016")
);
// export genesis state
let cmd = Command::new(cargo_bin("cumulus-test-parachain-collator"))
.arg("export-genesis-state")
.output()
.unwrap();
assert!(cmd.status.success());
let output = &cmd.stdout;
let genesis_state = hex::decode(&output[2..output.len() - 1]).unwrap();
// connect RPC clients
let transport_client_alice =
jsonrpsee::transport::http::HttpTransportClient::new("http://127.0.0.1:27015");
let mut client_alice = jsonrpsee::raw::RawClient::new(transport_client_alice);
let transport_client_bob =
jsonrpsee::transport::http::HttpTransportClient::new("http://127.0.0.1:27016");
let mut client_bob = jsonrpsee::raw::RawClient::new(transport_client_bob);
// retrieve nodes network id
let polkadot_alice_id = System::network_state(&mut client_alice).await.unwrap()["peerId"]
.as_str()
.unwrap()
.to_string();
let polkadot_bob_id = System::network_state(&mut client_bob).await.unwrap()["peerId"]
.as_str()
.unwrap()
.to_string();
// retrieve runtime version
let runtime_version = State::runtime_version(&mut client_alice).await.unwrap();
// get the current block
let current_block_hash = Chain::block_hash(&mut client_alice, None)
.await
.unwrap()
.unwrap();
let current_block = Chain::header(&mut client_alice, current_block_hash)
.await
.unwrap()
.unwrap()
.number
.saturated_into::<u64>();
let genesis_block = Chain::block_hash(&mut client_alice, 0)
.await
.unwrap()
.unwrap();
// create and sign transaction
let wasm = fs::read(target_dir().join(
"wbuild/cumulus-test-parachain-runtime/cumulus_test_parachain_runtime.compact.wasm",
))
.unwrap();
let call = pallet_sudo::Call::sudo(Box::new(
registrar::Call::<Runtime>::register_para(
100.into(),
Info {
scheduling: Scheduling::Always,
},
wasm.into(),
genesis_state.into(),
)
.into(),
));
let nonce = 0;
let period = BlockHashCount::get()
.checked_next_power_of_two()
.map(|c| c / 2)
.unwrap_or(2) as u64;
let tip = 0;
let extra: SignedExtra = (
OnlyStakingAndClaims,
frame_system::CheckVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
frame_system::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
registrar::LimitParathreadCommits::<Runtime>::new(),
parachains::ValidateDoubleVoteReports::<Runtime>::new(),
);
let raw_payload = SignedPayload::from_raw(
call.clone().into(),
extra.clone(),
(
(),
runtime_version.spec_version,
genesis_block,
current_block_hash,
(),
(),
(),
(),
(),
),
);
let signature = raw_payload.using_encoded(|e| Alice.sign(e));
// register parachain
let ex = polkadot_runtime::UncheckedExtrinsic::new_signed(
call.into(),
Alice.into(),
sp_runtime::MultiSignature::Sr25519(signature),
extra,
);
let _register_block_hash =
Author::submit_extrinsic(&mut client_alice, format!("0x{}", hex::encode(ex.encode())))
.await
.unwrap();
// run cumulus charlie
let cumulus_charlie_dir = tempdir().unwrap();
let mut cumulus_charlie = Command::new(cargo_bin("cumulus-test-parachain-collator"))
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("--base-path")
.arg(cumulus_charlie_dir.path())
.arg("--unsafe-rpc-expose")
.arg("--rpc-port=27017")
.arg("--port=27117")
.arg("--")
.arg(format!(
"--bootnodes=/ip4/127.0.0.1/tcp/27115/p2p/{}",
polkadot_alice_id
))
.arg(format!(
"--bootnodes=/ip4/127.0.0.1/tcp/27116/p2p/{}",
polkadot_bob_id
))
.arg("--charlie")
.spawn()
.unwrap();
let cumulus_charlie_helper = ChildHelper::new("cumulus-charlie", &mut cumulus_charlie);
wait_for_tcp("127.0.0.1:27017").await;
// connect rpc client to cumulus
let transport_client_cumulus_charlie =
jsonrpsee::transport::http::HttpTransportClient::new("http://127.0.0.1:27017");
let mut client_cumulus_charlie =
jsonrpsee::raw::RawClient::new(transport_client_cumulus_charlie);
wait_for_blocks(4, &mut client_cumulus_charlie).await;
// run cumulus dave
let cumulus_dave_dir = tempdir().unwrap();
let mut cumulus_dave = Command::new(cargo_bin("cumulus-test-parachain-collator"))
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("--base-path")
.arg(cumulus_dave_dir.path())
.arg("--unsafe-rpc-expose")
.arg("--rpc-port=27018")
.arg("--port=27118")
.arg("--")
.arg(format!(
"--bootnodes=/ip4/127.0.0.1/tcp/27115/p2p/{}",
polkadot_alice_id
))
.arg(format!(
"--bootnodes=/ip4/127.0.0.1/tcp/27116/p2p/{}",
polkadot_bob_id
))
.arg("--dave")
.spawn()
.unwrap();
let cumulus_dave_helper = ChildHelper::new("cumulus-dave", &mut cumulus_dave);
wait_for_tcp("127.0.0.1:27018").await;
// connect rpc client to cumulus
let transport_client_cumulus_dave =
jsonrpsee::transport::http::HttpTransportClient::new("http://127.0.0.1:27018");
let mut client_cumulus_dave = jsonrpsee::raw::RawClient::new(transport_client_cumulus_dave);
wait_for_blocks(4, &mut client_cumulus_dave).await;
}
.fuse();
pin_mut!(t1, t2);
select! {
_ = t1 => {
panic!("the test took too long, maybe no parachain blocks have been produced");
},
_ = t2 => {},
}
}