Skip to content

Commit

Permalink
feat: impl remove_blocks_above to static file service (paradigmxyz#9553)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected authored Jul 16, 2024
1 parent d5964ca commit 4381b0c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
45 changes: 35 additions & 10 deletions crates/engine/tree/src/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use reth_db::database::Database;
use reth_errors::ProviderResult;
use reth_primitives::{SealedBlock, StaticFileSegment, TransactionSignedNoHash, B256, U256};
use reth_provider::{ProviderFactory, StaticFileProviderFactory, StaticFileWriter};
use reth_provider::{
ProviderFactory, StaticFileProviderFactory, StaticFileWriter, TransactionsProviderExt,
};
use std::sync::{
mpsc::{Receiver, SendError, Sender},
Arc,
Expand Down Expand Up @@ -162,17 +164,40 @@ where
block_num: u64,
sender: oneshot::Sender<()>,
) -> ProviderResult<()> {
let provider = self.provider.static_file_provider();
let sf_provider = self.provider.static_file_provider();
let db_provider_rw = self.provider.provider_rw()?;

// get highest static file block for the total block range
let highest_static_file_block = sf_provider
.get_highest_static_file_block(StaticFileSegment::Headers)
.expect("todo: error handling, headers should exist");

// Get the total txs for the block range, so we have the correct number of columns for
// receipts and transactions
let tx_range = db_provider_rw
.transaction_range_by_block_range(block_num..=highest_static_file_block)?;
let total_txs = tx_range.end().saturating_sub(*tx_range.start());

// get the writers
let mut _header_writer = provider.get_writer(block_num, StaticFileSegment::Headers)?;
let mut _transactions_writer =
provider.get_writer(block_num, StaticFileSegment::Transactions)?;
let mut _receipts_writer = provider.get_writer(block_num, StaticFileSegment::Receipts)?;

// TODO: how do we delete s.t. `block_num` is the start? Additionally, do we need to index
// by tx num for the transactions segment?
todo!("implement remove_blocks_above")
let mut header_writer = sf_provider.get_writer(block_num, StaticFileSegment::Headers)?;
let mut transactions_writer =
sf_provider.get_writer(block_num, StaticFileSegment::Transactions)?;
let mut receipts_writer = sf_provider.get_writer(block_num, StaticFileSegment::Receipts)?;

// finally actually truncate, these internally commit
receipts_writer.truncate(StaticFileSegment::Receipts, total_txs, Some(block_num))?;
transactions_writer.truncate(
StaticFileSegment::Transactions,
total_txs,
Some(block_num),
)?;
header_writer.truncate(
StaticFileSegment::Headers,
highest_static_file_block.saturating_sub(block_num),
None,
)?;

Ok(())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ impl StaticFileProviderRW {
///
/// # Note
/// Commits to the configuration file at the end.
fn truncate(
pub fn truncate(
&mut self,
segment: StaticFileSegment,
num_rows: u64,
Expand Down

0 comments on commit 4381b0c

Please sign in to comment.