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

Parachain Consensus abstractions #329

Merged
merged 10 commits into from
Feb 16, 2021
Merged

Parachain Consensus abstractions #329

merged 10 commits into from
Feb 16, 2021

Conversation

bkchr
Copy link
Member

@bkchr bkchr commented Feb 15, 2021

This pr introduces abstractions around the parachain consensus that is driven by the collator. It moves the current implementation into the RelayChainConsensus as a consensus that is entirely dependent on the relay chain.

Fixes: #36

@bkchr bkchr requested a review from pepyakin February 15, 2021 20:01
//! total number of parachain validators.
//!
//! 4. The parachain candidate with the highest number of approvals is choosen by the relay-chain
//! block producer to be backed.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approvals means supporting parachain validators I guess.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, prefer the term backing as approval means something else in parachain consensus.

///
/// # NOTE
///
/// It is expected that the block is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing

Copy link
Contributor

@pepyakin pepyakin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. I am not sure how it fixes #36 though

@@ -343,14 +260,15 @@ where
relay_parent: PHash,
validation_data: PersistedValidationData,
) -> Option<CollationResult> {
trace!(target: LOG_TARGET, "Producing candidate");
tracing::trace!(target: LOG_TARGET, "Producing candidate");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth adding relay_parent tag here?

}

impl<Block: BlockT, PF, BI, BS, Backend, PBackend, PClient, PBackend2> Clone
for Collator<Block, PF, BI, BS, Backend, PBackend, PClient, PBackend2>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay!

@bkchr
Copy link
Member Author

bkchr commented Feb 16, 2021

Looks good overall. I am not sure how it fixes #36 though

#36 was a rather shitty issue. In the end I settled with myself on requiring that people will need to implement ParachainConsensus for each consensus. Maybe I will provide an implementation of ParachainConsensus for T where T: Slots, but for that I will first need to have some examples :D

@bkchr bkchr merged commit 4820fa1 into master Feb 16, 2021
@bkchr bkchr deleted the bkchr-parachain-consensus branch February 16, 2021 11:45
Copy link
Contributor

@JoshOrndorff JoshOrndorff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a little late here, but I'll submit this anyway. Thanks for this PR.

/// implementation could for example check if this specific collator is part of the validator.
#[async_trait::async_trait]
pub trait ParachainConsensus<B: BlockT>: Send + Sync + dyn_clone::DynClone {
/// Produce a new candidate at the given parent block.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Produce a new candidate at the given parent block.
/// Produce a new candidate at the given parent and relay-parent blocks.

Comment on lines +523 to +526
/// The collator will call [`Self::produce_candidate`] every time there is a free core for the parachain
/// this collator is collating for. It is the job of the consensus implementation to decide if this
/// specific collator should build candidate for the given relay chain block. The consensus
/// implementation could for example check if this specific collator is part of the validator.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The collator will call [`Self::produce_candidate`] every time there is a free core for the parachain
/// this collator is collating for. It is the job of the consensus implementation to decide if this
/// specific collator should build candidate for the given relay chain block. The consensus
/// implementation could for example check if this specific collator is part of the validator.
/// The collator will call [`Self::produce_candidate`] every time there is a free core for the parachain
/// this collator is collating for. It is the job of the consensus implementation to decide whether this
/// specific collator should build a candidate for the given relay chain block. The consensus
/// implementation could, for example, check whether this specific collator is part of a staked set.

@@ -511,6 +510,52 @@ where
}
}

/// The result of [`ParachainConsensus::produce_candidate`].
pub struct ParachainCandidate<B> {
/// The block that was build for this candidate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The block that was build for this candidate.
/// The block that was built for this candidate.

Comment on lines +28 to +29
//! 3. The parachain validators validate at most X different parachain candidates, where X is the
//! total number of parachain validators.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! 3. The parachain validators validate at most X different parachain candidates, where X is the
//! total number of parachain validators.
//! 3. The validators assigned to this parachain validate at most X different parachain candidates, where X is the
//! total number of validators assigned to this parachain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly suggested this one because I worry that people who haven't read all the docs might think that "parachain validators" means collators.

@bkchr
Copy link
Member Author

bkchr commented Feb 16, 2021

@JoshOrndorff could you open a pr with your suggestions please?

@JoshOrndorff
Copy link
Contributor

Could you explain a little bit more about the RelayChainConsensusBuilder?

Why is it necessary? I don't quite understand the comment:

/// Builds a [RelayChainConsensus] for a parachain. As this requires
/// a concrete relay chain client instance, the builder takes a [polkadot_service::Client]
/// that wraps this concrete instanace. By using [polkadot_service::ExecuteWithClient]
/// the builder gets access to this concrete instance.

I mostly understand the explanation, but I don't understand why the builder needs a concrete relay chain client instance. Is this pattern used somewhere else? This seems more complex than the analogous code in sc_consensus_aura

@JoshOrndorff
Copy link
Contributor

I guess this is the part that is really bending my brain:

	/// Build the relay chain consensus.
	fn build(self) -> Box<dyn ParachainConsensus<Block>> {
		self.relay_chain_client.clone().execute_with(self)
	}

@bkchr
Copy link
Member Author

bkchr commented Feb 26, 2021

This works around some missing type system features :P

A polkadot client can either be Kusama/Polkadot/Rococo/Test and all use some small different types. Actually most of them are equal, but the compiler can not resolve this. To circumvent this problem on the polkadot side, there is this Client enum that combines all of them into one enum. Than we use this machinery here to get access to the real type inside this Client enum that implements all the required traits etc.

@JoshOrndorff
Copy link
Contributor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch from Proposer to Slots
5 participants