Skip to content

Commit

Permalink
Create savepoints more robustly (#2365)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmart7t2 authored Dec 20, 2024
1 parent e0e8d40 commit 83d87d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub(crate) enum Statistic {
Runes = 13,
SatRanges = 14,
UnboundInscriptions = 16,
LastSavepointHeight = 17,
}

impl Statistic {
Expand Down
30 changes: 19 additions & 11 deletions src/index/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,21 @@ impl Reorg {
return Ok(());
}

if (height < SAVEPOINT_INTERVAL || height % SAVEPOINT_INTERVAL == 0)
&& u32::try_from(
index
.settings
.bitcoin_rpc_client(None)?
.get_blockchain_info()?
.headers,
)
.unwrap()
.saturating_sub(height)
<= CHAIN_TIP_DISTANCE
let height = u64::from(height);

let last_savepoint_height = index
.begin_read()?
.0
.open_table(STATISTIC_TO_COUNT)?
.get(&Statistic::LastSavepointHeight.key())?
.map(|last_savepoint_height| last_savepoint_height.value())
.unwrap_or(0);

let blocks = index.client.get_blockchain_info()?.headers;

if (height < SAVEPOINT_INTERVAL.into()
|| height.saturating_sub(last_savepoint_height) >= SAVEPOINT_INTERVAL.into())
&& blocks.saturating_sub(height) <= CHAIN_TIP_DISTANCE.into()
{
let wtx = index.begin_write()?;

Expand All @@ -111,6 +115,10 @@ impl Reorg {
log::debug!("creating savepoint at height {}", height);
wtx.persistent_savepoint()?;

wtx
.open_table(STATISTIC_TO_COUNT)?
.insert(&Statistic::LastSavepointHeight.key(), &height)?;

Index::increment_statistic(&wtx, Statistic::Commits, 1)?;
wtx.commit()?;
}
Expand Down

0 comments on commit 83d87d4

Please sign in to comment.