cumulus_client_consensus_aura/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5// Cumulus is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9
10// Cumulus is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with Cumulus. If not, see <https://www.gnu.org/licenses/>.
17
18//! The AuRa consensus algorithm for parachains.
19//!
20//! This extends the Substrate provided AuRa consensus implementation to make it compatible for
21//! parachains.
22//!
23//! For more information about AuRa, the Substrate crate should be checked.
24
25use codec::Encode;
26use cumulus_primitives_core::PersistedValidationData;
27
28use cumulus_primitives_core::relay_chain::HeadData;
29use polkadot_primitives::{BlockNumber as RBlockNumber, Hash as RHash};
30use sp_runtime::traits::{Block as BlockT, NumberFor};
31use std::{fs, fs::File, path::PathBuf};
32
33mod import_queue;
34
35pub use import_queue::{build_verifier, import_queue, BuildVerifierParams, ImportQueueParams};
36use polkadot_node_primitives::PoV;
37pub use sc_consensus_aura::{
38	slot_duration, standalone::slot_duration_at, AuraVerifier, BuildAuraWorkerParams,
39	SlotProportion,
40};
41pub use sc_consensus_slots::InherentDataProviderExt;
42
43pub mod collator;
44pub mod collators;
45pub mod equivocation_import_queue;
46
47const LOG_TARGET: &str = "aura::cumulus";
48
49/// Export the given `pov` to the file system at `path`.
50///
51/// The file will be named `block_hash_block_number.pov`.
52///
53/// The `parent_header`, `relay_parent_storage_root` and `relay_parent_number` will also be
54/// stored in the file alongside the `pov`. This enables stateless validation of the `pov`.
55pub(crate) fn export_pov_to_path<Block: BlockT>(
56	path: PathBuf,
57	pov: PoV,
58	block_hash: Block::Hash,
59	block_number: NumberFor<Block>,
60	parent_header: Block::Header,
61	relay_parent_storage_root: RHash,
62	relay_parent_number: RBlockNumber,
63	max_pov_size: u32,
64) {
65	if let Err(error) = fs::create_dir_all(&path) {
66		tracing::error!(target: LOG_TARGET, %error, path = %path.display(), "Failed to create PoV export directory");
67		return
68	}
69
70	let mut file = match File::create(path.join(format!("{block_hash:?}_{block_number}.pov"))) {
71		Ok(f) => f,
72		Err(error) => {
73			tracing::error!(target: LOG_TARGET, %error, "Failed to export PoV.");
74			return
75		},
76	};
77
78	pov.encode_to(&mut file);
79	PersistedValidationData {
80		parent_head: HeadData(parent_header.encode()),
81		relay_parent_number,
82		relay_parent_storage_root,
83		max_pov_size,
84	}
85	.encode_to(&mut file);
86}