From ed12c0a49d3c64d170aca9e66ef32a57d7933eeb Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 27 Apr 2022 12:21:22 +0200 Subject: blockstorage, refactor: make GetFirstStoredBlock() a member of BlockManager instead of a global --- src/index/base.cpp | 2 +- src/node/blockstorage.cpp | 3 ++- src/node/blockstorage.h | 6 +++--- src/rpc/blockchain.cpp | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/index/base.cpp b/src/index/base.cpp index 488a214ccf..db4070b911 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -75,7 +75,7 @@ bool BaseIndex::Init() if (!m_best_block_index) { // index is not built yet // make sure we have all block data back to the genesis - prune_violation = node::GetFirstStoredBlock(active_chain.Tip()) != active_chain.Genesis(); + prune_violation = m_chainstate->m_blockman.GetFirstStoredBlock(active_chain.Tip()) != active_chain.Genesis(); } // in case the index has a best block set and is not fully synced // check if we have the required blocks to continue building the index diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 25771aacee..be45eb7ba9 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -390,7 +390,8 @@ bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex) return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0); } -const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) { +const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex* start_block) +{ AssertLockHeld(::cs_main); assert(start_block); const CBlockIndex* last_block = start_block; diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index 622eac7fef..c5317e5a95 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -178,6 +178,9 @@ public: //! Returns last CBlockIndex* that is a checkpoint const CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main); + //! Find the first block that is not pruned + const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + /** True if any block files have ever been pruned. */ bool m_have_pruned = false; @@ -188,9 +191,6 @@ public: void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); }; -//! Find the first block that is not pruned -const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); - void CleanupBlockRevFiles(); /** Open a block file (blk?????.dat) */ diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 96e9efb288..6347036b9a 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -787,7 +787,7 @@ static RPCHelpMan pruneblockchain() PruneBlockFilesManual(active_chainstate, height); const CBlockIndex* block = CHECK_NONFATAL(active_chain.Tip()); - const CBlockIndex* last_block = node::GetFirstStoredBlock(block); + const CBlockIndex* last_block{active_chainstate.m_blockman.GetFirstStoredBlock(block)}; return static_cast(last_block->nHeight); }, @@ -1215,7 +1215,7 @@ RPCHelpMan getblockchaininfo() obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage()); obj.pushKV("pruned", node::fPruneMode); if (node::fPruneMode) { - obj.pushKV("pruneheight", node::GetFirstStoredBlock(&tip)->nHeight); + obj.pushKV("pruneheight", chainman.m_blockman.GetFirstStoredBlock(&tip)->nHeight); // if 0, execution bypasses the whole if block. bool automatic_pruning{args.GetIntArg("-prune", 0) != 1}; -- cgit v1.2.3 From 86ce844d3b287012f27c7b0bad6d11c9bdd3120e Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 28 Apr 2022 11:15:38 +0200 Subject: blockstorage, refactor: pass GetFirstStoredBlock() start_block by reference instead of by pointer, so as to not accept a nullptr. --- src/index/base.cpp | 2 +- src/node/blockstorage.cpp | 5 ++--- src/node/blockstorage.h | 2 +- src/rpc/blockchain.cpp | 7 ++++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/index/base.cpp b/src/index/base.cpp index db4070b911..f3c9395928 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -75,7 +75,7 @@ bool BaseIndex::Init() if (!m_best_block_index) { // index is not built yet // make sure we have all block data back to the genesis - prune_violation = m_chainstate->m_blockman.GetFirstStoredBlock(active_chain.Tip()) != active_chain.Genesis(); + prune_violation = m_chainstate->m_blockman.GetFirstStoredBlock(*active_chain.Tip()) != active_chain.Genesis(); } // in case the index has a best block set and is not fully synced // check if we have the required blocks to continue building the index diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index be45eb7ba9..17ab226a30 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -390,11 +390,10 @@ bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex) return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0); } -const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex* start_block) +const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex& start_block) { AssertLockHeld(::cs_main); - assert(start_block); - const CBlockIndex* last_block = start_block; + const CBlockIndex* last_block = &start_block; while (last_block->pprev && (last_block->pprev->nStatus & BLOCK_HAVE_DATA)) { last_block = last_block->pprev; } diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index c5317e5a95..c0fd70e524 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -179,7 +179,7 @@ public: const CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main); //! Find the first block that is not pruned - const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + const CBlockIndex* GetFirstStoredBlock(const CBlockIndex& start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); /** True if any block files have ever been pruned. */ bool m_have_pruned = false; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 6347036b9a..8265e6d5ba 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -760,8 +760,9 @@ static RPCHelpMan pruneblockchain() CChain& active_chain = active_chainstate.m_chain; int heightParam = request.params[0].get_int(); - if (heightParam < 0) + if (heightParam < 0) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative block height."); + } // Height value more than a billion is too high to be a block height, and // too low to be a block time (corresponds to timestamp from Sep 2001). @@ -786,7 +787,7 @@ static RPCHelpMan pruneblockchain() } PruneBlockFilesManual(active_chainstate, height); - const CBlockIndex* block = CHECK_NONFATAL(active_chain.Tip()); + const CBlockIndex& block{*CHECK_NONFATAL(active_chain.Tip())}; const CBlockIndex* last_block{active_chainstate.m_blockman.GetFirstStoredBlock(block)}; return static_cast(last_block->nHeight); @@ -1215,7 +1216,7 @@ RPCHelpMan getblockchaininfo() obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage()); obj.pushKV("pruned", node::fPruneMode); if (node::fPruneMode) { - obj.pushKV("pruneheight", chainman.m_blockman.GetFirstStoredBlock(&tip)->nHeight); + obj.pushKV("pruneheight", chainman.m_blockman.GetFirstStoredBlock(tip)->nHeight); // if 0, execution bypasses the whole if block. bool automatic_pruning{args.GetIntArg("-prune", 0) != 1}; -- cgit v1.2.3 From e2b954e87f0c4cd5c8ac6e4d9c6b4d784844b4d2 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 28 Apr 2022 20:44:13 +0200 Subject: rpc: use GetBlockTime() for getblockchaininfo#time --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 8265e6d5ba..7bce31c519 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1208,7 +1208,7 @@ RPCHelpMan getblockchaininfo() obj.pushKV("headers", chainman.m_best_header ? chainman.m_best_header->nHeight : -1); obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex()); obj.pushKV("difficulty", GetDifficulty(&tip)); - obj.pushKV("time", int64_t{tip.nTime}); + obj.pushKV("time", tip.GetBlockTime()); obj.pushKV("mediantime", tip.GetMedianTimePast()); obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), &tip)); obj.pushKV("initialblockdownload", active_chainstate.IsInitialBlockDownload()); -- cgit v1.2.3