Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

Data structure that represents a partial merkle tree. More...

#include <merkleblock.h>

Public Member Functions

template<typename Stream , typename Operation >
void SerializationOp (Stream &s, Operation ser_action)
 
 CPartialMerkleTree (const std::vector< uint256 > &vTxid, const std::vector< bool > &vMatch)
 Construct a partial merkle tree from a list of transaction ids, and a mask that selects a subset of them. More...
 
 CPartialMerkleTree ()
 
uint256 ExtractMatches (std::vector< uint256 > &vMatch, std::vector< unsigned int > &vnIndex)
 extract the matching txid's represented by this partial merkle tree and their respective indices within the partial tree. More...
 

Public Attributes

 ADD_SERIALIZE_METHODS
 serialization implementation More...
 

Protected Member Functions

unsigned int CalcTreeWidth (int height) const
 helper function to efficiently calculate the number of nodes at given height in the merkle tree More...
 
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) More...
 
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 More...
 
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 TraverseAndBuild. More...
 

Protected Attributes

unsigned int nTransactions
 the total number of transactions in the block More...
 
std::vector< bool > vBits
 node-is-parent-of-matched-txid bits More...
 
std::vector< uint256vHash
 txids and internal hashes More...
 
bool fBad
 flag set when encountering invalid data More...
 

Detailed Description

Data structure that represents a partial merkle tree.

It represents a subset of the txid's of a known block, in a way that allows recovery of the list of txid's and the merkle root, in an authenticated way.

The encoding works as follows: we traverse the tree in depth-first order, storing a bit for each traversed node, signifying whether the node is the parent of at least one matched leaf txid (or a matched txid itself). In case we are at the leaf level, or this bit is 0, its merkle node hash is stored, and its children are not explored further. Otherwise, no hash is stored, but we recurse into both (or the only) child branch. During decoding, the same depth-first traversal is performed, consuming bits and hashes as they written during encoding.

The serialization is fixed and provides a hard guarantee about the encoded size:

SIZE <= 10 + ceil(32.25*N)

Where N represents the number of leaf nodes of the partial tree. N itself is bounded by:

N <= total_transactions N <= 1 + matched_transactions*tree_height

The serialization format:

  • uint32 total_transactions (4 bytes)
  • varint number of hashes (1-3 bytes)
  • uint256[] hashes in depth-first order (<= 32*N bytes)
  • varint number of bytes of flag bits (1-3 bytes)
  • byte[] flag bits, packed per 8 in a byte, least significant bit first (<= 2*N-1 bits) The size constraints follow from this.

Definition at line 50 of file merkleblock.h.

Constructor & Destructor Documentation

◆ CPartialMerkleTree() [1/2]

CPartialMerkleTree::CPartialMerkleTree ( const std::vector< uint256 > &  vTxid,
const std::vector< bool > &  vMatch 
)

Construct a partial merkle tree from a list of transaction ids, and a mask that selects a subset of them.

Definition at line 128 of file merkleblock.cpp.

References CalcTreeWidth(), TraverseAndBuild(), vBits, and vHash.

◆ CPartialMerkleTree() [2/2]

CPartialMerkleTree::CPartialMerkleTree ( )

Definition at line 142 of file merkleblock.cpp.

Member Function Documentation

◆ CalcHash()

uint256 CPartialMerkleTree::CalcHash ( int  height,
unsigned int  pos,
const std::vector< uint256 > &  vTxid 
)
protected

calculate the hash of a node in the merkle tree (at leaf level: the txid's themselves)

Definition at line 52 of file merkleblock.cpp.

References BEGIN, CalcTreeWidth(), END, and Hash().

Referenced by TraverseAndBuild().

◆ CalcTreeWidth()

unsigned int CPartialMerkleTree::CalcTreeWidth ( int  height) const
inlineprotected

helper function to efficiently calculate the number of nodes at given height in the merkle tree

Definition at line 66 of file merkleblock.h.

References nTransactions.

Referenced by CalcHash(), CPartialMerkleTree(), ExtractMatches(), TraverseAndBuild(), and TraverseAndExtract().

◆ ExtractMatches()

uint256 CPartialMerkleTree::ExtractMatches ( std::vector< uint256 > &  vMatch,
std::vector< unsigned int > &  vnIndex 
)

extract the matching txid's represented by this partial merkle tree and their respective indices within the partial tree.

returns the merkle root, or 0 in case of failure

Definition at line 144 of file merkleblock.cpp.

References CalcTreeWidth(), fBad, MaxBlockSize(), nTransactions, TraverseAndExtract(), vBits, and vHash.

◆ SerializationOp()

template<typename Stream , typename Operation >
void CPartialMerkleTree::SerializationOp ( Stream &  s,
Operation  ser_action 
)
inline

Definition at line 88 of file merkleblock.h.

References fBad, nTransactions, READWRITE, vBits, and vHash.

◆ TraverseAndBuild()

void CPartialMerkleTree::TraverseAndBuild ( int  height,
unsigned int  pos,
const std::vector< uint256 > &  vTxid,
const std::vector< bool > &  vMatch 
)
protected

recursive function that traverses tree nodes, storing the data as bits and hashes

Definition at line 72 of file merkleblock.cpp.

References CalcHash(), CalcTreeWidth(), nTransactions, vBits, and vHash.

Referenced by CPartialMerkleTree().

◆ TraverseAndExtract()

uint256 CPartialMerkleTree::TraverseAndExtract ( int  height,
unsigned int  pos,
unsigned int &  nBitsUsed,
unsigned int &  nHashUsed,
std::vector< uint256 > &  vMatch,
std::vector< unsigned int > &  vnIndex 
)
protected

recursive function that traverses tree nodes, consuming the bits and hashes produced by TraverseAndBuild.

it returns the hash of the respective node and its respective index.

Definition at line 90 of file merkleblock.cpp.

References BEGIN, CalcTreeWidth(), END, fBad, Hash(), vBits, and vHash.

Referenced by ExtractMatches().

Member Data Documentation

◆ ADD_SERIALIZE_METHODS

CPartialMerkleTree::ADD_SERIALIZE_METHODS

serialization implementation

Definition at line 85 of file merkleblock.h.

◆ fBad

bool CPartialMerkleTree::fBad
protected

flag set when encountering invalid data

Definition at line 63 of file merkleblock.h.

Referenced by ExtractMatches(), SerializationOp(), and TraverseAndExtract().

◆ nTransactions

unsigned int CPartialMerkleTree::nTransactions
protected

the total number of transactions in the block

Definition at line 54 of file merkleblock.h.

Referenced by CalcTreeWidth(), ExtractMatches(), SerializationOp(), and TraverseAndBuild().

◆ vBits

std::vector<bool> CPartialMerkleTree::vBits
protected

node-is-parent-of-matched-txid bits

Definition at line 57 of file merkleblock.h.

Referenced by CPartialMerkleTree(), ExtractMatches(), SerializationOp(), TraverseAndBuild(), and TraverseAndExtract().

◆ vHash

std::vector<uint256> CPartialMerkleTree::vHash
protected

txids and internal hashes

Definition at line 60 of file merkleblock.h.

Referenced by CPartialMerkleTree(), ExtractMatches(), SerializationOp(), TraverseAndBuild(), and TraverseAndExtract().


The documentation for this class was generated from the following files:
Released under the MIT license