feat: make more network components generic over primitives (#12481)

This commit is contained in:
Arsenii Kulikov
2024-11-12 23:29:42 +04:00
committed by GitHub
parent fa5daef07d
commit b39957612a
15 changed files with 197 additions and 235 deletions

View File

@ -6,7 +6,7 @@ use alloy_consensus::{BlockHeader, Transaction, TxType};
use alloy_eips::{eip4895::Withdrawal, eip7685::Requests};
use alloy_primitives::{Address, B256};
use crate::{Block, InMemorySize};
use crate::InMemorySize;
/// Abstraction for block's body.
pub trait BlockBody:
@ -47,11 +47,6 @@ pub trait BlockBody:
/// Returns [`Requests`] in block, if any.
fn requests(&self) -> Option<&Requests>;
/// Create a [`Block`] from the body and its header.
fn into_block<T: Block<Header = Self::Header, Body = Self>>(self, header: Self::Header) -> T {
T::from((header, self))
}
/// Calculate the transaction root for the block body.
fn calculate_tx_root(&self) -> B256;

View File

@ -3,12 +3,11 @@
pub mod body;
pub mod header;
use alloc::{fmt, vec::Vec};
use alloc::fmt;
use alloy_primitives::{Address, B256};
use reth_codecs::Compact;
use crate::{BlockBody, BlockHeader, FullBlockHeader, InMemorySize};
use crate::{BlockHeader, FullBlockHeader, InMemorySize};
/// Helper trait that unifies all behaviour required by block to support full node operations.
pub trait FullBlock: Block<Header: Compact> + Compact {}
@ -30,79 +29,17 @@ pub trait Block:
+ Eq
+ serde::Serialize
+ for<'a> serde::Deserialize<'a>
+ From<(Self::Header, Self::Body)>
+ Into<(Self::Header, Self::Body)>
+ InMemorySize
{
/// Header part of the block.
type Header: BlockHeader;
/// The block's body contains the transactions in the block.
type Body: BlockBody;
type Body: Send + Sync + Unpin + 'static;
/// A block and block hash.
type SealedBlock<H, B>;
/// A block and addresses of senders of transactions in it.
type BlockWithSenders<T>;
/// Returns reference to [`BlockHeader`] type.
/// Returns reference to block header.
fn header(&self) -> &Self::Header;
/// Returns reference to [`BlockBody`] type.
/// Returns reference to block body.
fn body(&self) -> &Self::Body;
/// Calculate the header hash and seal the block so that it can't be changed.
// todo: can be default impl if sealed block type is made generic over header and body and
// migrated to alloy
fn seal_slow(self) -> Self::SealedBlock<Self::Header, Self::Body>;
/// Seal the block with a known hash.
///
/// WARNING: This method does not perform validation whether the hash is correct.
// todo: can be default impl if sealed block type is made generic over header and body and
// migrated to alloy
fn seal(self, hash: B256) -> Self::SealedBlock<Self::Header, Self::Body>;
/// Expensive operation that recovers transaction signer. See
/// `SealedBlockWithSenders`.
fn senders(&self) -> Option<Vec<Address>> {
self.body().recover_signers()
}
/// Transform into a `BlockWithSenders`.
///
/// # Panics
///
/// If the number of senders does not match the number of transactions in the block
/// and the signer recovery for one of the transactions fails.
///
/// Note: this is expected to be called with blocks read from disk.
#[track_caller]
fn with_senders_unchecked(self, senders: Vec<Address>) -> Self::BlockWithSenders<Self> {
self.try_with_senders_unchecked(senders).expect("stored block is valid")
}
/// Transform into a `BlockWithSenders` using the given senders.
///
/// If the number of senders does not match the number of transactions in the block, this falls
/// back to manually recovery, but _without ensuring that the signature has a low `s` value_.
/// See also `SignedTransaction::recover_signer_unchecked`.
///
/// Returns an error if a signature is invalid.
// todo: can be default impl if block with senders type is made generic over block and migrated
// to alloy
#[track_caller]
fn try_with_senders_unchecked(
self,
senders: Vec<Address>,
) -> Result<Self::BlockWithSenders<Self>, Self>;
/// **Expensive**. Transform into a `BlockWithSenders` by recovering senders in the contained
/// transactions.
///
/// Returns `None` if a transaction is invalid.
// todo: can be default impl if sealed block type is made generic over header and body and
// migrated to alloy
fn with_recovered_senders(self) -> Option<Self::BlockWithSenders<Self>>;
}