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