Skip to content

Commit

Permalink
Avoid calling GetSerializeSize on each tx in a block if !fTxIndex
Browse files Browse the repository at this point in the history
Cherry-picked from: 22fddde
  • Loading branch information
TheBlueMatt authored and xanimo committed Jul 23, 2024
1 parent a92bd41 commit d32274e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
void CChainState::InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
if (!state.CorruptionPossible()) {
pindex->nStatus |= BLOCK_FAILED_VALID;
gFailedBlocks.insert(pindex);
g_failed_blocks.insert(pindex);
setDirtyBlockIndex.insert(pindex);
setBlockIndexCandidates.erase(pindex);
InvalidChainFound(pindex);
Expand Down Expand Up @@ -1566,6 +1566,8 @@ static bool WriteUndoDataForBlock(const CBlockUndo& blockundo, CValidationState&

static bool WriteTxIndexDataForBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex)
{
if (!fTxIndex) return true;

CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
std::vector<std::pair<uint256, CDiskTxPos> > vPos;
vPos.reserve(block.vtx.size());
Expand All @@ -1575,9 +1577,9 @@ static bool WriteTxIndexDataForBlock(const CBlock& block, CValidationState& stat
pos.nTxOffset += ::GetSerializeSize(*tx, SER_DISK, CLIENT_VERSION);
}

if (fTxIndex)
if (!pblocktree->WriteTxIndex(vPos))
return AbortNode(state, "Failed to write transaction index");
if (!pblocktree->WriteTxIndex(vPos)) {
return AbortNode(state, "Failed to write transaction index");
}

return true;
}
Expand Down Expand Up @@ -2631,7 +2633,7 @@ bool CChainState::InvalidateBlock(CValidationState& state, const CChainParams& c
while (it != mapBlockIndex.end()) {
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
setBlockIndexCandidates.insert(it->second);
gFailedBlocks.erase(it->second);
g_failed_blocks.erase(it->second);
}
it++;
}
Expand Down Expand Up @@ -3167,7 +3169,7 @@ bool CChainState::AcceptBlockHeader(const CBlockHeader& block, CValidationState&
return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));

if (!pindexPrev->IsValid(BLOCK_VALID_SCRIPTS)) {
for (const CBlockIndex* failedit : gFailedBlocks) {
for (const CBlockIndex* failedit : g_failed_blocks) {
if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) {
assert(failedit->nStatus & BLOCK_FAILED_VALID);
CBlockIndex* invalidWalk = pindexPrev;
Expand Down

0 comments on commit d32274e

Please sign in to comment.