Files
bitcoin/src/test/merkleblock_tests.cpp
rustaceanrob 5ac3579520 refactor: Add compile-time-checked hex txid
Suggested by @l0rinc in #34004

Message by @l0rinc:

This adds a consteval constructor to transaction_identifier (Txid/Wtxid) to allow parsing hex strings at compile-time.
This replaces runtime FromHex checks in tests, ensuring that malformed hardcoded hashes cause build failures rather than runtime test failures.

Test variables are explicitly marked constexpr. This is required to workaround a regression in GCC 14 (Bug 117501) where the compiler incorrectly flags consteval initialization of non-constexpr variables as "statements with no effect".

GCC Bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117501
Reproducer: https://godbolt.org/z/xb5TMaPs6

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
2025-12-13 18:23:48 +00:00

81 lines
2.4 KiB
C++

// Copyright (c) 2012-2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <merkleblock.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <boost/test/unit_test.hpp>
#include <set>
#include <vector>
BOOST_AUTO_TEST_SUITE(merkleblock_tests)
/**
* Create a CMerkleBlock using a list of txids which will be found in the
* given block.
*/
BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found)
{
CBlock block = getBlock13b8a();
std::set<Txid> txids;
// Last txn in block.
constexpr Txid txhash1{"74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"};
// Second txn in block.
constexpr Txid txhash2{"f9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07"};
txids.insert(txhash1);
txids.insert(txhash2);
CMerkleBlock merkleBlock(block, txids);
BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());
// vMatchedTxn is only used when bloom filter is specified.
BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0U);
std::vector<Txid> vMatched;
std::vector<unsigned int> vIndex;
BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched, vIndex).GetHex(), block.hashMerkleRoot.GetHex());
BOOST_CHECK_EQUAL(vMatched.size(), 2U);
// Ordered by occurrence in depth-first tree traversal.
BOOST_CHECK_EQUAL(vMatched[0], txhash2);
BOOST_CHECK_EQUAL(vIndex[0], 1U);
BOOST_CHECK_EQUAL(vMatched[1], txhash1);
BOOST_CHECK_EQUAL(vIndex[1], 8U);
}
/**
* Create a CMerkleBlock using a list of txids which will not be found in the
* given block.
*/
BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_not_found)
{
CBlock block = getBlock13b8a();
std::set<Txid> txids2;
txids2.insert(Txid{"c0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"});
CMerkleBlock merkleBlock(block, txids2);
BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());
BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0U);
std::vector<Txid> vMatched;
std::vector<unsigned int> vIndex;
BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched, vIndex).GetHex(), block.hashMerkleRoot.GetHex());
BOOST_CHECK_EQUAL(vMatched.size(), 0U);
BOOST_CHECK_EQUAL(vIndex.size(), 0U);
}
BOOST_AUTO_TEST_SUITE_END()