Skip to content

Commit 83d87d4

Browse files
authored
Create savepoints more robustly (ordinals#2365)
1 parent e0e8d40 commit 83d87d4

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/index.rs

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub(crate) enum Statistic {
9191
Runes = 13,
9292
SatRanges = 14,
9393
UnboundInscriptions = 16,
94+
LastSavepointHeight = 17,
9495
}
9596

9697
impl Statistic {

src/index/reorg.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,21 @@ impl Reorg {
8383
return Ok(());
8484
}
8585

86-
if (height < SAVEPOINT_INTERVAL || height % SAVEPOINT_INTERVAL == 0)
87-
&& u32::try_from(
88-
index
89-
.settings
90-
.bitcoin_rpc_client(None)?
91-
.get_blockchain_info()?
92-
.headers,
93-
)
94-
.unwrap()
95-
.saturating_sub(height)
96-
<= CHAIN_TIP_DISTANCE
86+
let height = u64::from(height);
87+
88+
let last_savepoint_height = index
89+
.begin_read()?
90+
.0
91+
.open_table(STATISTIC_TO_COUNT)?
92+
.get(&Statistic::LastSavepointHeight.key())?
93+
.map(|last_savepoint_height| last_savepoint_height.value())
94+
.unwrap_or(0);
95+
96+
let blocks = index.client.get_blockchain_info()?.headers;
97+
98+
if (height < SAVEPOINT_INTERVAL.into()
99+
|| height.saturating_sub(last_savepoint_height) >= SAVEPOINT_INTERVAL.into())
100+
&& blocks.saturating_sub(height) <= CHAIN_TIP_DISTANCE.into()
97101
{
98102
let wtx = index.begin_write()?;
99103

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

118+
wtx
119+
.open_table(STATISTIC_TO_COUNT)?
120+
.insert(&Statistic::LastSavepointHeight.key(), &height)?;
121+
114122
Index::increment_statistic(&wtx, Statistic::Commits, 1)?;
115123
wtx.commit()?;
116124
}

0 commit comments

Comments
 (0)