Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Consensus utilities and rearchitecture for more dynamic collators #2382

Merged
merged 34 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ea03cb6
implement a proposer utility for consensus
rphmeier Mar 25, 2023
1a4ef13
tidy up deps of new proposer crate
rphmeier Mar 25, 2023
2ce534d
implement a collator-service crate
rphmeier Mar 25, 2023
e1980de
rewrite cumulus-collator to use new service struct
rphmeier Mar 25, 2023
936cc7f
implement a module for relay-chain-driven collators
rphmeier Mar 25, 2023
b8fd4db
adapt start_collator to use the new relay_chain_driven module
rphmeier Mar 25, 2023
24a43dc
move collator-service to a public submodule
rphmeier Mar 26, 2023
eebad21
create an interface trait for the proposer
rphmeier Mar 27, 2023
a179033
begin aura reimplementation
rphmeier Mar 31, 2023
6e7fc8d
address review comments
rphmeier Mar 31, 2023
29d7ed8
update substrrate git ref
rphmeier Mar 31, 2023
37b0152
update polkadot-primitives refs
rphmeier Mar 31, 2023
5fbd533
rough draft of aura collation using standalone fns
rphmeier Mar 31, 2023
72362e3
add a ServiceInterface
rphmeier Mar 31, 2023
c0c367b
port aura reimpl to use new service trait
rphmeier Mar 31, 2023
ed0b09e
add an import queue utility crate
rphmeier Apr 6, 2023
7553563
remove import queue crate in favor of module in common
rphmeier Apr 6, 2023
6261829
implement new verification queue for aura
rphmeier Apr 7, 2023
7e4f235
implement remaining behaviors
rphmeier Apr 7, 2023
e3d257b
Merge branch 'master' into rh-new-consensus
rphmeier Apr 10, 2023
b2697f5
split 'collate' into smaller functions that could be pub
rphmeier Apr 18, 2023
93eea31
add telemetry
rphmeier Apr 20, 2023
58dfa16
fix doc job?
rphmeier Apr 29, 2023
fa6ca12
Specify async-trait patch version
rphmeier May 4, 2023
8c50bc7
remove 'fn@' in doc string.
rphmeier May 4, 2023
d083d46
update variable names to be more readable
rphmeier May 4, 2023
604ba8a
refactor proposer errors to anyhow/thiserror
rphmeier May 4, 2023
460715e
remove manual span instrumentation
rphmeier May 4, 2023
8266532
make slot_claim private
rphmeier May 4, 2023
496c688
fix unused import
rphmeier May 4, 2023
32e3b28
fmt
rphmeier May 6, 2023
83b4b90
Merge branch 'master' into rh-new-consensus
rphmeier May 13, 2023
50c1f62
fmt
rphmeier May 13, 2023
8bc4ba4
make clippy happy
rphmeier May 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
begin aura reimplementation
  • Loading branch information
rphmeier committed Mar 31, 2023
commit a1790335ba6ccfd38493bf4e812b8d17f48b9e8e
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions client/consensus/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-keystore = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" }
substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate", branch = "master" }

# Cumulus
cumulus-client-consensus-common = { path = "../common" }
cumulus-client-consensus-proposer = { path = "../proposer" }
cumulus-primitives-core = { path = "../../../primitives/core" }
cumulus-client-collator = { path = "../../collator" }

# Polkadot
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" }
polkadot-overseer = { git = "https://github.com/paritytech/polkadot", branch = "master" }
2 changes: 2 additions & 0 deletions client/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub use import_queue::{build_verifier, import_queue, BuildVerifierParams, Import
pub use sc_consensus_aura::{slot_duration, AuraVerifier, BuildAuraWorkerParams, SlotProportion};
pub use sc_consensus_slots::InherentDataProviderExt;

pub mod unstable_reimpl;

const LOG_TARGET: &str = "aura::cumulus";

/// The implementation of the AURA consensus for parachains.
Expand Down
173 changes: 173 additions & 0 deletions client/consensus/aura/src/unstable_reimpl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright 2023 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/>.

//! The AuRa consensus algorithm for parachains.
//!
//! This extends the Substrate provided AuRa consensus implementation to make it compatible for
//! parachains. The main entry points for of this consensus algorithm are [`fn@run`]
//! and [`fn@import_queue`].
//!
//! For more information about AuRa, the Substrate crate should be checked.

use codec::{Decode, Encode};
use cumulus_client_consensus_common::{
ParachainBlockImportMarker, ParachainCandidate, ParachainConsensus,
};
use cumulus_client_consensus_proposer::ProposerInterface;
use cumulus_client_collator::service::CollatorService;
use cumulus_primitives_core::{relay_chain::Hash as PHash, PersistedValidationData};

use polkadot_overseer::Handle as OverseerHandle;
use polkadot_primitives::{CollatorPair, Id as ParaId};

use futures::{prelude::*, lock::Mutex};
use sc_client_api::{backend::AuxStore, BlockBackend, BlockOf};
use sc_consensus::BlockImport;
use sc_consensus_aura::SlotProportion;
use sc_consensus_slots::{BackoffAuthoringBlocksStrategy, SimpleSlotWorker, SlotInfo};
use sc_telemetry::TelemetryHandle;
use sp_api::ProvideRuntimeApi;
use sp_application_crypto::AppPublic;
use sp_blockchain::HeaderBackend;
use sp_consensus::{EnableProofRecording, Environment, ProofRecording, Proposal, Proposer, SyncOracle};
use sp_consensus_aura::{AuraApi, SlotDuration};
use sp_core::crypto::Pair;
use sp_inherents::{CreateInherentDataProviders, InherentData};
use sp_keystore::KeystorePtr;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Member, NumberFor};
use sp_runtime::Digest;
use std::{convert::TryFrom, hash::Hash, marker::PhantomData, sync::Arc, time::Duration};

/// Parameters of [`run`].
pub struct Params<Block: BlockT, BI, CIDP, Client, SO, Proposer> {
pub create_inherent_data_providers: CIDP,
pub block_import: BI,
pub para_client: Arc<Client>,
pub sync_oracle: SO,
pub keystore: KeystorePtr,
pub key: CollatorPair,
pub para_id: ParaId,
pub overseer_handle: OverseerHandle,
pub slot_duration: SlotDuration,
pub telemetry: Option<TelemetryHandle>,
pub block_proposal_slot_portion: SlotProportion,
pub max_block_proposal_slot_portion: Option<SlotProportion>,
pub service: CollatorService<Block, Client, Client>,
pub proposer: Proposer,
}

/// Run Aura consensus as a parachain.
pub async fn run<Block, P, BI, CIDP, Client, SO, Proposer>(
params: Params<Block, BI, CIDP, Client, SO, Proposer>,
)
where
Block: BlockT,
Client:
ProvideRuntimeApi<Block> + BlockOf + AuxStore + HeaderBackend<Block> + BlockBackend<Block> + Send + Sync + 'static,
Client::Api: AuraApi<Block, P::Public>,
CIDP: CreateInherentDataProviders<B, (PHash, PersistedValidationData)> + 'static,
CIDP::InherentDataProviders: InherentDataProviderExt,
BI: BlockImport<Block, Transaction = sp_api::TransactionFor<Client, Block>>
+ ParachainBlockImportMarker
+ Send
+ Sync
+ 'static,
SO: SyncOracle + Send + Sync + Clone + 'static,
Proposer: ProposerInterface<Block, Transaction = sp_api::TransactionFor<Client, Block>>,
Proposer::Transaction: Sync,
P: Pair + Send + Sync,
P::Public: AppPublic + Hash + Member + Encode + Decode,
P::Signature: TryFrom<Vec<u8>> + Hash + Member + Encode + Decode,
{
let mut worker = sc_consensus_aura::build_aura_worker::<P, _, _, _, _, _, _, _, _>(
sc_consensus_aura::BuildAuraWorkerParams {
client: params.para_client,
block_import: params.block_import,
justification_sync_link: (),
proposer_factory: FakeSubstrateEnvironment(std::marker::PhantomData::<Proposer::Transaction>),
sync_oracle: params.sync_oracle,
force_authoring: false, // TODO [now]: how useful is this for parachains?
backoff_authoring_blocks: Option::<()>::None, // TODO [now]: how useful is this for parachains?
keystore: params.keystore,
telemetry: params.telemetry,
block_proposal_slot_portion: params.block_proposal_slot_portion,
max_block_proposal_slot_portion: params.max_block_proposal_slot_portion,
compatibility_mode: sc_consensus_aura::CompatibilityMode::None,
},
);

let mut collator_service = params.service;
let mut proposer = params.proposer;

let mut collation_requests = cumulus_client_collator::relay_chain_driven::init(
params.key,
params.para_id,
params.overseer_handle,
).await;

while let Some(request) = collation_requests.next().await {
collate(&mut worker, &mut proposer).await;
}
}

// Collate a block using the aura slot worker.
async fn collate<Block: BlockT, S, CIDP>(
slot_worker: &mut S,
proposer: &mut impl ProposerInterface<Block>,
create_inherent_data_providers: &CIDP,
)
where
Block: BlockT,
S: SimpleSlotWorker<Block>,
CIDP: CreateInherentDataProviders<Block, ()>,
{

}

// A fake environment which will never be invoked, as we don't use this part of the
// Aura slot worker.
struct FakeSubstrateEnvironment<T: Send + 'static>(std::marker::PhantomData<T>);
// A fake proposer which will never be invoked, as we don't use this part of the
// Aura slot worker.
struct FakeProposer<T: Send + 'static>(std::marker::PhantomData<T>);

impl<Block: BlockT, T: Default + Send + 'static> Environment<Block> for FakeSubstrateEnvironment<T> {
type Proposer = FakeProposer<T>;
type CreateProposer = futures::future::Pending<Result<Self::Proposer, Self::Error>>;
type Error = sp_consensus::Error;

fn init(&mut self, parent_header: &Block::Header) -> Self::CreateProposer {
futures::future::pending()
}
}

impl<Block: BlockT, T: Default + Send + 'static> Proposer<Block> for FakeProposer<T> {
type Error = sp_consensus::Error;
type Transaction = T;
type Proposal = futures::future::Pending<Result<Proposal<Block, Self::Transaction, Self::Proof>, Self::Error>>;
type ProofRecording = EnableProofRecording;
type Proof = sp_state_machine::StorageProof;

fn propose(
self,
_inherent_data: InherentData,
_inherent_digests: Digest,
_max_duration: Duration,
_block_size_limit: Option<usize>,
) -> Self::Proposal {
futures::future::pending()
}
}
2 changes: 1 addition & 1 deletion client/consensus/proposer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ enum ErrorInner {
#[async_trait]
pub trait ProposerInterface<Block: BlockT> {
/// The underlying DB transaction type produced with the block proposal.
type Transaction;
type Transaction: Default + Send + 'static;

/// Propose a collation using the supplied `InherentData` and the provided
/// `ParachainInherentData`.
Expand Down