Raven Core  3.0.0
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 Core developers
3 // Copyright (c) 2017-2019 The Raven 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 RAVEN_MERKLEBLOCK_H
8 #define RAVEN_MERKLEBLOCK_H
9 
10 #include "serialize.h"
11 #include "uint256.h"
12 #include "primitives/block.h"
13 #include "bloom.h"
14 
15 #include <vector>
16 
52 {
53 protected:
55  unsigned int nTransactions;
56 
58  std::vector<bool> vBits;
59 
61  std::vector<uint256> vHash;
62 
64  bool fBad;
65 
67  unsigned int CalcTreeWidth(int height) const {
68  return (nTransactions+(1 << height)-1) >> height;
69  }
70 
72  uint256 CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid);
73 
75  void TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch);
76 
81  uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex);
82 
83 public:
84 
87 
88  template <typename Stream, typename Operation>
89  inline void SerializationOp(Stream& s, Operation ser_action) {
90  READWRITE(nTransactions);
91  READWRITE(vHash);
92  std::vector<unsigned char> vBytes;
93  if (ser_action.ForRead()) {
94  READWRITE(vBytes);
95  CPartialMerkleTree &us = *(const_cast<CPartialMerkleTree*>(this));
96  us.vBits.resize(vBytes.size() * 8);
97  for (unsigned int p = 0; p < us.vBits.size(); p++)
98  us.vBits[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0;
99  us.fBad = false;
100  } else {
101  vBytes.resize((vBits.size()+7)/8);
102  for (unsigned int p = 0; p < vBits.size(); p++)
103  vBytes[p / 8] |= vBits[p] << (p % 8);
104  READWRITE(vBytes);
105  }
106  }
107 
109  CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch);
110 
112 
118  uint256 ExtractMatches(std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex);
119 };
120 
121 
129 {
130 public:
134 
141  std::vector<std::pair<unsigned int, uint256> > vMatchedTxn;
142 
148  CMerkleBlock(const CBlock& block, CBloomFilter& filter) : CMerkleBlock(block, &filter, nullptr) { }
149 
150  // Create from a CBlock, matching the txids in the set
151  CMerkleBlock(const CBlock& block, const std::set<uint256>& txids) : CMerkleBlock(block, nullptr, &txids) { }
152 
154 
156 
157  template <typename Stream, typename Operation>
158  inline void SerializationOp(Stream& s, Operation ser_action) {
159  READWRITE(header);
160  READWRITE(txn);
161  }
162 
163 private:
164  // Combined constructor to consolidate code
165  CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids);
166 };
167 
168 #endif // RAVEN_MERKLEBLOCK_H
CBlockHeader header
Public only for unit testing.
Definition: merkleblock.h:132
uint256 ExtractMatches(std::vector< uint256 > &vMatch, std::vector< unsigned int > &vnIndex)
extract the matching txid&#39;s represented by this partial merkle tree and their respective indices with...
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:62
#define READWRITE(obj)
Definition: serialize.h:163
void SerializationOp(Stream &s, Operation ser_action)
Definition: merkleblock.h:158
unsigned int nTransactions
the total number of transactions in the block
Definition: merkleblock.h:55
Definition: block.h:73
bool fBad
flag set when encountering invalid data
Definition: merkleblock.h:64
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:45
unsigned int CalcTreeWidth(int height) const
helper function to efficiently calculate the number of nodes at given height in the merkle tree ...
Definition: merkleblock.h:67
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:51
std::vector< uint256 > vHash
txids and internal hashes
Definition: merkleblock.h:61
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:128
CPartialMerkleTree txn
Definition: merkleblock.h:133
ADD_SERIALIZE_METHODS
serialization implementation
Definition: merkleblock.h:86
std::vector< bool > vBits
node-is-parent-of-matched-txid bits
Definition: merkleblock.h:58
256-bit opaque blob.
Definition: uint256.h:123
CMerkleBlock(const CBlock &block, const std::set< uint256 > &txids)
Definition: merkleblock.h:151
uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector< uint256 > &vMatch, std::vector< unsigned int > &vnIndex)
recursive function that traverses tree nodes, consuming the bits and hashes produced by TraverseAndBu...
Definition: merkleblock.cpp:80
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition: merkleblock.h:141
CMerkleBlock(const CBlock &block, CBloomFilter &filter)
Create from a CBlock, filtering transactions according to filter Note that this will call IsRelevantA...
Definition: merkleblock.h:148
void SerializationOp(Stream &s, Operation ser_action)
Definition: merkleblock.h:89
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21
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&#39;s themselves) ...
Definition: merkleblock.cpp:42