mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
[validation] Cache merkle root and witness commitment checks
Slight performance improvement by avoiding duplicate work.
This commit is contained in:
parent
5bf4f5ba32
commit
1ec6bbeb8d
2 changed files with 12 additions and 2 deletions
|
@ -71,8 +71,10 @@ public:
|
|||
// network and disk
|
||||
std::vector<CTransactionRef> vtx;
|
||||
|
||||
// memory only
|
||||
mutable bool fChecked;
|
||||
// Memory-only flags for caching expensive checks
|
||||
mutable bool fChecked; // CheckBlock()
|
||||
mutable bool m_checked_witness_commitment{false}; // CheckWitnessCommitment()
|
||||
mutable bool m_checked_merkle_root{false}; // CheckMerkleRoot()
|
||||
|
||||
CBlock()
|
||||
{
|
||||
|
@ -95,6 +97,8 @@ public:
|
|||
CBlockHeader::SetNull();
|
||||
vtx.clear();
|
||||
fChecked = false;
|
||||
m_checked_witness_commitment = false;
|
||||
m_checked_merkle_root = false;
|
||||
}
|
||||
|
||||
CBlockHeader GetBlockHeader() const
|
||||
|
|
|
@ -3641,6 +3641,8 @@ static bool CheckBlockHeader(const CBlockHeader& block, BlockValidationState& st
|
|||
|
||||
static bool CheckMerkleRoot(const CBlock& block, BlockValidationState& state)
|
||||
{
|
||||
if (block.m_checked_merkle_root) return true;
|
||||
|
||||
bool mutated;
|
||||
uint256 merkle_root = BlockMerkleRoot(block, &mutated);
|
||||
if (block.hashMerkleRoot != merkle_root) {
|
||||
|
@ -3660,6 +3662,7 @@ static bool CheckMerkleRoot(const CBlock& block, BlockValidationState& state)
|
|||
/*debug_message=*/"duplicate transaction");
|
||||
}
|
||||
|
||||
block.m_checked_merkle_root = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -3672,6 +3675,8 @@ static bool CheckMerkleRoot(const CBlock& block, BlockValidationState& state)
|
|||
static bool CheckWitnessMalleation(const CBlock& block, bool expect_witness_commitment, BlockValidationState& state)
|
||||
{
|
||||
if (expect_witness_commitment) {
|
||||
if (block.m_checked_witness_commitment) return true;
|
||||
|
||||
int commitpos = GetWitnessCommitmentIndex(block);
|
||||
if (commitpos != NO_WITNESS_COMMITMENT) {
|
||||
assert(!block.vtx.empty() && !block.vtx[0]->vin.empty());
|
||||
|
@ -3697,6 +3702,7 @@ static bool CheckWitnessMalleation(const CBlock& block, bool expect_witness_comm
|
|||
/*debug_message=*/strprintf("%s : witness merkle commitment mismatch", __func__));
|
||||
}
|
||||
|
||||
block.m_checked_witness_commitment = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue