This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathlib.rs
748 lines (670 loc) · 22.3 KB
/
lib.rs
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Cumulus 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.
// Cumulus 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
//! Crate used for testing with Cumulus.
#![warn(missing_docs)]
mod chain_spec;
mod genesis;
use core::future::Future;
use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus};
use cumulus_client_network::BlockAnnounceValidator;
use cumulus_client_service::{
prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,
};
use cumulus_primitives_core::ParaId;
use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi};
use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData};
use sc_client_api::execution_extensions::ExecutionStrategies;
use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sc_network::{config::TransportConfig, multiaddr, NetworkService};
use sc_service::{
config::{
DatabaseConfig, KeepBlocks, KeystoreConfig, MultiaddrWithPeerId, NetworkConfiguration,
OffchainWorkerConfig, PruningMode, TransactionStorageMode, WasmExecutionMethod,
},
BasePath, ChainSpec, Configuration, Error as ServiceError, PartialComponents, Role,
RpcHandlers, TFullBackend, TFullClient, TaskExecutor, TaskManager,
};
use sp_arithmetic::traits::SaturatedConversion;
use sp_blockchain::HeaderBackend;
use sp_core::{Pair, H256};
use sp_keyring::Sr25519Keyring;
use sp_runtime::{codec::Encode, generic, traits::BlakeTwo256};
use sp_state_machine::BasicExternalities;
use sp_trie::PrefixedMemoryDB;
use std::sync::Arc;
use substrate_test_client::{
BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput,
};
pub use chain_spec::*;
pub use cumulus_test_runtime as runtime;
pub use genesis::*;
pub use sp_keyring::Sr25519Keyring as Keyring;
/// A consensus that will never produce any block.
#[derive(Clone)]
struct NullConsensus;
#[async_trait::async_trait]
impl ParachainConsensus<Block> for NullConsensus {
async fn produce_candidate(
&mut self,
_: &Header,
_: PHash,
_: &PersistedValidationData,
) -> Option<ParachainCandidate<Block>> {
None
}
}
/// The signature of the announce block fn.
pub type AnnounceBlockFn = Arc<dyn Fn(Hash, Option<Vec<u8>>) + Send + Sync>;
// Native executor instance.
native_executor_instance!(
pub RuntimeExecutor,
cumulus_test_runtime::api::dispatch,
cumulus_test_runtime::native_version,
);
/// The client type being used by the test service.
pub type Client = TFullClient<runtime::NodeBlock, runtime::RuntimeApi, RuntimeExecutor>;
/// Starts a `ServiceBuilder` for a full service.
///
/// Use this macro if you don't actually need the full service, but just the builder in order to
/// be able to perform chain operations.
pub fn new_partial(
config: &mut Configuration,
) -> Result<
PartialComponents<
Client,
TFullBackend<Block>,
(),
sp_consensus::import_queue::BasicQueue<Block, PrefixedMemoryDB<BlakeTwo256>>,
sc_transaction_pool::FullPool<Block, Client>,
(),
>,
sc_service::Error,
> {
let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, RuntimeExecutor>(&config, None)?;
let client = Arc::new(client);
let registry = config.prometheus_registry();
let transaction_pool = sc_transaction_pool::BasicPool::new_full(
config.transaction_pool.clone(),
config.role.is_authority().into(),
config.prometheus_registry(),
task_manager.spawn_essential_handle(),
client.clone(),
);
let import_queue = cumulus_client_consensus_relay_chain::import_queue(
client.clone(),
client.clone(),
|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) },
&task_manager.spawn_essential_handle(),
registry.clone(),
)?;
let params = PartialComponents {
backend,
client,
import_queue,
keystore_container,
task_manager,
transaction_pool,
select_chain: (),
other: (),
};
Ok(params)
}
/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.
///
/// This is the actual implementation that is abstract over the executor and the runtime api.
#[sc_tracing::logging::prefix_logs_with(parachain_config.network.node_name.as_str())]
async fn start_node_impl<RB>(
parachain_config: Configuration,
collator_key: Option<CollatorPair>,
relay_chain_config: Configuration,
para_id: ParaId,
wrap_announce_block: Option<Box<dyn FnOnce(AnnounceBlockFn) -> AnnounceBlockFn>>,
rpc_ext_builder: RB,
consensus: Consensus,
) -> sc_service::error::Result<(
TaskManager,
Arc<TFullClient<Block, RuntimeApi, RuntimeExecutor>>,
Arc<NetworkService<Block, H256>>,
RpcHandlers,
)>
where
RB: Fn(
Arc<TFullClient<Block, RuntimeApi, RuntimeExecutor>>,
) -> jsonrpc_core::IoHandler<sc_rpc::Metadata>
+ Send
+ 'static,
{
if matches!(parachain_config.role, Role::Light) {
return Err("Light client not supported!".into());
}
let mut parachain_config = prepare_node_config(parachain_config);
let params = new_partial(&mut parachain_config)?;
let transaction_pool = params.transaction_pool.clone();
let mut task_manager = params.task_manager;
let relay_chain_full_node = polkadot_test_service::new_full(
relay_chain_config,
if let Some(ref key) = collator_key {
polkadot_service::IsCollator::Yes(key.clone())
} else {
polkadot_service::IsCollator::No
},
None,
)
.map_err(|e| match e {
polkadot_service::Error::Sub(x) => x,
s => format!("{}", s).into(),
})?;
let client = params.client.clone();
let backend = params.backend.clone();
let block_announce_validator = BlockAnnounceValidator::new(
relay_chain_full_node.client.clone(),
para_id,
Box::new(relay_chain_full_node.network.clone()),
relay_chain_full_node.backend.clone(),
relay_chain_full_node.client.clone(),
);
let block_announce_validator_builder = move |_| Box::new(block_announce_validator) as Box<_>;
let prometheus_registry = parachain_config.prometheus_registry().cloned();
let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue);
let (network, system_rpc_tx, start_network) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: ¶chain_config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
spawn_handle: task_manager.spawn_handle(),
import_queue: import_queue.clone(),
on_demand: None,
block_announce_validator_builder: Some(Box::new(block_announce_validator_builder)),
})?;
let rpc_extensions_builder = {
let client = client.clone();
Box::new(move |_, _| rpc_ext_builder(client.clone()))
};
let rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams {
on_demand: None,
remote_blockchain: None,
rpc_extensions_builder,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
task_manager: &mut task_manager,
config: parachain_config,
keystore: params.keystore_container.sync_keystore(),
backend,
network: network.clone(),
system_rpc_tx,
telemetry: None,
})?;
let announce_block = {
let network = network.clone();
Arc::new(move |hash, data| network.announce_block(hash, data))
};
let announce_block = wrap_announce_block
.map(|w| (w)(announce_block.clone()))
.unwrap_or_else(|| announce_block);
if let Some(collator_key) = collator_key {
let parachain_consensus: Box<dyn ParachainConsensus<Block>> = match consensus {
Consensus::RelayChain => {
let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(
task_manager.spawn_handle(),
client.clone(),
transaction_pool,
prometheus_registry.as_ref(),
None,
);
let relay_chain_client = relay_chain_full_node.client.clone();
let relay_chain_backend = relay_chain_full_node.backend.clone();
Box::new(
cumulus_client_consensus_relay_chain::RelayChainConsensus::new(
para_id,
proposer_factory,
move |_, (relay_parent, validation_data)| {
let parachain_inherent =
cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(
relay_parent,
&*relay_chain_client,
&*relay_chain_backend,
&validation_data,
para_id,
);
async move {
let time = sp_timestamp::InherentDataProvider::from_system_time();
let parachain_inherent = parachain_inherent.ok_or_else(|| {
Box::<dyn std::error::Error + Send + Sync>::from(String::from(
"error",
))
})?;
Ok((time, parachain_inherent))
}
},
client.clone(),
relay_chain_full_node.client.clone(),
relay_chain_full_node.backend.clone(),
),
)
}
Consensus::Null => Box::new(NullConsensus),
};
let relay_chain_full_node =
relay_chain_full_node.with_client(polkadot_test_service::TestClient);
let params = StartCollatorParams {
block_status: client.clone(),
announce_block,
client: client.clone(),
spawner: task_manager.spawn_handle(),
task_manager: &mut task_manager,
para_id,
parachain_consensus,
relay_chain_full_node: cumulus_client_service::RFullNode {
relay_chain_full_node,
collator_key,
},
import_queue,
};
start_collator(params).await?;
} else {
let relay_chain_full_node =
relay_chain_full_node.with_client(polkadot_test_service::TestClient);
let params = StartFullNodeParams {
client: client.clone(),
announce_block,
task_manager: &mut task_manager,
para_id,
relay_chain_full_node: cumulus_client_service::RFullNode {
relay_chain_full_node,
collator_key: CollatorPair::generate().0,
},
};
start_full_node(params)?;
}
start_network.start_network();
Ok((task_manager, client, network, rpc_handlers))
}
/// A Cumulus test node instance used for testing.
pub struct TestNode {
/// TaskManager's instance.
pub task_manager: TaskManager,
/// Client's instance.
pub client: Arc<Client>,
/// Node's network.
pub network: Arc<NetworkService<Block, H256>>,
/// The `MultiaddrWithPeerId` to this node. This is useful if you want to pass it as "boot node"
/// to other nodes.
pub addr: MultiaddrWithPeerId,
/// RPCHandlers to make RPC queries.
pub rpc_handlers: RpcHandlers,
}
enum Consensus {
/// Use the relay-chain provided consensus.
RelayChain,
/// Use the null consensus that will never produce any block.
Null,
}
/// A builder to create a [`TestNode`].
pub struct TestNodeBuilder {
para_id: ParaId,
task_executor: TaskExecutor,
key: Sr25519Keyring,
collator_key: Option<CollatorPair>,
parachain_nodes: Vec<MultiaddrWithPeerId>,
parachain_nodes_exclusive: bool,
relay_chain_nodes: Vec<MultiaddrWithPeerId>,
wrap_announce_block: Option<Box<dyn FnOnce(AnnounceBlockFn) -> AnnounceBlockFn>>,
storage_update_func_parachain: Option<Box<dyn Fn()>>,
storage_update_func_relay_chain: Option<Box<dyn Fn()>>,
consensus: Consensus,
}
impl TestNodeBuilder {
/// Create a new instance of `Self`.
///
/// `para_id` - The parachain id this node is running for.
/// `task_executor` - The task executor to use.
/// `key` - The key that will be used to generate the name and that will be passed as `dev_seed`.
pub fn new(para_id: ParaId, task_executor: TaskExecutor, key: Sr25519Keyring) -> Self {
TestNodeBuilder {
key,
para_id,
task_executor,
collator_key: None,
parachain_nodes: Vec::new(),
parachain_nodes_exclusive: false,
relay_chain_nodes: Vec::new(),
wrap_announce_block: None,
storage_update_func_parachain: None,
storage_update_func_relay_chain: None,
consensus: Consensus::RelayChain,
}
}
/// Enable collator for this node.
pub fn enable_collator(mut self) -> Self {
let collator_key = CollatorPair::generate().0;
self.collator_key = Some(collator_key);
self
}
/// Instruct the node to exclusively connect to registered parachain nodes.
///
/// Parachain nodes can be registered using [`Self::connect_to_parachain_node`] and
/// [`Self::connect_to_parachain_nodes`].
pub fn exclusively_connect_to_registered_parachain_nodes(mut self) -> Self {
self.parachain_nodes_exclusive = true;
self
}
/// Make the node connect to the given parachain node.
///
/// By default the node will not be connected to any node or will be able to discover any other
/// node.
pub fn connect_to_parachain_node(mut self, node: &TestNode) -> Self {
self.parachain_nodes.push(node.addr.clone());
self
}
/// Make the node connect to the given parachain nodes.
///
/// By default the node will not be connected to any node or will be able to discover any other
/// node.
pub fn connect_to_parachain_nodes<'a>(
mut self,
nodes: impl Iterator<Item = &'a TestNode>,
) -> Self {
self.parachain_nodes.extend(nodes.map(|n| n.addr.clone()));
self
}
/// Make the node connect to the given relay chain node.
///
/// By default the node will not be connected to any node or will be able to discover any other
/// node.
pub fn connect_to_relay_chain_node(
mut self,
node: &polkadot_test_service::PolkadotTestNode,
) -> Self {
self.relay_chain_nodes.push(node.addr.clone());
self
}
/// Make the node connect to the given relay chain nodes.
///
/// By default the node will not be connected to any node or will be able to discover any other
/// node.
pub fn connect_to_relay_chain_nodes<'a>(
mut self,
nodes: impl IntoIterator<Item = &'a polkadot_test_service::PolkadotTestNode>,
) -> Self {
self.relay_chain_nodes
.extend(nodes.into_iter().map(|n| n.addr.clone()));
self
}
/// Wrap the announce block function of this node.
pub fn wrap_announce_block(
mut self,
wrap: impl FnOnce(AnnounceBlockFn) -> AnnounceBlockFn + 'static,
) -> Self {
self.wrap_announce_block = Some(Box::new(wrap));
self
}
/// Allows accessing the parachain storage before the test node is built.
pub fn update_storage_parachain(mut self, updater: impl Fn() + 'static) -> Self {
self.storage_update_func_parachain = Some(Box::new(updater));
self
}
/// Allows accessing the relay chain storage before the test node is built.
pub fn update_storage_relay_chain(mut self, updater: impl Fn() + 'static) -> Self {
self.storage_update_func_relay_chain = Some(Box::new(updater));
self
}
/// Use the null consensus that will never author any block.
pub fn use_null_consensus(mut self) -> Self {
self.consensus = Consensus::Null;
self
}
/// Build the [`TestNode`].
pub async fn build(self) -> TestNode {
let parachain_config = node_config(
self.storage_update_func_parachain
.unwrap_or_else(|| Box::new(|| ())),
self.task_executor.clone(),
self.key.clone(),
self.parachain_nodes,
self.parachain_nodes_exclusive,
self.para_id,
self.collator_key.is_some(),
)
.expect("could not generate Configuration");
let mut relay_chain_config = polkadot_test_service::node_config(
self.storage_update_func_relay_chain
.unwrap_or_else(|| Box::new(|| ())),
self.task_executor,
self.key,
self.relay_chain_nodes,
false,
);
relay_chain_config.network.node_name =
format!("{} (relay chain)", relay_chain_config.network.node_name);
let multiaddr = parachain_config.network.listen_addresses[0].clone();
let (task_manager, client, network, rpc_handlers) = start_node_impl(
parachain_config,
self.collator_key,
relay_chain_config,
self.para_id,
self.wrap_announce_block,
|_| Default::default(),
self.consensus,
)
.await
.expect("could not create Cumulus test service");
let peer_id = network.local_peer_id().clone();
let addr = MultiaddrWithPeerId { multiaddr, peer_id };
TestNode {
task_manager,
client,
network,
addr,
rpc_handlers,
}
}
}
/// Create a Cumulus `Configuration`.
///
/// By default an in-memory socket will be used, therefore you need to provide nodes if you want the
/// node to be connected to other nodes. If `nodes_exclusive` is `true`, the node will only connect
/// to the given `nodes` and not to any other node. The `storage_update_func` can be used to make
/// adjustments to the runtime genesis.
pub fn node_config(
storage_update_func: impl Fn(),
task_executor: TaskExecutor,
key: Sr25519Keyring,
nodes: Vec<MultiaddrWithPeerId>,
nodes_exlusive: bool,
para_id: ParaId,
is_collator: bool,
) -> Result<Configuration, ServiceError> {
let base_path = BasePath::new_temp_dir()?;
let root = base_path.path().to_path_buf();
let role = if is_collator {
Role::Authority
} else {
Role::Full
};
let key_seed = key.to_seed();
let mut spec = Box::new(chain_spec::get_chain_spec(para_id));
let mut storage = spec
.as_storage_builder()
.build_storage()
.expect("could not build storage");
BasicExternalities::execute_with_storage(&mut storage, storage_update_func);
spec.set_storage(storage);
let mut network_config = NetworkConfiguration::new(
format!("{} (parachain)", key_seed.to_string()),
"network/test/0.1",
Default::default(),
None,
);
if nodes_exlusive {
network_config.default_peers_set.reserved_nodes = nodes;
network_config.default_peers_set.non_reserved_mode =
sc_network::config::NonReservedPeerMode::Deny;
} else {
network_config.boot_nodes = nodes;
}
network_config.allow_non_globals_in_dht = true;
network_config
.listen_addresses
.push(multiaddr::Protocol::Memory(rand::random()).into());
network_config.transport = TransportConfig::MemoryOnly;
Ok(Configuration {
impl_name: "cumulus-test-node".to_string(),
impl_version: "0.1".to_string(),
role,
task_executor,
transaction_pool: Default::default(),
network: network_config,
keystore: KeystoreConfig::InMemory,
keystore_remote: Default::default(),
database: DatabaseConfig::RocksDb {
path: root.join("db"),
cache_size: 128,
},
state_cache_size: 67108864,
state_cache_child_ratio: None,
state_pruning: PruningMode::ArchiveAll,
keep_blocks: KeepBlocks::All,
transaction_storage: TransactionStorageMode::BlockBody,
chain_spec: spec,
wasm_method: WasmExecutionMethod::Interpreted,
// NOTE: we enforce the use of the native runtime to make the errors more debuggable
execution_strategies: ExecutionStrategies {
syncing: sc_client_api::ExecutionStrategy::NativeWhenPossible,
importing: sc_client_api::ExecutionStrategy::NativeWhenPossible,
block_construction: sc_client_api::ExecutionStrategy::NativeWhenPossible,
offchain_worker: sc_client_api::ExecutionStrategy::NativeWhenPossible,
other: sc_client_api::ExecutionStrategy::NativeWhenPossible,
},
rpc_http: None,
rpc_ws: None,
rpc_ipc: None,
rpc_ws_max_connections: None,
rpc_http_threads: None,
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_payload: None,
prometheus_config: None,
telemetry_endpoints: None,
telemetry_external_transport: None,
default_heap_pages: None,
offchain_worker: OffchainWorkerConfig {
enabled: true,
indexing_enabled: false,
},
force_authoring: false,
disable_grandpa: false,
dev_key_seed: Some(key_seed),
tracing_targets: None,
tracing_receiver: Default::default(),
max_runtime_instances: 8,
announce_block: true,
base_path: Some(base_path),
informant_output_format: Default::default(),
wasm_runtime_overrides: None,
disable_log_reloading: false,
})
}
impl TestNode {
/// Wait for `count` blocks to be imported in the node and then exit. This function will not
/// return if no blocks are ever created, thus you should restrict the maximum amount of time of
/// the test execution.
pub fn wait_for_blocks(&self, count: usize) -> impl Future<Output = ()> {
self.client.wait_for_blocks(count)
}
/// Send an extrinsic to this node.
pub async fn send_extrinsic(
&self,
function: impl Into<runtime::Call>,
caller: Sr25519Keyring,
) -> Result<RpcTransactionOutput, RpcTransactionError> {
let extrinsic = construct_extrinsic(&*self.client, function, caller);
self.rpc_handlers.send_transaction(extrinsic.into()).await
}
/// Register a parachain at this relay chain.
pub async fn schedule_upgrade(&self, validation: Vec<u8>) -> Result<(), RpcTransactionError> {
let call = frame_system::Call::set_code_without_checks(validation);
self.send_extrinsic(
runtime::SudoCall::sudo_unchecked_weight(Box::new(call.into()), 1_000),
Sr25519Keyring::Alice,
)
.await
.map(drop)
}
}
/// Construct an extrinsic that can be applied to the test runtime.
pub fn construct_extrinsic(
client: &Client,
function: impl Into<runtime::Call>,
caller: Sr25519Keyring,
) -> runtime::UncheckedExtrinsic {
let function = function.into();
let current_block_hash = client.info().best_hash;
let current_block = client.info().best_number.saturated_into();
let genesis_block = client.hash(0).unwrap().unwrap();
let nonce = 0;
let period = runtime::BlockHashCount::get()
.checked_next_power_of_two()
.map(|c| c / 2)
.unwrap_or(2) as u64;
let tip = 0;
let extra: runtime::SignedExtra = (
frame_system::CheckSpecVersion::<runtime::Runtime>::new(),
frame_system::CheckGenesis::<runtime::Runtime>::new(),
frame_system::CheckEra::<runtime::Runtime>::from(generic::Era::mortal(
period,
current_block,
)),
frame_system::CheckNonce::<runtime::Runtime>::from(nonce),
frame_system::CheckWeight::<runtime::Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<runtime::Runtime>::from(tip),
);
let raw_payload = runtime::SignedPayload::from_raw(
function.clone(),
extra.clone(),
(
runtime::VERSION.spec_version,
genesis_block,
current_block_hash,
(),
(),
(),
),
);
let signature = raw_payload.using_encoded(|e| caller.sign(e));
runtime::UncheckedExtrinsic::new_signed(
function.clone(),
caller.public().into(),
runtime::Signature::Sr25519(signature.clone()),
extra.clone(),
)
}
/// Run a relay-chain validator node.
///
/// This is essentially a wrapper around
/// [`run_validator_node`](polkadot_test_service::run_validator_node).
pub fn run_relay_chain_validator_node(
task_executor: TaskExecutor,
key: Sr25519Keyring,
storage_update_func: impl Fn(),
boot_nodes: Vec<MultiaddrWithPeerId>,
) -> polkadot_test_service::PolkadotTestNode {
polkadot_test_service::run_validator_node(
task_executor,
key,
storage_update_func,
boot_nodes,
Some(cumulus_test_relay_validation_worker_provider::VALIDATION_WORKER.into()),
)
}