PRCYCoin  2.0.0.7rc1
P2P Digital Currency
merkleblock.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin 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 "bloom.h"
10 #include "primitives/block.h"
11 #include "serialize.h"
12 #include "uint256.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  {
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);
82 
83 public:
86 
87  template <typename Stream, typename Operation>
88  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
89  {
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 
117  uint256 ExtractMatches(std::vector<uint256>& vMatch);
118 };
119 
120 
128 {
129 public:
133 
134 public:
136  std::vector<std::pair<unsigned int, uint256> > vMatchedTxn;
137 
143  CMerkleBlock(const CBlock& block, CBloomFilter& filter);
144 
146 
147  template <typename Stream, typename Operation>
148  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
149  {
150  READWRITE(header);
151  READWRITE(txn);
152  }
153 };
154 
155 #endif // BITCOIN_MERKLEBLOCK_H
block.h
CPartialMerkleTree::TraverseAndExtract
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:79
CPartialMerkleTree::vBits
std::vector< bool > vBits
node-is-parent-of-matched-txid bits
Definition: merkleblock.h:57
CPartialMerkleTree
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:50
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:52
CBloomFilter
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:43
CMerkleBlock
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:127
uint256.h
CPartialMerkleTree::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
serialization implementation
Definition: merkleblock.h:85
CPartialMerkleTree::ExtractMatches
uint256 ExtractMatches(std::vector< uint256 > &vMatch)
extract the matching txid's represented by this partial merkle tree.
Definition: merkleblock.cpp:127
CMerkleBlock::header
CBlockHeader header
Public only for unit testing.
Definition: merkleblock.h:131
bloom.h
CPartialMerkleTree::CalcHash
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:39
CPartialMerkleTree::TraverseAndBuild
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:60
CPartialMerkleTree::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: merkleblock.h:88
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CMerkleBlock::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: merkleblock.h:145
CPartialMerkleTree::fBad
bool fBad
flag set when encountering invalid data
Definition: merkleblock.h:63
CMerkleBlock::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: merkleblock.h:148
CBlock
Definition: block.h:142
CPartialMerkleTree::nTransactions
unsigned int nTransactions
the total number of transactions in the block
Definition: merkleblock.h:54
READWRITE
#define READWRITE(obj)
Definition: serialize.h:164
CMerkleBlock::txn
CPartialMerkleTree txn
Definition: merkleblock.h:132
CMerkleBlock::vMatchedTxn
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed)
Definition: merkleblock.h:136
serialize.h
CMerkleBlock::CMerkleBlock
CMerkleBlock(const CBlock &block, CBloomFilter &filter)
Create from a CBlock, filtering transactions according to filter Note that this will call IsRelevantA...
Definition: merkleblock.cpp:16
CPartialMerkleTree::vHash
std::vector< uint256 > vHash
txids and internal hashes
Definition: merkleblock.h:60
CPartialMerkleTree::CalcTreeWidth
unsigned int CalcTreeWidth(int height)
helper function to efficiently calculate the number of nodes at given height in the merkle tree
Definition: merkleblock.h:66
CPartialMerkleTree::CPartialMerkleTree
CPartialMerkleTree()
Definition: merkleblock.cpp:125