Skip to content
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

feat: make Consensus trait generic over block parts #12451

Merged
merged 5 commits into from
Nov 11, 2024

Conversation

klkvr
Copy link
Collaborator

@klkvr klkvr commented Nov 11, 2024

Preparation for abstraction of block for network/downloaders.

Moves ensure_valid_body_response fn from networking crate to Consensus trait as validate_body_against_header.
Makes Consensus generic over header and body

emhane
emhane previously requested changes Nov 11, 2024
crates/consensus/common/src/validation.rs Outdated Show resolved Hide resolved
@@ -44,11 +44,11 @@ impl<'a> PostExecutionInput<'a> {

/// Consensus is a protocol that chooses canonical chain.
#[auto_impl::auto_impl(&, Arc)]
pub trait Consensus: Debug + Send + Sync {
pub trait Consensus<H = Header, B = BlockBody>: Debug + Send + Sync {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pub trait Consensus<H = Header, B = BlockBody>: Debug + Send + Sync {
pub trait Consensus<N: NodePrimitives<BlockHeader = Header, BlockBody = BlockBody>>: Debug + Send + Sync {

prefer this, and adding header and block body as ATs parallel to block in NodePrimitives. it's nice to grow the generics list by only a single element everywhere via this task of generalising the data primitives.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

imo ideally this has a single block AT bounded by

Consensus responsibility is to validate blocks so don't think it should know about entire node storage model. this would also be consistent with pool traits which only know the transaction AT

I've done this as 2 generics right now so that we can still keep dyn Consensus everywhere

does it make sense?

Copy link
Member

Choose a reason for hiding this comment

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

why can't you keep the trait object if you add the generic?

NetworkPrimitives has header and body as ATs, and the block, so I'm not sure you're intentions are really clear?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

why can't you keep the trait object if you add the generic?

I mean that we can't keep it as is if we'd add AT because we'd need to specify Block = Block everywhere, so went with generics for now

NetworkPrimitives has header and body as ATs, and the block, so I'm not sure you're intentions are really clear?

sorry, not sure I follow, how are NetworkPrimitives related to consensus trait?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah a single AT would be ideal, but for now we can introduce both AT just to move forward

Copy link
Member

Choose a reason for hiding this comment

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

i

why can't you keep the trait object if you add the generic?

I mean that we can't keep it as is if we'd add AT because we'd need to specify Block = Block everywhere, so went with generics for now

I see, seems to be a misunderstanding, I didn't mean adding an AT to the consensus trait

NetworkPrimitives has header and body as ATs, and the block, so I'm not sure your intentions are really clear?

sorry, not sure I follow, how are NetworkPrimitives related to consensus trait?

if you remove the rlp encoding trait bounds from ATs on NetworkPrimitives, and just have auto trait bounds, then the associated types can easily just be the unit struct ().

by passing one type, that aggregates the data primitives as associated types, we make a framework that is extensible without breaking the public api.

in the future, and even towards the end of this sdk integration task, it may well be, that somewhere in consensus, another data primitive type is needed. then we have to change the type definition again to put another generic to the list if we do it the way you impl it now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

by passing one type, that aggregates the data primitives as associated types, we make a framework that is extensible without breaking the public api.

in the future, and even towards the end of this sdk integration task, it may well be, that somewhere in consensus, another data primitive type is needed. then we have to change the type definition again to put another generic to the list if we do it the way you impl it now.

hmm, in scope of sdk the Consensus trait API only needs block and its components right now. If we were to add additional methods requiring something else this would anyway be a breaking change to API I think? and internally trait implementations are free to use any components

Copy link
Member

Choose a reason for hiding this comment

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

just recently op impl of transaction rpc, needed a receipt type, who would have known.

#11164
#11732

keep the code usable for use cases you could not have had time to think of yourself at the time of impl.

@emhane emhane added C-debt Refactor of code section that is hard to understand or maintain A-sdk Related to reth's use as a library labels Nov 11, 2024
@@ -44,11 +44,11 @@ impl<'a> PostExecutionInput<'a> {

/// Consensus is a protocol that chooses canonical chain.
#[auto_impl::auto_impl(&, Arc)]
pub trait Consensus: Debug + Send + Sync {
pub trait Consensus<H = Header, B = BlockBody>: Debug + Send + Sync {
Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah a single AT would be ideal, but for now we can introduce both AT just to move forward

@emhane
Copy link
Member

emhane commented Nov 11, 2024

prefer if we don't create more debt for no apparent reason whatsoever @mattsse , it's a very easy change to have the one aggregate generic

@mattsse mattsse dismissed emhane’s stale review November 11, 2024 16:31

this is sufficient for now

@klkvr klkvr added this pull request to the merge queue Nov 11, 2024
Merged via the queue into main with commit 24b3e63 Nov 11, 2024
40 checks passed
@klkvr klkvr deleted the klkvr/validate-body-against-header branch November 11, 2024 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sdk Related to reth's use as a library C-debt Refactor of code section that is hard to understand or maintain
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants