PIVX Core  5.6.99
P2P Digital Currency
merkleblock.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin developers
3 // Copyright (c) 2017-2020 The PIVX Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef PIVX_MERKLEBLOCK_H
8 #define PIVX_MERKLEBLOCK_H
9 
10 #include "bloom.h"
11 #include "primitives/block.h"
12 #include "serialize.h"
13 #include "uint256.h"
14 
15 #include <vector>
16 
17 // Helper functions for serialization.
18 std::vector<unsigned char> BitsToBytes(const std::vector<bool>& bits);
19 std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes);
20 
56 {
57 protected:
59  unsigned int nTransactions;
60 
62  std::vector<bool> vBits;
63 
65  std::vector<uint256> vHash;
66 
68  bool fBad;
69 
71  unsigned int CalcTreeWidth(int height)
72  {
73  return (nTransactions + (1 << height) - 1) >> height;
74  }
75 
77  uint256 CalcHash(int height, unsigned int pos, const std::vector<uint256>& vTxid);
78 
80  void TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256>& vTxid, const std::vector<bool>& vMatch);
81 
86  uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int& nBitsUsed, unsigned int& nHashUsed, std::vector<uint256>& vMatch);
87 
88 public:
89 
91  {
92  READWRITE(obj.nTransactions, obj.vHash);
93  std::vector<unsigned char> bytes;
94  SER_WRITE(obj, bytes = BitsToBytes(obj.vBits));
95  READWRITE(bytes);
96  SER_READ(obj, obj.vBits = BytesToBits(bytes));
97  SER_READ(obj, obj.fBad = false);
98  }
99 
101  CPartialMerkleTree(const std::vector<uint256>& vTxid, const std::vector<bool>& vMatch);
102 
104 
109  uint256 ExtractMatches(std::vector<uint256>& vMatch);
110 };
111 
112 
118 {
119 public:
123 
130  std::vector<std::pair<unsigned int, uint256> > vMatchedTxn;
131 
137  CMerkleBlock(const CBlock& block, CBloomFilter& filter) : CMerkleBlock(block, &filter, nullptr) { }
138 
139  // Create from a CBlock, matching the txids in the set
140  CMerkleBlock(const CBlock& block, const std::set<uint256>& txids) : CMerkleBlock(block, nullptr, &txids) { }
141 
143 
144  SERIALIZE_METHODS(CMerkleBlock, obj) { READWRITE(obj.header, obj.txn); }
145 
146 private:
147  // Combined constructor to consolidate code
148  CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids);
149 
150 };
151 
152 #endif // PIVX_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:80
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:45
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:118
SERIALIZE_METHODS(CMerkleBlock, obj)
Definition: merkleblock.h:144
CBlockHeader header
Public only for unit testing.
Definition: merkleblock.h:121
CMerkleBlock(const CBlock &block, CBloomFilter &filter)
Create from a CBlock, filtering transactions according to filter Note that this will call IsRelevantA...
Definition: merkleblock.h:137
CMerkleBlock(const CBlock &block, const std::set< uint256 > &txids)
Definition: merkleblock.h:140
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition: merkleblock.h:130
CPartialMerkleTree txn
Definition: merkleblock.h:122
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:56
unsigned int nTransactions
the total number of transactions in the block
Definition: merkleblock.h:59
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
uint256 ExtractMatches(std::vector< uint256 > &vMatch)
extract the matching txid's represented by this partial merkle tree.
uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector< uint256 > &vMatch)
recursive function that traverses tree nodes, consuming the bits and hashes produced by TraverseAndBu...
Definition: merkleblock.cpp:96
void TraverseAndBuild(int height, unsigned int pos, const std::vector< uint256 > &vTxid, const std::vector< bool > &vMatch)
recursive function that traverses tree nodes, storing the data as bits and hashes
Definition: merkleblock.cpp:77
uint256 CalcHash(int height, unsigned int pos, const std::vector< uint256 > &vTxid)
calculate the hash of a node in the merkle tree (at leaf level: the txid's themselves)
Definition: merkleblock.cpp:59
unsigned int CalcTreeWidth(int height)
helper function to efficiently calculate the number of nodes at given height in the merkle tree
Definition: merkleblock.h:71
std::vector< uint256 > vHash
txids and internal hashes
Definition: merkleblock.h:65
SERIALIZE_METHODS(CPartialMerkleTree, obj)
Definition: merkleblock.h:90
256-bit opaque blob.
Definition: uint256.h:138
std::vector< bool > BytesToBits(const std::vector< unsigned char > &bytes)
Definition: merkleblock.cpp:24
std::vector< unsigned char > BitsToBytes(const std::vector< bool > &bits)
Definition: merkleblock.cpp:15
#define SER_WRITE(obj, code)
Definition: serialize.h:186
#define SER_READ(obj, code)
Definition: serialize.h:185
#define READWRITE(...)
Definition: serialize.h:183