Changeset View
Changeset View
Standalone View
Standalone View
src/merkleblock.h
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | protected: | ||||
std::vector<bool> vBits; | std::vector<bool> vBits; | ||||
/** txids and internal hashes */ | /** txids and internal hashes */ | ||||
std::vector<uint256> vHash; | std::vector<uint256> vHash; | ||||
/** flag set when encountering invalid data */ | /** flag set when encountering invalid data */ | ||||
bool fBad; | bool fBad; | ||||
/** helper function to efficiently calculate the number of nodes at given | /** | ||||
* height in the merkle tree. */ | * Helper function to efficiently calculate the number of nodes at given | ||||
* height in the merkle tree. | |||||
*/ | |||||
unsigned int CalcTreeWidth(int height) { | unsigned int CalcTreeWidth(int height) { | ||||
return (nTransactions + (1 << height) - 1) >> height; | return (nTransactions + (1 << height) - 1) >> height; | ||||
} | } | ||||
/** Calculate the hash of a node in the merkle tree (at leaf level: the | /** | ||||
* txid's themselves) */ | * Calculate the hash of a node in the merkle tree (at leaf level: the | ||||
* txid's themselves) | |||||
*/ | |||||
uint256 CalcHash(int height, unsigned int pos, | uint256 CalcHash(int height, unsigned int pos, | ||||
const std::vector<uint256> &vTxid); | const std::vector<uint256> &vTxid); | ||||
/** Recursive function that traverses tree nodes, storing the data as bits | /** | ||||
* and hashes. */ | * Recursive function that traverses tree nodes, storing the data as bits | ||||
* and hashes. | |||||
*/ | |||||
void TraverseAndBuild(int height, unsigned int pos, | void TraverseAndBuild(int height, unsigned int pos, | ||||
const std::vector<uint256> &vTxid, | const std::vector<uint256> &vTxid, | ||||
const std::vector<bool> &vMatch); | const std::vector<bool> &vMatch); | ||||
/** | /** | ||||
* Recursive function that traverses tree nodes, consuming the bits and | * Recursive function that traverses tree nodes, consuming the bits and | ||||
* hashes produced by TraverseAndBuild. It returns the hash of the | * hashes produced by TraverseAndBuild. It returns the hash of the | ||||
* respective node and its respective index. | * respective node and its respective index. | ||||
Show All 11 Lines | public: | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
READWRITE(nTransactions); | READWRITE(nTransactions); | ||||
READWRITE(vHash); | READWRITE(vHash); | ||||
std::vector<uint8_t> vBytes; | std::vector<uint8_t> vBytes; | ||||
if (ser_action.ForRead()) { | if (ser_action.ForRead()) { | ||||
READWRITE(vBytes); | READWRITE(vBytes); | ||||
CPartialMerkleTree &us = *(const_cast<CPartialMerkleTree *>(this)); | CPartialMerkleTree &us = *(const_cast<CPartialMerkleTree *>(this)); | ||||
us.vBits.resize(vBytes.size() * 8); | us.vBits.resize(vBytes.size() * 8); | ||||
for (unsigned int p = 0; p < us.vBits.size(); p++) | for (unsigned int p = 0; p < us.vBits.size(); p++) { | ||||
us.vBits[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0; | us.vBits[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0; | ||||
} | |||||
us.fBad = false; | us.fBad = false; | ||||
} else { | } else { | ||||
vBytes.resize((vBits.size() + 7) / 8); | vBytes.resize((vBits.size() + 7) / 8); | ||||
for (unsigned int p = 0; p < vBits.size(); p++) | for (unsigned int p = 0; p < vBits.size(); p++) { | ||||
vBytes[p / 8] |= vBits[p] << (p % 8); | vBytes[p / 8] |= vBits[p] << (p % 8); | ||||
} | |||||
READWRITE(vBytes); | READWRITE(vBytes); | ||||
} | } | ||||
} | } | ||||
/** Construct a partial merkle tree from a list of transaction ids, and a | /** | ||||
* mask that selects a subset of them. */ | * Construct a partial merkle tree from a list of transaction ids, and a | ||||
* mask that selects a subset of them. | |||||
*/ | |||||
CPartialMerkleTree(const std::vector<uint256> &vTxid, | CPartialMerkleTree(const std::vector<uint256> &vTxid, | ||||
const std::vector<bool> &vMatch); | const std::vector<bool> &vMatch); | ||||
CPartialMerkleTree(); | CPartialMerkleTree(); | ||||
/** | /** | ||||
* Extract the matching txid's represented by this partial merkle tree and | * Extract the matching txid's represented by this partial merkle tree and | ||||
* their respective indices within the partial tree. Returns the merkle | * their respective indices within the partial tree. Returns the merkle | ||||
* root, or 0 in case of failure | * root, or 0 in case of failure. | ||||
*/ | */ | ||||
uint256 ExtractMatches(std::vector<uint256> &vMatch, | uint256 ExtractMatches(std::vector<uint256> &vMatch, | ||||
std::vector<unsigned int> &vnIndex); | std::vector<unsigned int> &vnIndex); | ||||
}; | }; | ||||
/** | /** | ||||
* Used to relay blocks as header + vector<merkle branch> | * Used to relay blocks as header + vector<merkle branch> | ||||
* to filtered nodes. | * to filtered nodes. | ||||
Show All 33 Lines |