feat: add block traits for alloy types (#13470)

This commit is contained in:
Matthias Seitz
2024-12-20 16:27:45 +01:00
committed by GitHub
parent d217c67129
commit dc6394b05b
5 changed files with 158 additions and 84 deletions

View File

@ -4,7 +4,7 @@ use crate::{
BlockHeader, FullSignedTx, InMemorySize, MaybeSerde, MaybeSerdeBincodeCompat, SignedTransaction,
};
use alloc::{fmt, vec::Vec};
use alloy_consensus::Transaction;
use alloy_consensus::{Header, Transaction};
use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawals};
use alloy_primitives::{Bytes, B256};
@ -99,6 +99,30 @@ pub trait BlockBody:
}
}
impl<T> BlockBody for alloy_consensus::BlockBody<T>
where
T: SignedTransaction,
{
type Transaction = T;
type OmmerHeader = Header;
fn transactions(&self) -> &[Self::Transaction] {
&self.transactions
}
fn into_transactions(self) -> Vec<Self::Transaction> {
self.transactions
}
fn withdrawals(&self) -> Option<&Withdrawals> {
self.withdrawals.as_ref()
}
fn ommers(&self) -> Option<&[Self::OmmerHeader]> {
Some(&self.ommers)
}
}
/// This is a helper alias to make it easy to refer to the inner `Transaction` associated type of a
/// given type that implements [`BlockBody`].
pub type BodyTx<N> = <N as BlockBody>::Transaction;

View File

@ -4,9 +4,13 @@ pub mod body;
pub mod header;
use alloc::fmt;
use alloy_consensus::Header;
use alloy_rlp::{Decodable, Encodable};
use crate::{BlockBody, BlockHeader, FullBlockBody, FullBlockHeader, InMemorySize, MaybeSerde};
use crate::{
BlockBody, BlockHeader, FullBlockBody, FullBlockHeader, InMemorySize, MaybeSerde,
SignedTransaction,
};
/// Helper trait that unifies all behaviour required by block to support full node operations.
pub trait FullBlock:
@ -60,3 +64,27 @@ pub trait Block:
/// Splits the block into its header and body.
fn split(self) -> (Self::Header, Self::Body);
}
impl<T> Block for alloy_consensus::Block<T>
where
T: SignedTransaction,
{
type Header = Header;
type Body = alloy_consensus::BlockBody<T>;
fn new(header: Self::Header, body: Self::Body) -> Self {
Self { header, body }
}
fn header(&self) -> &Self::Header {
&self.header
}
fn body(&self) -> &Self::Body {
&self.body
}
fn split(self) -> (Self::Header, Self::Body) {
(self.header, self.body)
}
}

View File

@ -2,6 +2,7 @@ use alloy_consensus::{
transaction::PooledTransaction, Header, TxEip1559, TxEip2930, TxEip4844, TxEip4844WithSidecar,
TxEip7702, TxLegacy, TxType,
};
use alloy_eips::eip4895::Withdrawals;
use alloy_primitives::{PrimitiveSignature as Signature, TxHash};
use revm_primitives::Log;
@ -82,6 +83,27 @@ impl InMemorySize for PooledTransaction {
}
}
impl<T: InMemorySize> InMemorySize for alloy_consensus::BlockBody<T> {
/// Calculates a heuristic for the in-memory size of the block body
#[inline]
fn size(&self) -> usize {
self.transactions.iter().map(T::size).sum::<usize>() +
self.transactions.capacity() * core::mem::size_of::<T>() +
self.ommers.iter().map(Header::size).sum::<usize>() +
self.ommers.capacity() * core::mem::size_of::<Header>() +
self.withdrawals
.as_ref()
.map_or(core::mem::size_of::<Option<Withdrawals>>(), Withdrawals::total_size)
}
}
impl<T: InMemorySize> InMemorySize for alloy_consensus::Block<T> {
#[inline]
fn size(&self) -> usize {
self.header.size() + self.body.size()
}
}
#[cfg(feature = "op")]
impl InMemorySize for op_alloy_consensus::OpDepositReceipt {
fn size(&self) -> usize {