referrerpolicy=no-referrer-when-downgrade
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
// Copyright (C) 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/>.

mod cli;

use std::sync::Arc;

use cli::{RelayChainCli, Subcommand, TestCollatorCli};
use cumulus_primitives_core::relay_chain::CollatorPair;
use cumulus_test_service::{chain_spec, new_partial, AnnounceBlockFn};
use sc_cli::{CliConfiguration, SubstrateCli};
use sp_core::Pair;

pub fn wrap_announce_block() -> Box<dyn FnOnce(AnnounceBlockFn) -> AnnounceBlockFn> {
	tracing::info!("Block announcements disabled.");
	Box::new(|_| {
		// Never announce any block
		Arc::new(|_, _| {})
	})
}

fn main() -> Result<(), sc_cli::Error> {
	let cli = TestCollatorCli::from_args();

	match &cli.subcommand {
		Some(Subcommand::BuildSpec(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
		},

		Some(Subcommand::ExportGenesisHead(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			runner.sync_run(|mut config| {
				let partial = new_partial(&mut config, false)?;
				cmd.run(partial.client)
			})
		},
		Some(Subcommand::ExportGenesisWasm(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			runner.sync_run(|config| cmd.run(&*config.chain_spec))
		},
		None => {
			let log_filters = cli.run.normalize().log_filters();
			let mut builder = sc_cli::LoggerBuilder::new(log_filters.unwrap_or_default());
			builder.with_colors(true);
			let _ = builder.init();

			let collator_options = cli.run.collator_options();
			let tokio_runtime = sc_cli::build_runtime()?;
			let tokio_handle = tokio_runtime.handle();
			let parachain_config = cli
				.run
				.normalize()
				.create_configuration(&cli, tokio_handle.clone())
				.expect("Should be able to generate config");

			let relay_chain_cli = RelayChainCli::new(
				&parachain_config,
				[RelayChainCli::executable_name()].iter().chain(cli.relaychain_args.iter()),
			);
			let tokio_handle = parachain_config.tokio_handle.clone();
			let relay_chain_config = SubstrateCli::create_configuration(
				&relay_chain_cli,
				&relay_chain_cli,
				tokio_handle,
			)
			.map_err(|err| format!("Relay chain argument error: {}", err))?;

			let parachain_id = chain_spec::Extensions::try_get(&*parachain_config.chain_spec)
				.map(|e| e.para_id)
				.ok_or("Could not find parachain extension in chain-spec.")?;

			tracing::info!("Parachain id: {:?}", parachain_id);
			tracing::info!(
				"Is collating: {}",
				if parachain_config.role.is_authority() { "yes" } else { "no" }
			);
			if cli.fail_pov_recovery {
				tracing::info!("PoV recovery failure enabled");
			}

			let collator_key =
				parachain_config.role.is_authority().then(|| CollatorPair::generate().0);

			let consensus = cli
				.use_null_consensus
				.then(|| {
					tracing::info!("Using null consensus.");
					cumulus_test_service::Consensus::Null
				})
				.unwrap_or(cumulus_test_service::Consensus::Aura);

			let (mut task_manager, _, _, _, _, _) = tokio_runtime
				.block_on(async move {
					match relay_chain_config.network.network_backend {
						sc_network::config::NetworkBackendType::Libp2p =>
							cumulus_test_service::start_node_impl::<
								_,
								sc_network::NetworkWorker<_, _>,
							>(
								parachain_config,
								collator_key,
								relay_chain_config,
								parachain_id.into(),
								cli.disable_block_announcements.then(wrap_announce_block),
								cli.fail_pov_recovery,
								|_| Ok(jsonrpsee::RpcModule::new(())),
								consensus,
								collator_options,
								true,
								cli.experimental_use_slot_based,
							)
							.await,
						sc_network::config::NetworkBackendType::Litep2p =>
							cumulus_test_service::start_node_impl::<
								_,
								sc_network::Litep2pNetworkBackend,
							>(
								parachain_config,
								collator_key,
								relay_chain_config,
								parachain_id.into(),
								cli.disable_block_announcements.then(wrap_announce_block),
								cli.fail_pov_recovery,
								|_| Ok(jsonrpsee::RpcModule::new(())),
								consensus,
								collator_options,
								true,
								cli.experimental_use_slot_based,
							)
							.await,
					}
				})
				.expect("could not create Cumulus test service");

			tokio_runtime
				.block_on(task_manager.future())
				.expect("Could not run service to completion");
			Ok(())
		},
	}
}