Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
pmt_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2012-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6#include <consensus/merkle.h>
7#include <merkleblock.h>
8#include <serialize.h>
9#include <streams.h>
10#include <test/util/common.h>
11#include <test/util/random.h>
13#include <uint256.h>
14
15#include <vector>
16
17#include <boost/test/unit_test.hpp>
18
20{
21public:
23
24 // flip one bit in one of the hashes - this should break the authentication
25 void Damage() {
26 unsigned int n = m_rng.randrange(vHash.size());
27 int bit = m_rng.randbits(8);
28 *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
29 }
30
32};
33
35
37{
38 static const unsigned int tx_counts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
39
40 for (int i = 0; i < 12; i++) {
41 unsigned int nTx = tx_counts[i];
42
43 // build a block with some dummy transactions
44 CBlock block;
45 for (unsigned int j=0; j<nTx; j++) {
47 tx.nLockTime = j; // actual transaction data doesn't matter; just make the nLockTime's unique
48 block.vtx.push_back(MakeTransactionRef(std::move(tx)));
49 }
50
51 // calculate actual merkle root and height
53 std::vector<Txid> vTxid(nTx);
54 for (unsigned int j=0; j<nTx; j++)
55 vTxid[j] = block.vtx[j]->GetHash();
56 int nHeight = 1, nTx_ = nTx;
57 while (nTx_ > 1) {
58 nTx_ = (nTx_+1)/2;
59 nHeight++;
60 }
61
62 // check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/128
63 for (int att = 1; att < 15; att++) {
64 // build random subset of txid's
65 std::vector<bool> vMatch(nTx, false);
66 std::vector<Txid> vMatchTxid1;
67 for (unsigned int j=0; j<nTx; j++) {
68 bool fInclude = m_rng.randbits(att / 2) == 0;
69 vMatch[j] = fInclude;
70 if (fInclude)
71 vMatchTxid1.push_back(vTxid[j]);
72 }
73
74 // build the partial merkle tree
76
77 // serialize
78 DataStream ss{};
79 ss << pmt1;
80
81 // verify CPartialMerkleTree's size guarantees
82 unsigned int n = std::min<unsigned int>(nTx, 1 + vMatchTxid1.size()*nHeight);
83 BOOST_CHECK(ss.size() <= 10 + (258*n+7)/8);
84
85 // deserialize into a tester copy
87 ss >> pmt2;
88
89 // extract merkle root and matched txids from copy
90 std::vector<Txid> vMatchTxid2;
91 std::vector<unsigned int> vIndex;
92 uint256 merkleRoot2 = pmt2.ExtractMatches(vMatchTxid2, vIndex);
93
94 // check that it has the same merkle root as the original, and a valid one
96 BOOST_CHECK(!merkleRoot2.IsNull());
97
98 // check that it contains the matched transactions (in the same order!)
100
101 // check that random bit flips break the authentication
102 for (int j=0; j<4; j++) {
104 pmt3.Damage();
105 std::vector<Txid> vMatchTxid3;
106 uint256 merkleRoot3 = pmt3.ExtractMatches(vMatchTxid3, vIndex);
108 }
109 }
110 }
111}
112
114{
115 std::vector<Txid> vTxid{
122 };
123 std::vector<bool> vMatch = {false, false, false, false, false, false, false, false, false, true, true, false};
124
126 std::vector<unsigned int> vIndex;
127 BOOST_CHECK(tree.ExtractMatches(vTxid, vIndex).IsNull());
128}
129
131{
132 // Deserialize a partial merkle tree with the given fields and extract its matches.
133 const auto extract{[](uint32_t tx_count, uint32_t hash_count, std::vector<unsigned char> bits) {
134 std::vector<uint256> hashes;
135 for (uint32_t i{1}; i <= hash_count; ++i) hashes.emplace_back(i);
137 ss << tx_count << hashes << bits;
139 ss >> tree;
140 std::vector<Txid> matches;
141 std::vector<unsigned int> indexes;
142 return tree.ExtractMatches(matches, indexes);
143 }};
144
145 // The transaction count is limited to MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT.
147 static_assert(max_tx_count == 16666);
149 BOOST_CHECK(extract(max_tx_count + 1, 1, {0x00}).IsNull());
150 // An empty tree, more hashes than transactions, and fewer bits than hashes are
151 // rejected before traversal. Traversal would reject each of them anyway, so these
152 // check the result, not the early returns.
153 BOOST_CHECK(extract(0, 0, {0x00}).IsNull());
154 BOOST_CHECK(extract(1, 2, {0x00}).IsNull());
155 BOOST_CHECK(extract(9, 9, {0xff}).IsNull());
156 // Traversal runs out of hashes: the root is a parent of a match, so both of its
157 // leaves need a hash, but only one is provided.
158 BOOST_CHECK(extract(2, 1, {0x01}).IsNull());
159 // Traversal runs out of bits: eight bits reach the third leaf but not the fourth.
160 BOOST_CHECK(extract(16, 8, {0xff}).IsNull());
161 // Not all bits are consumed: a second byte of bits is more than padding.
162 BOOST_CHECK(extract(1, 1, {0x00, 0x00}).IsNull());
163 // Not all hashes are consumed: the root is not a parent of a match, so only its
164 // own hash is used.
165 BOOST_CHECK(extract(2, 2, {0x00}).IsNull());
166}
167
Definition block.h:74
std::vector< CTransactionRef > vtx
Definition block.h:77
Data structure that represents a partial merkle tree.
Definition merkleblock.h:57
std::vector< uint256 > vHash
txids and internal hashes
Definition merkleblock.h:66
CPartialMerkleTreeTester(FastRandomContext &rng)
Definition pmt_tests.cpp:22
FastRandomContext & m_rng
Definition pmt_tests.cpp:31
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:165
size_type size() const
Definition streams.h:198
Fast randomness source.
Definition random.h:386
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Definition random.h:254
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition random.h:204
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition uint256.h:196
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition merkle.cpp:76
constexpr size_t MIN_TRANSACTION_WEIGHT
Definition consensus.h:23
constexpr unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
Definition consensus.h:15
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
unsigned int nHeight
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:17
#define BOOST_CHECK(expr)
Definition object.cpp:16
BOOST_AUTO_TEST_CASE(pmt_test1)
Definition pmt_tests.cpp:36
static CTransactionRef MakeTransactionRef(Tx &&txIn)
#define extract(n)
Extract the lowest 64 bits of (c0,c1,c2) into n, and left shift the number 64 bits.
Basic testing setup.
A mutable version of CTransaction.
FastRandomContext rng
DataStream ss
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82