Skip to content
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

Create savepoint if it has been SAVEPOINT_INTERVAL blocks since the… #2365

Merged
merged 9 commits into from
Dec 20, 2024
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
Loading