Skip to content

Commit 194b414

Browse files
committed
Merge bitcoin/bitcoin#25016: refactor: GetFirstStoredBlock() and getblockchaininfo follow-ups
e2b954e rpc: use GetBlockTime() for getblockchaininfo#time (Jon Atack) 86ce844 blockstorage, refactor: pass GetFirstStoredBlock() start_block by reference (Jon Atack) ed12c0a 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 e2b954e Tree-SHA512: 546e3c2e18245996b5b286829a605ae919eff3510963ec71b7c9ede521b1f501697e5b2f9d35d7a0606a74cbc8907201c58acf1e2cf7daaa86eefe2e3a8e296b
2 parents 246db98 + e2b954e commit 194b414

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

src/index/base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ bool BaseIndex::Init()
7575
if (!m_best_block_index) {
7676
// index is not built yet
7777
// make sure we have all block data back to the genesis
78-
prune_violation = node::GetFirstStoredBlock(active_chain.Tip()) != active_chain.Genesis();
78+
prune_violation = m_chainstate->m_blockman.GetFirstStoredBlock(*active_chain.Tip()) != active_chain.Genesis();
7979
}
8080
// in case the index has a best block set and is not fully synced
8181
// check if we have the required blocks to continue building the index

src/node/blockstorage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,10 @@ bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex)
390390
return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
391391
}
392392

393-
const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) {
393+
const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex& start_block)
394+
{
394395
AssertLockHeld(::cs_main);
395-
assert(start_block);
396-
const CBlockIndex* last_block = start_block;
396+
const CBlockIndex* last_block = &start_block;
397397
while (last_block->pprev && (last_block->pprev->nStatus & BLOCK_HAVE_DATA)) {
398398
last_block = last_block->pprev;
399399
}

src/node/blockstorage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ class BlockManager
178178
//! Returns last CBlockIndex* that is a checkpoint
179179
const CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
180180

181+
//! Find the first block that is not pruned
182+
const CBlockIndex* GetFirstStoredBlock(const CBlockIndex& start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
183+
181184
/** True if any block files have ever been pruned. */
182185
bool m_have_pruned = false;
183186

@@ -188,9 +191,6 @@ class BlockManager
188191
void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
189192
};
190193

191-
//! Find the first block that is not pruned
192-
const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
193-
194194
void CleanupBlockRevFiles();
195195

196196
/** Open a block file (blk?????.dat) */

src/rpc/blockchain.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,9 @@ static RPCHelpMan pruneblockchain()
760760
CChain& active_chain = active_chainstate.m_chain;
761761

762762
int heightParam = request.params[0].get_int();
763-
if (heightParam < 0)
763+
if (heightParam < 0) {
764764
throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative block height.");
765+
}
765766

766767
// Height value more than a billion is too high to be a block height, and
767768
// too low to be a block time (corresponds to timestamp from Sep 2001).
@@ -786,8 +787,8 @@ static RPCHelpMan pruneblockchain()
786787
}
787788

788789
PruneBlockFilesManual(active_chainstate, height);
789-
const CBlockIndex* block = CHECK_NONFATAL(active_chain.Tip());
790-
const CBlockIndex* last_block = node::GetFirstStoredBlock(block);
790+
const CBlockIndex& block{*CHECK_NONFATAL(active_chain.Tip())};
791+
const CBlockIndex* last_block{active_chainstate.m_blockman.GetFirstStoredBlock(block)};
791792

792793
return static_cast<uint64_t>(last_block->nHeight);
793794
},
@@ -1207,15 +1208,15 @@ RPCHelpMan getblockchaininfo()
12071208
obj.pushKV("headers", chainman.m_best_header ? chainman.m_best_header->nHeight : -1);
12081209
obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex());
12091210
obj.pushKV("difficulty", GetDifficulty(&tip));
1210-
obj.pushKV("time", int64_t{tip.nTime});
1211+
obj.pushKV("time", tip.GetBlockTime());
12111212
obj.pushKV("mediantime", tip.GetMedianTimePast());
12121213
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), &tip));
12131214
obj.pushKV("initialblockdownload", active_chainstate.IsInitialBlockDownload());
12141215
obj.pushKV("chainwork", tip.nChainWork.GetHex());
12151216
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
12161217
obj.pushKV("pruned", node::fPruneMode);
12171218
if (node::fPruneMode) {
1218-
obj.pushKV("pruneheight", node::GetFirstStoredBlock(&tip)->nHeight);
1219+
obj.pushKV("pruneheight", chainman.m_blockman.GetFirstStoredBlock(tip)->nHeight);
12191220

12201221
// if 0, execution bypasses the whole if block.
12211222
bool automatic_pruning{args.GetIntArg("-prune", 0) != 1};

0 commit comments

Comments
 (0)