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		Some(Subcommand::ExportChainSpec(cmd)) => {
40			let chain_spec = cli.load_spec(&cmd.chain)?;
41			cmd.run(chain_spec)
42		},
43
44		Some(Subcommand::ExportGenesisHead(cmd)) => {
45			let runner = cli.create_runner(cmd)?;
46			runner.sync_run(|mut config| {
47				let partial = new_partial(&mut config, false)?;
48				cmd.run(partial.client)
49			})
50		},
51		Some(Subcommand::ExportGenesisWasm(cmd)) => {
52			let runner = cli.create_runner(cmd)?;
53			runner.sync_run(|config| cmd.run(&*config.chain_spec))
54		},
55		None => {
56			let log_filters = cli.run.normalize().log_filters();
57			let mut builder = sc_cli::LoggerBuilder::new(log_filters.unwrap_or_default());
58			builder.with_colors(false);
59			let _ = builder.init();
60
61			let collator_options = cli.run.collator_options();
62			let tokio_runtime = sc_cli::build_runtime()?;
63			let tokio_handle = tokio_runtime.handle();
64			let parachain_config = cli
65				.run
66				.normalize()
67				.create_configuration(&cli, tokio_handle.clone())
68				.expect("Should be able to generate config");
69
70			let relay_chain_cli = RelayChainCli::new(
71				&parachain_config,
72				[RelayChainCli::executable_name()].iter().chain(cli.relaychain_args.iter()),
73			);
74			let tokio_handle = parachain_config.tokio_handle.clone();
75			let mut relay_chain_config = SubstrateCli::create_configuration(
76				&relay_chain_cli,
77				&relay_chain_cli,
78				tokio_handle,
79			)
80			.map_err(|err| format!("Relay chain argument error: {}", err))?;
81
82			// The relay chain side of a collator doesn't listen on WebRTC by default;
83			// `--force-enable-webrtc` or explicit `--listen-addr` take precedence.
84			let relay_network_params = &relay_chain_cli.base.base.network_params;
85			if parachain_config.role.is_authority() &&
86				!relay_network_params.force_enable_webrtc &&
87				relay_network_params.listen_addr.is_empty()
88			{
89				relay_chain_config.network.remove_webrtc_addresses();
90			}
91
92			tracing::info!(
93				"Is collating: {}",
94				if parachain_config.role.is_authority() { "yes" } else { "no" }
95			);
96			if cli.fail_pov_recovery {
97				tracing::info!("PoV recovery failure enabled");
98			}
99
100			let collator_key =
101				parachain_config.role.is_authority().then(|| CollatorPair::generate().0);
102
103			let use_slot_based_collator = cli.authoring == AuthoringPolicy::SlotBased;
104			let collator_reserved_slots = cli.collator_reserved_slots;
105			let (mut task_manager, _, _, _, _, _) = tokio_runtime
106				.block_on(async move {
107					match relay_chain_config.network.network_backend {
108						sc_network::config::NetworkBackendType::Libp2p => {
109							cumulus_test_service::start_node_impl::<
110								_,
111								sc_network::NetworkWorker<_, _>,
112							>(
113								parachain_config,
114								collator_key,
115								relay_chain_config,
116								cli.disable_block_announcements.then(wrap_announce_block),
117								cli.fail_pov_recovery,
118								|_| Ok(jsonrpsee::RpcModule::new(())),
119								collator_options,
120								true,
121								use_slot_based_collator,
122								collator_reserved_slots,
123							)
124							.await
125						},
126						sc_network::config::NetworkBackendType::Litep2p => {
127							cumulus_test_service::start_node_impl::<
128								_,
129								sc_network::Litep2pNetworkBackend,
130							>(
131								parachain_config,
132								collator_key,
133								relay_chain_config,
134								cli.disable_block_announcements.then(wrap_announce_block),
135								cli.fail_pov_recovery,
136								|_| Ok(jsonrpsee::RpcModule::new(())),
137								collator_options,
138								true,
139								use_slot_based_collator,
140								collator_reserved_slots,
141							)
142							.await
143						},
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}