-
Couldn't load subscription status.
- Fork 2k
feat: add pruning of transactions from static-files #19241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+280
−69
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d422073
add reth-prune-static-files
joshieDo e953a2f
add test & inclusive fix
joshieDo aca5eb5
add docs to setup_static_file_jars
joshieDo c66d34b
fmt
joshieDo cbea4cd
add len() to SegmentRangeInclusive
joshieDo 3b40e86
Merge remote-tracking branch 'origin/main' into joshie/prune-sf
joshieDo 4c20d36
move to prune crate
joshieDo 46b33e9
lint
joshieDo d7a9b40
run bodies segment first
joshieDo 2a97e0d
ensure we never delete the highest static file of the segment
joshieDo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| use crate::{ | ||
| segments::{PruneInput, Segment}, | ||
| PrunerError, | ||
| }; | ||
| use reth_provider::{BlockReader, StaticFileProviderFactory}; | ||
| use reth_prune_types::{ | ||
| PruneMode, PruneProgress, PrunePurpose, PruneSegment, SegmentOutput, SegmentOutputCheckpoint, | ||
| }; | ||
| use reth_static_file_types::StaticFileSegment; | ||
|
|
||
| /// Segment responsible for pruning transactions in static files. | ||
| /// | ||
| /// This segment is controlled by the `bodies_history` configuration. | ||
| #[derive(Debug)] | ||
| pub struct Bodies { | ||
| mode: PruneMode, | ||
| } | ||
|
|
||
| impl Bodies { | ||
| /// Creates a new [`Bodies`] segment with the given prune mode. | ||
| pub const fn new(mode: PruneMode) -> Self { | ||
| Self { mode } | ||
| } | ||
| } | ||
|
|
||
| impl<Provider> Segment<Provider> for Bodies | ||
| where | ||
| Provider: StaticFileProviderFactory + BlockReader, | ||
| { | ||
| fn segment(&self) -> PruneSegment { | ||
| PruneSegment::Bodies | ||
| } | ||
|
|
||
| fn mode(&self) -> Option<PruneMode> { | ||
| Some(self.mode) | ||
| } | ||
|
|
||
| fn purpose(&self) -> PrunePurpose { | ||
| PrunePurpose::User | ||
| } | ||
|
|
||
| fn prune(&self, provider: &Provider, input: PruneInput) -> Result<SegmentOutput, PrunerError> { | ||
| let deleted_headers = provider | ||
| .static_file_provider() | ||
| .delete_segment_below_block(StaticFileSegment::Transactions, input.to_block + 1)?; | ||
|
|
||
| if deleted_headers.is_empty() { | ||
| return Ok(SegmentOutput::done()) | ||
| } | ||
|
|
||
| let tx_ranges = deleted_headers.iter().filter_map(|header| header.tx_range()); | ||
|
|
||
| let pruned = tx_ranges.clone().map(|range| range.len()).sum::<u64>() as usize; | ||
|
|
||
| Ok(SegmentOutput { | ||
| progress: PruneProgress::Finished, | ||
| pruned, | ||
| checkpoint: Some(SegmentOutputCheckpoint { | ||
| block_number: Some(input.to_block), | ||
| tx_number: tx_ranges.map(|range| range.end()).max(), | ||
| }), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::Pruner; | ||
| use alloy_primitives::BlockNumber; | ||
| use reth_exex_types::FinishedExExHeight; | ||
| use reth_provider::{ | ||
| test_utils::{create_test_provider_factory, MockNodeTypesWithDB}, | ||
| ProviderFactory, StaticFileWriter, | ||
| }; | ||
| use reth_prune_types::{PruneMode, PruneProgress, PruneSegment}; | ||
| use reth_static_file_types::{ | ||
| SegmentHeader, SegmentRangeInclusive, StaticFileSegment, DEFAULT_BLOCKS_PER_STATIC_FILE, | ||
| }; | ||
|
|
||
| /// Creates empty static file jars at 500k block intervals up to the tip block. | ||
| /// | ||
| /// Each jar contains sequential transaction ranges for testing deletion logic. | ||
| fn setup_static_file_jars<P: StaticFileProviderFactory>(provider: &P, tip_block: u64) { | ||
| let num_jars = (tip_block + 1) / DEFAULT_BLOCKS_PER_STATIC_FILE; | ||
| let txs_per_jar = 1000; | ||
| let static_file_provider = provider.static_file_provider(); | ||
|
|
||
| let mut writer = | ||
| static_file_provider.latest_writer(StaticFileSegment::Transactions).unwrap(); | ||
|
|
||
| for jar_idx in 0..num_jars { | ||
| let block_start = jar_idx * DEFAULT_BLOCKS_PER_STATIC_FILE; | ||
| let block_end = ((jar_idx + 1) * DEFAULT_BLOCKS_PER_STATIC_FILE - 1).min(tip_block); | ||
|
|
||
| let tx_start = jar_idx * txs_per_jar; | ||
| let tx_end = tx_start + txs_per_jar - 1; | ||
|
|
||
| *writer.user_header_mut() = SegmentHeader::new( | ||
| SegmentRangeInclusive::new(block_start, block_end), | ||
| Some(SegmentRangeInclusive::new(block_start, block_end)), | ||
| Some(SegmentRangeInclusive::new(tx_start, tx_end)), | ||
| StaticFileSegment::Transactions, | ||
| ); | ||
|
|
||
| writer.inner().set_dirty(); | ||
| writer.commit().expect("commit empty jar"); | ||
|
|
||
| if jar_idx < num_jars - 1 { | ||
| writer.increment_block(block_end + 1).expect("increment block"); | ||
| } | ||
| } | ||
|
|
||
| static_file_provider.initialize_index().expect("initialize index"); | ||
| } | ||
|
|
||
| struct PruneTestCase { | ||
| prune_mode: PruneMode, | ||
| expected_pruned: usize, | ||
| expected_lowest_block: Option<BlockNumber>, | ||
| } | ||
|
|
||
| fn run_prune_test( | ||
| factory: &ProviderFactory<MockNodeTypesWithDB>, | ||
| finished_exex_height_rx: &tokio::sync::watch::Receiver<FinishedExExHeight>, | ||
| test_case: PruneTestCase, | ||
| tip: BlockNumber, | ||
| ) { | ||
| let bodies = Bodies::new(test_case.prune_mode); | ||
| let segments: Vec<Box<dyn Segment<_>>> = vec![Box::new(bodies)]; | ||
|
|
||
| let mut pruner = Pruner::new_with_factory( | ||
| factory.clone(), | ||
| segments, | ||
| 5, | ||
| 10000, | ||
| None, | ||
| finished_exex_height_rx.clone(), | ||
| ); | ||
|
|
||
| let result = pruner.run(tip).expect("pruner run"); | ||
|
|
||
| assert_eq!(result.progress, PruneProgress::Finished); | ||
| assert_eq!(result.segments.len(), 1); | ||
|
|
||
| let (segment, output) = &result.segments[0]; | ||
| assert_eq!(*segment, PruneSegment::Bodies); | ||
| assert_eq!(output.pruned, test_case.expected_pruned); | ||
|
|
||
| let static_provider = factory.static_file_provider(); | ||
| assert_eq!( | ||
| static_provider.get_lowest_static_file_block(StaticFileSegment::Transactions), | ||
| test_case.expected_lowest_block | ||
| ); | ||
| assert_eq!( | ||
| static_provider.get_highest_static_file_block(StaticFileSegment::Transactions), | ||
| Some(tip) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bodies_prune_through_pruner() { | ||
| let factory = create_test_provider_factory(); | ||
| let tip = 2_499_999; | ||
| setup_static_file_jars(&factory, tip); | ||
|
|
||
| let (_, finished_exex_height_rx) = tokio::sync::watch::channel(FinishedExExHeight::NoExExs); | ||
|
|
||
| let test_cases = vec![ | ||
| // Test 1: PruneMode::Before(750_000) → deletes jar 1 (0-499_999) | ||
| PruneTestCase { | ||
| prune_mode: PruneMode::Before(750_000), | ||
| expected_pruned: 1000, | ||
| expected_lowest_block: Some(999_999), | ||
| }, | ||
| // Test 2: PruneMode::Before(850_000) → no deletion (jar 2: 500_000-999_999 contains | ||
| // target) | ||
| PruneTestCase { | ||
| prune_mode: PruneMode::Before(850_000), | ||
| expected_pruned: 0, | ||
| expected_lowest_block: Some(999_999), | ||
| }, | ||
| // Test 3: PruneMode::Before(1_599_999) → deletes jar 2 (500_000-999_999) and jar 3 | ||
| // (1_000_000-1_499_999) | ||
| PruneTestCase { | ||
| prune_mode: PruneMode::Before(1_599_999), | ||
| expected_pruned: 2000, | ||
| expected_lowest_block: Some(1_999_999), | ||
| }, | ||
| // Test 4: PruneMode::Distance(500_000) with tip=2_499_999 → deletes jar 4 | ||
| // (1_500_000-1_999_999) | ||
| PruneTestCase { | ||
| prune_mode: PruneMode::Distance(500_000), | ||
| expected_pruned: 1000, | ||
| expected_lowest_block: Some(2_499_999), | ||
| }, | ||
| // Test 5: PruneMode::Before(2_300_000) → no deletion (jar 5: 2_000_000-2_499_999 | ||
| // contains target) | ||
| PruneTestCase { | ||
| prune_mode: PruneMode::Before(2_300_000), | ||
| expected_pruned: 0, | ||
| expected_lowest_block: Some(2_499_999), | ||
| }, | ||
| ]; | ||
|
|
||
| for test_case in test_cases { | ||
| run_prune_test(&factory, &finished_exex_height_rx, test_case, tip); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we should do this in this pr
I'd prefer a dedicated pr for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather have it in the same PR, because it removes the explicit
ctx.expire_pre_merge_transactions()call, and we now rely on the pruner running on node startup with new bodies pruning.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, makes sense