From 604ce7370a68f23e372c30bd26be5264f3cc2f30 Mon Sep 17 00:00:00 2001 From: Oscar Mira Date: Wed, 21 May 2025 00:23:35 +0200 Subject: [PATCH] checkpoints: support nearest lower checkpoint lookup (cherry picked from commit e5c424b38d51705ffd1722aa2fe3a0e7db9a40d4) --- src/checkpoints/checkpoints.cpp | 13 +++++++++++++ src/checkpoints/checkpoints.h | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/src/checkpoints/checkpoints.cpp b/src/checkpoints/checkpoints.cpp index 2902bafb2..45589d985 100644 --- a/src/checkpoints/checkpoints.cpp +++ b/src/checkpoints/checkpoints.cpp @@ -158,6 +158,19 @@ namespace cryptonote return m_points.rbegin()->first; } //--------------------------------------------------------------------------- + uint64_t checkpoints::get_nearest_checkpoint_height(uint64_t block_height) const + { + if (m_points.empty()) + return 0; + + auto it = m_points.upper_bound(block_height); + if (it == m_points.begin()) + return 0; + + --it; + return it->first; + } + //--------------------------------------------------------------------------- const std::map& checkpoints::get_points() const { return m_points; diff --git a/src/checkpoints/checkpoints.h b/src/checkpoints/checkpoints.h index 07daeb4c0..d8de54eff 100644 --- a/src/checkpoints/checkpoints.h +++ b/src/checkpoints/checkpoints.h @@ -129,6 +129,14 @@ namespace cryptonote */ uint64_t get_max_height() const; + /** + * @brief gets the highest checkpoint height less than the given block height + * + * @param block_height the reference block height + * @return the nearest checkpoint height below block_height, or 0 if none + */ + uint64_t get_nearest_checkpoint_height(uint64_t block_height) const; + /** * @brief gets the checkpoints container *