1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_MERKLEBLOCK_H
7 #define BITCOIN_MERKLEBLOCK_H
11 #include "primitives/block.h"
16 /** Data structure that represents a partial merkle tree.
18 * It represents a subset of the txid's of a known block, in a way that
19 * allows recovery of the list of txid's and the merkle root, in an
22 * The encoding works as follows: we traverse the tree in depth-first order,
23 * storing a bit for each traversed node, signifying whether the node is the
24 * parent of at least one matched leaf txid (or a matched txid itself). In
25 * case we are at the leaf level, or this bit is 0, its merkle node hash is
26 * stored, and its children are not explored further. Otherwise, no hash is
27 * stored, but we recurse into both (or the only) child branch. During
28 * decoding, the same depth-first traversal is performed, consuming bits and
29 * hashes as they written during encoding.
31 * The serialization is fixed and provides a hard guarantee about the
34 * SIZE <= 10 + ceil(32.25*N)
36 * Where N represents the number of leaf nodes of the partial tree. N itself
39 * N <= total_transactions
40 * N <= 1 + matched_transactions*tree_height
42 * The serialization format:
43 * - uint32 total_transactions (4 bytes)
44 * - varint number of hashes (1-3 bytes)
45 * - uint256[] hashes in depth-first order (<= 32*N bytes)
46 * - varint number of bytes of flag bits (1-3 bytes)
47 * - byte[] flag bits, packed per 8 in a byte, least significant bit first (<= 2*N-1 bits)
48 * The size constraints follow from this.
50 class CPartialMerkleTree
53 /** the total number of transactions in the block */
54 unsigned int nTransactions
;
56 /** node-is-parent-of-matched-txid bits */
57 std::vector
<bool> vBits
;
59 /** txids and internal hashes */
60 std::vector
<uint256
> vHash
;
62 /** flag set when encountering invalid data */
65 /** helper function to efficiently calculate the number of nodes at given height in the merkle tree */
66 unsigned int CalcTreeWidth(int height
) {
67 return (nTransactions
+(1 << height
)-1) >> height
;
70 /** calculate the hash of a node in the merkle tree (at leaf level: the txid's themselves) */
71 uint256
CalcHash(int height
, unsigned int pos
, const std::vector
<uint256
> &vTxid
);
73 /** recursive function that traverses tree nodes, storing the data as bits and hashes */
74 void TraverseAndBuild(int height
, unsigned int pos
, const std::vector
<uint256
> &vTxid
, const std::vector
<bool> &vMatch
);
77 * recursive function that traverses tree nodes, consuming the bits and hashes produced by TraverseAndBuild.
78 * it returns the hash of the respective node and its respective index.
80 uint256
TraverseAndExtract(int height
, unsigned int pos
, unsigned int &nBitsUsed
, unsigned int &nHashUsed
, std::vector
<uint256
> &vMatch
, std::vector
<unsigned int> &vnIndex
);
84 /** serialization implementation */
85 ADD_SERIALIZE_METHODS
;
87 template <typename Stream
, typename Operation
>
88 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
89 READWRITE(nTransactions
);
91 std::vector
<unsigned char> vBytes
;
92 if (ser_action
.ForRead()) {
94 CPartialMerkleTree
&us
= *(const_cast<CPartialMerkleTree
*>(this));
95 us
.vBits
.resize(vBytes
.size() * 8);
96 for (unsigned int p
= 0; p
< us
.vBits
.size(); p
++)
97 us
.vBits
[p
] = (vBytes
[p
/ 8] & (1 << (p
% 8))) != 0;
100 vBytes
.resize((vBits
.size()+7)/8);
101 for (unsigned int p
= 0; p
< vBits
.size(); p
++)
102 vBytes
[p
/ 8] |= vBits
[p
] << (p
% 8);
107 /** Construct a partial merkle tree from a list of transaction ids, and a mask that selects a subset of them */
108 CPartialMerkleTree(const std::vector
<uint256
> &vTxid
, const std::vector
<bool> &vMatch
);
110 CPartialMerkleTree();
113 * extract the matching txid's represented by this partial merkle tree
114 * and their respective indices within the partial tree.
115 * returns the merkle root, or 0 in case of failure
117 uint256
ExtractMatches(std::vector
<uint256
> &vMatch
, std::vector
<unsigned int> &vnIndex
);
122 * Used to relay blocks as header + vector<merkle branch>
125 * NOTE: The class assumes that the given CBlock has *at least* 1 transaction. If the CBlock has 0 txs, it will hit an assertion.
130 /** Public only for unit testing */
132 CPartialMerkleTree txn
;
135 /** Public only for unit testing and relay testing (not relayed) */
136 std::vector
<std::pair
<unsigned int, uint256
> > vMatchedTxn
;
139 * Create from a CBlock, filtering transactions according to filter
140 * Note that this will call IsRelevantAndUpdate on the filter for each transaction,
141 * thus the filter will likely be modified.
143 CMerkleBlock(const CBlock
& block
, CBloomFilter
& filter
);
145 // Create from a CBlock, matching the txids in the set
146 CMerkleBlock(const CBlock
& block
, const std::set
<uint256
>& txids
);
150 ADD_SERIALIZE_METHODS
;
152 template <typename Stream
, typename Operation
>
153 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
159 #endif // BITCOIN_MERKLEBLOCK_H