From cd7bf1249765a011210653c393b3a0fdb7106d55 Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Thu, 28 Sep 2023 22:53:49 +0200 Subject: [PATCH] [WIP] index: Allow index to start at a specific height --- src/index/base.cpp | 11 +++++++---- src/index/base.h | 3 ++- src/index/silentpaymentindex.cpp | 12 +++--------- src/index/silentpaymentindex.h | 4 ++-- src/init.cpp | 4 +++- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/index/base.cpp b/src/index/base.cpp index f18205a76ffd0..4c72a29ac6825 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -68,8 +68,8 @@ void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator batch.Write(DB_BEST_BLOCK, locator); } -BaseIndex::BaseIndex(std::unique_ptr chain, std::string name) - : m_chain{std::move(chain)}, m_name{std::move(name)} {} +BaseIndex::BaseIndex(std::unique_ptr chain, std::string name, int start_height) + : m_chain{std::move(chain)}, m_name{std::move(name)}, m_start_height{std::move(start_height)} {} BaseIndex::~BaseIndex() { @@ -119,11 +119,14 @@ bool BaseIndex::Init() return true; } -static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main) +static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain, int start_height) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { AssertLockHeld(cs_main); if (!pindex_prev) { + if (start_height > 0) { + return chain[start_height]; + } return chain.Genesis(); } @@ -153,7 +156,7 @@ void BaseIndex::ThreadSync() { LOCK(cs_main); - const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain); + const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain, m_start_height); if (!pindex_next) { SetBestBlockIndex(pindex); m_synced = true; diff --git a/src/index/base.h b/src/index/base.h index 9b2a41dc92b13..b41dbfe0b8740 100644 --- a/src/index/base.h +++ b/src/index/base.h @@ -101,6 +101,7 @@ class BaseIndex : public CValidationInterface std::unique_ptr m_chain; Chainstate* m_chainstate{nullptr}; const std::string m_name; + int m_start_height{0}; void BlockConnected(const std::shared_ptr& block, const CBlockIndex* pindex) override; @@ -129,7 +130,7 @@ class BaseIndex : public CValidationInterface void SetBestBlockIndex(const CBlockIndex* block); public: - BaseIndex(std::unique_ptr chain, std::string name); + BaseIndex(std::unique_ptr chain, std::string name, int start_height = 0); /// Destructor interrupts sync thread if running and blocks until it exits. virtual ~BaseIndex(); diff --git a/src/index/silentpaymentindex.cpp b/src/index/silentpaymentindex.cpp index b1b6d268f3382..4cb2ddc84e5f8 100644 --- a/src/index/silentpaymentindex.cpp +++ b/src/index/silentpaymentindex.cpp @@ -39,8 +39,8 @@ bool SilentPaymentIndex::DB::WriteSilentPayments(const std::pair chain, size_t n_cache_size, bool f_memory, bool f_wipe) - : BaseIndex(std::move(chain), "silentpaymentindex"), m_db(std::make_unique(n_cache_size, f_memory, f_wipe)) +SilentPaymentIndex::SilentPaymentIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory, bool f_wipe, int start_height) + : BaseIndex(std::move(chain), "silentpaymentindex", start_height), m_db(std::make_unique(n_cache_size, f_memory, f_wipe)) {} SilentPaymentIndex::~SilentPaymentIndex() {} @@ -98,12 +98,6 @@ bool SilentPaymentIndex::CustomAppend(const interfaces::BlockInfo& block) assert(block.data); - Consensus::Params consensus = Params().GetConsensus(); - - if (block.height < consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height) { - return true; - } - std::vector> items; const CBlockIndex* block_index = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash)); @@ -129,4 +123,4 @@ bool SilentPaymentIndex::FindSilentPayment(const uint256& block_hash, std::vecto return m_db->Read(std::make_pair(DB_SILENT_PAYMENT_INDEX, block_hash), tweaked_pub_key_sums); } -BaseIndex::DB& SilentPaymentIndex::GetDB() const { return *m_db; } \ No newline at end of file +BaseIndex::DB& SilentPaymentIndex::GetDB() const { return *m_db; } diff --git a/src/index/silentpaymentindex.h b/src/index/silentpaymentindex.h index 2f160b1f97b7b..c63e72d2e3dc0 100644 --- a/src/index/silentpaymentindex.h +++ b/src/index/silentpaymentindex.h @@ -45,7 +45,7 @@ class SilentPaymentIndex final : public BaseIndex BaseIndex::DB& GetDB() const override; public: - explicit SilentPaymentIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false); + explicit SilentPaymentIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false, int start_height = 0); // Destructor is declared because this class contains a unique_ptr to an incomplete type. virtual ~SilentPaymentIndex() override; @@ -56,4 +56,4 @@ class SilentPaymentIndex final : public BaseIndex /// The global txo silent payment index. May be null. extern std::unique_ptr g_silent_payment_index; -#endif // BITCOIN_INDEX_SILENTPAYMENTINDEX_H \ No newline at end of file +#endif // BITCOIN_INDEX_SILENTPAYMENTINDEX_H diff --git a/src/init.cpp b/src/init.cpp index f886226d473d0..ce2598cc7a7ad 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1591,8 +1591,10 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) node.indexes.emplace_back(g_coin_stats_index.get()); } + Consensus::Params consensus = Params().GetConsensus(); + const int sp_start_height = consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height; if (args.GetBoolArg("-silentpaymentindex", DEFAULT_SILENT_PAYMENT_INDEX)) { - g_silent_payment_index = std::make_unique(interfaces::MakeChain(node), cache_sizes.tx_index, false, fReindex); + g_silent_payment_index = std::make_unique(interfaces::MakeChain(node), cache_sizes.tx_index, false, fReindex, sp_start_height); node.indexes.emplace_back(g_silent_payment_index.get()); }