Skip to content

Commit 4c29326

Browse files
mzumsandestickies-v
andcommitted
validation: cache all headers with enough PoW in invalidateblock
We now include blocks without HaveNumChainTxs() / lower validation status than BLOCK_VALID_TRANSACTIONS. These checks are still performed at the spot where we use the cache to insert into setBlockIndexCandidates. This is in preparation for using the cache for more things than just setBlockIndexCandidates candidates in the following commits. Co-authored-by: stickies-v <[email protected]>
1 parent 15fa5b5 commit 4c29326

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/validation.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3690,7 +3690,7 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* pinde
36903690
// To avoid walking the block index repeatedly in search of candidates,
36913691
// build a map once so that we can look up candidate blocks by chain
36923692
// work as we go.
3693-
std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3693+
std::multimap<const arith_uint256, CBlockIndex*> highpow_outofchain_headers;
36943694

36953695
{
36963696
LOCK(cs_main);
@@ -3699,13 +3699,12 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* pinde
36993699
// We don't need to put anything in our active chain into the
37003700
// multimap, because those candidates will be found and considered
37013701
// as we disconnect.
3702-
// Instead, consider only non-active-chain blocks that have at
3703-
// least as much work as where we expect the new tip to end up.
3702+
// Instead, consider only non-active-chain blocks that score
3703+
// at least as good with CBlockIndexWorkComparator as the new tip.
37043704
if (!m_chain.Contains(candidate) &&
3705-
!CBlockIndexWorkComparator()(candidate, pindex->pprev) &&
3706-
candidate->IsValid(BLOCK_VALID_TRANSACTIONS) &&
3707-
candidate->HaveNumChainTxs()) {
3708-
candidate_blocks_by_work.insert(std::make_pair(candidate->nChainWork, candidate));
3705+
!CBlockIndexWorkComparator()(candidate, pindex->pprev) &&
3706+
!(candidate->nStatus & BLOCK_FAILED_MASK)) {
3707+
highpow_outofchain_headers.insert({candidate->nChainWork, candidate});
37093708
}
37103709
}
37113710
}
@@ -3755,11 +3754,14 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* pinde
37553754
}
37563755

37573756
// Add any equal or more work headers to setBlockIndexCandidates
3758-
auto candidate_it = candidate_blocks_by_work.lower_bound(invalid_walk_tip->pprev->nChainWork);
3759-
while (candidate_it != candidate_blocks_by_work.end()) {
3760-
if (!CBlockIndexWorkComparator()(candidate_it->second, invalid_walk_tip->pprev)) {
3761-
setBlockIndexCandidates.insert(candidate_it->second);
3762-
candidate_it = candidate_blocks_by_work.erase(candidate_it);
3757+
auto candidate_it = highpow_outofchain_headers.lower_bound(invalid_walk_tip->pprev->nChainWork);
3758+
while (candidate_it != highpow_outofchain_headers.end()) {
3759+
CBlockIndex* candidate{candidate_it->second};
3760+
if (!CBlockIndexWorkComparator()(candidate, invalid_walk_tip->pprev) &&
3761+
candidate->IsValid(BLOCK_VALID_TRANSACTIONS) &&
3762+
candidate->HaveNumChainTxs()) {
3763+
setBlockIndexCandidates.insert(candidate);
3764+
candidate_it = highpow_outofchain_headers.erase(candidate_it);
37633765
} else {
37643766
++candidate_it;
37653767
}

0 commit comments

Comments
 (0)