From fa02c08c93e5867b7ea07d79ca1c0917dcde88e0 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 28 Nov 2023 17:03:19 +0100 Subject: [PATCH] refactor: Use Txid in CMerkleBlock --- src/merkleblock.cpp | 4 ++-- src/merkleblock.h | 5 +++-- src/rpc/txoutproof.cpp | 6 +++--- src/test/fuzz/merkleblock.cpp | 4 ++-- src/test/merkleblock_tests.cpp | 10 +++++----- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/merkleblock.cpp b/src/merkleblock.cpp index 3ffe1465da5..c75f5c5e603 100644 --- a/src/merkleblock.cpp +++ b/src/merkleblock.cpp @@ -27,7 +27,7 @@ std::vector BytesToBits(const std::vector& bytes) return ret; } -CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set* txids) +CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set* txids) { header = block.GetBlockHeader(); @@ -39,7 +39,7 @@ CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std: for (unsigned int i = 0; i < block.vtx.size(); i++) { - const uint256& hash = block.vtx[i]->GetHash(); + const Txid& hash{block.vtx[i]->GetHash()}; if (txids && txids->count(hash)) { vMatch.push_back(true); } else if (filter && filter->IsRelevantAndUpdate(*block.vtx[i])) { diff --git a/src/merkleblock.h b/src/merkleblock.h index b546b17f525..12b41a581e3 100644 --- a/src/merkleblock.h +++ b/src/merkleblock.h @@ -11,6 +11,7 @@ #include #include +#include #include // Helper functions for serialization. @@ -144,7 +145,7 @@ public: CMerkleBlock(const CBlock& block, CBloomFilter& filter) : CMerkleBlock(block, &filter, nullptr) { } // Create from a CBlock, matching the txids in the set - CMerkleBlock(const CBlock& block, const std::set& txids) : CMerkleBlock(block, nullptr, &txids) { } + CMerkleBlock(const CBlock& block, const std::set& txids) : CMerkleBlock{block, nullptr, &txids} {} CMerkleBlock() {} @@ -152,7 +153,7 @@ public: private: // Combined constructor to consolidate code - CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set* txids); + CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set* txids); }; #endif // BITCOIN_MERKLEBLOCK_H diff --git a/src/rpc/txoutproof.cpp b/src/rpc/txoutproof.cpp index 46a22e93388..7958deb6770 100644 --- a/src/rpc/txoutproof.cpp +++ b/src/rpc/txoutproof.cpp @@ -41,13 +41,13 @@ static RPCHelpMan gettxoutproof() RPCExamples{""}, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - std::set setTxids; + std::set setTxids; UniValue txids = request.params[0].get_array(); if (txids.empty()) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter 'txids' cannot be empty"); } for (unsigned int idx = 0; idx < txids.size(); idx++) { - auto ret = setTxids.insert(ParseHashV(txids[idx], "txid")); + auto ret{setTxids.insert(Txid::FromUint256(ParseHashV(txids[idx], "txid")))}; if (!ret.second) { throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ") + txids[idx].get_str()); } @@ -69,7 +69,7 @@ static RPCHelpMan gettxoutproof() // Loop through txids and try to find which block they're in. Exit loop once a block is found. for (const auto& tx : setTxids) { - const Coin& coin = AccessByTxid(active_chainstate.CoinsTip(), Txid::FromUint256(tx)); + const Coin& coin{AccessByTxid(active_chainstate.CoinsTip(), tx)}; if (!coin.IsSpent()) { pblockindex = active_chainstate.m_chain[coin.nHeight]; break; diff --git a/src/test/fuzz/merkleblock.cpp b/src/test/fuzz/merkleblock.cpp index 15e8db75363..e03e628444e 100644 --- a/src/test/fuzz/merkleblock.cpp +++ b/src/test/fuzz/merkleblock.cpp @@ -29,13 +29,13 @@ FUZZ_TARGET(merkleblock) CMerkleBlock merkle_block; const std::optional opt_block = ConsumeDeserializable(fuzzed_data_provider, TX_WITH_WITNESS); CBloomFilter bloom_filter; - std::set txids; + std::set txids; if (opt_block && !opt_block->vtx.empty()) { if (fuzzed_data_provider.ConsumeBool()) { merkle_block = CMerkleBlock{*opt_block, bloom_filter}; } else if (fuzzed_data_provider.ConsumeBool()) { LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) { - txids.insert(ConsumeUInt256(fuzzed_data_provider)); + txids.insert(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider))); } merkle_block = CMerkleBlock{*opt_block, txids}; } diff --git a/src/test/merkleblock_tests.cpp b/src/test/merkleblock_tests.cpp index c7ec5fec15f..9c6008bdca0 100644 --- a/src/test/merkleblock_tests.cpp +++ b/src/test/merkleblock_tests.cpp @@ -21,13 +21,13 @@ BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found) { CBlock block = getBlock13b8a(); - std::set txids; + std::set txids; // Last txn in block. - uint256 txhash1 = uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"); + Txid txhash1{TxidFromString("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")}; // Second txn in block. - uint256 txhash2 = uint256S("0xf9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07"); + Txid txhash2{TxidFromString("0xf9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07")}; txids.insert(txhash1); txids.insert(txhash2); @@ -62,8 +62,8 @@ BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_not_found) { CBlock block = getBlock13b8a(); - std::set txids2; - txids2.insert(uint256S("0xc0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); + std::set txids2; + txids2.insert(TxidFromString("0xc0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); CMerkleBlock merkleBlock(block, txids2); BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());