From de0b511a31c2aff645e2f5fbd38d5323b287dfc9 Mon Sep 17 00:00:00 2001 From: Greg Martin Date: Thu, 24 Aug 2023 20:57:25 +0000 Subject: [PATCH 1/7] Create savepoint if it has been `SAVEPOINT_INTERVAL` blocks since the last one --- src/index.rs | 1 + src/index/reorg.rs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.rs b/src/index.rs index 75e3f0ace5..630fd58ab7 100644 --- a/src/index.rs +++ b/src/index.rs @@ -73,6 +73,7 @@ pub(crate) enum Statistic { OutputsTraversed = 3, SatRanges = 4, UnboundInscriptions = 5, + LastSavePointHeight = 6, } impl Statistic { diff --git a/src/index/reorg.rs b/src/index/reorg.rs index fc6164282c..2ffc1dadb3 100644 --- a/src/index/reorg.rs +++ b/src/index/reorg.rs @@ -83,7 +83,15 @@ impl Reorg { return Ok(()); } - if (height < SAVEPOINT_INTERVAL || height % SAVEPOINT_INTERVAL == 0) + let last_save_point_height = index + .begin_read()? + .0 + .open_table(STATISTIC_TO_COUNT)? + .get(&Statistic::LastSavePointHeight.key())? + .map(|last_save_point_height| last_save_point_height.value()) + .unwrap_or(0); + + if (height < SAVEPOINT_INTERVAL || height - last_save_point_height >= SAVEPOINT_INTERVAL) && index .client .get_blockchain_info()? @@ -107,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()?; } From c8d8cbd4f1057ea3a3c83c7ff58a861cf5adab42 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 19 Dec 2024 18:18:35 -0500 Subject: [PATCH 2/7] Amend --- src/index/reorg.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index/reorg.rs b/src/index/reorg.rs index b9c6f68e5e..c8bc1e7803 100644 --- a/src/index/reorg.rs +++ b/src/index/reorg.rs @@ -100,7 +100,8 @@ impl Reorg { .try_into() .unwrap(); - if (height < SAVEPOINT_INTERVAL || height - last_save_point_height >= SAVEPOINT_INTERVAL) + if (height < SAVEPOINT_INTERVAL + || height.saturating_sub(last_save_point_height) >= SAVEPOINT_INTERVAL) && blocks.saturating_sub(height) <= CHAIN_TIP_DISTANCE { let wtx = index.begin_write()?; From be137e25e1674779dd79104e6cc0703164585f08 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 19 Dec 2024 18:20:08 -0500 Subject: [PATCH 3/7] Bump index schema version --- src/index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.rs b/src/index.rs index cd48de6bc5..631accba00 100644 --- a/src/index.rs +++ b/src/index.rs @@ -50,7 +50,7 @@ mod utxo_entry; #[cfg(test)] pub(crate) mod testing; -const SCHEMA_VERSION: u64 = 30; +const SCHEMA_VERSION: u64 = 31; define_multimap_table! { SAT_TO_SEQUENCE_NUMBER, u64, u32 } define_multimap_table! { SEQUENCE_NUMBER_TO_CHILDREN, u32, u32 } From b8436e64a54bfb0c7fe973f4683c9a14a997071f Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 19 Dec 2024 19:17:26 -0500 Subject: [PATCH 4/7] Amend --- src/index/reorg.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index/reorg.rs b/src/index/reorg.rs index c8bc1e7803..b69c9eb985 100644 --- a/src/index/reorg.rs +++ b/src/index/reorg.rs @@ -83,12 +83,12 @@ impl Reorg { return Ok(()); } - let last_save_point_height: u32 = index + let last_savepoint_height: u32 = index .begin_read()? .0 .open_table(STATISTIC_TO_COUNT)? .get(&Statistic::LastSavePointHeight.key())? - .map(|last_save_point_height| last_save_point_height.value()) + .map(|last_savepoint_height| last_savepoint_height.value()) .unwrap_or(0) .try_into() .unwrap(); @@ -101,7 +101,7 @@ impl Reorg { .unwrap(); if (height < SAVEPOINT_INTERVAL - || height.saturating_sub(last_save_point_height) >= SAVEPOINT_INTERVAL) + || height.saturating_sub(last_savepoint_height) >= SAVEPOINT_INTERVAL) && blocks.saturating_sub(height) <= CHAIN_TIP_DISTANCE { let wtx = index.begin_write()?; From 96bc2fd45ef4da14efe5983331e44b40926a3faf Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 19 Dec 2024 19:20:26 -0500 Subject: [PATCH 5/7] Amend --- src/index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.rs b/src/index.rs index 631accba00..cd48de6bc5 100644 --- a/src/index.rs +++ b/src/index.rs @@ -50,7 +50,7 @@ mod utxo_entry; #[cfg(test)] pub(crate) mod testing; -const SCHEMA_VERSION: u64 = 31; +const SCHEMA_VERSION: u64 = 30; define_multimap_table! { SAT_TO_SEQUENCE_NUMBER, u64, u32 } define_multimap_table! { SEQUENCE_NUMBER_TO_CHILDREN, u32, u32 } From 2b94254fbf71efff0eeed8fc762f0447dffb8041 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 19 Dec 2024 19:21:33 -0500 Subject: [PATCH 6/7] Amend --- src/index.rs | 2 +- src/index/reorg.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.rs b/src/index.rs index cd48de6bc5..bac14b8725 100644 --- a/src/index.rs +++ b/src/index.rs @@ -91,7 +91,7 @@ pub(crate) enum Statistic { Runes = 13, SatRanges = 14, UnboundInscriptions = 16, - LastSavePointHeight = 17, + LastSavepointHeight = 17, } impl Statistic { diff --git a/src/index/reorg.rs b/src/index/reorg.rs index b69c9eb985..7d34c42974 100644 --- a/src/index/reorg.rs +++ b/src/index/reorg.rs @@ -87,7 +87,7 @@ impl Reorg { .begin_read()? .0 .open_table(STATISTIC_TO_COUNT)? - .get(&Statistic::LastSavePointHeight.key())? + .get(&Statistic::LastSavepointHeight.key())? .map(|last_savepoint_height| last_savepoint_height.value()) .unwrap_or(0) .try_into() @@ -122,7 +122,7 @@ impl Reorg { wtx .open_table(STATISTIC_TO_COUNT)? - .insert(&Statistic::LastSavePointHeight.key(), &u64::from(height))?; + .insert(&Statistic::LastSavepointHeight.key(), &u64::from(height))?; Index::increment_statistic(&wtx, Statistic::Commits, 1)?; wtx.commit()?; From 97feafd7f468cee646b7ceae815b3e457864e403 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 19 Dec 2024 16:25:44 -0800 Subject: [PATCH 7/7] Convert height to u64 first --- src/index/reorg.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/index/reorg.rs b/src/index/reorg.rs index 7d34c42974..9a1ff58b06 100644 --- a/src/index/reorg.rs +++ b/src/index/reorg.rs @@ -83,26 +83,21 @@ impl Reorg { return Ok(()); } - let last_savepoint_height: u32 = index + 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) - .try_into() - .unwrap(); - - let blocks: u32 = index - .client - .get_blockchain_info()? - .headers - .try_into() - .unwrap(); - - if (height < SAVEPOINT_INTERVAL - || height.saturating_sub(last_savepoint_height) >= SAVEPOINT_INTERVAL) - && blocks.saturating_sub(height) <= CHAIN_TIP_DISTANCE + .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()?; @@ -122,7 +117,7 @@ impl Reorg { wtx .open_table(STATISTIC_TO_COUNT)? - .insert(&Statistic::LastSavepointHeight.key(), &u64::from(height))?; + .insert(&Statistic::LastSavepointHeight.key(), &height)?; Index::increment_statistic(&wtx, Statistic::Commits, 1)?; wtx.commit()?;