Skip to content

Commit faec889

Browse files
author
MarcoFalke
committed
refactor: Add LIFETIMEBOUND to all (w)txid getters
Then, use the compiler warnings to create copies only where needed. Also, fix iwyu includes while touching the includes.
1 parent e789b30 commit faec889

File tree

6 files changed

+19
-17
lines changed

6 files changed

+19
-17
lines changed

src/primitives/transaction.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#ifndef BITCOIN_PRIMITIVES_TRANSACTION_H
77
#define BITCOIN_PRIMITIVES_TRANSACTION_H
88

9+
#include <attributes.h>
910
#include <consensus/amount.h>
10-
#include <prevector.h>
1111
#include <script/script.h>
1212
#include <serialize.h>
1313
#include <uint256.h>
@@ -335,8 +335,8 @@ class CTransaction
335335
return vin.empty() && vout.empty();
336336
}
337337

338-
const Txid& GetHash() const { return hash; }
339-
const Wtxid& GetWitnessHash() const { return m_witness_hash; };
338+
const Txid& GetHash() const LIFETIMEBOUND { return hash; }
339+
const Wtxid& GetWitnessHash() const LIFETIMEBOUND { return m_witness_hash; };
340340

341341
// Return sum of txouts.
342342
CAmount GetValueOut() const;
@@ -433,7 +433,7 @@ class GenTxid
433433
static GenTxid Txid(const uint256& hash) { return GenTxid{false, hash}; }
434434
static GenTxid Wtxid(const uint256& hash) { return GenTxid{true, hash}; }
435435
bool IsWtxid() const { return m_is_wtxid; }
436-
const uint256& GetHash() const { return m_hash; }
436+
const uint256& GetHash() const LIFETIMEBOUND { return m_hash; }
437437
friend bool operator==(const GenTxid& a, const GenTxid& b) { return a.m_is_wtxid == b.m_is_wtxid && a.m_hash == b.m_hash; }
438438
friend bool operator<(const GenTxid& a, const GenTxid& b) { return std::tie(a.m_is_wtxid, a.m_hash) < std::tie(b.m_is_wtxid, b.m_hash); }
439439
};

src/test/fuzz/tx_pool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ FUZZ_TARGET(tx_pool, .init = initialize_tx_pool)
343343
tx_pool.RollingFeeUpdate();
344344
}
345345
if (fuzzed_data_provider.ConsumeBool()) {
346-
const auto& txid = fuzzed_data_provider.ConsumeBool() ?
346+
const auto txid = fuzzed_data_provider.ConsumeBool() ?
347347
mut_tx.GetHash().ToUint256() :
348348
PickValue(fuzzed_data_provider, txids);
349349
const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);

src/util/transaction_identifier.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
22
#define BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
33

4+
#include <attributes.h>
45
#include <uint256.h>
56
#include <util/types.h>
67

@@ -35,7 +36,7 @@ class transaction_identifier
3536
template <typename Other>
3637
bool operator<(const Other& other) const { return Compare(other) < 0; }
3738

38-
uint256 ToUint256() const { return m_wrapped; }
39+
const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }
3940
static transaction_identifier FromUint256(const uint256& id) { return {id}; }
4041

4142
/** Wrapped `uint256` methods. */
@@ -56,7 +57,7 @@ class transaction_identifier
5657
* TODO: This should be removed once the majority of the code has switched
5758
* to using the Txid and Wtxid types. Until then it makes for a smoother
5859
* transition to allow this conversion. */
59-
operator uint256() const { return m_wrapped; }
60+
operator const uint256&() const LIFETIMEBOUND { return m_wrapped; }
6061
};
6162

6263
/** Txid commits to all transaction fields except the witness. */

src/wallet/test/group_outputs_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void addCoin(CoinsResult& coins,
4040
tx.vout[0].nValue = nValue;
4141
tx.vout[0].scriptPubKey = GetScriptForDestination(dest);
4242

43-
const uint256& txid = tx.GetHash();
43+
const auto txid{tx.GetHash().ToUint256()};
4444
LOCK(wallet.cs_wallet);
4545
auto ret = wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid), std::forward_as_tuple(MakeTransactionRef(std::move(tx)), TxStateInactive{}));
4646
assert(ret.second);

src/wallet/test/wallet_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup)
963963
mtx.vin.clear();
964964
mtx.vin.emplace_back(tx_id_to_spend, 0);
965965
wallet.transactionAddedToMempool(MakeTransactionRef(mtx));
966-
const uint256& good_tx_id = mtx.GetHash();
966+
const auto good_tx_id{mtx.GetHash().ToUint256()};
967967

968968
{
969969
// Verify balance update for the new tx and the old one

src/wallet/transaction.h

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
#ifndef BITCOIN_WALLET_TRANSACTION_H
66
#define BITCOIN_WALLET_TRANSACTION_H
77

8-
#include <bitset>
9-
#include <cstdint>
8+
#include <attributes.h>
109
#include <consensus/amount.h>
1110
#include <primitives/transaction.h>
12-
#include <serialize.h>
13-
#include <wallet/types.h>
14-
#include <threadsafety.h>
1511
#include <tinyformat.h>
12+
#include <uint256.h>
1613
#include <util/overloaded.h>
1714
#include <util/strencodings.h>
1815
#include <util/string.h>
16+
#include <wallet/types.h>
1917

20-
#include <list>
18+
#include <bitset>
19+
#include <cstdint>
20+
#include <map>
21+
#include <utility>
2122
#include <variant>
2223
#include <vector>
2324

@@ -330,8 +331,8 @@ class CWalletTx
330331
bool isInactive() const { return state<TxStateInactive>(); }
331332
bool isUnconfirmed() const { return !isAbandoned() && !isConflicted() && !isConfirmed(); }
332333
bool isConfirmed() const { return state<TxStateConfirmed>(); }
333-
const Txid& GetHash() const { return tx->GetHash(); }
334-
const Wtxid& GetWitnessHash() const { return tx->GetWitnessHash(); }
334+
const Txid& GetHash() const LIFETIMEBOUND { return tx->GetHash(); }
335+
const Wtxid& GetWitnessHash() const LIFETIMEBOUND { return tx->GetWitnessHash(); }
335336
bool IsCoinBase() const { return tx->IsCoinBase(); }
336337

337338
private:

0 commit comments

Comments
 (0)