diff options
author | fanquake <fanquake@gmail.com> | 2022-04-29 12:33:39 +0100 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2022-04-29 12:36:34 +0100 |
commit | 194b414697777b5ac9d9918004b851dbd4f8ce17 (patch) | |
tree | 72a5b407091fa6ce26f9f37ffd9cc16ee90557d3 | |
parent | 246db9889743b826d9d36c199eb97f6c9c6e071e (diff) | |
parent | e2b954e87f0c4cd5c8ac6e4d9c6b4d784844b4d2 (diff) |
Merge bitcoin/bitcoin#25016: refactor: GetFirstStoredBlock() and getblockchaininfo follow-ups
e2b954e87f0c4cd5c8ac6e4d9c6b4d784844b4d2 rpc: use GetBlockTime() for getblockchaininfo#time (Jon Atack)
86ce844d3b287012f27c7b0bad6d11c9bdd3120e blockstorage, refactor: pass GetFirstStoredBlock() start_block by reference (Jon Atack)
ed12c0a49d3c64d170aca9e66ef32a57d7933eeb blockstorage, refactor: make GetFirstStoredBlock() a member of BlockManager (Jon Atack)
Pull request description:
Picks up the remaining review feedback in #21726 and #24956.
- make the global function `GetFirstStoredBlock()` a member of the `BlockManager` class
- pass the `start_block` param of `GetFirstStoredBlock()` by reference instead of a pointer
- use `GetBlockTime()` for RPC getblockchaininfo#time
ACKs for top commit:
MarcoFalke:
ACK e2b954e87f0c4cd5c8ac6e4d9c6b4d784844b4d2
Tree-SHA512: 546e3c2e18245996b5b286829a605ae919eff3510963ec71b7c9ede521b1f501697e5b2f9d35d7a0606a74cbc8907201c58acf1e2cf7daaa86eefe2e3a8e296b
-rw-r--r-- | src/index/base.cpp | 2 | ||||
-rw-r--r-- | src/node/blockstorage.cpp | 6 | ||||
-rw-r--r-- | src/node/blockstorage.h | 6 | ||||
-rw-r--r-- | src/rpc/blockchain.cpp | 11 |
4 files changed, 13 insertions, 12 deletions
diff --git a/src/index/base.cpp b/src/index/base.cpp index 488a214ccf..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 = 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..17ab226a30 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -390,10 +390,10 @@ 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; + 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 622eac7fef..c0fd70e524 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..7bce31c519 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,8 +787,8 @@ static RPCHelpMan pruneblockchain() } PruneBlockFilesManual(active_chainstate, height); - const CBlockIndex* block = CHECK_NONFATAL(active_chain.Tip()); - const CBlockIndex* last_block = node::GetFirstStoredBlock(block); + const CBlockIndex& block{*CHECK_NONFATAL(active_chain.Tip())}; + const CBlockIndex* last_block{active_chainstate.m_blockman.GetFirstStoredBlock(block)}; return static_cast<uint64_t>(last_block->nHeight); }, @@ -1207,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()); @@ -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", 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}; |