Skip to content

Commit 74502fc

Browse files
committed
fix: add config option for clearing address db on start
Signed-off-by: William Hankins <[email protected]>
1 parent 16e8973 commit 74502fc

File tree

4 files changed

+45
-26
lines changed

4 files changed

+45
-26
lines changed

modules/address_state/src/address_state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const DEFAULT_PARAMETERS_SUBSCRIBE_TOPIC: (&str, &str) =
3333

3434
// Configuration defaults
3535
const DEFAULT_ADDRESS_DB_PATH: (&str, &str) = ("db-path", "./db");
36+
const DEFAULT_CLEAR_ON_START: (&str, bool) = ("clear-on-start", true);
3637
const DEFAULT_STORE_INFO: (&str, bool) = ("store-info", false);
3738
const DEFAULT_STORE_TOTALS: (&str, bool) = ("store-totals", false);
3839
const DEFAULT_STORE_TRANSACTIONS: (&str, bool) = ("store-transactions", false);
@@ -178,6 +179,7 @@ impl AddressState {
178179
// Get configuration flags and query topic
179180
let storage_config = AddressStorageConfig {
180181
db_path: get_string_flag(&config, DEFAULT_ADDRESS_DB_PATH),
182+
clear_on_start: get_bool_flag(&config, DEFAULT_CLEAR_ON_START),
181183
skip_until: None,
182184
store_info: get_bool_flag(&config, DEFAULT_STORE_INFO),
183185
store_totals: get_bool_flag(&config, DEFAULT_STORE_TOTALS),

modules/address_state/src/immutable_address_store.rs

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ pub struct ImmutableAddressStore {
3030
}
3131

3232
impl ImmutableAddressStore {
33-
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
33+
pub fn new(path: impl AsRef<Path>, clear_on_start: bool) -> Result<Self> {
3434
let path = path.as_ref();
35-
if path.exists() {
35+
if path.exists() && clear_on_start {
3636
std::fs::remove_dir_all(path)?;
3737
}
3838
let cfg = fjall::Config::new(path).max_write_buffer_size(512 * 1024 * 1024);
@@ -56,15 +56,36 @@ impl ImmutableAddressStore {
5656
/// for an entire epoch. Skips any partitions that have already stored the given epoch.
5757
/// All writes are batched and committed atomically, preventing on-disk corruption in case of failure.
5858
pub async fn persist_epoch(&self, epoch: u64, config: &AddressStorageConfig) -> Result<()> {
59-
let persist_utxos = config.store_info
60-
&& !self.epoch_exists(self.utxos.clone(), ADDRESS_UTXOS_EPOCH_COUNTER, epoch).await?;
61-
let persist_txs = config.store_transactions
62-
&& !self.epoch_exists(self.txs.clone(), ADDRESS_TXS_EPOCH_COUNTER, epoch).await?;
63-
let persist_totals = config.store_totals
64-
&& !self.epoch_exists(self.totals.clone(), ADDRESS_TOTALS_EPOCH_COUNTER, epoch).await?;
59+
// Skip if all options disabled
60+
if !(config.store_info || config.store_transactions || config.store_totals) {
61+
debug!("no persistence needed for epoch {epoch} (all stores disabled)");
62+
return Ok(());
63+
}
64+
65+
// Determine which partitions need persistence
66+
let (persist_utxos, persist_txs, persist_totals) = if config.clear_on_start {
67+
(
68+
config.store_info,
69+
config.store_transactions,
70+
config.store_totals,
71+
)
72+
} else {
73+
let utxos = config.store_info
74+
&& !self
75+
.epoch_exists(self.utxos.clone(), ADDRESS_UTXOS_EPOCH_COUNTER, epoch)
76+
.await?;
77+
let txs = config.store_transactions
78+
&& !self.epoch_exists(self.txs.clone(), ADDRESS_TXS_EPOCH_COUNTER, epoch).await?;
79+
let totals = config.store_totals
80+
&& !self
81+
.epoch_exists(self.totals.clone(), ADDRESS_TOTALS_EPOCH_COUNTER, epoch)
82+
.await?;
83+
(utxos, txs, totals)
84+
};
6585

86+
// Skip if all partitions have already been persisted for the epoch
6687
if !(persist_utxos || persist_txs || persist_totals) {
67-
debug!("no persistence needed for epoch {epoch} (already persisted or disabled)");
88+
debug!("no persistence needed for epoch {epoch}");
6889
return Ok(());
6990
}
7091

@@ -124,22 +145,14 @@ impl ImmutableAddressStore {
124145
}
125146

126147
// Metadata markers
127-
if persist_utxos {
128-
batch.insert(
129-
&self.utxos,
130-
ADDRESS_UTXOS_EPOCH_COUNTER,
131-
epoch.to_le_bytes(),
132-
);
133-
}
134-
if persist_txs {
135-
batch.insert(&self.txs, ADDRESS_TXS_EPOCH_COUNTER, epoch.to_le_bytes());
136-
}
137-
if persist_totals {
138-
batch.insert(
139-
&self.totals,
140-
ADDRESS_TOTALS_EPOCH_COUNTER,
141-
epoch.to_le_bytes(),
142-
);
148+
for (enabled, part, key) in [
149+
(persist_utxos, &self.utxos, ADDRESS_UTXOS_EPOCH_COUNTER),
150+
(persist_txs, &self.txs, ADDRESS_TXS_EPOCH_COUNTER),
151+
(persist_totals, &self.totals, ADDRESS_TOTALS_EPOCH_COUNTER),
152+
] {
153+
if enabled {
154+
batch.insert(part, key, epoch.to_le_bytes());
155+
}
143156
}
144157

145158
match batch.commit() {

modules/address_state/src/state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::{
1717
#[derive(Debug, Default, Clone)]
1818
pub struct AddressStorageConfig {
1919
pub db_path: String,
20+
pub clear_on_start: bool,
2021
pub skip_until: Option<u64>,
2122

2223
pub store_info: bool,
@@ -60,7 +61,7 @@ impl State {
6061
PathBuf::from(&config.db_path)
6162
};
6263

63-
let store = Arc::new(ImmutableAddressStore::new(&db_path)?);
64+
let store = Arc::new(ImmutableAddressStore::new(&db_path, config.clear_on_start)?);
6465

6566
let mut config = config.clone();
6667
config.skip_until = store.get_last_epoch_stored().await?;
@@ -252,6 +253,7 @@ mod tests {
252253
let dir = tempdir().unwrap();
253254
AddressStorageConfig {
254255
db_path: dir.path().to_string_lossy().into_owned(),
256+
clear_on_start: true,
255257
skip_until: None,
256258
store_info: true,
257259
store_transactions: true,

processes/omnibus/omnibus.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ index-by-policy = false
138138
clear-on-start = true
139139

140140
[module.address-state]
141+
# Clear state on start up (default true)
142+
clear-on-start = true
141143
# Enables /addresses/{address}, /addresses/{address}/extended,
142144
# /addresses/{address}/utxos/{asset}, and /addresses/{address}/utxos endpoints
143145
store-info = false

0 commit comments

Comments
 (0)