Skip to content

Commit 8cf2137

Browse files
committed
Merge bitcoin/bitcoin#28958: refactor: Use Txid in CMerkleBlock
fa02c08 refactor: Use Txid in CMerkleBlock (MarcoFalke) Pull request description: This should also fix a gcc-13 compiler warning, see bitcoin/bitcoin#28922 (comment) ``` rpc/txoutproof.cpp: In lambda function: rpc/txoutproof.cpp:72:33: error: possibly dangling reference to a temporary [-Werror=dangling-reference] 72 | const Coin& coin = AccessByTxid(active_chainstate.CoinsTip(), Txid::FromUint256(tx)); | ^~~~ rpc/txoutproof.cpp:72:52: note: the temporary was destroyed at the end of the full expression ‘AccessByTxid((*(const CCoinsViewCache*)(&(& active_chainstate)->Chainstate::CoinsTip())), transaction_identifier<false>::FromUint256((* & tx)))’ 72 | const Coin& coin = AccessByTxid(active_chainstate.CoinsTip(), Txid::FromUint256(tx)); | ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1plus: all warnings being treated as errors ACKs for top commit: TheCharlatan: Re-ACK fa02c08 dergoegge: reACK fa02c08 Tree-SHA512: 2e6837b9d0c90bd6e9d766330e7086d68c6ec80bb27fe2cfc4702b251b00d91a79f8bfbc76d998cbcd90bee5317402cf617f61099eee96d94e7ac8f37ba7a642
2 parents 453c9ca + fa02c08 commit 8cf2137

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

src/merkleblock.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes)
2727
return ret;
2828
}
2929

30-
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids)
30+
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<Txid>* txids)
3131
{
3232
header = block.GetBlockHeader();
3333

@@ -39,7 +39,7 @@ CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std:
3939

4040
for (unsigned int i = 0; i < block.vtx.size(); i++)
4141
{
42-
const uint256& hash = block.vtx[i]->GetHash();
42+
const Txid& hash{block.vtx[i]->GetHash()};
4343
if (txids && txids->count(hash)) {
4444
vMatch.push_back(true);
4545
} else if (filter && filter->IsRelevantAndUpdate(*block.vtx[i])) {

src/merkleblock.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <serialize.h>
1212
#include <uint256.h>
1313

14+
#include <set>
1415
#include <vector>
1516

1617
// Helper functions for serialization.
@@ -144,15 +145,15 @@ class CMerkleBlock
144145
CMerkleBlock(const CBlock& block, CBloomFilter& filter) : CMerkleBlock(block, &filter, nullptr) { }
145146

146147
// Create from a CBlock, matching the txids in the set
147-
CMerkleBlock(const CBlock& block, const std::set<uint256>& txids) : CMerkleBlock(block, nullptr, &txids) { }
148+
CMerkleBlock(const CBlock& block, const std::set<Txid>& txids) : CMerkleBlock{block, nullptr, &txids} {}
148149

149150
CMerkleBlock() {}
150151

151152
SERIALIZE_METHODS(CMerkleBlock, obj) { READWRITE(obj.header, obj.txn); }
152153

153154
private:
154155
// Combined constructor to consolidate code
155-
CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids);
156+
CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<Txid>* txids);
156157
};
157158

158159
#endif // BITCOIN_MERKLEBLOCK_H

src/rpc/txoutproof.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ static RPCHelpMan gettxoutproof()
4141
RPCExamples{""},
4242
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
4343
{
44-
std::set<uint256> setTxids;
44+
std::set<Txid> setTxids;
4545
UniValue txids = request.params[0].get_array();
4646
if (txids.empty()) {
4747
throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter 'txids' cannot be empty");
4848
}
4949
for (unsigned int idx = 0; idx < txids.size(); idx++) {
50-
auto ret = setTxids.insert(ParseHashV(txids[idx], "txid"));
50+
auto ret{setTxids.insert(Txid::FromUint256(ParseHashV(txids[idx], "txid")))};
5151
if (!ret.second) {
5252
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ") + txids[idx].get_str());
5353
}
@@ -69,7 +69,7 @@ static RPCHelpMan gettxoutproof()
6969

7070
// Loop through txids and try to find which block they're in. Exit loop once a block is found.
7171
for (const auto& tx : setTxids) {
72-
const Coin& coin = AccessByTxid(active_chainstate.CoinsTip(), Txid::FromUint256(tx));
72+
const Coin& coin{AccessByTxid(active_chainstate.CoinsTip(), tx)};
7373
if (!coin.IsSpent()) {
7474
pblockindex = active_chainstate.m_chain[coin.nHeight];
7575
break;

src/test/fuzz/merkleblock.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ FUZZ_TARGET(merkleblock)
2929
CMerkleBlock merkle_block;
3030
const std::optional<CBlock> opt_block = ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS);
3131
CBloomFilter bloom_filter;
32-
std::set<uint256> txids;
32+
std::set<Txid> txids;
3333
if (opt_block && !opt_block->vtx.empty()) {
3434
if (fuzzed_data_provider.ConsumeBool()) {
3535
merkle_block = CMerkleBlock{*opt_block, bloom_filter};
3636
} else if (fuzzed_data_provider.ConsumeBool()) {
3737
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
38-
txids.insert(ConsumeUInt256(fuzzed_data_provider));
38+
txids.insert(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));
3939
}
4040
merkle_block = CMerkleBlock{*opt_block, txids};
4141
}

src/test/merkleblock_tests.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found)
2121
{
2222
CBlock block = getBlock13b8a();
2323

24-
std::set<uint256> txids;
24+
std::set<Txid> txids;
2525

2626
// Last txn in block.
27-
uint256 txhash1 = uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20");
27+
Txid txhash1{TxidFromString("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")};
2828

2929
// Second txn in block.
30-
uint256 txhash2 = uint256S("0xf9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07");
30+
Txid txhash2{TxidFromString("0xf9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07")};
3131

3232
txids.insert(txhash1);
3333
txids.insert(txhash2);
@@ -62,8 +62,8 @@ BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_not_found)
6262
{
6363
CBlock block = getBlock13b8a();
6464

65-
std::set<uint256> txids2;
66-
txids2.insert(uint256S("0xc0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"));
65+
std::set<Txid> txids2;
66+
txids2.insert(TxidFromString("0xc0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"));
6767
CMerkleBlock merkleBlock(block, txids2);
6868

6969
BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());

0 commit comments

Comments
 (0)