Skip to content

Commit f544e23

Browse files
committed
Merge #14863: refactor: Add and use HaveTxsDownloaded() where appropriate
fa4fc88 validation: Add and use HaveTxsDownloaded where appropriate (MarcoFalke) Pull request description: `nChainTx` is an implementation detail that shouldn't be exposed without a wrapper that comes with appropriate documentation. Tree-SHA512: 56ab7378c2ce97794498724c271f861de982de69099e90ec09632a26230ae6fded3c59668adb378bd64dcb8ef714769b970210977b88a53fc7550774ddba3d59
2 parents f845625 + fa4fc88 commit f544e23

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

src/chain.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ class CBlockIndex
294294
return *phashBlock;
295295
}
296296

297+
/**
298+
* Check whether this block's and all previous blocks' transactions have been
299+
* downloaded (and stored to disk) at some point.
300+
*
301+
* Does not imply the transactions are consensus-valid (ConnectTip might fail)
302+
* Does not imply the transactions are still stored on disk. (IsBlockPruned might return true)
303+
*/
304+
bool HaveTxsDownloaded() const { return nChainTx != 0; }
305+
297306
int64_t GetBlockTime() const
298307
{
299308
return (int64_t)nTime;

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ static void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vec
566566
return;
567567
}
568568
if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
569-
if (pindex->nChainTx)
569+
if (pindex->HaveTxsDownloaded())
570570
state->pindexLastCommonBlock = pindex;
571571
} else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
572572
// The block is not already downloaded, and not yet in flight.
@@ -1124,7 +1124,7 @@ void static ProcessGetBlockData(CNode* pfrom, const CChainParams& chainparams, c
11241124
LOCK(cs_main);
11251125
const CBlockIndex* pindex = LookupBlockIndex(inv.hash);
11261126
if (pindex) {
1127-
if (pindex->nChainTx && !pindex->IsValid(BLOCK_VALID_SCRIPTS) &&
1127+
if (pindex->HaveTxsDownloaded() && !pindex->IsValid(BLOCK_VALID_SCRIPTS) &&
11281128
pindex->IsValid(BLOCK_VALID_TREE)) {
11291129
// If we have the block and all of its parents, but have not yet validated it,
11301130
// we might be in the middle of connecting it (ie in the unlock of cs_main

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ static UniValue getchaintips(const JSONRPCRequest& request)
14241424
} else if (block->nStatus & BLOCK_FAILED_MASK) {
14251425
// This block or one of its ancestors is invalid.
14261426
status = "invalid";
1427-
} else if (block->nChainTx == 0) {
1427+
} else if (!block->HaveTxsDownloaded()) {
14281428
// This block cannot be connected because full block data for it or one of its parents is missing.
14291429
status = "headers-only";
14301430
} else if (block->IsValid(BLOCK_VALID_SCRIPTS)) {

src/validation.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,7 +2487,7 @@ CBlockIndex* CChainState::FindMostWorkChain() {
24872487
CBlockIndex *pindexTest = pindexNew;
24882488
bool fInvalidAncestor = false;
24892489
while (pindexTest && !chainActive.Contains(pindexTest)) {
2490-
assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
2490+
assert(pindexTest->HaveTxsDownloaded() || pindexTest->nHeight == 0);
24912491

24922492
// Pruned nodes may have entries in setBlockIndexCandidates for
24932493
// which block files have been deleted. Remove those as candidates
@@ -2777,7 +2777,7 @@ bool CChainState::PreciousBlock(CValidationState& state, const CChainParams& par
27772777
// call preciousblock 2**31-1 times on the same set of tips...
27782778
nBlockReverseSequenceId--;
27792779
}
2780-
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->nChainTx) {
2780+
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->HaveTxsDownloaded()) {
27812781
setBlockIndexCandidates.insert(pindex);
27822782
PruneBlockIndexCandidates();
27832783
}
@@ -2838,7 +2838,7 @@ bool CChainState::InvalidateBlock(CValidationState& state, const CChainParams& c
28382838
// add it again.
28392839
BlockMap::iterator it = mapBlockIndex.begin();
28402840
while (it != mapBlockIndex.end()) {
2841-
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
2841+
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->HaveTxsDownloaded() && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
28422842
setBlockIndexCandidates.insert(it->second);
28432843
}
28442844
it++;
@@ -2868,7 +2868,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
28682868
if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
28692869
it->second->nStatus &= ~BLOCK_FAILED_MASK;
28702870
setDirtyBlockIndex.insert(it->second);
2871-
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
2871+
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
28722872
setBlockIndexCandidates.insert(it->second);
28732873
}
28742874
if (it->second == pindexBestInvalid) {
@@ -2946,7 +2946,7 @@ void CChainState::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pi
29462946
pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
29472947
setDirtyBlockIndex.insert(pindexNew);
29482948

2949-
if (pindexNew->pprev == nullptr || pindexNew->pprev->nChainTx) {
2949+
if (pindexNew->pprev == nullptr || pindexNew->pprev->HaveTxsDownloaded()) {
29502950
// If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
29512951
std::deque<CBlockIndex*> queue;
29522952
queue.push_back(pindexNew);
@@ -3839,7 +3839,7 @@ bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlo
38393839
// Pruned nodes may have deleted the block.
38403840
if (pindex->nTx > 0) {
38413841
if (pindex->pprev) {
3842-
if (pindex->pprev->nChainTx) {
3842+
if (pindex->pprev->HaveTxsDownloaded()) {
38433843
pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
38443844
} else {
38453845
pindex->nChainTx = 0;
@@ -3853,7 +3853,7 @@ bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlo
38533853
pindex->nStatus |= BLOCK_FAILED_CHILD;
38543854
setDirtyBlockIndex.insert(pindex);
38553855
}
3856-
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == nullptr))
3856+
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->HaveTxsDownloaded() || pindex->pprev == nullptr))
38573857
setBlockIndexCandidates.insert(pindex);
38583858
if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
38593859
pindexBestInvalid = pindex;
@@ -4222,7 +4222,7 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
42224222
++ret.first;
42234223
}
42244224
}
4225-
} else if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && pindexIter->nChainTx) {
4225+
} else if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && pindexIter->HaveTxsDownloaded()) {
42264226
setBlockIndexCandidates.insert(pindexIter);
42274227
}
42284228
}
@@ -4523,7 +4523,7 @@ void CChainState::CheckBlockIndex(const Consensus::Params& consensusParams)
45234523
assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
45244524
assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
45254525
}
4526-
if (pindex->nChainTx == 0) assert(pindex->nSequenceId <= 0); // nSequenceId can't be set positive for blocks that aren't linked (negative is used for preciousblock)
4526+
if (!pindex->HaveTxsDownloaded()) assert(pindex->nSequenceId <= 0); // nSequenceId can't be set positive for blocks that aren't linked (negative is used for preciousblock)
45274527
// VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
45284528
// HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
45294529
if (!fHavePruned) {
@@ -4536,9 +4536,9 @@ void CChainState::CheckBlockIndex(const Consensus::Params& consensusParams)
45364536
}
45374537
if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
45384538
assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
4539-
// All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
4540-
assert((pindexFirstNeverProcessed != nullptr) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
4541-
assert((pindexFirstNotTransactionsValid != nullptr) == (pindex->nChainTx == 0));
4539+
// All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to HaveTxsDownloaded().
4540+
assert((pindexFirstNeverProcessed == nullptr) == pindex->HaveTxsDownloaded());
4541+
assert((pindexFirstNotTransactionsValid == nullptr) == pindex->HaveTxsDownloaded());
45424542
assert(pindex->nHeight == nHeight); // nHeight must be consistent.
45434543
assert(pindex->pprev == nullptr || pindex->nChainWork >= pindex->pprev->nChainWork); // For every block except the genesis block, the chainwork must be larger than the parent's.
45444544
assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.

0 commit comments

Comments
 (0)