Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
merkleblock.h
Go to the documentation of this file.
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.
5
6#ifndef BITCOIN_MERKLEBLOCK_H
7#define BITCOIN_MERKLEBLOCK_H
8
9#include <common/bloom.h>
10#include <primitives/block.h>
11#include <serialize.h>
12#include <uint256.h>
13
14#include <vector>
15
16// Helper functions for serialization.
17std::vector<uint8_t> BitsToBytes(const std::vector<bool> &bits);
18std::vector<bool> BytesToBits(const std::vector<uint8_t> &bytes);
19
57protected:
60
62 std::vector<bool> vBits;
63
65 std::vector<uint256> vHash;
66
68 bool fBad;
69
74 size_t CalcTreeWidth(int height) const {
75 return (nTransactions + (1 << height) - 1) >> height;
76 }
77
82 uint256 CalcHash(int height, size_t pos, const std::vector<uint256> &vTxid);
83
88 void TraverseAndBuild(int height, size_t pos,
89 const std::vector<uint256> &vTxid,
90 const std::vector<bool> &vMatch);
91
97 uint256 TraverseAndExtract(int height, size_t pos, size_t &nBitsUsed,
98 size_t &nHashUsed, std::vector<uint256> &vMatch,
99 std::vector<size_t> &vnIndex);
100
101public:
103 READWRITE(obj.nTransactions, obj.vHash);
104 std::vector<uint8_t> bytes;
105 SER_WRITE(obj, bytes = BitsToBytes(obj.vBits));
106 READWRITE(bytes);
107 SER_READ(obj, obj.vBits = BytesToBits(bytes));
108 SER_READ(obj, obj.fBad = false);
109 }
110
115 CPartialMerkleTree(const std::vector<uint256> &vTxid,
116 const std::vector<bool> &vMatch);
117
119
125 uint256 ExtractMatches(std::vector<uint256> &vMatch,
126 std::vector<size_t> &vnIndex);
127
133};
134
148public:
152
159 std::vector<std::pair<size_t, uint256>> vMatchedTxn;
160
166 CMerkleBlock(const CBlock &block, CBloomFilter &filter)
167 : CMerkleBlock(block, &filter, nullptr) {}
168
172 CMerkleBlock(const CBlock &block, const std::set<TxId> &txids)
173 : CMerkleBlock(block, nullptr, &txids) {}
174
176
178
179private:
184 CMerkleBlock(const CBlock &block, CBloomFilter *filter,
185 const std::set<TxId> *txids);
186};
187
188#endif // BITCOIN_MERKLEBLOCK_H
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition block.h:23
Definition block.h:60
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition bloom.h:44
Used to create a Merkle proof (usually from a subset of transactions), which consists of a block head...
SERIALIZE_METHODS(CMerkleBlock, obj)
CMerkleBlock(const CBlock &block, const std::set< TxId > &txids)
Create a Merkle proof for a set of transactions.
CBlockHeader header
Public only for unit testing.
CMerkleBlock(const CBlock &block, CBloomFilter &filter)
Create a Merkle proof according to a bloom filter.
std::vector< std::pair< size_t, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
CPartialMerkleTree txn
Data structure that represents a partial merkle tree.
Definition merkleblock.h:56
uint32_t nTransactions
the total number of transactions in the block
Definition merkleblock.h:59
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...
size_t CalcTreeWidth(int height) const
Helper function to efficiently calculate the number of nodes at given height in the merkle tree.
Definition merkleblock.h:74
std::vector< bool > vBits
node-is-parent-of-matched-txid bits
Definition merkleblock.h:62
bool fBad
flag set when encountering invalid data
Definition merkleblock.h:68
uint32_t GetNumTransactions() const
Get number of transactions the merkle proof is indicating for cross-reference with local blockchain k...
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...
std::vector< uint256 > vHash
txids and internal hashes
Definition merkleblock.h:65
SERIALIZE_METHODS(CPartialMerkleTree, obj)
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)
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.
256-bit opaque blob.
Definition uint256.h:129
std::vector< uint8_t > BitsToBytes(const std::vector< bool > &bits)
std::vector< bool > BytesToBits(const std::vector< uint8_t > &bytes)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
#define SER_WRITE(obj, code)
Definition serialize.h:173
#define SER_READ(obj, code)
Definition serialize.h:169
#define READWRITE(...)
Definition serialize.h:166