From 1bae08358703448ba3028508c687d6a0fd0a8d1c Mon Sep 17 00:00:00 2001 From: SChernykh Date: Thu, 17 Jun 2021 10:26:17 +0200 Subject: [PATCH] Fixed CalculateMerkleTreeHash --- src/base/tools/cryptonote/BlockTemplate.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/base/tools/cryptonote/BlockTemplate.cpp b/src/base/tools/cryptonote/BlockTemplate.cpp index a9b29666e..39bc8b819 100644 --- a/src/base/tools/cryptonote/BlockTemplate.cpp +++ b/src/base/tools/cryptonote/BlockTemplate.cpp @@ -158,28 +158,29 @@ void BlockTemplate::CalculateMerkleTreeHash() { miner_tx_merkle_tree_branch.clear(); + const uint64_t count = num_hashes + 1; uint8_t* h = hashes.data(); - if (num_hashes == 1) { + if (count == 1) { memcpy(root_hash, h, HASH_SIZE); } - else if (num_hashes == 2) { + else if (count == 2) { miner_tx_merkle_tree_branch.insert(miner_tx_merkle_tree_branch.end(), h + HASH_SIZE, h + HASH_SIZE * 2); keccak(h, HASH_SIZE * 2, root_hash, HASH_SIZE); } else { size_t i, j, cnt; - for (i = 0, cnt = 1; cnt <= num_hashes; ++i, cnt <<= 1) {} + for (i = 0, cnt = 1; cnt <= count; ++i, cnt <<= 1) {} cnt >>= 1; miner_tx_merkle_tree_branch.reserve(HASH_SIZE * (i - 1)); Buffer ints(cnt * HASH_SIZE); - memcpy(ints.data(), h, (cnt * 2 - num_hashes) * HASH_SIZE); + memcpy(ints.data(), h, (cnt * 2 - count) * HASH_SIZE); - for (i = cnt * 2 - num_hashes, j = cnt * 2 - num_hashes; j < cnt; i += 2, ++j) { + for (i = cnt * 2 - count, j = cnt * 2 - count; j < cnt; i += 2, ++j) { if (i == 0) { miner_tx_merkle_tree_branch.insert(miner_tx_merkle_tree_branch.end(), h + HASH_SIZE, h + HASH_SIZE * 2); }