Bitcoin Core  26.1.0
P2P Digital Currency
transaction.h
Go to the documentation of this file.
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 <bitset>
9 #include <cstdint>
10 #include <consensus/amount.h>
11 #include <primitives/transaction.h>
12 #include <serialize.h>
13 #include <wallet/types.h>
14 #include <threadsafety.h>
15 #include <tinyformat.h>
16 #include <util/overloaded.h>
17 #include <util/strencodings.h>
18 #include <util/string.h>
19 
20 #include <list>
21 #include <variant>
22 #include <vector>
23 
24 namespace wallet {
30 
31  explicit TxStateConfirmed(const uint256& block_hash, int height, int index) : confirmed_block_hash(block_hash), confirmed_block_height(height), position_in_block(index) {}
32  std::string toString() const { return strprintf("Confirmed (block=%s, height=%i, index=%i)", confirmed_block_hash.ToString(), confirmed_block_height, position_in_block); }
33 };
34 
37  std::string toString() const { return strprintf("InMempool"); }
38 };
39 
44 
45  explicit TxStateConflicted(const uint256& block_hash, int height) : conflicting_block_hash(block_hash), conflicting_block_height(height) {}
46  std::string toString() const { return strprintf("Conflicted (block=%s, height=%i)", conflicting_block_hash.ToString(), conflicting_block_height); }
47 };
48 
54  bool abandoned;
55 
56  explicit TxStateInactive(bool abandoned = false) : abandoned(abandoned) {}
57  std::string toString() const { return strprintf("Inactive (abandoned=%i)", abandoned); }
58 };
59 
66  int index;
67 
69  std::string toString() const { return strprintf("Unrecognized (block=%s, index=%i)", block_hash.ToString(), index); }
70 };
71 
73 using TxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateConflicted, TxStateInactive, TxStateUnrecognized>;
74 
76 using SyncTxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateInactive>;
77 
80 {
81  if (data.block_hash == uint256::ZERO) {
82  if (data.index == 0) return TxStateInactive{};
83  } else if (data.block_hash == uint256::ONE) {
84  if (data.index == -1) return TxStateInactive{/*abandoned=*/true};
85  } else if (data.index >= 0) {
86  return TxStateConfirmed{data.block_hash, /*height=*/-1, data.index};
87  } else if (data.index == -1) {
88  return TxStateConflicted{data.block_hash, /*height=*/-1};
89  }
90  return data;
91 }
92 
94 static inline uint256 TxStateSerializedBlockHash(const TxState& state)
95 {
96  return std::visit(util::Overloaded{
97  [](const TxStateInactive& inactive) { return inactive.abandoned ? uint256::ONE : uint256::ZERO; },
98  [](const TxStateInMempool& in_mempool) { return uint256::ZERO; },
99  [](const TxStateConfirmed& confirmed) { return confirmed.confirmed_block_hash; },
100  [](const TxStateConflicted& conflicted) { return conflicted.conflicting_block_hash; },
101  [](const TxStateUnrecognized& unrecognized) { return unrecognized.block_hash; }
102  }, state);
103 }
104 
106 static inline int TxStateSerializedIndex(const TxState& state)
107 {
108  return std::visit(util::Overloaded{
109  [](const TxStateInactive& inactive) { return inactive.abandoned ? -1 : 0; },
110  [](const TxStateInMempool& in_mempool) { return 0; },
111  [](const TxStateConfirmed& confirmed) { return confirmed.position_in_block; },
112  [](const TxStateConflicted& conflicted) { return -1; },
113  [](const TxStateUnrecognized& unrecognized) { return unrecognized.index; }
114  }, state);
115 }
116 
118 template<typename T>
119 std::string TxStateString(const T& state)
120 {
121  return std::visit([](const auto& s) { return s.toString(); }, state);
122 }
123 
128 {
129  // NO and ALL are never (supposed to be) cached
130  std::bitset<ISMINE_ENUM_ELEMENTS> m_cached;
132  inline void Reset()
133  {
134  m_cached.reset();
135  }
136  void Set(isminefilter filter, CAmount value)
137  {
138  m_cached.set(filter);
139  m_value[filter] = value;
140  }
141 };
142 
143 
144 typedef std::map<std::string, std::string> mapValue_t;
145 
146 
153 {
154 public:
155  template<typename Stream>
156  void Unserialize(Stream& s)
157  {
158  CTransactionRef tx;
159  uint256 hashBlock;
160  std::vector<uint256> vMerkleBranch;
161  int nIndex;
162 
163  s >> tx >> hashBlock >> vMerkleBranch >> nIndex;
164  }
165 };
166 
172 {
173 public:
200  std::vector<std::pair<std::string, std::string> > vOrderForm;
201  unsigned int fTimeReceivedIsTxTime;
202  unsigned int nTimeReceived;
203 
212  unsigned int nTimeSmart;
218  bool fFromMe;
219  int64_t nOrderPos;
220  std::multimap<int64_t, CWalletTx*>::const_iterator m_it_wtxOrdered;
221 
222  // memory only
231  mutable bool m_is_cache_empty{true};
232  mutable bool fChangeCached;
234 
236  {
237  Init();
238  }
239 
240  void Init()
241  {
242  mapValue.clear();
243  vOrderForm.clear();
244  fTimeReceivedIsTxTime = false;
245  nTimeReceived = 0;
246  nTimeSmart = 0;
247  fFromMe = false;
248  fChangeCached = false;
249  nChangeCached = 0;
250  nOrderPos = -1;
251  }
252 
255 
256  template<typename Stream>
257  void Serialize(Stream& s) const
258  {
259  mapValue_t mapValueCopy = mapValue;
260 
261  mapValueCopy["fromaccount"] = "";
262  if (nOrderPos != -1) {
263  mapValueCopy["n"] = ToString(nOrderPos);
264  }
265  if (nTimeSmart) {
266  mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart);
267  }
268 
269  std::vector<uint8_t> dummy_vector1;
270  std::vector<uint8_t> dummy_vector2;
271  bool dummy_bool = false;
272  uint256 serializedHash = TxStateSerializedBlockHash(m_state);
273  int serializedIndex = TxStateSerializedIndex(m_state);
274  s << tx << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << mapValueCopy << vOrderForm << fTimeReceivedIsTxTime << nTimeReceived << fFromMe << dummy_bool;
275  }
276 
277  template<typename Stream>
278  void Unserialize(Stream& s)
279  {
280  Init();
281 
282  std::vector<uint256> dummy_vector1;
283  std::vector<CMerkleTx> dummy_vector2;
284  bool dummy_bool;
285  uint256 serialized_block_hash;
286  int serializedIndex;
287  s >> tx >> serialized_block_hash >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >> nTimeReceived >> fFromMe >> dummy_bool;
288 
289  m_state = TxStateInterpretSerialized({serialized_block_hash, serializedIndex});
290 
291  const auto it_op = mapValue.find("n");
292  nOrderPos = (it_op != mapValue.end()) ? LocaleIndependentAtoi<int64_t>(it_op->second) : -1;
293  const auto it_ts = mapValue.find("timesmart");
294  nTimeSmart = (it_ts != mapValue.end()) ? static_cast<unsigned int>(LocaleIndependentAtoi<int64_t>(it_ts->second)) : 0;
295 
296  mapValue.erase("fromaccount");
297  mapValue.erase("spent");
298  mapValue.erase("n");
299  mapValue.erase("timesmart");
300  }
301 
303  {
304  tx = std::move(arg);
305  }
306 
308  void MarkDirty()
309  {
310  m_amounts[DEBIT].Reset();
314  fChangeCached = false;
315  m_is_cache_empty = true;
316  }
317 
319  bool IsEquivalentTo(const CWalletTx& tx) const;
320 
321  bool InMempool() const;
322 
323  int64_t GetTxTime() const;
324 
325  template<typename T> const T* state() const { return std::get_if<T>(&m_state); }
326  template<typename T> T* state() { return std::get_if<T>(&m_state); }
327 
328  bool isAbandoned() const { return state<TxStateInactive>() && state<TxStateInactive>()->abandoned; }
329  bool isConflicted() const { return state<TxStateConflicted>(); }
330  bool isInactive() const { return state<TxStateInactive>(); }
331  bool isUnconfirmed() const { return !isAbandoned() && !isConflicted() && !isConfirmed(); }
332  bool isConfirmed() const { return state<TxStateConfirmed>(); }
333  const uint256& GetHash() const { return tx->GetHash(); }
334  const uint256& GetWitnessHash() const { return tx->GetWitnessHash(); }
335  bool IsCoinBase() const { return tx->IsCoinBase(); }
336 
337 private:
338  // Disable copying of CWalletTx objects to prevent bugs where instances get
339  // copied in and out of the mapWallet map, and fields are updated in the
340  // wrong copy.
341  CWalletTx(const CWalletTx&) = default;
342  CWalletTx& operator=(const CWalletTx&) = default;
343 public:
344  // Instead have an explicit copy function
345  void CopyFrom(const CWalletTx&);
346 };
347 
349  bool operator()(const CWalletTx* a, const CWalletTx* b) const
350  {
351  return a->nOrderPos < b->nOrderPos;
352  }
353 };
354 } // namespace wallet
355 
356 #endif // BITCOIN_WALLET_TRANSACTION_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
State of transaction added to mempool.
Definition: transaction.h:36
static const uint256 ONE
Definition: uint256.h:112
void Serialize(Stream &s) const
Definition: transaction.h:257
mapValue_t mapValue
Key/value map with information about the transaction.
Definition: transaction.h:199
void MarkDirty()
make sure balances are recalculated
Definition: transaction.h:308
const T * state() const
Definition: transaction.h:325
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162
std::map< std::string, std::string > mapValue_t
Definition: transaction.h:144
std::vector< std::pair< std::string, std::string > > vOrderForm
Definition: transaction.h:200
CWalletTx(CTransactionRef tx, const TxState &state)
Definition: transaction.h:235
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
Definition: transaction.h:231
std::string TxStateString(const T &state)
Return TxState or SyncTxState as a string for logging or debugging.
Definition: transaction.h:119
State of transaction not confirmed or conflicting with a known block and not in the mempool...
Definition: transaction.h:53
TxStateConfirmed(const uint256 &block_hash, int height, int index)
Definition: transaction.h:31
std::string toString() const
Definition: transaction.h:37
std::variant< TxStateConfirmed, TxStateInMempool, TxStateInactive > SyncTxState
Subset of states transaction sync logic is implemented to handle.
Definition: transaction.h:76
TxStateConflicted(const uint256 &block_hash, int height)
Definition: transaction.h:45
bool isConflicted() const
Definition: transaction.h:329
State of transaction confirmed in a block.
Definition: transaction.h:26
bool IsEquivalentTo(const CWalletTx &tx) const
True if only scriptSigs are different.
Definition: transaction.cpp:8
std::string toString() const
Definition: transaction.h:57
CAmount m_value[ISMINE_ENUM_ELEMENTS]
Definition: transaction.h:131
bool isAbandoned() const
Definition: transaction.h:328
int64_t GetTxTime() const
Definition: transaction.cpp:22
State of transaction loaded in an unrecognized state with unexpected hash or index values...
Definition: transaction.h:64
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:171
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:109
TxStateUnrecognized(const uint256 &block_hash, int index)
Definition: transaction.h:68
std::underlying_type< isminetype >::type isminefilter
used for bitflags of isminetype
Definition: wallet.h:43
std::bitset< ISMINE_ENUM_ELEMENTS > m_cached
Definition: transaction.h:130
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
Definition: transaction.h:220
static TxState TxStateInterpretSerialized(TxStateUnrecognized data)
Try to interpret deserialized TxStateUnrecognized data as a recognized state.
Definition: transaction.h:79
static const uint256 ZERO
Definition: uint256.h:111
void Unserialize(Stream &s)
Definition: transaction.h:278
static int TxStateSerializedIndex(const TxState &state)
Get TxState serialized block index. Inverse of TxStateInterpretSerialized.
Definition: transaction.h:106
std::string toString() const
Definition: transaction.h:46
std::string toString() const
Definition: transaction.h:32
std::string ToString() const
Definition: uint256.cpp:55
void CopyFrom(const CWalletTx &)
Definition: transaction.cpp:28
static uint256 TxStateSerializedBlockHash(const TxState &state)
Get TxState serialized block hash. Inverse of TxStateInterpretSerialized.
Definition: transaction.h:94
Cachable amount subdivided into watchonly and spendable parts.
Definition: transaction.h:127
TxStateInactive(bool abandoned=false)
Definition: transaction.h:56
256-bit opaque blob.
Definition: uint256.h:106
unsigned int fTimeReceivedIsTxTime
Definition: transaction.h:201
State of rejected transaction that conflicts with a confirmed block.
Definition: transaction.h:41
bool fFromMe
From me flag is set to 1 for transactions that were created by the wallet on this bitcoin node...
Definition: transaction.h:218
bool InMempool() const
Definition: transaction.cpp:17
const uint256 & GetHash() const
Definition: transaction.h:333
unsigned int nTimeSmart
Stable timestamp that never changes, and reflects the order a transaction was added to the wallet...
Definition: transaction.h:212
void Unserialize(Stream &s)
Definition: transaction.h:156
std::string toString() const
Definition: transaction.h:69
CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS]
Definition: transaction.h:224
bool IsCoinBase() const
Definition: transaction.h:335
unsigned int nTimeReceived
time received by this node
Definition: transaction.h:202
int64_t nOrderPos
position in ordered transaction list
Definition: transaction.h:219
void Set(isminefilter filter, CAmount value)
Definition: transaction.h:136
bool operator()(const CWalletTx *a, const CWalletTx *b) const
Definition: transaction.h:349
CAmount nChangeCached
Definition: transaction.h:233
CWalletTx & operator=(const CWalletTx &)=default
Overloaded helper for std::visit.
Definition: overloaded.h:16
bool isInactive() const
Definition: transaction.h:330
void SetTx(CTransactionRef arg)
Definition: transaction.h:302
#define T(expected, seed, data)
CTransactionRef tx
Definition: transaction.h:253
const uint256 & GetWitnessHash() const
Definition: transaction.h:334
bool isConfirmed() const
Definition: transaction.h:332
std::variant< TxStateConfirmed, TxStateInMempool, TxStateConflicted, TxStateInactive, TxStateUnrecognized > TxState
All possible CWalletTx states.
Definition: transaction.h:73
bool isUnconfirmed() const
Definition: transaction.h:331
Legacy class used for deserializing vtxPrev for backwards compatibility.
Definition: transaction.h:152