Skip to content

Commit

Permalink
add append_block_meta and prune_block_meta
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo committed Jan 8, 2025
1 parent 3e2f18f commit abcc7d5
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 15 deletions.
2 changes: 1 addition & 1 deletion crates/storage/db/src/static_file/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ add_static_file_mask! {
}
add_static_file_mask! {
#[doc = "Mask for a `StoredBlockWithdrawals` from BlockMeta static file segment"]
OmmerMask, <BlockOmmers as Table>::Value, 0b010
OmmerMask<H>, H, 0b010
}
add_static_file_mask! {
#[doc = "Mask for a `StoredBlockWithdrawals` from BlockMeta static file segment"]
Expand Down
52 changes: 47 additions & 5 deletions crates/storage/provider/src/providers/static_file/jar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ use crate::{
TransactionsProvider,
};
use alloy_consensus::transaction::TransactionMeta;
use alloy_eips::{eip2718::Encodable2718, BlockHashOrNumber};
use alloy_eips::{
eip2718::Encodable2718,
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber,
};
use alloy_primitives::{Address, BlockHash, BlockNumber, TxHash, TxNumber, B256, U256};
use reth_chainspec::ChainInfo;
use reth_db::{
models::StoredBlockBodyIndices,
static_file::{
BlockHashMask, HeaderMask, HeaderWithHashMask, ReceiptMask, StaticFileCursor,
TDWithHashMask, TotalDifficultyMask, TransactionMask,
BlockHashMask, BodyIndiceMask, HeaderMask, HeaderWithHashMask, OmmerMask, ReceiptMask,
StaticFileCursor, TDWithHashMask, TotalDifficultyMask, TransactionMask, WithdrawalMask,
},
table::{Decompress, Value},
};
use reth_node_types::NodePrimitives;
use reth_primitives_traits::{SealedHeader, SignedTransaction};
use reth_node_types::{FullNodePrimitives, NodePrimitives};
use reth_primitives::{transaction::recover_signers, SealedHeader};
use reth_primitives_traits::SignedTransaction;
use reth_storage_api::{BlockBodyIndicesProvider, OmmersProvider, WithdrawalsProvider};
use reth_storage_errors::provider::{ProviderError, ProviderResult};
use std::{
fmt::Debug,
Expand Down Expand Up @@ -351,3 +358,38 @@ impl<N: NodePrimitives<SignedTx: Decompress + SignedTransaction, Receipt: Decomp
Ok(receipts)
}
}

impl<N: NodePrimitives> WithdrawalsProvider for StaticFileJarProvider<'_, N> {
fn withdrawals_by_block(
&self,
id: BlockHashOrNumber,
_: u64,
) -> ProviderResult<Option<Withdrawals>> {
if let Some(num) = id.as_number() {
return Ok(self.cursor()?.get_one::<WithdrawalMask>(num.into())?.map(|s| s.withdrawals))
}
// Only accepts block number quries
Err(ProviderError::UnsupportedProvider)
}

fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
// Required data not present in static_files
Err(ProviderError::UnsupportedProvider)
}
}

impl<N: FullNodePrimitives<BlockHeader: Value>> OmmersProvider for StaticFileJarProvider<'_, N> {
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
if let Some(num) = id.as_number() {
return Ok(self.cursor()?.get_one::<OmmerMask>(num.into())?.map(|s| s.ommers))
}
// Only accepts block number quries
Err(ProviderError::UnsupportedProvider)
}
}

impl<N: NodePrimitives> BlockBodyIndicesProvider for StaticFileJarProvider<'_, N> {
fn block_body_indices(&self, num: u64) -> ProviderResult<Option<StoredBlockBodyIndices>> {
self.cursor()?.get_one::<BodyIndiceMask>(num.into())
}
}
49 changes: 40 additions & 9 deletions crates/storage/provider/src/providers/static_file/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,25 +1666,56 @@ impl<N: FullNodePrimitives<SignedTx: Value, Receipt: Value, BlockHeader: Value>>
impl<N: NodePrimitives> WithdrawalsProvider for StaticFileProvider<N> {
fn withdrawals_by_block(
&self,
_id: BlockHashOrNumber,
_timestamp: u64,
id: BlockHashOrNumber,
_: u64,
) -> ProviderResult<Option<Withdrawals>> {
// Required data not present in static_files
if let Some(num) = id.as_number() {
return self
.get_segment_provider_from_block(StaticFileSegment::BlockMeta, num, None)
.and_then(|provider| provider.withdrawals_by_block(num))
.or_else(|err| {
if let ProviderError::MissingStaticFileBlock(_, _) = err {
Ok(None)
} else {
Err(err)
}
})
}
// Only accepts block number quries
Err(ProviderError::UnsupportedProvider)
}
}

impl<N: FullNodePrimitives<BlockHeader: Value>> OmmersProvider for StaticFileProvider<N> {
fn ommers(&self, _id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
// Required data not present in static_files
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
if let Some(num) = id.as_number() {
return self
.get_segment_provider_from_block(StaticFileSegment::BlockMeta, num, None)
.and_then(|provider| provider.ommers(id))
.or_else(|err| {
if let ProviderError::MissingStaticFileBlock(_, _) = err {
Ok(None)
} else {
Err(err)
}
})
}
// Only accepts block number quries
Err(ProviderError::UnsupportedProvider)
}
}

impl<N: Send + Sync> BlockBodyIndicesProvider for StaticFileProvider<N> {
fn block_body_indices(&self, _num: u64) -> ProviderResult<Option<StoredBlockBodyIndices>> {
// Required data not present in static_files
Err(ProviderError::UnsupportedProvider)
impl<N: NodePrimitives> BlockBodyIndicesProvider for StaticFileProvider<N> {
fn block_body_indices(&self, num: u64) -> ProviderResult<Option<StoredBlockBodyIndices>> {
self.get_segment_provider_from_block(StaticFileSegment::BlockMeta, num, None)
.and_then(|provider| provider.block_body_indices(num))
.or_else(|err| {
if let ProviderError::MissingStaticFileBlock(_, _) = err {
Ok(None)
} else {
Err(err)
}
})
}
}

Expand Down
46 changes: 46 additions & 0 deletions crates/storage/provider/src/providers/static_file/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use alloy_consensus::BlockHeader;
use alloy_primitives::{BlockHash, BlockNumber, TxNumber, U256};
use parking_lot::{lock_api::RwLockWriteGuard, RawRwLock, RwLock};
use reth_codecs::Compact;
use reth_db::models::StoredBlockBodyIndices;
use reth_db_api::models::CompactU256;
use reth_nippy_jar::{NippyJar, NippyJarError, NippyJarWriter};
use reth_node_types::NodePrimitives;
Expand Down Expand Up @@ -554,6 +555,45 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
Ok(())
}

/// Appends [`StoredBlockBodyIndices`] and any other two arbitrary types belonging to the block
/// body ([`reth_db::models::StoredBlockOmmers`] and
/// [`reth_db::models::StoredBlockWithdrawals`] on ethereum) to static file.
///
/// It **CALLS** `increment_block()` since it's a block based segment.
pub fn append_block_meta<F1, F2>(
&mut self,
body_indices: &StoredBlockBodyIndices,
field1: &F1,
field2: &F2,
expected_block_number: BlockNumber,
) -> ProviderResult<()>
where
N::BlockHeader: Compact,
F1: Compact,
F2: Compact,
{
let start = Instant::now();
self.ensure_no_queued_prune()?;

debug_assert!(self.writer.user_header().segment() == StaticFileSegment::BlockMeta);

self.increment_block(expected_block_number)?;

self.append_column(body_indices)?;
self.append_column(field1)?;
self.append_column(field2)?;

if let Some(metrics) = &self.metrics {
metrics.record_segment_operation(
StaticFileSegment::BlockMeta,
StaticFileProviderOperation::Append,
Some(start.elapsed()),
);
}

Ok(())
}

/// Appends transaction to static file.
///
/// It **DOES NOT CALL** `increment_block()`, it should be handled elsewhere. There might be
Expand Down Expand Up @@ -681,6 +721,12 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
self.queue_prune(to_delete, None)
}

/// Adds an instruction to prune `to_delete` bloc_ meta rows during commit.
pub fn prune_block_meta(&mut self, to_delete: u64) -> ProviderResult<()> {
debug_assert_eq!(self.writer.user_header().segment(), StaticFileSegment::BlockMeta);
self.queue_prune(to_delete, None)
}

/// Adds an instruction to prune `to_delete` elements during commit.
///
/// Note: `last_block` refers to the block the unwinds ends at if dealing with transaction-based
Expand Down

0 comments on commit abcc7d5

Please sign in to comment.