2!=0)
merkle.push_back(merkle.back());
//List size is now even.
assert(merkle.size()% 2==0);
//New hash list.
bc::hash_digest_list new_merkle;
//Loop through hashes 2 atatime.
for(auto it=merkle.begin();it!=merkle.end();it+=2){
//Join both current hashes together(concatenate).
bc::data_chunk concat_data(bc::hash_size*2);
auto concat=bc::make_serializer(concat_data.begin());
concat.write_hash(*it);
concat.write_hash(*(it+1));
assert(concat.iterator()==concat_data.end());
//Hash both of the hashes.
bc::hash_digest new_root=bc::bitcoin_hash(concat_data);
//Add this to the new list.
new_merkle.push_back(new_root);
//This is the new list.
merkle=new_merkle;
//DEBUG output------------------------------------
std::cout<<"Current merkle hash list:"< for(const auto&hash:merkle) std::cout<<""< std::cout< //------------------------------------------------- //Finally we end up withasingle item. return merkle[0]; int main(){ //Replace these hashes with ones fromablock to reproduce the same merkle root. bc::hash_digest_list tx_hashes{ bc::decode_hash("0000000000000000000000000000000000000000000000000000000000000000"), bc::decode_hash("0000000000000000000000000000000000000000000000000000000000000011"), bc::decode_hash("0000000000000000000000000000000000000000000000000000000000000022"), }; const bc::hash_digest merkle_root=create_merkle(tx_hashes); std::cout<<"Result:"< return 0; 例2展示了编译以及运行上述代码后的结果。 例2 编译以及运行构造Merkle树代码。 $#Compile the merkle.cpp code $g++-o merkle merkle.cpp$(pkg-config--cflags--libs libbitcoin) $#Run the merkle executable $./merkle Current merkle hash list: 32650049a0418e4380db0af81788635d8b65424d397170b8499cdc28c4d27006 30861db96905c8dc8b99398ca1cd5bd5b84ac3264a4e1b3e65afa1bcee7540c4 Current merkle hash list: d47780c084bad3830bcdaf6eace035e4c6cbf646d103795d22104fb105014ba3 Result:d47780c084bad3830bcdaf6eace035e4c6cbf646d103795d22104fb105014ba3 Merkle树的高效率随
更多内容加载中...请稍候...
若您看到此段落,代表章节内容加载失败,请关闭浏览器的阅读模式、畅读模式、小说模式,以及关闭广告屏蔽功能,或复制网址到其他浏览器阅读!