Skip to content

Commit

Permalink
chore: add StorageLocation to BlockBodyWriter trait (#13266)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo authored Dec 10, 2024
1 parent d97449d commit 4c39b98
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 41 deletions.
8 changes: 5 additions & 3 deletions crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_primitives::{BlockBody, PooledTransactionsElement, TransactionSigned};
use reth_provider::{
providers::ChainStorage, BlockBodyReader, BlockBodyWriter, CanonStateSubscriptions,
ChainSpecProvider, DBProvider, EthStorage, ProviderResult, ReadBodyInput,
ChainSpecProvider, DBProvider, EthStorage, ProviderResult, ReadBodyInput, StorageLocation,
};
use reth_rpc_server_types::RethRpcModule;
use reth_tracing::tracing::{debug, info};
Expand All @@ -56,16 +56,18 @@ impl<Provider: DBProvider<Tx: DbTxMut>> BlockBodyWriter<Provider, BlockBody> for
&self,
provider: &Provider,
bodies: Vec<(u64, Option<BlockBody>)>,
write_to: StorageLocation,
) -> ProviderResult<()> {
self.0.write_block_bodies(provider, bodies)
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)
self.0.remove_block_bodies_above(provider, block, remove_from)
}
}

Expand Down
20 changes: 10 additions & 10 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2893,12 +2893,12 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypesForProvider + 'static> BlockWrite
fn append_block_bodies(
&self,
bodies: Vec<(BlockNumber, Option<BodyTy<N>>)>,
write_transactions_to: StorageLocation,
write_to: StorageLocation,
) -> ProviderResult<()> {
let Some(from_block) = bodies.first().map(|(block, _)| *block) else { return Ok(()) };

// Initialize writer if we will be writing transactions to staticfiles
let mut tx_static_writer = write_transactions_to
let mut tx_static_writer = write_to
.static_files()
.then(|| {
self.static_file_provider.get_writer(from_block, StaticFileSegment::Transactions)
Expand All @@ -2909,7 +2909,7 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypesForProvider + 'static> BlockWrite
let mut tx_block_cursor = self.tx.cursor_write::<tables::TransactionBlocks>()?;

// Initialize cursor if we will be writing transactions to database
let mut tx_cursor = write_transactions_to
let mut tx_cursor = write_to
.database()
.then(|| self.tx.cursor_write::<tables::Transactions<TxTy<N>>>())
.transpose()?;
Expand Down Expand Up @@ -2962,15 +2962,15 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypesForProvider + 'static> BlockWrite
);
}

self.storage.writer().write_block_bodies(self, bodies)?;
self.storage.writer().write_block_bodies(self, bodies, write_to)?;

Ok(())
}

fn remove_blocks_above(
&self,
block: BlockNumber,
remove_transactions_from: StorageLocation,
remove_from: StorageLocation,
) -> ProviderResult<()> {
let mut canonical_headers_cursor = self.tx.cursor_write::<tables::CanonicalHeaders>()?;
let mut rev_headers = canonical_headers_cursor.walk_back(None)?;
Expand Down Expand Up @@ -3010,17 +3010,17 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypesForProvider + 'static> BlockWrite

self.remove::<tables::TransactionSenders>(unwind_tx_from..)?;

self.remove_bodies_above(block, remove_transactions_from)?;
self.remove_bodies_above(block, remove_from)?;

Ok(())
}

fn remove_bodies_above(
&self,
block: BlockNumber,
remove_transactions_from: StorageLocation,
remove_from: StorageLocation,
) -> ProviderResult<()> {
self.storage.writer().remove_block_bodies_above(self, block)?;
self.storage.writer().remove_block_bodies_above(self, block, remove_from)?;

// First transaction to be removed
let unwind_tx_from = self
Expand All @@ -3032,11 +3032,11 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypesForProvider + 'static> BlockWrite
self.remove::<tables::BlockBodyIndices>(block + 1..)?;
self.remove::<tables::TransactionBlocks>(unwind_tx_from..)?;

if remove_transactions_from.database() {
if remove_from.database() {
self.remove::<tables::Transactions<TxTy<N>>>(unwind_tx_from..)?;
}

if remove_transactions_from.static_files() {
if remove_from.static_files() {
let static_file_tx_num = self
.static_file_provider
.get_highest_static_file_tx(StaticFileSegment::Transactions);
Expand Down
31 changes: 4 additions & 27 deletions crates/storage/provider/src/traits/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,10 @@ use reth_db_api::models::StoredBlockBodyIndices;
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_node_types::NodePrimitives;
use reth_primitives::SealedBlockWithSenders;
use reth_storage_api::NodePrimitivesProvider;
use reth_storage_api::{NodePrimitivesProvider, StorageLocation};
use reth_storage_errors::provider::ProviderResult;
use reth_trie::{updates::TrieUpdates, HashedPostStateSorted};

/// An enum that represents the storage location for a piece of data.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum StorageLocation {
/// Write only to static files.
StaticFiles,
/// Write only to the database.
Database,
/// Write to both the database and static files.
Both,
}

impl StorageLocation {
/// Returns true if the storage location includes static files.
pub const fn static_files(&self) -> bool {
matches!(self, Self::StaticFiles | Self::Both)
}

/// Returns true if the storage location includes the database.
pub const fn database(&self) -> bool {
matches!(self, Self::Database | Self::Both)
}
}

/// `BlockExecution` Writer
pub trait BlockExecutionWriter:
NodePrimitivesProvider<Primitives: NodePrimitives<Block = Self::Block>> + BlockWriter + Send + Sync
Expand Down Expand Up @@ -120,7 +97,7 @@ pub trait BlockWriter: Send + Sync {
fn append_block_bodies(
&self,
bodies: Vec<(BlockNumber, Option<<Self::Block as reth_primitives_traits::Block>::Body>)>,
write_transactions_to: StorageLocation,
write_to: StorageLocation,
) -> ProviderResult<()>;

/// Removes all blocks above the given block number from the database.
Expand All @@ -129,14 +106,14 @@ pub trait BlockWriter: Send + Sync {
fn remove_blocks_above(
&self,
block: BlockNumber,
remove_transactions_from: StorageLocation,
remove_from: StorageLocation,
) -> ProviderResult<()>;

/// Removes all block bodies above the given block number from the database.
fn remove_bodies_above(
&self,
block: BlockNumber,
remove_transactions_from: StorageLocation,
remove_from: StorageLocation,
) -> ProviderResult<()>;

/// Appends a batch of sealed blocks to the blockchain, including sender information, and
Expand Down
6 changes: 5 additions & 1 deletion crates/storage/storage-api/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::DBProvider;
use crate::{DBProvider, StorageLocation};
use alloy_primitives::BlockNumber;
use reth_chainspec::{ChainSpecProvider, EthereumHardforks};
use reth_db::{
Expand All @@ -22,13 +22,15 @@ pub trait BlockBodyWriter<Provider, Body: BlockBody> {
&self,
provider: &Provider,
bodies: Vec<(BlockNumber, Option<Body>)>,
write_to: StorageLocation,
) -> ProviderResult<()>;

/// Removes all block bodies above the given block number from the database.
fn remove_block_bodies_above(
&self,
provider: &Provider,
block: BlockNumber,
remove_from: StorageLocation,
) -> ProviderResult<()>;
}

Expand Down Expand Up @@ -87,6 +89,7 @@ where
&self,
provider: &Provider,
bodies: Vec<(u64, Option<reth_primitives::BlockBody>)>,
_write_to: StorageLocation,
) -> ProviderResult<()> {
let mut ommers_cursor = provider.tx_ref().cursor_write::<tables::BlockOmmers>()?;
let mut withdrawals_cursor =
Expand Down Expand Up @@ -116,6 +119,7 @@ where
&self,
provider: &Provider,
block: BlockNumber,
_remove_from: StorageLocation,
) -> ProviderResult<()> {
provider.tx_ref().unwind_table_by_num::<tables::BlockWithdrawals>(block)?;
provider.tx_ref().unwind_table_by_num::<tables::BlockOmmers>(block)?;
Expand Down
23 changes: 23 additions & 0 deletions crates/storage/storage-api/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,26 @@ pub trait StorageChangeSetReader: Send + Sync {
block_number: BlockNumber,
) -> ProviderResult<Vec<(BlockNumberAddress, StorageEntry)>>;
}

/// An enum that represents the storage location for a piece of data.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum StorageLocation {
/// Write only to static files.
StaticFiles,
/// Write only to the database.
Database,
/// Write to both the database and static files.
Both,
}

impl StorageLocation {
/// Returns true if the storage location includes static files.
pub const fn static_files(&self) -> bool {
matches!(self, Self::StaticFiles | Self::Both)
}

/// Returns true if the storage location includes the database.
pub const fn database(&self) -> bool {
matches!(self, Self::Database | Self::Both)
}
}

0 comments on commit 4c39b98

Please sign in to comment.