feat: generalize Block impls (#14133)

This commit is contained in:
Arsenii Kulikov
2025-02-01 02:05:09 +04:00
committed by GitHub
parent 1c9ef8c5a3
commit a39980a6f6
12 changed files with 49 additions and 39 deletions

View File

@ -1,6 +1,7 @@
use crate::{providers::NodeTypesForProvider, DatabaseProvider};
use reth_db::transaction::{DbTx, DbTxMut};
use reth_node_types::{FullNodePrimitives, FullSignedTx};
use reth_primitives_traits::FullBlockHeader;
use reth_storage_api::{ChainStorageReader, ChainStorageWriter, EthStorage};
/// Trait that provides access to implementations of [`ChainStorage`]
@ -18,13 +19,14 @@ pub trait ChainStorage<Primitives: FullNodePrimitives>: Send + Sync {
Types: NodeTypesForProvider<Primitives = Primitives>;
}
impl<N, T> ChainStorage<N> for EthStorage<T>
impl<N, T, H> ChainStorage<N> for EthStorage<T, H>
where
T: FullSignedTx,
H: FullBlockHeader,
N: FullNodePrimitives<
Block = reth_primitives::Block<T>,
BlockHeader = alloy_consensus::Header,
BlockBody = reth_primitives::BlockBody<T>,
Block = reth_primitives::Block<T, H>,
BlockHeader = H,
BlockBody = reth_primitives::BlockBody<T, H>,
SignedTx = T,
>,
{

View File

@ -10,7 +10,9 @@ use reth_db::{
DbTxUnwindExt,
};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::{Block, BlockBody, FullNodePrimitives, SignedTransaction};
use reth_primitives_traits::{
Block, BlockBody, FullBlockHeader, FullNodePrimitives, SignedTransaction,
};
use reth_storage_errors::provider::ProviderResult;
/// Trait that implements how block bodies are written to the storage.
@ -81,26 +83,28 @@ impl<T, Provider, Primitives: FullNodePrimitives> ChainStorageReader<Provider, P
/// Ethereum storage implementation.
#[derive(Debug, Clone, Copy)]
pub struct EthStorage<T = TransactionSigned>(std::marker::PhantomData<T>);
pub struct EthStorage<T = TransactionSigned, H = Header>(std::marker::PhantomData<(T, H)>);
impl<T> Default for EthStorage<T> {
impl<T, H> Default for EthStorage<T, H> {
fn default() -> Self {
Self(Default::default())
}
}
impl<Provider, T> BlockBodyWriter<Provider, reth_primitives::BlockBody<T>> for EthStorage<T>
impl<Provider, T, H> BlockBodyWriter<Provider, reth_primitives::BlockBody<T, H>>
for EthStorage<T, H>
where
Provider: DBProvider<Tx: DbTxMut>,
T: SignedTransaction,
H: FullBlockHeader,
{
fn write_block_bodies(
&self,
provider: &Provider,
bodies: Vec<(u64, Option<reth_primitives::BlockBody<T>>)>,
bodies: Vec<(u64, Option<reth_primitives::BlockBody<T, H>>)>,
_write_to: StorageLocation,
) -> ProviderResult<()> {
let mut ommers_cursor = provider.tx_ref().cursor_write::<tables::BlockOmmers>()?;
let mut ommers_cursor = provider.tx_ref().cursor_write::<tables::BlockOmmers<H>>()?;
let mut withdrawals_cursor =
provider.tx_ref().cursor_write::<tables::BlockWithdrawals>()?;
@ -137,14 +141,14 @@ where
}
}
impl<Provider, T> BlockBodyReader<Provider> for EthStorage<T>
impl<Provider, T, H> BlockBodyReader<Provider> for EthStorage<T, H>
where
Provider: DBProvider
+ ChainSpecProvider<ChainSpec: EthereumHardforks>
+ OmmersProvider<Header = Header>,
Provider:
DBProvider + ChainSpecProvider<ChainSpec: EthereumHardforks> + OmmersProvider<Header = H>,
T: SignedTransaction,
H: FullBlockHeader,
{
type Block = reth_primitives::Block<T>;
type Block = reth_primitives::Block<T, H>;
fn read_block_bodies(
&self,
@ -161,19 +165,19 @@ where
for (header, transactions) in inputs {
// If we are past shanghai, then all blocks should have a withdrawal list,
// even if empty
let withdrawals = if chain_spec.is_shanghai_active_at_timestamp(header.timestamp) {
let withdrawals = if chain_spec.is_shanghai_active_at_timestamp(header.timestamp()) {
withdrawals_cursor
.seek_exact(header.number)?
.seek_exact(header.number())?
.map(|(_, w)| w.withdrawals)
.unwrap_or_default()
.into()
} else {
None
};
let ommers = if chain_spec.final_paris_total_difficulty(header.number).is_some() {
let ommers = if chain_spec.final_paris_total_difficulty(header.number()).is_some() {
Vec::new()
} else {
provider.ommers(header.number.into())?.unwrap_or_default()
provider.ommers(header.number().into())?.unwrap_or_default()
};
bodies.push(reth_primitives::BlockBody { transactions, ommers, withdrawals });