42 static void MerkleComputation(
const std::vector<uint256>& leaves,
uint256* proot,
bool* pmutated, uint32_t branchpos, std::vector<uint256>* pbranch) {
43 if (pbranch) pbranch->clear();
44 if (leaves.size() == 0) {
45 if (pmutated) *pmutated =
false;
61 while (count < leaves.size()) {
63 bool matchh = count == branchpos;
69 for (level = 0; !(count & (((uint32_t)1) << level)); level++) {
72 pbranch->push_back(inner[level]);
73 }
else if (matchlevel == level) {
74 pbranch->push_back(h);
78 mutated |= (inner[level] == h);
93 while (!(count & (((uint32_t)1) << level))) {
97 bool matchh = matchlevel == level;
98 while (count != (((uint32_t)1) << level)) {
102 if (pbranch && matchh) {
103 pbranch->push_back(h);
108 count += (((uint32_t)1) << level);
111 while (!(count & (((uint32_t)1) << level))) {
114 pbranch->push_back(inner[level]);
115 }
else if (matchlevel == level) {
116 pbranch->push_back(h);
125 if (pmutated) *pmutated = mutated;
126 if (proot) *proot = h;
131 MerkleComputation(leaves, &hash, mutated, -1,
nullptr);
136 std::vector<uint256> ret;
137 MerkleComputation(leaves,
nullptr,
nullptr, position, &ret);
143 for (std::vector<uint256>::const_iterator it = vMerkleBranch.begin(); it != vMerkleBranch.end(); ++it) {
156 std::vector<uint256> leaves;
157 leaves.resize(block.
vtx.size());
158 for (
size_t s = 0; s < block.
vtx.size(); s++) {
159 leaves[s] = block.
vtx[s]->GetHash();
166 std::vector<uint256> leaves;
167 leaves.resize(block.
vtx.size());
168 for (
size_t s = 0; s < block.
vtx.size(); s++) {
169 leaves[s] = block.
vtx[s]->GetHash();
std::vector< CTransactionRef > vtx
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
CHash256 & Write(const unsigned char *data, size_t len)
void Finalize(unsigned char hash[OUTPUT_SIZE])
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
uint256 ComputeMerkleRootFromBranch(const uint256 &leaf, const std::vector< uint256 > &vMerkleBranch, uint32_t nIndex)
std::vector< uint256 > ComputeMerkleBranch(const std::vector< uint256 > &leaves, uint32_t position)
std::vector< uint256 > BlockMerkleBranch(const CBlock &block, uint32_t position)
uint256 ComputeMerkleRoot(const std::vector< uint256 > &leaves, bool *mutated)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
#define BEGIN(a)
Utilities for converting data from/to strings.