referrerpolicy=no-referrer-when-downgrade

test_parachain/
main.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3
4// Cumulus 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// Cumulus 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 Cumulus.  If not, see <http://www.gnu.org/licenses/>.
16
17mod cli;
18
19use std::sync::Arc;
20
21use cli::{AuthoringPolicy, RelayChainCli, Subcommand, TestCollatorCli};
22use cumulus_primitives_core::relay_chain::CollatorPair;
23use cumulus_test_service::{new_partial, AnnounceBlockFn};
24use sc_cli::{CliConfiguration, SubstrateCli};
25use sp_core::Pair;
26
27pub fn wrap_announce_block() -> Box<dyn FnOnce(AnnounceBlockFn) -> AnnounceBlockFn> {
28	tracing::info!("Block announcements disabled.");
29	Box::new(|_| {
30		// Never announce any block
31		Arc::new(|_, _| {})
32	})
33}
34
35fn main() -> Result<(), sc_cli::Error> {
36	let cli = TestCollatorCli::from_args();
37
38	match &cli.subcommand {
39		#[allow(deprecated)]
40		Some(Subcommand::BuildSpec(cmd)) => {
41			let runner = cli.create_runner(cmd)?;
42			runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
43		},
44
45		Some(Subcommand::ExportChainSpec(cmd)) => {
46			let chain_spec = cli.load_spec(&cmd.chain)?;
47			cmd.run(chain_spec)
48		},
49
50		Some(Subcommand::ExportGenesisHead(cmd)) => {
51			let runner = cli.create_runner(cmd)?;
52			runner.sync_run(|mut config| {
53				let partial = new_partial(&mut config, false)?;
54				cmd.run(partial.client)
55			})
56		},
57		Some(Subcommand::ExportGenesisWasm(cmd)) => {
58			let runner = cli.create_runner(cmd)?;
59			runner.sync_run(|config| cmd.run(&*config.chain_spec))
60		},
61		None => {
62			let log_filters = cli.run.normalize().log_filters();
63			let mut builder = sc_cli::LoggerBuilder::new(log_filters.unwrap_or_default());
64			builder.with_colors(false);
65			let _ = builder.init();
66
67			let collator_options = cli.run.collator_options();
68			let tokio_runtime = sc_cli::build_runtime()?;
69			let tokio_handle = tokio_runtime.handle();
70			let parachain_config = cli
71				.run
72				.normalize()
73				.create_configuration(&cli, tokio_handle.clone())
74				.expect("Should be able to generate config");
75
76			let relay_chain_cli = RelayChainCli::new(
77				&parachain_config,
78				[RelayChainCli::executable_name()].iter().chain(cli.relaychain_args.iter()),
79			);
80			let tokio_handle = parachain_config.tokio_handle.clone();
81			let relay_chain_config = SubstrateCli::create_configuration(
82				&relay_chain_cli,
83				&relay_chain_cli,
84				tokio_handle,
85			)
86			.map_err(|err| format!("Relay chain argument error: {}", err))?;
87
88			tracing::info!(
89				"Is collating: {}",
90				if parachain_config.role.is_authority() { "yes" } else { "no" }
91			);
92			if cli.fail_pov_recovery {
93				tracing::info!("PoV recovery failure enabled");
94			}
95
96			let collator_key =
97				parachain_config.role.is_authority().then(|| CollatorPair::generate().0);
98
99			let consensus = cli
100				.use_null_consensus
101				.then(|| {
102					tracing::info!("Using null consensus.");
103					cumulus_test_service::Consensus::Null
104				})
105				.unwrap_or(cumulus_test_service::Consensus::Aura);
106			let use_slot_based_collator = cli.authoring == AuthoringPolicy::SlotBased;
107			let (mut task_manager, _, _, _, _, _) = tokio_runtime
108				.block_on(async move {
109					match relay_chain_config.network.network_backend {
110						sc_network::config::NetworkBackendType::Libp2p =>
111							cumulus_test_service::start_node_impl::<
112								_,
113								sc_network::NetworkWorker<_, _>,
114							>(
115								parachain_config,
116								collator_key,
117								relay_chain_config,
118								cli.disable_block_announcements.then(wrap_announce_block),
119								cli.fail_pov_recovery,
120								|_| Ok(jsonrpsee::RpcModule::new(())),
121								consensus,
122								collator_options,
123								true,
124								use_slot_based_collator,
125							)
126							.await,
127						sc_network::config::NetworkBackendType::Litep2p =>
128							cumulus_test_service::start_node_impl::<
129								_,
130								sc_network::Litep2pNetworkBackend,
131							>(
132								parachain_config,
133								collator_key,
134								relay_chain_config,
135								cli.disable_block_announcements.then(wrap_announce_block),
136								cli.fail_pov_recovery,
137								|_| Ok(jsonrpsee::RpcModule::new(())),
138								consensus,
139								collator_options,
140								true,
141								use_slot_based_collator,
142							)
143							.await,
144					}
145				})
146				.expect("could not create Cumulus test service");
147
148			tokio_runtime
149				.block_on(task_manager.future())
150				.expect("Could not run service to completion");
151			Ok(())
152		},
153	}
154}