From 55fc577c9a33e471f45a2a11c2f188ca193b1b38 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Wed, 7 Jul 2021 00:02:16 +0000 Subject: [PATCH] Add internal interfaces for prune locks Including desc member of PruneLockInfo for a human-readable description --- src/interfaces/chain.h | 5 +++++ src/node/blockstorage.cpp | 10 ++++++++++ src/node/blockstorage.h | 3 +++ src/node/interfaces.cpp | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index be596b1765..33f6d6e45f 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -34,6 +34,7 @@ struct CBlockLocator; struct FeeCalculation; namespace node { struct NodeContext; +struct PruneLockInfo; } // namespace node namespace interfaces { @@ -148,6 +149,10 @@ public: //! pruned), and contains transactions. virtual bool haveBlockOnDisk(int height) = 0; + virtual bool pruneLockExists(const std::string& name) const = 0; + virtual void updatePruneLock(const std::string& name, const node::PruneLockInfo& lock_info) = 0; + virtual void deletePruneLock(const std::string& name) = 0; + //! Get locator for the current chain tip. virtual CBlockLocator getTipLocator() = 0; diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 810e8469be..1d46314ec0 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -403,11 +403,21 @@ void BlockManager::FindFilesToPrune( min_block_to_prune, last_block_can_prune, count); } +bool BlockManager::PruneLockExists(const std::string& name) const { + return m_prune_locks.count(name); +} + void BlockManager::UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) { AssertLockHeld(::cs_main); m_prune_locks[name] = lock_info; } +void BlockManager::DeletePruneLock(const std::string& name) +{ + AssertLockHeld(::cs_main); + m_prune_locks.erase(name); +} + CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash) { AssertLockHeld(cs_main); diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index eacc9c6387..068aa7d2af 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -92,6 +92,7 @@ struct CBlockIndexHeightOnlyComparator { }; struct PruneLockInfo { + std::string desc; //! Arbitrary human-readable description of the lock purpose int height_first{std::numeric_limits::max()}; //! Height of earliest block that should be kept and not pruned int height_last{std::numeric_limits::max()}; //! Height of latest block that should be kept and not pruned }; @@ -409,8 +410,10 @@ public: //! Check whether the block associated with this index entry is pruned or not. bool IsBlockPruned(const CBlockIndex& block) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + bool PruneLockExists(const std::string& name) const SHARED_LOCKS_REQUIRED(::cs_main); //! Create or update a prune lock identified by its name void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + void DeletePruneLock(const std::string& name) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); /** Open a block file (blk?????.dat) */ AutoFile OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false) const; diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 54b986c926..072155882d 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -554,6 +554,24 @@ public: const CBlockIndex* block{chainman().ActiveChain()[height]}; return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0; } + bool pruneLockExists(const std::string& name) const override + { + LOCK(cs_main); + auto& blockman = m_node.chainman->m_blockman; + return blockman.PruneLockExists(name); + } + void updatePruneLock(const std::string& name, const node::PruneLockInfo& lock_info) override + { + LOCK(cs_main); + auto& blockman = m_node.chainman->m_blockman; + blockman.UpdatePruneLock(name, lock_info); + } + void deletePruneLock(const std::string& name) override + { + LOCK(cs_main); + auto& blockman = m_node.chainman->m_blockman; + blockman.DeletePruneLock(name); + } CBlockLocator getTipLocator() override { LOCK(::cs_main);