Dash Core  0.12.2.1
P2P Digital Currency
merkleblock.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 "serialize.h"
10 #include "uint256.h"
11 #include "primitives/block.h"
12 #include "bloom.h"
13 
14 #include <vector>
15 
51 {
52 protected:
54  unsigned int nTransactions;
55 
57  std::vector<bool> vBits;
58 
60  std::vector<uint256> vHash;
61 
63  bool fBad;
64 
66  unsigned int CalcTreeWidth(int height) {
67  return (nTransactions+(1 << height)-1) >> height;
68  }
69 
71  uint256 CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid);
72 
74  void TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch);
75 
80  uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch);
81 
82 public:
83 
86 
87  template <typename Stream, typename Operation>
88  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
91  std::vector<unsigned char> vBytes;
92  if (ser_action.ForRead()) {
93  READWRITE(vBytes);
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;
98  us.fBad = false;
99  } else {
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);
103  READWRITE(vBytes);
104  }
105  }
106 
108  CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch);
109 
111 
116  uint256 ExtractMatches(std::vector<uint256> &vMatch);
117 };
118 
119 
125 {
126 public:
130 
131 public:
133  std::vector<std::pair<unsigned int, uint256> > vMatchedTxn;
134 
140  CMerkleBlock(const CBlock& block, CBloomFilter& filter);
141 
142  // Create from a CBlock, matching the txids in the set
143  CMerkleBlock(const CBlock& block, const std::set<uint256>& txids);
144 
146 
148 
149  template <typename Stream, typename Operation>
150  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
151  READWRITE(header);
152  READWRITE(txn);
153  }
154 };
155 
156 #endif // BITCOIN_MERKLEBLOCK_H
#define READWRITE(obj)
Definition: serialize.h:175
CPartialMerkleTree txn
Definition: merkleblock.h:129
uint256 ExtractMatches(std::vector< uint256 > &vMatch)
unsigned int CalcTreeWidth(int height)
Definition: merkleblock.h:66
unsigned int nTransactions
Definition: merkleblock.h:54
std::vector< uint256 > vHash
Definition: merkleblock.h:60
void TraverseAndBuild(int height, unsigned int pos, const std::vector< uint256 > &vTxid, const std::vector< bool > &vMatch)
Definition: merkleblock.cpp:80
uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector< uint256 > &vMatch)
Definition: merkleblock.cpp:98
std::vector< bool > vBits
Definition: merkleblock.h:57
CBlockHeader header
Definition: merkleblock.h:128
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: merkleblock.h:150
Definition: block.h:73
uint256 CalcHash(int height, unsigned int pos, const std::vector< uint256 > &vTxid)
Definition: merkleblock.cpp:63
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: merkleblock.h:88
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Definition: merkleblock.h:133