// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezcumulus.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// Pezcumulus 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.
// Pezcumulus 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 Pezcumulus. If not, see .
//! The core collator logic for Aura - slot claiming, block proposing, and collation
//! packaging.
//!
//! The [`Collator`] struct exposed here is meant to be a component of higher-level logic
//! which actually manages the control flow of the collator - which slots to claim, how
//! many collations to build, when to work, etc.
//!
//! This module also exposes some standalone functions for common operations when building
//! aura-based collators.
use codec::Codec;
use pezcumulus_client_collator::service::ServiceInterface as CollatorServiceInterface;
use pezcumulus_client_consensus_common::{
self as consensus_common, TeyrchainBlockImportMarker, TeyrchainCandidate,
};
use pezcumulus_client_consensus_proposer::ProposerInterface;
use pezcumulus_client_teyrchain_inherent::{TeyrchainInherentData, TeyrchainInherentDataProvider};
use pezcumulus_primitives_core::{
relay_chain::Hash as PHash, DigestItem, PersistedValidationData, TeyrchainBlockData,
};
use pezcumulus_relay_chain_interface::RelayChainInterface;
use pezkuwi_node_primitives::{Collation, MaybeCompressedPoV};
use pezkuwi_primitives::{Header as PHeader, Id as ParaId};
use crate::collators::RelayParentData;
use futures::prelude::*;
use pezsc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy, StateAction};
use pezsc_consensus_aura::standalone as aura_internal;
use pezsc_network_types::PeerId;
use pezsp_api::ProvideRuntimeApi;
use pezsp_application_crypto::AppPublic;
use pezsp_consensus::BlockOrigin;
use pezsp_consensus_aura::{AuraApi, Slot, SlotDuration};
use pezsp_core::crypto::Pair;
use pezsp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use pezsp_keystore::KeystorePtr;
use pezsp_runtime::{
generic::Digest,
traits::{Block as BlockT, HashingFor, Header as HeaderT, Member},
};
use pezsp_state_machine::StorageChanges;
use pezsp_timestamp::Timestamp;
use std::{error::Error, time::Duration};
/// Parameters for instantiating a [`Collator`].
pub struct Params {
/// A builder for inherent data builders.
pub create_inherent_data_providers: CIDP,
/// The block import handle.
pub block_import: BI,
/// An interface to the relay-chain client.
pub relay_client: RClient,
/// The keystore handle used for accessing teyrchain key material.
pub keystore: KeystorePtr,
/// The collator network peer id.
pub collator_peer_id: PeerId,
/// The identifier of the teyrchain within the relay-chain.
pub para_id: ParaId,
/// The block proposer used for building blocks.
pub proposer: Proposer,
/// The collator service used for bundling proposals into collations and announcing
/// to the network.
pub collator_service: CS,
}
/// A utility struct for writing collation logic that makes use of Aura entirely
/// or in part. See module docs for more details.
pub struct Collator {
create_inherent_data_providers: CIDP,
block_import: BI,
relay_client: RClient,
keystore: KeystorePtr,
para_id: ParaId,
proposer: Proposer,
collator_service: CS,
_marker: std::marker::PhantomData<(Block, Box)>,
}
impl Collator
where
Block: BlockT,
RClient: RelayChainInterface,
CIDP: CreateInherentDataProviders + 'static,
BI: BlockImport + TeyrchainBlockImportMarker + Send + Sync + 'static,
Proposer: ProposerInterface,
CS: CollatorServiceInterface,
P: Pair,
P::Public: AppPublic + Member,
P::Signature: TryFrom> + Member + Codec,
{
/// Instantiate a new instance of the `Aura` manager.
pub fn new(params: Params) -> Self {
Collator {
create_inherent_data_providers: params.create_inherent_data_providers,
block_import: params.block_import,
relay_client: params.relay_client,
keystore: params.keystore,
para_id: params.para_id,
proposer: params.proposer,
collator_service: params.collator_service,
_marker: std::marker::PhantomData,
}
}
/// Explicitly creates the inherent data for teyrchain block authoring and overrides
/// the timestamp inherent data with the one provided, if any. Additionally allows to specify
/// relay parent descendants that can be used to prevent authoring at the tip of the relay
/// chain.
pub async fn create_inherent_data_with_rp_offset(
&self,
relay_parent: PHash,
validation_data: &PersistedValidationData,
parent_hash: Block::Hash,
timestamp: impl Into