Skip to content

Commit

Permalink
Merge bitcoin#25016: refactor: GetFirstStoredBlock() and getblockchai…
Browse files Browse the repository at this point in the history
…ninfo 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 bitcoin#21726 and bitcoin#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
  • Loading branch information
fanquake committed Apr 29, 2022
2 parents 246db98 + e2b954e commit 194b414
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/node/blockstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ class BlockManager
//! 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;

Expand All @@ -188,9 +191,6 @@ class BlockManager
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) */
Expand Down
11 changes: 6 additions & 5 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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);
},
Expand Down Expand Up @@ -1207,15 +1208,15 @@ 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());
obj.pushKV("chainwork", tip.nChainWork.GetHex());
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};
Expand Down

0 comments on commit 194b414

Please sign in to comment.