bool get_block_hashing_blob(const Block& b, BinaryArray& ba) { if (!toBinaryArray(static_cast<const BlockHeader&>(b), ba)) { return false; } Hash treeRootHash = get_tx_tree_hash(b); ba.insert(ba.end(), treeRootHash.data, treeRootHash.data + 32); auto transactionCount = asBinaryArray(Tools::get_varint_data(b.transactionHashes.size() + 1)); ba.insert(ba.end(), transactionCount.begin(), transactionCount.end()); return true; }
const Crypto::Hash& CachedBlock::getBlockHash() const { if (!blockHash.is_initialized()) { BinaryArray blockBinaryArray = getBlockHashingBinaryArray(); if (BLOCK_MAJOR_VERSION_2 <= block.majorVersion) { const auto& parentBlock = getParentBlockHashingBinaryArray(false); blockBinaryArray.insert(blockBinaryArray.end(), parentBlock.begin(), parentBlock.end()); } blockHash = getObjectHash(blockBinaryArray); } return blockHash.get(); }
bool get_block_hash(const Block& b, Hash& res) { BinaryArray ba; if (!get_block_hashing_blob(b, ba)) { return false; } // The header of block version 1 differs from headers of blocks starting from v.2 if (BLOCK_MAJOR_VERSION_2 == b.majorVersion || BLOCK_MAJOR_VERSION_3 == b.majorVersion) { BinaryArray parent_blob; auto serializer = makeParentBlockSerializer(b, true, false); if (!toBinaryArray(serializer, parent_blob)) return false; ba.insert(ba.end(), parent_blob.begin(), parent_blob.end()); } return getObjectHash(ba, res); }