12 const std::set<TxId> *txids) {
15 std::vector<bool> vMatch;
16 std::vector<uint256> vHashes;
18 vMatch.reserve(block.
vtx.size());
19 vHashes.reserve(block.
vtx.size());
22 for (
const auto &tx : block.
vtx) {
27 for (
size_t i = 0; i < block.
vtx.size(); i++) {
38 vMatch.push_back(txids && txids->count(txid));
41 vHashes.push_back(txid);
48 const std::vector<uint256> &vTxid) {
52 assert(vTxid.size() != 0);
59 uint256 left = CalcHash(height - 1, pos * 2, vTxid), right;
62 if (pos * 2 + 1 < CalcTreeWidth(height - 1)) {
63 right = CalcHash(height - 1, pos * 2 + 1, vTxid);
69 return Hash(left.
begin(), left.
end(), right.begin(), right.end());
73 const std::vector<uint256> &vTxid,
74 const std::vector<bool> &vMatch) {
76 bool fParentOfMatch =
false;
77 for (
size_t p = pos << height; p < (pos + 1) << height && p < nTransactions;
79 fParentOfMatch |= vMatch[p];
83 vBits.push_back(fParentOfMatch);
84 if (height == 0 || !fParentOfMatch) {
86 vHash.push_back(CalcHash(height, pos, vTxid));
89 TraverseAndBuild(height - 1, pos * 2, vTxid, vMatch);
90 if (pos * 2 + 1 < CalcTreeWidth(height - 1)) {
91 TraverseAndBuild(height - 1, pos * 2 + 1, vTxid, vMatch);
99 std::vector<uint256> &vMatch,
100 std::vector<size_t> &vnIndex) {
101 if (nBitsUsed >= vBits.size()) {
107 bool fParentOfMatch = vBits[nBitsUsed++];
108 if (height == 0 || !fParentOfMatch) {
111 if (nHashUsed >= vHash.size()) {
116 const uint256 &hash = vHash[nHashUsed++];
118 if (height == 0 && fParentOfMatch) {
119 vMatch.push_back(hash);
120 vnIndex.push_back(pos);
126 uint256 left = TraverseAndExtract(height - 1, pos * 2, nBitsUsed, nHashUsed,
129 if (pos * 2 + 1 < CalcTreeWidth(height - 1)) {
130 right = TraverseAndExtract(height - 1, pos * 2 + 1, nBitsUsed,
131 nHashUsed, vMatch, vnIndex);
142 return Hash(left.
begin(), left.
end(), right.begin(), right.end());
146 const std::vector<bool> &vMatch)
147 : nTransactions(vTxid.size()), fBad(false) {
165 std::vector<size_t> &vnIndex) {
194 size_t nBitsUsed = 0, nHashUsed = 0;
205 if ((nBitsUsed + 7) / 8 != (
vBits.size() + 7) / 8) {
210 if (nHashUsed !=
vHash.size()) {
214 return hashMerkleRoot;
uint256 ExtractMatches(std::vector< uint256 > &vMatch, std::vector< size_t > &vnIndex)
Extract the matching txid's represented by this partial merkle tree and their respective indices with...
CBlockHeader header
Public only for unit testing.
CBlockHeader GetBlockHeader() const
size_t CalcTreeWidth(int height) const
Helper function to efficiently calculate the number of nodes at given height in the merkle tree...
bool fBad
flag set when encountering invalid data
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Data structure that represents a partial merkle tree.
uint32_t nTransactions
the total number of transactions in the block
bool MatchAndInsertOutputs(const CTransaction &tx)
Scans output scripts for matches and adds those outpoints to the filter for spend detection...
std::vector< uint256 > vHash
txids and internal hashes
std::vector< std::pair< size_t, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
uint256 TraverseAndExtract(int height, size_t pos, size_t &nBitsUsed, size_t &nHashUsed, std::vector< uint256 > &vMatch, std::vector< size_t > &vnIndex)
Recursive function that traverses tree nodes, consuming the bits and hashes produced by TraverseAndBu...
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
std::vector< bool > vBits
node-is-parent-of-matched-txid bits
std::vector< CTransactionRef > vtx
A TxId is the identifier of a transaction.
void TraverseAndBuild(int height, size_t pos, const std::vector< uint256 > &vTxid, const std::vector< bool > &vMatch)
Recursive function that traverses tree nodes, storing the data as bits and hashes.
uint256 CalcHash(int height, size_t pos, const std::vector< uint256 > &vTxid)
Calculate the hash of a node in the merkle tree (at leaf level: the txid's themselves) ...
The basic transaction that is broadcasted on the network and contained in blocks. ...
bool MatchInputs(const CTransaction &tx)
Scan inputs to see if the spent outpoints are a match, or the input scripts contain matching elements...