-
Notifications
You must be signed in to change notification settings - Fork 8
feat: implement custom consensus for Rollkit to allow same timestamps #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9f748bc
feat: implement custom consensus for Rollkit to allow same timestamps
tac0turtle 358fa04
minimize
tac0turtle 14c6c22
take into account proper validation and double check if the time is a…
tac0turtle cfc36f2
address clippy
tac0turtle f52f7d3
fix cargo check
tac0turtle bf44805
fix test case
tac0turtle d530ccc
add commnet
tac0turtle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| //! Rollkit custom consensus implementation that allows same timestamps across blocks. | ||
|
|
||
| use reth_chainspec::ChainSpec; | ||
| use reth_consensus::{Consensus, ConsensusError, FullConsensus, HeaderValidator}; | ||
| use reth_consensus_common::validation::validate_body_against_header; | ||
| use reth_ethereum::node::builder::{components::ConsensusBuilder, BuilderContext}; | ||
| use reth_ethereum_consensus::EthBeaconConsensus; | ||
| use reth_ethereum_primitives::{Block, BlockBody, EthPrimitives, Receipt}; | ||
| use reth_execution_types::BlockExecutionResult; | ||
| use reth_node_api::{FullNodeTypes, NodeTypes}; | ||
| use reth_primitives::{RecoveredBlock, SealedBlock, SealedHeader}; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Builder for `RollkitConsensus` | ||
| #[derive(Debug, Default, Clone)] | ||
| #[non_exhaustive] | ||
| pub struct RollkitConsensusBuilder; | ||
|
|
||
| impl RollkitConsensusBuilder { | ||
| /// Create a new `RollkitConsensusBuilder` | ||
| pub const fn new() -> Self { | ||
| Self | ||
| } | ||
|
|
||
| /// Build the consensus implementation | ||
| pub fn build(chain_spec: Arc<ChainSpec>) -> Arc<RollkitConsensus> { | ||
| Arc::new(RollkitConsensus::new(chain_spec)) | ||
| } | ||
| } | ||
|
|
||
| impl<Node> ConsensusBuilder<Node> for RollkitConsensusBuilder | ||
| where | ||
| Node: FullNodeTypes, | ||
| Node::Types: NodeTypes<ChainSpec = ChainSpec, Primitives = EthPrimitives>, | ||
| { | ||
| type Consensus = Arc<dyn FullConsensus<EthPrimitives, Error = ConsensusError>>; | ||
|
|
||
| async fn build_consensus(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Consensus> { | ||
| Ok(Arc::new(RollkitConsensus::new(ctx.chain_spec())) as Self::Consensus) | ||
| } | ||
| } | ||
|
|
||
| /// Rollkit consensus implementation that allows blocks with the same timestamp. | ||
| /// | ||
| /// This consensus implementation wraps the standard Ethereum beacon consensus | ||
| /// but modifies the timestamp validation to allow multiple blocks to have the | ||
| /// same timestamp, which is required for Rollkit's operation. | ||
| #[derive(Debug, Clone)] | ||
| pub struct RollkitConsensus { | ||
| /// Inner Ethereum beacon consensus for standard validation | ||
| inner: EthBeaconConsensus<ChainSpec>, | ||
| } | ||
|
|
||
| impl RollkitConsensus { | ||
| /// Create a new Rollkit consensus instance | ||
| pub const fn new(chain_spec: Arc<ChainSpec>) -> Self { | ||
| let inner = EthBeaconConsensus::new(chain_spec); | ||
| Self { inner } | ||
| } | ||
| } | ||
|
|
||
| impl HeaderValidator for RollkitConsensus { | ||
| fn validate_header(&self, header: &SealedHeader) -> Result<(), ConsensusError> { | ||
| // Use inner consensus for basic header validation | ||
| self.inner.validate_header(header) | ||
| } | ||
|
|
||
| fn validate_header_against_parent( | ||
| &self, | ||
| header: &SealedHeader, | ||
| parent: &SealedHeader, | ||
| ) -> Result<(), ConsensusError> { | ||
| match self.inner.validate_header_against_parent(header, parent) { | ||
| Ok(()) => Ok(()), | ||
| // upstream the check is that its greater than the parent's timestamp, if not we get | ||
| // TimestampIsInPast we check if the timestamp is equal to the parent's timestamp, if so we | ||
| // allow it | ||
| Err(ConsensusError::TimestampIsInPast { .. }) => { | ||
| if header.timestamp == parent.timestamp { | ||
| Ok(()) | ||
| } else { | ||
| Err(ConsensusError::TimestampIsInPast { | ||
| parent_timestamp: parent.timestamp, | ||
| timestamp: header.timestamp, | ||
| }) | ||
| } | ||
| } | ||
| Err(e) => Err(e), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Consensus<Block> for RollkitConsensus { | ||
| type Error = ConsensusError; | ||
|
|
||
| fn validate_body_against_header( | ||
| &self, | ||
| body: &BlockBody, | ||
| header: &SealedHeader, | ||
| ) -> Result<(), Self::Error> { | ||
| validate_body_against_header(body, header.header()) | ||
| } | ||
|
|
||
| fn validate_block_pre_execution(&self, block: &SealedBlock) -> Result<(), Self::Error> { | ||
| // Use inner consensus for pre-execution validation | ||
| self.inner.validate_block_pre_execution(block) | ||
| } | ||
| } | ||
|
|
||
| impl FullConsensus<EthPrimitives> for RollkitConsensus { | ||
| fn validate_block_post_execution( | ||
| &self, | ||
| block: &RecoveredBlock<Block>, | ||
| result: &BlockExecutionResult<Receipt>, | ||
| ) -> Result<(), ConsensusError> { | ||
| <EthBeaconConsensus<ChainSpec> as FullConsensus<EthPrimitives>>::validate_block_post_execution(&self.inner, block, result) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.