Test: Cover malformed merkle trees and nBits
What changed, and why it matters
This commit only adds new unit tests. It does not change any production code, so it cannot introduce a runtime vulnerability or fix one directly. The tests exercise existing validation behavior for malformed merkle-tree data and invalid proof-of-work target encodings, confirming that the current code rejects them.
No security action required; review as ordinary test-coverage improvement. If auditing, verify that the tested behaviors (ExtractMatches and GetBitsProof) already reject the malformed inputs in production as the tests assume.
Security signals we found
Tests exercise rejection of malformed partial merkle trees
Tests exercise rejection of zero/negative/overflowing nBits targets
No changes to consensus, networking, wallet, or cryptographic code
Evidence from the diff
The diff adds BOOST_AUTO_TEST_CASE test cases in src/test/pmt_tests.cpp and src/test/pow_tests.cpp. pmt_malformed constructs crafted CPartialMerkleTree serializations and asserts ExtractMatches returns a null hash for invalid shapes (tx count over 16666, empty tree, hash/bit mismatches, unconsumed hashes/bits). GetBitsProof_test asserts GetBitsProof returns zero for zero, negative, and overflowing nBits targets and returns a known value for the genesis target. No library or consensus logic is modified.
Changed components
src/test/pmt_tests.cppsrc/test/pow_tests.cppInspect captured patch +57 / −0
### src/test/pmt_tests.cpp
@@ -2,10 +2,12 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <consensus/consensus.h>
#include <consensus/merkle.h>
#include <merkleblock.h>
#include <serialize.h>
#include <streams.h>
+#include <test/util/common.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <uint256.h>
@@ -125,4 +127,42 @@ BOOST_AUTO_TEST_CASE(pmt_malleability)
BOOST_CHECK(tree.ExtractMatches(vTxid, vIndex).IsNull());
}
+BOOST_AUTO_TEST_CASE(pmt_malformed)
+{
+ // Deserialize a partial merkle tree with the given fields and extract its matches.
+ const auto extract{[](uint32_t tx_count, uint32_t hash_count, std::vector<unsigned char> bits) {
+ std::vector<uint256> hashes;
+ for (uint32_t i{1}; i <= hash_count; ++i) hashes.emplace_back(i);
+ DataStream ss;
+ ss << tx_count << hashes << bits;
+ CPartialMerkleTree tree;
+ ss >> tree;
+ std::vector<Txid> matches;
+ std::vector<unsigned int> indexes;
+ return tree.ExtractMatches(matches, indexes);
+ }};
+
+ // The transaction count is limited to MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT.
+ constexpr uint32_t max_tx_count{MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT};
+ static_assert(max_tx_count == 16666);
+ BOOST_CHECK_EQUAL(extract(max_tx_count, 1, {0x00}), uint256{1});
+ BOOST_CHECK(extract(max_tx_count + 1, 1, {0x00}).IsNull());
+ // An empty tree, more hashes than transactions, and fewer bits than hashes are
+ // rejected before traversal. Traversal would reject each of them anyway, so these
+ // check the result, not the early returns.
+ BOOST_CHECK(extract(0, 0, {0x00}).IsNull());
+ BOOST_CHECK(extract(1, 2, {0x00}).IsNull());
+ BOOST_CHECK(extract(9, 9, {0xff}).IsNull());
+ // Traversal runs out of hashes: the root is a parent of a match, so both of its
+ // leaves need a hash, but only one is provided.
+ BOOST_CHECK(extract(2, 1, {0x01}).IsNull());
+ // Traversal runs out of bits: eight bits reach the third leaf but not the fourth.
+ BOOST_CHECK(extract(16, 8, {0xff}).IsNull());
+ // Not all bits are consumed: a second byte of bits is more than padding.
+ BOOST_CHECK(extract(1, 1, {0x00, 0x00}).IsNull());
+ // Not all hashes are consumed: the root is not a parent of a match, so only its
+ // own hash is used.
+ BOOST_CHECK(extract(2, 2, {0x00}).IsNull());
+}
+
BOOST_AUTO_TEST_SUITE_END()
### src/test/pow_tests.cpp
@@ -135,6 +135,23 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
}
+BOOST_AUTO_TEST_CASE(GetBitsProof_test)
+{
+ // Targets that are zero, negative or overflow encode no work.
+ BOOST_CHECK_EQUAL(GetBitsProof(0x00000000), arith_uint256{0});
+ BOOST_CHECK_EQUAL(GetBitsProof(0x01003456), arith_uint256{0});
+ BOOST_CHECK_EQUAL(GetBitsProof(0x04923456), arith_uint256{0});
+ BOOST_CHECK_EQUAL(GetBitsProof(0xff123456), arith_uint256{0});
+ // The exponent byte is unsigned, so this is a large exponent that overflows.
+ BOOST_CHECK_EQUAL(GetBitsProof(0x80010203), arith_uint256{0});
+ // The two overflows above shift every mantissa bit out of 256 bits, so they would
+ // also give zero work without the overflow check. This one keeps the low bits of
+ // its mantissa when truncated.
+ BOOST_CHECK_EQUAL(GetBitsProof(0x21012345), arith_uint256{0});
+ // The genesis block target.
+ BOOST_CHECK_EQUAL(GetBitsProof(0x1d00ffff), arith_uint256{0x100010001});
+}
+
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
{
const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.