Bitcoin Core 31.99.0
P2P Digital Currency
transaction.h
Go to the documentation of this file.
1// Copyright (c) 2021-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_WALLET_TRANSACTION_H
6#define BITCOIN_WALLET_TRANSACTION_H
7
8#include <attributes.h>
9#include <consensus/amount.h>
11#include <tinyformat.h>
12#include <uint256.h>
13#include <util/check.h>
14#include <util/overloaded.h>
15#include <util/strencodings.h>
16#include <util/string.h>
17#include <wallet/types.h>
18
19#include <bitset>
20#include <cstdint>
21#include <map>
22#include <utility>
23#include <variant>
24#include <vector>
25
26namespace interfaces {
27class Chain;
28} // namespace interfaces
29
30namespace wallet {
36
37 explicit TxStateConfirmed(const uint256& block_hash, int height, int index) : confirmed_block_hash(block_hash), confirmed_block_height(height), position_in_block(index) {}
38 std::string toString() const { return strprintf("Confirmed (block=%s, height=%i, index=%i)", confirmed_block_hash.ToString(), confirmed_block_height, position_in_block); }
39};
40
43 std::string toString() const { return strprintf("InMempool"); }
44};
45
50
51 explicit TxStateBlockConflicted(const uint256& block_hash, int height) : conflicting_block_hash(block_hash), conflicting_block_height(height) {}
52 std::string toString() const { return strprintf("BlockConflicted (block=%s, height=%i)", conflicting_block_hash.ToString(), conflicting_block_height); }
53};
54
61
62 explicit TxStateInactive(bool abandoned = false) : abandoned(abandoned) {}
63 std::string toString() const { return strprintf("Inactive (abandoned=%i)", abandoned); }
64};
65
72 int index;
73
75 std::string toString() const { return strprintf("Unrecognized (block=%s, index=%i)", block_hash.ToString(), index); }
76};
77
79using TxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateBlockConflicted, TxStateInactive, TxStateUnrecognized>;
80
82using SyncTxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateInactive>;
83
86{
87 if (data.block_hash == uint256::ZERO) {
88 if (data.index == 0) return TxStateInactive{};
89 } else if (data.block_hash == uint256::ONE) {
90 if (data.index == -1) return TxStateInactive{/*abandoned=*/true};
91 } else if (data.index >= 0) {
92 return TxStateConfirmed{data.block_hash, /*height=*/-1, data.index};
93 } else if (data.index == -1) {
94 return TxStateBlockConflicted{data.block_hash, /*height=*/-1};
95 }
96 return data;
97}
98
100static inline uint256 TxStateSerializedBlockHash(const TxState& state)
101{
102 return std::visit(util::Overloaded{
103 [](const TxStateInactive& inactive) { return inactive.abandoned ? uint256::ONE : uint256::ZERO; },
104 [](const TxStateInMempool& in_mempool) { return uint256::ZERO; },
105 [](const TxStateConfirmed& confirmed) { return confirmed.confirmed_block_hash; },
106 [](const TxStateBlockConflicted& conflicted) { return conflicted.conflicting_block_hash; },
107 [](const TxStateUnrecognized& unrecognized) { return unrecognized.block_hash; }
108 }, state);
109}
110
112static inline int TxStateSerializedIndex(const TxState& state)
113{
114 return std::visit(util::Overloaded{
115 [](const TxStateInactive& inactive) { return inactive.abandoned ? -1 : 0; },
116 [](const TxStateInMempool& in_mempool) { return 0; },
117 [](const TxStateConfirmed& confirmed) { return confirmed.position_in_block; },
118 [](const TxStateBlockConflicted& conflicted) { return -1; },
119 [](const TxStateUnrecognized& unrecognized) { return unrecognized.index; }
120 }, state);
121}
122
124template<typename T>
125std::string TxStateString(const T& state)
126{
127 return std::visit([](const auto& s) { return s.toString(); }, state);
128}
129
134{
135 std::optional<CAmount> m_avoid_reuse_value;
136 std::optional<CAmount> m_all_value;
137 inline void Reset()
138 {
139 m_avoid_reuse_value.reset();
140 m_all_value.reset();
141 }
142 void Set(bool avoid_reuse, CAmount value)
143 {
144 if (avoid_reuse) {
145 m_avoid_reuse_value = value;
146 } else {
147 m_all_value = value;
148 }
149 }
150 CAmount Get(bool avoid_reuse)
151 {
152 if (avoid_reuse) {
153 Assert(m_avoid_reuse_value.has_value());
154 return m_avoid_reuse_value.value();
155 }
156 Assert(m_all_value.has_value());
157 return m_all_value.value();
158 }
159 bool IsCached(bool avoid_reuse)
160 {
161 if (avoid_reuse) return m_avoid_reuse_value.has_value();
162 return m_all_value.has_value();
163 }
164};
165
166
173{
174public:
175 template<typename Stream>
176 void Unserialize(Stream& s)
177 {
179 uint256 hashBlock;
180 std::vector<uint256> vMerkleBranch;
181 int nIndex;
182
183 s >> TX_WITH_WITNESS(tx) >> hashBlock >> vMerkleBranch >> nIndex;
184 }
185};
186
192{
193public:
194 // "from" and "message" are obsolete fields that could be set in
195 // the UI prior to 2011 (removed in commit 4d9b223)
196 // These fields are kept to avoid losing metadata.
197 std::optional<std::string> m_from;
198 std::optional<std::string> m_message;
199 // Comment strings provided by the user
200 std::optional<std::string> m_comment;
201 std::optional<std::string> m_comment_to;
202 std::optional<Txid> m_replaces_txid;
203 std::optional<Txid> m_replaced_by_txid;
204 // BIP 21 URI Messages
205 std::vector<std::string> m_messages;
206 // BIP 70 Payment Request (deprecated, field kept to preserve metadata from old wallets)
207 std::vector<std::string> m_payment_requests;
208 unsigned int nTimeReceived;
218 unsigned int nTimeSmart;
219 // Cached value for whether the transaction spends any inputs known to the wallet
220 mutable std::optional<bool> m_cached_from_me{std::nullopt};
221 int64_t nOrderPos;
222 std::multimap<int64_t, CWalletTx*>::const_iterator m_it_wtxOrdered;
223
224 // memory only
233 mutable bool m_is_cache_empty{true};
234 mutable bool fChangeCached;
236
238 {
239 Init();
240 }
241
242 void Init()
243 {
244 nTimeReceived = 0;
245 nTimeSmart = 0;
246 fChangeCached = false;
247 nChangeCached = 0;
248 nOrderPos = -1;
249 }
250
253
254 // Set of mempool transactions that conflict
255 // directly with the transaction, or that conflict
256 // with an ancestor transaction. This set will be
257 // empty if state is InMempool or Confirmed, but
258 // can be nonempty if state is Inactive or
259 // BlockConflicted.
260 std::set<Txid> mempool_conflicts;
261
262 // Track v3 mempool tx that spends from this tx
263 // so that we don't try to create another unconfirmed child
264 std::optional<Txid> truc_child_in_mempool;
265
266 template<typename Stream>
267 void Serialize(Stream& s) const
268 {
269 std::map<std::string, std::string> string_values;
270 if (m_from) string_values["from"] = *m_from;
271 if (m_message) string_values["message"] = *m_message;
272 if (m_comment) string_values["comment"] = *m_comment;
273 if (m_comment_to) string_values["to"] = *m_comment_to;
274 if (m_replaces_txid) string_values["replaces_txid"] = m_replaces_txid->ToString();
275 if (m_replaced_by_txid) string_values["replaced_by_txid"] = m_replaced_by_txid->ToString();
276 string_values["fromaccount"] = "";
277 if (nOrderPos != -1) string_values["n"] = util::ToString(nOrderPos);
278 if (nTimeSmart) string_values["timesmart"] = strprintf("%u", nTimeSmart);
279
280 std::vector<std::pair<std::string, std::string>> msgs_reqs;
281 msgs_reqs.reserve(m_messages.size() + m_payment_requests.size());
282 for (const std::string& msg : m_messages) {
283 msgs_reqs.emplace_back("Message", msg);
284 }
285 for (const std::string& req : m_payment_requests) {
286 msgs_reqs.emplace_back("PaymentRequest", req);
287 }
288
289 std::vector<uint8_t> dummy_vector1; // Used to be vMerkleBranch
290 std::vector<uint8_t> dummy_vector2; // Used to be vtxPrev
291 bool dummy_bool = false; // Used to be fFromMe, and fSpent
292 uint32_t dummy_int = 0; // Used to be fTimeReceivedIsTxTime
294 int serializedIndex = TxStateSerializedIndex(m_state);
295 s << TX_WITH_WITNESS(tx) << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << string_values << msgs_reqs << dummy_int << nTimeReceived << dummy_bool << dummy_bool;
296 }
297
298 template<typename Stream>
299 void Unserialize(Stream& s)
300 {
301 Init();
302
303 std::vector<uint256> dummy_vector1; // Used to be vMerkleBranch
304 std::vector<CMerkleTx> dummy_vector2; // Used to be vtxPrev
305 bool dummy_bool; // Used to be fFromMe, and fSpent
306 uint32_t dummy_int; // Used to be fTimeReceivedIsTxTime
307 uint256 serialized_block_hash;
308 int serializedIndex;
309 std::map<std::string, std::string> string_values;
310 std::vector<std::pair<std::string, std::string>> msgs_reqs;
311 s >> TX_WITH_WITNESS(tx) >> serialized_block_hash >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> string_values >> msgs_reqs >> dummy_int >> nTimeReceived >> dummy_bool >> dummy_bool;
312
313 m_state = TxStateInterpretSerialized({serialized_block_hash, serializedIndex});
314
315 string_values.erase("fromaccount");
316 string_values.erase("spent");
317 for (const auto& [key, value] : string_values) {
318 if (key == "n") nOrderPos = LocaleIndependentAtoi<int64_t>(value);
319 else if (key == "timesmart") nTimeSmart = LocaleIndependentAtoi<int64_t>(value);
320 else if (key == "from") m_from = value;
321 else if (key == "message") m_message = value;
322 else if (key == "comment") m_comment = value;
323 else if (key == "to") m_comment_to = value;
324 else if (key == "replaces_txid") m_replaces_txid = Txid::FromHex(value);
325 else if (key == "replaced_by_txid") m_replaced_by_txid = Txid::FromHex(value);
326 else {
327 throw std::runtime_error("Unexpected value in CWalletTx strings value map");
328 }
329 }
330
331 for (const auto& [type, data] : msgs_reqs) {
332 if (type == "Message") m_messages.emplace_back(data);
333 else if (type == "PaymentRequest") m_payment_requests.emplace_back(data);
334 else {
335 throw std::runtime_error("Unknown type in CWalletTx messages and requests vector");
336 }
337 }
338 }
339
341 {
342 tx = std::move(arg);
343 }
344
347 {
350 fChangeCached = false;
351 m_is_cache_empty = true;
352 m_cached_from_me = std::nullopt;
353 }
354
356 bool IsEquivalentTo(const CWalletTx& tx) const;
357
358 bool InMempool() const;
359
360 int64_t GetTxTime() const;
361
362 template<typename T> const T* state() const { return std::get_if<T>(&m_state); }
363 template<typename T> T* state() { return std::get_if<T>(&m_state); }
364
367 void updateState(interfaces::Chain& chain);
368
369 bool isAbandoned() const { return state<TxStateInactive>() && state<TxStateInactive>()->abandoned; }
370 bool isMempoolConflicted() const { return !mempool_conflicts.empty(); }
371 bool isBlockConflicted() const { return state<TxStateBlockConflicted>(); }
372 bool isInactive() const { return state<TxStateInactive>(); }
373 bool isUnconfirmed() const { return !isAbandoned() && !isBlockConflicted() && !isMempoolConflicted() && !isConfirmed(); }
374 bool isConfirmed() const { return state<TxStateConfirmed>(); }
375 const Txid& GetHash() const LIFETIMEBOUND { return tx->GetHash(); }
376 const Wtxid& GetWitnessHash() const LIFETIMEBOUND { return tx->GetWitnessHash(); }
377 bool IsCoinBase() const { return tx->IsCoinBase(); }
378
379private:
380 // Disable copying of CWalletTx objects to prevent bugs where instances get
381 // copied in and out of the mapWallet map, and fields are updated in the
382 // wrong copy.
383 CWalletTx(const CWalletTx&) = default;
384 CWalletTx& operator=(const CWalletTx&) = default;
385public:
386 // Instead have an explicit copy function
387 void CopyFrom(const CWalletTx&);
388};
389
391 bool operator()(const CWalletTx* a, const CWalletTx* b) const
392 {
393 return a->nOrderPos < b->nOrderPos;
394 }
395};
396
398{
399private:
402
403public:
404 WalletTXO(const CWalletTx& wtx, const CTxOut& output)
405 : m_wtx(wtx),
406 m_output(output)
407 {
408 Assume(std::ranges::find(wtx.tx->vout, output) != wtx.tx->vout.end());
409 }
410
411 const CWalletTx& GetWalletTx() const { return m_wtx; }
412
413 const CTxOut& GetTxOut() const { return m_output; }
414};
415} // namespace wallet
416
417#endif // BITCOIN_WALLET_TRANSACTION_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
#define LIFETIMEBOUND
Definition: attributes.h:16
#define Assert(val)
Identity function.
Definition: check.h:116
#define Assume(val)
Assume is the identity function.
Definition: check.h:128
An output of a transaction.
Definition: transaction.h:140
std::string ToString() const
Definition: uint256.cpp:21
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:118
static std::optional< transaction_identifier > FromHex(std::string_view hex)
256-bit opaque blob.
Definition: uint256.h:196
static const uint256 ONE
Definition: uint256.h:205
static const uint256 ZERO
Definition: uint256.h:204
Legacy class used for deserializing vtxPrev for backwards compatibility.
Definition: transaction.h:173
void Unserialize(Stream &s)
Definition: transaction.h:176
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:192
bool isConfirmed() const
Definition: transaction.h:374
bool isBlockConflicted() const
Definition: transaction.h:371
std::vector< std::string > m_messages
Definition: transaction.h:205
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:375
const T * state() const
Definition: transaction.h:362
bool IsEquivalentTo(const CWalletTx &tx) const
True if only scriptSigs are different.
Definition: transaction.cpp:12
std::set< Txid > mempool_conflicts
Definition: transaction.h:260
std::optional< Txid > m_replaces_txid
Definition: transaction.h:202
void updateState(interfaces::Chain &chain)
Update transaction state when attaching to a chain, filling in heights of conflicted and confirmed bl...
Definition: transaction.cpp:38
std::optional< std::string > m_comment_to
Definition: transaction.h:201
CAmount nChangeCached
Definition: transaction.h:235
void Serialize(Stream &s) const
Definition: transaction.h:267
std::optional< std::string > m_message
Definition: transaction.h:198
int64_t nOrderPos
position in ordered transaction list
Definition: transaction.h:221
bool isUnconfirmed() const
Definition: transaction.h:373
void CopyFrom(const CWalletTx &)
Definition: transaction.cpp:59
std::optional< bool > m_cached_from_me
Definition: transaction.h:220
std::optional< std::string > m_comment
Definition: transaction.h:200
unsigned int nTimeReceived
time received by this node
Definition: transaction.h:208
CTransactionRef tx
Definition: transaction.h:251
std::optional< std::string > m_from
Definition: transaction.h:197
std::optional< Txid > m_replaced_by_txid
Definition: transaction.h:203
void Unserialize(Stream &s)
Definition: transaction.h:299
void SetTx(CTransactionRef arg)
Definition: transaction.h:340
bool isMempoolConflicted() const
Definition: transaction.h:370
bool IsCoinBase() const
Definition: transaction.h:377
CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS]
Definition: transaction.h:226
CWalletTx & operator=(const CWalletTx &)=default
bool InMempool() const
Definition: transaction.cpp:27
bool isAbandoned() const
Definition: transaction.h:369
std::optional< Txid > truc_child_in_mempool
Definition: transaction.h:264
bool isInactive() const
Definition: transaction.h:372
std::vector< std::string > m_payment_requests
Definition: transaction.h:207
int64_t GetTxTime() const
Definition: transaction.cpp:32
CWalletTx(CTransactionRef tx, const TxState &state)
Definition: transaction.h:237
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
Definition: transaction.h:233
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
Definition: transaction.h:222
CWalletTx(const CWalletTx &)=default
unsigned int nTimeSmart
Stable timestamp that never changes, and reflects the order a transaction was added to the wallet.
Definition: transaction.h:218
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
Definition: transaction.h:376
void MarkDirty()
make sure balances are recalculated
Definition: transaction.h:346
WalletTXO(const CWalletTx &wtx, const CTxOut &output)
Definition: transaction.h:404
const CTxOut & m_output
Definition: transaction.h:401
const CWalletTx & m_wtx
Definition: transaction.h:400
const CWalletTx & GetWalletTx() const
Definition: transaction.h:411
const CTxOut & GetTxOut() const
Definition: transaction.h:413
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
#define T(expected, seed, data)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:249
std::variant< TxStateConfirmed, TxStateInMempool, TxStateInactive > SyncTxState
Subset of states transaction sync logic is implemented to handle.
Definition: transaction.h:82
std::variant< TxStateConfirmed, TxStateInMempool, TxStateBlockConflicted, TxStateInactive, TxStateUnrecognized > TxState
All possible CWalletTx states.
Definition: transaction.h:79
static int TxStateSerializedIndex(const TxState &state)
Get TxState serialized block index. Inverse of TxStateInterpretSerialized.
Definition: transaction.h:112
static TxState TxStateInterpretSerialized(TxStateUnrecognized data)
Try to interpret deserialized TxStateUnrecognized data as a recognized state.
Definition: transaction.h:85
static uint256 TxStateSerializedBlockHash(const TxState &state)
Get TxState serialized block hash. Inverse of TxStateInterpretSerialized.
Definition: transaction.h:100
std::string TxStateString(const T &state)
Return TxState or SyncTxState as a string for logging or debugging.
Definition: transaction.h:125
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:180
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
Overloaded helper for std::visit.
Definition: overloaded.h:16
Cachable amount subdivided into avoid reuse and all balances.
Definition: transaction.h:134
std::optional< CAmount > m_avoid_reuse_value
Definition: transaction.h:135
std::optional< CAmount > m_all_value
Definition: transaction.h:136
bool IsCached(bool avoid_reuse)
Definition: transaction.h:159
CAmount Get(bool avoid_reuse)
Definition: transaction.h:150
void Set(bool avoid_reuse, CAmount value)
Definition: transaction.h:142
State of rejected transaction that conflicts with a confirmed block.
Definition: transaction.h:47
std::string toString() const
Definition: transaction.h:52
TxStateBlockConflicted(const uint256 &block_hash, int height)
Definition: transaction.h:51
State of transaction confirmed in a block.
Definition: transaction.h:32
std::string toString() const
Definition: transaction.h:38
TxStateConfirmed(const uint256 &block_hash, int height, int index)
Definition: transaction.h:37
State of transaction added to mempool.
Definition: transaction.h:42
std::string toString() const
Definition: transaction.h:43
State of transaction not confirmed or conflicting with a known block and not in the mempool.
Definition: transaction.h:59
std::string toString() const
Definition: transaction.h:63
TxStateInactive(bool abandoned=false)
Definition: transaction.h:62
State of transaction loaded in an unrecognized state with unexpected hash or index values.
Definition: transaction.h:70
TxStateUnrecognized(const uint256 &block_hash, int index)
Definition: transaction.h:74
std::string toString() const
Definition: transaction.h:75
bool operator()(const CWalletTx *a, const CWalletTx *b) const
Definition: transaction.h:391
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
is a home for public enum and struct type definitions that are used by internally by wallet code,...