referrerpolicy=no-referrer-when-downgrade

adder_collator/
main.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot 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// Polkadot 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 Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Collator for the adder test parachain.
18
19use polkadot_cli::{Error, Result};
20use polkadot_node_primitives::CollationGenerationConfig;
21use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProtocolMessage};
22use polkadot_primitives::Id as ParaId;
23use sc_cli::{Error as SubstrateCliError, SubstrateCli};
24use sp_core::hexdisplay::HexDisplay;
25use std::{
26	collections::HashSet,
27	fs,
28	io::{self, Write},
29};
30use test_parachain_adder_collator::Collator;
31
32/// The parachain ID to collate for in case it wasn't set explicitly through CLI.
33const DEFAULT_PARA_ID: ParaId = ParaId::new(100);
34
35mod cli;
36use cli::Cli;
37
38fn main() -> Result<()> {
39	let cli = Cli::from_args();
40
41	match cli.subcommand {
42		Some(cli::Subcommand::ExportGenesisState(params)) => {
43			let collator = Collator::new();
44			let output_buf =
45				format!("0x{:?}", HexDisplay::from(&collator.genesis_head())).into_bytes();
46
47			if let Some(output) = params.output {
48				std::fs::write(output, output_buf)?;
49			} else {
50				std::io::stdout().write_all(&output_buf)?;
51			}
52
53			Ok::<_, Error>(())
54		},
55		Some(cli::Subcommand::ExportGenesisWasm(params)) => {
56			let collator = Collator::new();
57			let output_buf =
58				format!("0x{:?}", HexDisplay::from(&collator.validation_code())).into_bytes();
59
60			if let Some(output) = params.output {
61				fs::write(output, output_buf)?;
62			} else {
63				io::stdout().write_all(&output_buf)?;
64			}
65
66			Ok(())
67		},
68		None => {
69			let runner = cli.create_runner(&cli.run.base).map_err(|e| {
70				SubstrateCliError::Application(
71					Box::new(e) as Box<(dyn 'static + Send + Sync + std::error::Error)>
72				)
73			})?;
74
75			runner.run_node_until_exit(|config| async move {
76				let collator = Collator::new();
77
78				let full_node = polkadot_service::build_full(
79					config,
80					polkadot_service::NewFullParams {
81						is_parachain_node: polkadot_service::IsParachainNode::Collator(
82							collator.collator_key(),
83						),
84						enable_beefy: false,
85						force_authoring_backoff: false,
86						telemetry_worker_handle: None,
87
88						// Collators don't spawn PVF workers, so we can disable version checks.
89						node_version: None,
90						secure_validator_mode: false,
91						workers_path: None,
92						workers_names: None,
93
94						overseer_gen: polkadot_service::CollatorOverseerGen,
95						overseer_message_channel_capacity_override: None,
96						malus_finality_delay: None,
97						hwbench: None,
98						execute_workers_max_num: None,
99						prepare_workers_hard_max_num: None,
100						prepare_workers_soft_max_num: None,
101						keep_finalized_for: None,
102						invulnerable_ah_collators: HashSet::new(),
103						collator_protocol_hold_off: None,
104					},
105				)
106				.map_err(|e| e.to_string())?;
107				let mut overseer_handle = full_node
108					.overseer_handle
109					.expect("Overseer handle should be initialized for collators");
110
111				let genesis_head_hex =
112					format!("0x{:?}", HexDisplay::from(&collator.genesis_head()));
113				let validation_code_hex =
114					format!("0x{:?}", HexDisplay::from(&collator.validation_code()));
115
116				let para_id = cli.run.parachain_id.map(ParaId::from).unwrap_or(DEFAULT_PARA_ID);
117
118				log::info!("Running adder collator for parachain id: {}", para_id);
119				log::info!("Genesis state: {}", genesis_head_hex);
120				log::info!("Validation code: {}", validation_code_hex);
121
122				let config = CollationGenerationConfig {
123					key: collator.collator_key(),
124					collator: Some(
125						collator.create_collation_function(full_node.task_manager.spawn_handle()),
126					),
127					para_id,
128				};
129				overseer_handle
130					.send_msg(CollationGenerationMessage::Initialize(config), "Collator")
131					.await;
132
133				overseer_handle
134					.send_msg(CollatorProtocolMessage::CollateOn(para_id), "Collator")
135					.await;
136
137				Ok(full_node.task_manager)
138			})
139		},
140	}?;
141	Ok(())
142}