diff options
author | dergoegge <n.goeggi@gmail.com> | 2024-01-19 15:09:28 +0000 |
---|---|---|
committer | glozow <gloriajzhao@gmail.com> | 2024-03-05 10:21:16 +0000 |
commit | aff368fa817b065d99729186d304fff02f6e527b (patch) | |
tree | 3abfb0b320e9145ad6c3324a09fd854d472d252a | |
parent | 076c67c3aae424e58863dde3bc37cedecc496935 (diff) |
[validation] Introduce IsBlockMutated
Github-Pull: #29412
Rebased-From: 66abce1d98115e41f394bc4f4f52341960ddc839
-rw-r--r-- | src/validation.cpp | 20 | ||||
-rw-r--r-- | src/validation.h | 3 |
2 files changed, 23 insertions, 0 deletions
diff --git a/src/validation.cpp b/src/validation.cpp index 97f71db8b1..1ad4ebcdaa 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3806,6 +3806,26 @@ bool HasValidProofOfWork(const std::vector<CBlockHeader>& 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)) { + LogPrint(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)) { + LogPrint(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString()); + return true; + } + + return false; +} + arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader>& headers) { arith_uint256 total_work{0}; diff --git a/src/validation.h b/src/validation.h index 7ce60da634..e9f21528c6 100644 --- a/src/validation.h +++ b/src/validation.h @@ -360,6 +360,9 @@ bool TestBlockValidity(BlockValidationState& state, /** Check with the proof of work on each blockheader matches the value in nBits */ bool HasValidProofOfWork(const std::vector<CBlockHeader>& 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<CBlockHeader>& headers); |