|
|
@ -1,4 +1,5 @@ |
|
|
|
#include "Block.h" |
|
|
|
#include "Block.h" |
|
|
|
|
|
|
|
#include "Log.h" |
|
|
|
|
|
|
|
|
|
|
|
namespace Bitcoin |
|
|
|
namespace Bitcoin |
|
|
|
{ |
|
|
|
{ |
|
|
@ -6,28 +7,20 @@ namespace Bitcoin |
|
|
|
{ |
|
|
|
{ |
|
|
|
merkleTree.clear(); |
|
|
|
merkleTree.clear(); |
|
|
|
|
|
|
|
|
|
|
|
uint64 branches = 1; |
|
|
|
|
|
|
|
uint32 levels = 0; |
|
|
|
|
|
|
|
while (branches < tx.size()) { |
|
|
|
|
|
|
|
branches *= 2; |
|
|
|
|
|
|
|
++levels; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Used when sending merkle branches
|
|
|
|
|
|
|
|
merkleBranches = branches; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add transactions
|
|
|
|
// Add transactions
|
|
|
|
for (uint64 i = 0; i < branches; ++i) |
|
|
|
for (uint64 i = 0; i < tx.size(); ++i) |
|
|
|
merkleTree.push_back(tx[std::min(i, tx.size()-1)].GetHash()); |
|
|
|
merkleTree.push_back(tx[i].GetHash()); |
|
|
|
|
|
|
|
|
|
|
|
uint32 merkleIndex = 0; |
|
|
|
uint32 j = 0; |
|
|
|
for (uint32 level = levels; level > 0; --level) |
|
|
|
for (uint32 size = tx.size(); size > 1; size = (size+1)/2) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Decrease before calculating because bottom level is transactions
|
|
|
|
for (uint32 i = 0; i < size; i += 2) |
|
|
|
branches /= 2; |
|
|
|
{ |
|
|
|
|
|
|
|
uint32 i2 = std::min(i+1, size-1); |
|
|
|
|
|
|
|
merkleTree.push_back(Crypto::SHA256D(Util::Join(merkleTree[j+i], merkleTree[j+i2]))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (uint32 branch = 0; branch < branches; ++branch) |
|
|
|
j += size; |
|
|
|
merkleTree.push_back(Crypto::SHA256D(Util::Join(merkleTree[merkleIndex++], merkleTree[merkleIndex++]))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Last hash is merkle root
|
|
|
|
// Last hash is merkle root
|
|
|
@ -39,12 +32,12 @@ namespace Bitcoin |
|
|
|
// Set coinbase tx hash
|
|
|
|
// Set coinbase tx hash
|
|
|
|
merkleTree[0] = tx[0].GetHash(); |
|
|
|
merkleTree[0] = tx[0].GetHash(); |
|
|
|
|
|
|
|
|
|
|
|
uint64 branches = merkleBranches; |
|
|
|
// Only rebuild left side of the tree
|
|
|
|
uint64 index = 0; |
|
|
|
uint32 j = 0; |
|
|
|
while (branches > 1) { |
|
|
|
for (uint32 size = tx.size(); size > 1; size = (size+1)/2) |
|
|
|
merkleTree[index+branches] = Crypto::SHA256D(Util::Join(merkleTree[index], merkleTree[index+1])); |
|
|
|
{ |
|
|
|
index += branches; |
|
|
|
merkleTree[size] = Crypto::SHA256D(Util::Join(merkleTree[j], merkleTree[j+1])); |
|
|
|
branches /= 2; |
|
|
|
j += size; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Last hash is merkle root
|
|
|
|
// Last hash is merkle root
|
|
|
|