reth_prune/segments/
set.rs

1use crate::segments::{
2    AccountHistory, ReceiptsByLogs, Segment, SenderRecovery, StorageHistory, TransactionLookup,
3    UserReceipts,
4};
5use alloy_eips::eip2718::Encodable2718;
6use reth_db_api::{table::Value, transaction::DbTxMut};
7use reth_primitives_traits::NodePrimitives;
8use reth_provider::{
9    providers::StaticFileProvider, BlockReader, DBProvider, PruneCheckpointReader,
10    PruneCheckpointWriter, StaticFileProviderFactory,
11};
12use reth_prune_types::PruneModes;
13
14use super::{StaticFileHeaders, StaticFileReceipts, StaticFileTransactions};
15
16/// Collection of [`Segment`]. Thread-safe, allocated on the heap.
17#[derive(Debug)]
18pub struct SegmentSet<Provider> {
19    inner: Vec<Box<dyn Segment<Provider>>>,
20}
21
22impl<Provider> SegmentSet<Provider> {
23    /// Returns empty [`SegmentSet`] collection.
24    pub fn new() -> Self {
25        Self::default()
26    }
27
28    /// Adds new [`Segment`] to collection.
29    pub fn segment<S: Segment<Provider> + 'static>(mut self, segment: S) -> Self {
30        self.inner.push(Box::new(segment));
31        self
32    }
33
34    /// Adds new [Segment] to collection if it's [Some].
35    pub fn segment_opt<S: Segment<Provider> + 'static>(self, segment: Option<S>) -> Self {
36        if let Some(segment) = segment {
37            return self.segment(segment)
38        }
39        self
40    }
41
42    /// Consumes [`SegmentSet`] and returns a [Vec].
43    pub fn into_vec(self) -> Vec<Box<dyn Segment<Provider>>> {
44        self.inner
45    }
46}
47
48impl<Provider> SegmentSet<Provider>
49where
50    Provider: StaticFileProviderFactory<
51            Primitives: NodePrimitives<SignedTx: Value, Receipt: Value, BlockHeader: Value>,
52        > + DBProvider<Tx: DbTxMut>
53        + PruneCheckpointWriter
54        + PruneCheckpointReader
55        + BlockReader<Transaction: Encodable2718>,
56{
57    /// Creates a [`SegmentSet`] from an existing components, such as [`StaticFileProvider`] and
58    /// [`PruneModes`].
59    pub fn from_components(
60        static_file_provider: StaticFileProvider<Provider::Primitives>,
61        prune_modes: PruneModes,
62    ) -> Self {
63        let PruneModes {
64            sender_recovery,
65            transaction_lookup,
66            receipts,
67            account_history,
68            storage_history,
69            bodies_history: _,
70            receipts_log_filter,
71        } = prune_modes;
72
73        Self::default()
74            // Static file headers
75            .segment(StaticFileHeaders::new(static_file_provider.clone()))
76            // Static file transactions
77            .segment(StaticFileTransactions::new(static_file_provider.clone()))
78            // Static file receipts
79            .segment(StaticFileReceipts::new(static_file_provider))
80            // Account history
81            .segment_opt(account_history.map(AccountHistory::new))
82            // Storage history
83            .segment_opt(storage_history.map(StorageHistory::new))
84            // User receipts
85            .segment_opt(receipts.map(UserReceipts::new))
86            // Receipts by logs
87            .segment_opt(
88                (!receipts_log_filter.is_empty())
89                    .then(|| ReceiptsByLogs::new(receipts_log_filter.clone())),
90            )
91            // Transaction lookup
92            .segment_opt(transaction_lookup.map(TransactionLookup::new))
93            // Sender recovery
94            .segment_opt(sender_recovery.map(SenderRecovery::new))
95    }
96}
97
98impl<Provider> Default for SegmentSet<Provider> {
99    fn default() -> Self {
100        Self { inner: Vec::new() }
101    }
102}