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 more block types generic #12812

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

feat: make more block types generic #12812

wants to merge 7 commits into from

Conversation

klkvr
Copy link
Collaborator

@klkvr klkvr commented Nov 23, 2024

Preparation for abstraction of block readers

Changes in this PR:

  • Methods from Block struct are moved to a new extension trait BlockSealExt which is implemented for every Block trait implementations, allowing them to be called once we abstract the block type
  • BlockWriter AT is changed from Body to Block
  • BlockWithSenders and SealedBlockWithSenders are made generic over a single B: Block generic. SealedBlock is still generic over both. This is a bit tricky to simplify because some of the networking components which construct blocks only know separate header and body types when constructing SealedBlock, and converting those 2 generics to a single one would need an additional generic for those types. One of such components is BodiesDownloader which uses BodiesRequestFuture:
    pub(crate) struct BodiesRequestFuture<B: BodiesClient> {

This issue doesn't apply to other components because they usually receive full blocks

@@ -144,7 +145,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
for block in blocks.into_iter().rev() {
let block_number = block.number;
let sealed_block = block
.try_seal_with_senders()
.try_seal_with_senders::<<N::Primitives as NodePrimitives>::Block>()
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
.try_seal_with_senders::<<N::Primitives as NodePrimitives>::Block>()
.try_seal_with_senders::<BlockTy<N>>()

add another type alias here for BlockTy

/// Helper adapter type for accessing [`NodePrimitives::BlockHeader`] on [`NodeTypes`].
pub type HeaderTy<N> = <<N as NodeTypes>::Primitives as NodePrimitives>::BlockHeader;
/// Helper adapter type for accessing [`NodePrimitives::BlockBody`] on [`NodeTypes`].
pub type BodyTy<N> = <<N as NodeTypes>::Primitives as NodePrimitives>::BlockBody;
/// Helper adapter type for accessing [`NodePrimitives::SignedTx`] on [`NodeTypes`].
pub type TxTy<N> = <<N as NodeTypes>::Primitives as NodePrimitives>::SignedTx;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated in a587d19

/// Extension trait for [`reth_primitives_traits::Block`] implementations
/// allowing for conversions into common block parts containers such as [`SealedBlock`],
/// [`BlockWithSenders`], etc.
pub trait BlockSealExt: Block {
Copy link
Member

Choose a reason for hiding this comment

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

this name is better as either BlockSeal or BlockExt

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

renamed to BlockExt in d54846d

@@ -33,7 +33,7 @@ pub fn setup_without_evm<Provider>(
where
Provider: StaticFileProviderFactory
+ StageCheckpointWriter
+ BlockWriter<Body: reth_node_api::BlockBody>,
+ BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>,
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
+ BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>,
+ BlockWriter<BlockHeader = reth_primitives::Header>,

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

BlockWriter doesn't have this AT

@@ -64,7 +64,8 @@ fn append_first_block<Provider>(
total_difficulty: U256,
) -> Result<(), eyre::Error>
where
Provider: BlockWriter<Body: reth_node_api::BlockBody> + StaticFileProviderFactory,
Provider: BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>
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
Provider: BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>
Provider: BlockWriter<BlockHeader = reth_primitives::Header>

@@ -204,72 +152,83 @@ impl<'a> arbitrary::Arbitrary<'a> for Block {

/// Sealed block with senders recovered from transactions.
#[derive(Debug, Clone, PartialEq, Eq, Default, Deref, DerefMut)]
pub struct BlockWithSenders {
pub struct BlockWithSenders<B: reth_primitives_traits::Block = Block> {
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 struct BlockWithSenders<B: reth_primitives_traits::Block = Block> {
pub struct BlockWithSenders<B = Block> {

it's always better to not constraint the generic in the type definition with a trait bound that implements a bunch of behaviour (ie trait methods), makes the code more flexible in testing

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated in f7c301d

/// List of senders that match the transactions in the block
pub senders: Vec<Address>,
}

impl BlockWithSenders {
impl<B: reth_primitives_traits::Block> BlockWithSenders<B> {
Copy link
Member

Choose a reason for hiding this comment

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

BlockPrimitives would have been ideal here, then you have the transaction type accessible without fully qualified syntax and without making yet another adapter type
#12721 @mattsse

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

Successfully merging this pull request may close these issues.

2 participants