Skip to content

Commit 66abce1

Browse files
committed
[validation] Introduce IsBlockMutated
1 parent e7669e1 commit 66abce1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/validation.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,6 +3824,26 @@ bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consens
38243824
[&](const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);});
38253825
}
38263826

3827+
bool IsBlockMutated(const CBlock& block, bool check_witness_root)
3828+
{
3829+
BlockValidationState state;
3830+
if (!CheckMerkleRoot(block, state)) {
3831+
LogDebug(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString());
3832+
return true;
3833+
}
3834+
3835+
if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) {
3836+
return false;
3837+
}
3838+
3839+
if (!CheckWitnessMalleation(block, check_witness_root, state)) {
3840+
LogDebug(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString());
3841+
return true;
3842+
}
3843+
3844+
return false;
3845+
}
3846+
38273847
arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader>& headers)
38283848
{
38293849
arith_uint256 total_work{0};

src/validation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ bool TestBlockValidity(BlockValidationState& state,
383383
/** Check with the proof of work on each blockheader matches the value in nBits */
384384
bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams);
385385

386+
/** Check if a block has been mutated (with respect to its merkle root and witness commitments). */
387+
bool IsBlockMutated(const CBlock& block, bool check_witness_root);
388+
386389
/** Return the sum of the work on a given set of headers */
387390
arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader>& headers);
388391

0 commit comments

Comments
 (0)