reth_db_api/models/metadata.rs
1//! Storage metadata models.
2
3use reth_codecs::{add_arbitrary_tests, Compact};
4use serde::{Deserialize, Serialize};
5
6/// Storage configuration settings for this node.
7///
8/// These should be set during `init_genesis` or `init_db` depending on whether we want dictate
9/// behaviour of new or old nodes respectively.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, Compact)]
11#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
12#[add_arbitrary_tests(compact)]
13pub struct StorageSettings {
14 /// Whether this node always writes receipts to static files.
15 ///
16 /// If this is set to FALSE AND receipt pruning IS ENABLED, all receipts should be written to DB. Otherwise, they should be written to static files. This ensures that older nodes do not need to migrate their current DB tables to static files. For more, read: <https://github.com/paradigmxyz/reth/issues/18890#issuecomment-3457760097>
17 #[serde(default)]
18 pub receipts_in_static_files: bool,
19 /// Whether this node always writes transaction senders to static files.
20 #[serde(default)]
21 pub transaction_senders_in_static_files: bool,
22 /// Whether `StoragesHistory` is stored in `RocksDB`.
23 #[serde(default)]
24 pub storages_history_in_rocksdb: bool,
25 /// Whether `TransactionHashNumbers` is stored in `RocksDB`.
26 #[serde(default)]
27 pub transaction_hash_numbers_in_rocksdb: bool,
28 /// Whether `AccountsHistory` is stored in `RocksDB`.
29 #[serde(default)]
30 pub account_history_in_rocksdb: bool,
31 /// Whether this node should read and write account changesets from static files.
32 #[serde(default)]
33 pub account_changesets_in_static_files: bool,
34 /// Whether this node should read and write storage changesets from static files.
35 #[serde(default)]
36 pub storage_changesets_in_static_files: bool,
37}
38
39impl StorageSettings {
40 /// Returns the default base `StorageSettings` for this build.
41 ///
42 /// When the `edge` feature is enabled, returns [`Self::edge()`].
43 /// Otherwise, returns [`Self::legacy()`].
44 pub const fn base() -> Self {
45 #[cfg(feature = "edge")]
46 {
47 Self::edge()
48 }
49 #[cfg(not(feature = "edge"))]
50 {
51 Self::legacy()
52 }
53 }
54
55 /// Creates `StorageSettings` for edge nodes with all storage features enabled:
56 /// - Receipts and transaction senders in static files
57 /// - History indices in `RocksDB` (storages, accounts, transaction hashes)
58 /// - Account changesets in static files
59 #[cfg(feature = "edge")]
60 pub const fn edge() -> Self {
61 Self {
62 receipts_in_static_files: true,
63 transaction_senders_in_static_files: true,
64 account_changesets_in_static_files: true,
65 storage_changesets_in_static_files: true,
66 storages_history_in_rocksdb: false,
67 transaction_hash_numbers_in_rocksdb: true,
68 account_history_in_rocksdb: false,
69 }
70 }
71
72 /// Creates `StorageSettings` for legacy nodes.
73 ///
74 /// This explicitly sets `receipts_in_static_files` and `transaction_senders_in_static_files` to
75 /// `false`, ensuring older nodes continue writing receipts and transaction senders to the
76 /// database when receipt pruning is enabled.
77 pub const fn legacy() -> Self {
78 Self {
79 receipts_in_static_files: false,
80 transaction_senders_in_static_files: false,
81 storages_history_in_rocksdb: false,
82 transaction_hash_numbers_in_rocksdb: false,
83 account_history_in_rocksdb: false,
84 account_changesets_in_static_files: false,
85 storage_changesets_in_static_files: false,
86 }
87 }
88
89 /// Sets the `receipts_in_static_files` flag to the provided value.
90 pub const fn with_receipts_in_static_files(mut self, value: bool) -> Self {
91 self.receipts_in_static_files = value;
92 self
93 }
94
95 /// Sets the `transaction_senders_in_static_files` flag to the provided value.
96 pub const fn with_transaction_senders_in_static_files(mut self, value: bool) -> Self {
97 self.transaction_senders_in_static_files = value;
98 self
99 }
100
101 /// Sets the `storages_history_in_rocksdb` flag to the provided value.
102 pub const fn with_storages_history_in_rocksdb(mut self, value: bool) -> Self {
103 self.storages_history_in_rocksdb = value;
104 self
105 }
106
107 /// Sets the `transaction_hash_numbers_in_rocksdb` flag to the provided value.
108 pub const fn with_transaction_hash_numbers_in_rocksdb(mut self, value: bool) -> Self {
109 self.transaction_hash_numbers_in_rocksdb = value;
110 self
111 }
112
113 /// Sets the `account_history_in_rocksdb` flag to the provided value.
114 pub const fn with_account_history_in_rocksdb(mut self, value: bool) -> Self {
115 self.account_history_in_rocksdb = value;
116 self
117 }
118
119 /// Sets the `account_changesets_in_static_files` flag to the provided value.
120 pub const fn with_account_changesets_in_static_files(mut self, value: bool) -> Self {
121 self.account_changesets_in_static_files = value;
122 self
123 }
124
125 /// Sets the `storage_changesets_in_static_files` flag to the provided value.
126 pub const fn with_storage_changesets_in_static_files(mut self, value: bool) -> Self {
127 self.storage_changesets_in_static_files = value;
128 self
129 }
130
131 /// Returns `true` if any tables are configured to be stored in `RocksDB`.
132 pub const fn any_in_rocksdb(&self) -> bool {
133 self.transaction_hash_numbers_in_rocksdb ||
134 self.account_history_in_rocksdb ||
135 self.storages_history_in_rocksdb
136 }
137}