Skip to content

Commit

Permalink
chore: make EthStorage generic over transaction (#13549)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Dec 25, 2024
1 parent 90edbff commit edd2511
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 72 deletions.
69 changes: 4 additions & 65 deletions crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::{
};
use alloy_consensus::Header;
use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig};
use reth_chainspec::{EthChainSpec, EthereumHardforks, Hardforks};
use reth_db::transaction::{DbTx, DbTxMut};
use reth_chainspec::{EthChainSpec, Hardforks};
use reth_evm::{execute::BasicBlockExecutorProvider, ConfigureEvm};
use reth_network::{NetworkConfig, NetworkHandle, NetworkManager, NetworkPrimitives, PeersInfo};
use reth_node_api::{AddOnsContext, EngineValidator, FullNodeComponents, NodeAddOns, TxTy};
Expand All @@ -36,11 +35,8 @@ use reth_optimism_rpc::{
OpEthApi, SequencerClient,
};
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_primitives::{BlockBody, PooledTransaction, TransactionSigned};
use reth_provider::{
providers::ChainStorage, BlockBodyReader, BlockBodyWriter, CanonStateSubscriptions,
ChainSpecProvider, DBProvider, EthStorage, ProviderResult, ReadBodyInput, StorageLocation,
};
use reth_primitives::{PooledTransaction, TransactionSigned};
use reth_provider::{CanonStateSubscriptions, EthStorage};
use reth_rpc_server_types::RethRpcModule;
use reth_tracing::tracing::{debug, info};
use reth_transaction_pool::{
Expand All @@ -51,64 +47,7 @@ use reth_trie_db::MerklePatriciaTrie;
use std::sync::Arc;

/// Storage implementation for Optimism.
#[derive(Debug, Default, Clone)]
pub struct OpStorage(EthStorage);

impl<Provider: DBProvider<Tx: DbTxMut>> BlockBodyWriter<Provider, BlockBody> for OpStorage {
fn write_block_bodies(
&self,
provider: &Provider,
bodies: Vec<(u64, Option<BlockBody>)>,
write_to: StorageLocation,
) -> ProviderResult<()> {
self.0.write_block_bodies(provider, bodies, write_to)
}

fn remove_block_bodies_above(
&self,
provider: &Provider,
block: alloy_primitives::BlockNumber,
remove_from: StorageLocation,
) -> ProviderResult<()> {
self.0.remove_block_bodies_above(provider, block, remove_from)
}
}

impl<Provider: DBProvider + ChainSpecProvider<ChainSpec: EthereumHardforks>>
BlockBodyReader<Provider> for OpStorage
{
type Block = reth_primitives::Block;

fn read_block_bodies(
&self,
provider: &Provider,
inputs: Vec<ReadBodyInput<'_, Self::Block>>,
) -> ProviderResult<Vec<BlockBody>> {
self.0.read_block_bodies(provider, inputs)
}
}

impl ChainStorage<OpPrimitives> for OpStorage {
fn reader<TX, Types>(
&self,
) -> impl reth_provider::ChainStorageReader<reth_provider::DatabaseProvider<TX, Types>, OpPrimitives>
where
TX: DbTx + 'static,
Types: reth_provider::providers::NodeTypesForProvider<Primitives = OpPrimitives>,
{
self
}

fn writer<TX, Types>(
&self,
) -> impl reth_provider::ChainStorageWriter<reth_provider::DatabaseProvider<TX, Types>, OpPrimitives>
where
TX: DbTxMut + DbTx + 'static,
Types: NodeTypes<Primitives = OpPrimitives>,
{
self
}
}
pub type OpStorage = EthStorage;

/// Type configuration for a regular Optimism node.
#[derive(Debug, Default, Clone)]
Expand Down
23 changes: 16 additions & 7 deletions crates/storage/storage-api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use reth_db::{
transaction::{DbTx, DbTxMut},
DbTxUnwindExt,
};
use reth_primitives_traits::{Block, BlockBody, FullNodePrimitives};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::{Block, BlockBody, FullNodePrimitives, SignedTransaction};
use reth_storage_errors::provider::ProviderResult;

/// Trait that implements how block bodies are written to the storage.
Expand Down Expand Up @@ -78,17 +79,24 @@ impl<T, Provider, Primitives: FullNodePrimitives> ChainStorageReader<Provider, P
}

/// Ethereum storage implementation.
#[derive(Debug, Default, Clone, Copy)]
pub struct EthStorage;
#[derive(Debug, Clone, Copy)]
pub struct EthStorage<T = TransactionSigned>(std::marker::PhantomData<T>);

impl<Provider> BlockBodyWriter<Provider, reth_primitives::BlockBody> for EthStorage
impl<T> Default for EthStorage<T> {
fn default() -> Self {
Self(Default::default())
}
}

impl<Provider, T> BlockBodyWriter<Provider, reth_primitives::BlockBody<T>> for EthStorage<T>
where
Provider: DBProvider<Tx: DbTxMut>,
T: SignedTransaction,
{
fn write_block_bodies(
&self,
provider: &Provider,
bodies: Vec<(u64, Option<reth_primitives::BlockBody>)>,
bodies: Vec<(u64, Option<reth_primitives::BlockBody<T>>)>,
_write_to: StorageLocation,
) -> ProviderResult<()> {
let mut ommers_cursor = provider.tx_ref().cursor_write::<tables::BlockOmmers>()?;
Expand Down Expand Up @@ -128,11 +136,12 @@ where
}
}

impl<Provider> BlockBodyReader<Provider> for EthStorage
impl<Provider, T> BlockBodyReader<Provider> for EthStorage<T>
where
Provider: DBProvider + ChainSpecProvider<ChainSpec: EthereumHardforks>,
T: SignedTransaction,
{
type Block = reth_primitives::Block;
type Block = reth_primitives::Block<T>;

fn read_block_bodies(
&self,
Expand Down

0 comments on commit edd2511

Please sign in to comment.