diff options
Diffstat (limited to 'src/consensus')
-rw-r--r-- | src/consensus/merkle.cpp | 20 | ||||
-rw-r--r-- | src/consensus/merkle.h | 15 |
2 files changed, 35 insertions, 0 deletions
diff --git a/src/consensus/merkle.cpp b/src/consensus/merkle.cpp index 6be9c26df2..9a8afa8a33 100644 --- a/src/consensus/merkle.cpp +++ b/src/consensus/merkle.cpp @@ -150,3 +150,23 @@ uint256 ComputeMerkleRootFromBranch(const uint256& leaf, const std::vector<uint2 } return hash; } + +uint256 BlockMerkleRoot(const CBlock& block, bool* mutated) +{ + std::vector<uint256> leaves; + leaves.resize(block.vtx.size()); + for (size_t s = 0; s < block.vtx.size(); s++) { + leaves[s] = block.vtx[s].GetHash(); + } + return ComputeMerkleRoot(leaves, mutated); +} + +std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position) +{ + std::vector<uint256> leaves; + leaves.resize(block.vtx.size()); + for (size_t s = 0; s < block.vtx.size(); s++) { + leaves[s] = block.vtx[s].GetHash(); + } + return ComputeMerkleBranch(leaves, position); +} diff --git a/src/consensus/merkle.h b/src/consensus/merkle.h index 7fd13d3e43..6ef59745ac 100644 --- a/src/consensus/merkle.h +++ b/src/consensus/merkle.h @@ -8,10 +8,25 @@ #include <stdint.h> #include <vector> +#include "primitives/transaction.h" +#include "primitives/block.h" #include "uint256.h" uint256 ComputeMerkleRoot(const std::vector<uint256>& leaves, bool* mutated = NULL); std::vector<uint256> ComputeMerkleBranch(const std::vector<uint256>& leaves, uint32_t position); uint256 ComputeMerkleRootFromBranch(const uint256& leaf, const std::vector<uint256>& branch, uint32_t position); +/* + * Compute the Merkle root of the transactions in a block. + * *mutated is set to true if a duplicated subtree was found. + */ +uint256 BlockMerkleRoot(const CBlock& block, bool* mutated = NULL); + +/* + * Compute the Merkle branch for the tree of transactions in a block, for a + * given position. + * This can be verified using ComputeMerkleRootFromBranch. + */ +std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position); + #endif |