From 66abce1d98115e41f394bc4f4f52341960ddc839 Mon Sep 17 00:00:00 2001 From: dergoegge Date: Fri, 19 Jan 2024 15:09:28 +0000 Subject: [PATCH] [validation] Introduce IsBlockMutated --- src/validation.cpp | 20 ++++++++++++++++++++ src/validation.h | 3 +++ 2 files changed, 23 insertions(+) diff --git a/src/validation.cpp b/src/validation.cpp index e02ce2ad1d..e1133138df 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3824,6 +3824,26 @@ bool HasValidProofOfWork(const std::vector& headers, const Consens [&](const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);}); } +bool IsBlockMutated(const CBlock& block, bool check_witness_root) +{ + BlockValidationState state; + if (!CheckMerkleRoot(block, state)) { + LogDebug(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString()); + return true; + } + + if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) { + return false; + } + + if (!CheckWitnessMalleation(block, check_witness_root, state)) { + LogDebug(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString()); + return true; + } + + return false; +} + arith_uint256 CalculateHeadersWork(const std::vector& headers) { arith_uint256 total_work{0}; diff --git a/src/validation.h b/src/validation.h index fd9b53df8f..566153c150 100644 --- a/src/validation.h +++ b/src/validation.h @@ -383,6 +383,9 @@ bool TestBlockValidity(BlockValidationState& state, /** Check with the proof of work on each blockheader matches the value in nBits */ bool HasValidProofOfWork(const std::vector& headers, const Consensus::Params& consensusParams); +/** Check if a block has been mutated (with respect to its merkle root and witness commitments). */ +bool IsBlockMutated(const CBlock& block, bool check_witness_root); + /** Return the sum of the work on a given set of headers */ arith_uint256 CalculateHeadersWork(const std::vector& headers);