Branch data Line data Source code
1 : : // Copyright (c) 2021-2022 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>
10 : : #include <primitives/transaction.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 : :
26 : : namespace interfaces {
27 : : class Chain;
28 : : } // namespace interfaces
29 : :
30 : : namespace wallet {
31 : : //! State of transaction confirmed in a block.
32 : : struct TxStateConfirmed {
33 : : uint256 confirmed_block_hash;
34 : : int confirmed_block_height;
35 : : int position_in_block;
36 : :
37 [ + - # # ]: 141409 : explicit TxStateConfirmed(const uint256& block_hash, int height, int index) : confirmed_block_hash(block_hash), confirmed_block_height(height), position_in_block(index) {}
[ + - + - ]
38 [ + - ]: 40372 : 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 : :
41 : : //! State of transaction added to mempool.
42 : : struct TxStateInMempool {
43 : 3498 : std::string toString() const { return strprintf("InMempool"); }
44 : : };
45 : :
46 : : //! State of rejected transaction that conflicts with a confirmed block.
47 : : struct TxStateBlockConflicted {
48 : : uint256 conflicting_block_hash;
49 : : int conflicting_block_height;
50 : :
51 : 258 : explicit TxStateBlockConflicted(const uint256& block_hash, int height) : conflicting_block_hash(block_hash), conflicting_block_height(height) {}
52 [ # # ]: 0 : std::string toString() const { return strprintf("BlockConflicted (block=%s, height=%i)", conflicting_block_hash.ToString(), conflicting_block_height); }
53 : : };
54 : :
55 : : //! State of transaction not confirmed or conflicting with a known block and
56 : : //! not in the mempool. May conflict with the mempool, or with an unknown block,
57 : : //! or be abandoned, never broadcast, or rejected from the mempool for another
58 : : //! reason.
59 : : struct TxStateInactive {
60 : : bool abandoned;
61 : :
62 [ + + # # : 131532 : explicit TxStateInactive(bool abandoned = false) : abandoned(abandoned) {}
# # # # #
# # # ][ +
- + - - +
+ - + - -
+ ]
[ + - + - ]
63 : 2069 : std::string toString() const { return strprintf("Inactive (abandoned=%i)", abandoned); }
64 : : };
65 : :
66 : : //! State of transaction loaded in an unrecognized state with unexpected hash or
67 : : //! index values. Treated as inactive (with serialized hash and index values
68 : : //! preserved) by default, but may enter another state if transaction is added
69 : : //! to the mempool, or confirmed, or abandoned, or found conflicting.
70 : : struct TxStateUnrecognized {
71 : : uint256 block_hash;
72 : : int index;
73 : :
74 : 8252 : TxStateUnrecognized(const uint256& block_hash, int index) : block_hash(block_hash), index(index) {}
75 [ # # ]: 0 : std::string toString() const { return strprintf("Unrecognized (block=%s, index=%i)", block_hash.ToString(), index); }
76 : : };
77 : :
78 : : //! All possible CWalletTx states
79 : : using TxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateBlockConflicted, TxStateInactive, TxStateUnrecognized>;
80 : :
81 : : //! Subset of states transaction sync logic is implemented to handle.
82 : : using SyncTxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateInactive>;
83 : :
84 : : //! Try to interpret deserialized TxStateUnrecognized data as a recognized state.
85 : 8252 : static inline TxState TxStateInterpretSerialized(TxStateUnrecognized data)
86 : : {
87 [ + + ]: 8252 : if (data.block_hash == uint256::ZERO) {
88 [ + + ]: 80 : if (data.index == 0) return TxStateInactive{};
89 [ + + ]: 8172 : } else if (data.block_hash == uint256::ONE) {
90 [ + + ]: 125 : if (data.index == -1) return TxStateInactive{/*abandoned=*/true};
91 [ + + ]: 8047 : } else if (data.index >= 0) {
92 : 7947 : return TxStateConfirmed{data.block_hash, /*height=*/-1, data.index};
93 [ + + ]: 100 : } else if (data.index == -1) {
94 : 97 : return TxStateBlockConflicted{data.block_hash, /*height=*/-1};
95 : : }
96 : 11 : return data;
97 : : }
98 : :
99 : : //! Get TxState serialized block hash. Inverse of TxStateInterpretSerialized.
100 : 27113 : static inline uint256 TxStateSerializedBlockHash(const TxState& state)
101 : : {
102 [ + + ]: 27113 : return std::visit(util::Overloaded{
103 [ + + ]: 4895 : [](const TxStateInactive& inactive) { return inactive.abandoned ? uint256::ONE : uint256::ZERO; },
104 : 5111 : [](const TxStateInMempool& in_mempool) { return uint256::ZERO; },
105 : 21618 : [](const TxStateConfirmed& confirmed) { return confirmed.confirmed_block_hash; },
106 : 164 : [](const TxStateBlockConflicted& conflicted) { return conflicted.conflicting_block_hash; },
107 : 11 : [](const TxStateUnrecognized& unrecognized) { return unrecognized.block_hash; }
108 : : }, state);
109 : : }
110 : :
111 : : //! Get TxState serialized block index. Inverse of TxStateInterpretSerialized.
112 : 27113 : static inline int TxStateSerializedIndex(const TxState& state)
113 : : {
114 [ + + ]: 27113 : return std::visit(util::Overloaded{
115 [ + + ]: 3241 : [](const TxStateInactive& inactive) { return inactive.abandoned ? -1 : 0; },
116 : : [](const TxStateInMempool& in_mempool) { return 0; },
117 : 21618 : [](const TxStateConfirmed& confirmed) { return confirmed.position_in_block; },
118 : : [](const TxStateBlockConflicted& conflicted) { return -1; },
119 : 11 : [](const TxStateUnrecognized& unrecognized) { return unrecognized.index; }
120 : : }, state);
121 : : }
122 : :
123 : : //! Return TxState or SyncTxState as a string for logging or debugging.
124 : : template<typename T>
125 : 25753 : std::string TxStateString(const T& state)
126 : : {
127 : 25753 : return std::visit([](const auto& s) { return s.toString(); }, state);
128 : : }
129 : :
130 : : /**
131 : : * Cachable amount subdivided into avoid reuse and all balances
132 : : */
133 : : struct CachableAmount
134 : : {
135 : : std::optional<CAmount> m_avoid_reuse_value;
136 : : std::optional<CAmount> m_all_value;
137 : 122296 : inline void Reset()
138 : : {
139 : 244592 : m_avoid_reuse_value.reset();
140 [ + + + + ]: 122296 : m_all_value.reset();
141 : : }
142 : 11170 : void Set(bool avoid_reuse, CAmount value)
143 : : {
144 [ - + ]: 11170 : if (avoid_reuse) {
145 : 0 : m_avoid_reuse_value = value;
146 : : } else {
147 : 11170 : m_all_value = value;
148 : : }
149 : : }
150 : 256030 : CAmount Get(bool avoid_reuse)
151 : : {
152 [ - + ]: 256030 : if (avoid_reuse) {
153 : 0 : Assert(m_avoid_reuse_value.has_value());
154 [ # # ]: 0 : return m_avoid_reuse_value.value();
155 : : }
156 : 256030 : Assert(m_all_value.has_value());
157 [ + - ]: 256030 : return m_all_value.value();
158 : : }
159 : 256030 : bool IsCached(bool avoid_reuse)
160 : : {
161 [ - + ]: 256030 : if (avoid_reuse) return m_avoid_reuse_value.has_value();
162 : 256030 : return m_all_value.has_value();
163 : : }
164 : : };
165 : :
166 : :
167 : : typedef std::map<std::string, std::string> mapValue_t;
168 : :
169 : :
170 : : /** Legacy class used for deserializing vtxPrev for backwards compatibility.
171 : : * vtxPrev was removed in commit 93a18a3650292afbb441a47d1fa1b94aeb0164e3,
172 : : * but old wallet.dat files may still contain vtxPrev vectors of CMerkleTxs.
173 : : * These need to get deserialized for field alignment when deserializing
174 : : * a CWalletTx, but the deserialized values are discarded.**/
175 : : class CMerkleTx
176 : : {
177 : : public:
178 : : template<typename Stream>
179 : 0 : void Unserialize(Stream& s)
180 : : {
181 : 0 : CTransactionRef tx;
182 : 0 : uint256 hashBlock;
183 [ # # ]: 0 : std::vector<uint256> vMerkleBranch;
184 : : int nIndex;
185 : :
186 [ # # # # : 0 : s >> TX_WITH_WITNESS(tx) >> hashBlock >> vMerkleBranch >> nIndex;
# # # # ]
187 [ # # ]: 0 : }
188 : : };
189 : :
190 : : /**
191 : : * A transaction with a bunch of additional info that only the owner cares about.
192 : : * It includes any unrecorded transactions needed to link it back to the block chain.
193 : : */
194 : : class CWalletTx
195 : : {
196 : : public:
197 : : /**
198 : : * Key/value map with information about the transaction.
199 : : *
200 : : * The following keys can be read and written through the map and are
201 : : * serialized in the wallet database:
202 : : *
203 : : * "comment", "to" - comment strings provided to sendtoaddress,
204 : : * and sendmany wallet RPCs
205 : : * "replaces_txid" - txid (as HexStr) of transaction replaced by
206 : : * bumpfee on transaction created by bumpfee
207 : : * "replaced_by_txid" - txid (as HexStr) of transaction created by
208 : : * bumpfee on transaction replaced by bumpfee
209 : : * "from", "message" - obsolete fields that could be set in UI prior to
210 : : * 2011 (removed in commit 4d9b223)
211 : : *
212 : : * The following keys are serialized in the wallet database, but shouldn't
213 : : * be read or written through the map (they will be temporarily added and
214 : : * removed from the map during serialization):
215 : : *
216 : : * "fromaccount" - serialized strFromAccount value
217 : : * "n" - serialized nOrderPos value
218 : : * "timesmart" - serialized nTimeSmart value
219 : : * "spent" - serialized vfSpent value that existed prior to
220 : : * 2014 (removed in commit 93a18a3)
221 : : */
222 : : mapValue_t mapValue;
223 : : std::vector<std::pair<std::string, std::string> > vOrderForm;
224 : : unsigned int nTimeReceived; //!< time received by this node
225 : : /**
226 : : * Stable timestamp that never changes, and reflects the order a transaction
227 : : * was added to the wallet. Timestamp is based on the block time for a
228 : : * transaction added as part of a block, or else the time when the
229 : : * transaction was received if it wasn't part of a block, with the timestamp
230 : : * adjusted in both cases so timestamp order matches the order transactions
231 : : * were added to the wallet. More details can be found in
232 : : * CWallet::ComputeTimeSmart().
233 : : */
234 : : unsigned int nTimeSmart;
235 : : int64_t nOrderPos; //!< position in ordered transaction list
236 : : std::multimap<int64_t, CWalletTx*>::const_iterator m_it_wtxOrdered;
237 : :
238 : : // memory only
239 : : enum AmountType { DEBIT, CREDIT, AMOUNTTYPE_ENUM_ELEMENTS };
240 : : mutable CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS];
241 : : /**
242 : : * This flag is true if all m_amounts caches are empty. This is particularly
243 : : * useful in places where MarkDirty is conditionally called and the
244 : : * condition can be expensive and thus can be skipped if the flag is true.
245 : : * See MarkDestinationsDirty.
246 : : */
247 : : mutable bool m_is_cache_empty{true};
248 : : mutable bool fChangeCached;
249 : : mutable CAmount nChangeCached;
250 : :
251 : 150311 : CWalletTx(CTransactionRef tx, const TxState& state) : tx(std::move(tx)), m_state(state)
252 : : {
253 : 150311 : Init();
254 : 150311 : }
255 : :
256 : 158538 : void Init()
257 : : {
258 : 158538 : mapValue.clear();
259 : 158538 : vOrderForm.clear();
260 : 158538 : nTimeReceived = 0;
261 : 158538 : nTimeSmart = 0;
262 : 158538 : fChangeCached = false;
263 : 158538 : nChangeCached = 0;
264 : 158538 : nOrderPos = -1;
265 : 158538 : }
266 : :
267 : : CTransactionRef tx;
268 : : TxState m_state;
269 : :
270 : : // Set of mempool transactions that conflict
271 : : // directly with the transaction, or that conflict
272 : : // with an ancestor transaction. This set will be
273 : : // empty if state is InMempool or Confirmed, but
274 : : // can be nonempty if state is Inactive or
275 : : // BlockConflicted.
276 : : std::set<Txid> mempool_conflicts;
277 : :
278 : : // Track v3 mempool tx that spends from this tx
279 : : // so that we don't try to create another unconfirmed child
280 : : std::optional<Txid> truc_child_in_mempool;
281 : :
282 : : template<typename Stream>
283 : 24056 : void Serialize(Stream& s) const
284 : : {
285 : 24056 : mapValue_t mapValueCopy = mapValue;
286 : :
287 [ + - + - : 24056 : mapValueCopy["fromaccount"] = "";
+ - ]
288 [ + - ]: 24056 : if (nOrderPos != -1) {
289 [ + - + - : 24056 : mapValueCopy["n"] = util::ToString(nOrderPos);
+ - ]
290 : : }
291 [ + - ]: 24056 : if (nTimeSmart) {
292 [ + - + - : 24056 : mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart);
+ - ]
293 : : }
294 : :
295 : 24056 : std::vector<uint8_t> dummy_vector1; //!< Used to be vMerkleBranch
296 : 24056 : std::vector<uint8_t> dummy_vector2; //!< Used to be vtxPrev
297 : 24056 : bool dummy_bool = false; //!< Used to be fFromMe, and fSpent
298 : 24056 : uint32_t dummy_int = 0; // Used to be fTimeReceivedIsTxTime
299 [ + - ]: 24056 : uint256 serializedHash = TxStateSerializedBlockHash(m_state);
300 : 24056 : int serializedIndex = TxStateSerializedIndex(m_state);
301 [ + - + - : 48112 : s << TX_WITH_WITNESS(tx) << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << mapValueCopy << vOrderForm << dummy_int << nTimeReceived << dummy_bool << dummy_bool;
+ - + - +
- + - + -
+ - + - +
- + - ]
302 : 24056 : }
303 : :
304 : : template<typename Stream>
305 : 8227 : void Unserialize(Stream& s)
306 : : {
307 : 8227 : Init();
308 : :
309 : 8227 : std::vector<uint256> dummy_vector1; //!< Used to be vMerkleBranch
310 : 8227 : std::vector<CMerkleTx> dummy_vector2; //!< Used to be vtxPrev
311 : : bool dummy_bool; //! Used to be fFromMe, and fSpent
312 : : uint32_t dummy_int; // Used to be fTimeReceivedIsTxTime
313 : 8227 : uint256 serialized_block_hash;
314 : : int serializedIndex;
315 [ + - + - : 8227 : s >> TX_WITH_WITNESS(tx) >> serialized_block_hash >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> mapValue >> vOrderForm >> dummy_int >> nTimeReceived >> dummy_bool >> dummy_bool;
+ - + - +
- + - + -
+ - + - +
- + - ]
316 : :
317 : 8227 : m_state = TxStateInterpretSerialized({serialized_block_hash, serializedIndex});
318 : :
319 [ + - ]: 16454 : const auto it_op = mapValue.find("n");
320 [ + - - + : 8227 : nOrderPos = (it_op != mapValue.end()) ? LocaleIndependentAtoi<int64_t>(it_op->second) : -1;
+ - ]
321 [ + - ]: 16454 : const auto it_ts = mapValue.find("timesmart");
322 [ + - - + : 8227 : nTimeSmart = (it_ts != mapValue.end()) ? static_cast<unsigned int>(LocaleIndependentAtoi<int64_t>(it_ts->second)) : 0;
+ - ]
323 : :
324 [ + - ]: 16454 : mapValue.erase("fromaccount");
325 [ + - ]: 16454 : mapValue.erase("spent");
326 [ + - ]: 16454 : mapValue.erase("n");
327 [ + - ]: 16454 : mapValue.erase("timesmart");
328 : 8227 : }
329 : :
330 : 14 : void SetTx(CTransactionRef arg)
331 : : {
332 [ - + - - ]: 14 : tx = std::move(arg);
333 : : }
334 : :
335 : : //! make sure balances are recalculated
336 : 61148 : void MarkDirty()
337 : : {
338 [ - + ]: 61148 : m_amounts[DEBIT].Reset();
339 [ - + ]: 61148 : m_amounts[CREDIT].Reset();
340 : 61148 : fChangeCached = false;
341 : 61148 : m_is_cache_empty = true;
342 : 61148 : }
343 : :
344 : : /** True if only scriptSigs are different */
345 : : bool IsEquivalentTo(const CWalletTx& tx) const;
346 : :
347 : : bool InMempool() const;
348 : :
349 : : int64_t GetTxTime() const;
350 : :
351 [ + + # # : 2748716 : template<typename T> const T* state() const { return std::get_if<T>(&m_state); }
# # # # #
# # # # #
# # ]
[ # # # # ]
[ + + + +
- + - + +
+ - + + +
+ + ]
352 [ + + + + : 31485 : template<typename T> T* state() { return std::get_if<T>(&m_state); }
# # # # #
# # # #
# ][ + + +
+ + + + -
+ - + + -
+ ]
353 : :
354 : : //! Update transaction state when attaching to a chain, filling in heights
355 : : //! of conflicted and confirmed blocks
356 : : void updateState(interfaces::Chain& chain);
357 : :
358 [ # # # # : 345926 : bool isAbandoned() const { return state<TxStateInactive>() && state<TxStateInactive>()->abandoned; }
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + + + +
+ - + + +
+ + - + +
+ + + - +
+ + + +
- ][ + + +
- + - + -
+ + + - -
- - - - -
- - + + +
+ + + +
+ ]
359 [ + + ]: 337393 : bool isMempoolConflicted() const { return !mempool_conflicts.empty(); }
360 [ + + + + : 416271 : bool isBlockConflicted() const { return state<TxStateBlockConflicted>(); }
# # # # #
# # # # #
# # ][ + +
+ - + - +
+ + - + -
+ + + + ]
361 [ + + # # : 34517 : bool isInactive() const { return state<TxStateInactive>(); }
# # ][ + +
+ - + - ]
362 [ + + + + : 13635 : bool isUnconfirmed() const { return !isAbandoned() && !isBlockConflicted() && !isMempoolConflicted() && !isConfirmed(); }
+ - ]
363 [ + + + + : 580609 : bool isConfirmed() const { return state<TxStateConfirmed>(); }
+ + + + ]
[ # # ][ + -
+ + # # #
# ]
364 [ + + # # : 989144 : const Txid& GetHash() const LIFETIMEBOUND { return tx->GetHash(); }
# # # # #
# # # # #
# # ][ + -
+ - + - ]
[ + - - -
- - - - +
- + - + -
- - ]
365 : 1991 : const Wtxid& GetWitnessHash() const LIFETIMEBOUND { return tx->GetWitnessHash(); }
366 [ # # # # : 783301 : bool IsCoinBase() const { return tx->IsCoinBase(); }
# # # # ]
[ + + + +
+ + + + ]
[ + + ]
367 : :
368 : : private:
369 : : // Disable copying of CWalletTx objects to prevent bugs where instances get
370 : : // copied in and out of the mapWallet map, and fields are updated in the
371 : : // wrong copy.
372 : : CWalletTx(const CWalletTx&) = default;
373 : 14 : CWalletTx& operator=(const CWalletTx&) = default;
374 : : public:
375 : : // Instead have an explicit copy function
376 : : void CopyFrom(const CWalletTx&);
377 : : };
378 : :
379 : : struct WalletTxOrderComparator {
380 : 282 : bool operator()(const CWalletTx* a, const CWalletTx* b) const
381 : : {
382 [ + + + + : 282 : return a->nOrderPos < b->nOrderPos;
+ - ]
383 : : }
384 : : };
385 : :
386 : : class WalletTXO
387 : : {
388 : : private:
389 : : const CWalletTx& m_wtx;
390 : : const CTxOut& m_output;
391 : :
392 : : public:
393 : 45377 : WalletTXO(const CWalletTx& wtx, const CTxOut& output)
394 : 45377 : : m_wtx(wtx),
395 : 45377 : m_output(output)
396 : : {
397 : 45377 : Assume(std::ranges::find(wtx.tx->vout, output) != wtx.tx->vout.end());
398 : 45377 : }
399 : :
400 [ + - + - ]: 795665 : const CWalletTx& GetWalletTx() const { return m_wtx; }
401 : :
402 [ + - + - : 751836 : const CTxOut& GetTxOut() const { return m_output; }
+ - + - ]
[ + - # #
# # # # ]
403 : : };
404 : : } // namespace wallet
405 : :
406 : : #endif // BITCOIN_WALLET_TRANSACTION_H
|