backport: partial bitcoin#23819 ConnectBlock: don't serialize block hash twice (#5042)

* block_connected: don't serialize block hash twice

In the validation:block_connected tracepoint, we call block->GetHash(),
which ends up calling CBlockHeader::GetHash(), executing around 8000
serialization instructions. We don't need to do this extra work, because
block->GetHash() is already called further up in the function. Let's
save that value as a local variable and re-use it in our tracepoint so
there is no unnecessary tracepoint overhead.

Signed-off-by: William Casarin <jb55@jb55.com>

* ConnectBlock: re-use hash on budget start

Signed-off-by: William Casarin <jb55@jb55.com>
Co-authored-by: William Casarin <jb55@jb55.com>
This commit is contained in:
JSKitty 2022-10-16 18:01:42 +00:00 committed by GitHub
parent 95194d28e9
commit 70717c901d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1978,7 +1978,10 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
AssertLockHeld(cs_main);
assert(pindex);
assert(*pindex->phashBlock == block.GetHash());
uint256 block_hash{block.GetHash()};
assert(*pindex->phashBlock == block_hash);
assert(m_clhandler);
assert(m_isman);
assert(m_quorum_block_processor);
@ -2026,7 +2029,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
// Special case for the genesis block, skipping connection of its transactions
// (its coinbase is unspendable)
if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
if (block_hash == chainparams.GetConsensus().hashGenesisBlock) {
if (!fJustCheck)
view.SetBestBlock(pindex->GetBlockHash());
return true;
@ -2102,7 +2105,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
// make sure old budget is the real one
if (pindex->nHeight == chainparams.GetConsensus().nSuperblockStartBlock &&
chainparams.GetConsensus().nSuperblockStartHash != uint256() &&
block.GetHash() != chainparams.GetConsensus().nSuperblockStartHash) {
block_hash != chainparams.GetConsensus().nSuperblockStartHash) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, error("ConnectBlock(): invalid superblock start"), REJECT_INVALID, "bad-sb-start");
}
/// END DASH