Bitcoin Core  25.99.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 };
33 
36 };
37 
42 
43  explicit TxStateConflicted(const uint256& block_hash, int height) : conflicting_block_hash(block_hash), conflicting_block_height(height) {}
44 };
45 
51  bool abandoned;
52 
53  explicit TxStateInactive(bool abandoned = false) : abandoned(abandoned) {}
54 };
55 
62  int index;
63 
65 };
66 
68 using TxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateConflicted, TxStateInactive, TxStateUnrecognized>;
69 
71 using SyncTxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateInactive>;
72 
75 {
76  if (data.block_hash == uint256::ZERO) {
77  if (data.index == 0) return TxStateInactive{};
78  } else if (data.block_hash == uint256::ONE) {
79  if (data.index == -1) return TxStateInactive{/*abandoned=*/true};
80  } else if (data.index >= 0) {
81  return TxStateConfirmed{data.block_hash, /*height=*/-1, data.index};
82  } else if (data.index == -1) {
83  return TxStateConflicted{data.block_hash, /*height=*/-1};
84  }
85  return data;
86 }
87 
89 static inline uint256 TxStateSerializedBlockHash(const TxState& state)
90 {
91  return std::visit(util::Overloaded{
92  [](const TxStateInactive& inactive) { return inactive.abandoned ? uint256::ONE : uint256::ZERO; },
93  [](const TxStateInMempool& in_mempool) { return uint256::ZERO; },
94  [](const TxStateConfirmed& confirmed) { return confirmed.confirmed_block_hash; },
95  [](const TxStateConflicted& conflicted) { return conflicted.conflicting_block_hash; },
96  [](const TxStateUnrecognized& unrecognized) { return unrecognized.block_hash; }
97  }, state);
98 }
99 
101 static inline int TxStateSerializedIndex(const TxState& state)
102 {
103  return std::visit(util::Overloaded{
104  [](const TxStateInactive& inactive) { return inactive.abandoned ? -1 : 0; },
105  [](const TxStateInMempool& in_mempool) { return 0; },
106  [](const TxStateConfirmed& confirmed) { return confirmed.position_in_block; },
107  [](const TxStateConflicted& conflicted) { return -1; },
108  [](const TxStateUnrecognized& unrecognized) { return unrecognized.index; }
109  }, state);
110 }
111 
112 
117 {
118  // NO and ALL are never (supposed to be) cached
119  std::bitset<ISMINE_ENUM_ELEMENTS> m_cached;
121  inline void Reset()
122  {
123  m_cached.reset();
124  }
125  void Set(isminefilter filter, CAmount value)
126  {
127  m_cached.set(filter);
128  m_value[filter] = value;
129  }
130 };
131 
132 
133 typedef std::map<std::string, std::string> mapValue_t;
134 
135 
142 {
143 public:
144  template<typename Stream>
145  void Unserialize(Stream& s)
146  {
147  CTransactionRef tx;
148  uint256 hashBlock;
149  std::vector<uint256> vMerkleBranch;
150  int nIndex;
151 
152  s >> tx >> hashBlock >> vMerkleBranch >> nIndex;
153  }
154 };
155 
161 {
162 public:
189  std::vector<std::pair<std::string, std::string> > vOrderForm;
190  unsigned int fTimeReceivedIsTxTime;
191  unsigned int nTimeReceived;
201  unsigned int nTimeSmart;
207  bool fFromMe;
208  int64_t nOrderPos;
209  std::multimap<int64_t, CWalletTx*>::const_iterator m_it_wtxOrdered;
210 
211  // memory only
220  mutable bool m_is_cache_empty{true};
221  mutable bool fChangeCached;
223 
225  {
226  Init();
227  }
228 
229  void Init()
230  {
231  mapValue.clear();
232  vOrderForm.clear();
233  fTimeReceivedIsTxTime = false;
234  nTimeReceived = 0;
235  nTimeSmart = 0;
236  fFromMe = false;
237  fChangeCached = false;
238  nChangeCached = 0;
239  nOrderPos = -1;
240  }
241 
244 
245  template<typename Stream>
246  void Serialize(Stream& s) const
247  {
248  mapValue_t mapValueCopy = mapValue;
249 
250  mapValueCopy["fromaccount"] = "";
251  if (nOrderPos != -1) {
252  mapValueCopy["n"] = ToString(nOrderPos);
253  }
254  if (nTimeSmart) {
255  mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart);
256  }
257 
258  std::vector<uint8_t> dummy_vector1;
259  std::vector<uint8_t> dummy_vector2;
260  bool dummy_bool = false;
261  uint256 serializedHash = TxStateSerializedBlockHash(m_state);
262  int serializedIndex = TxStateSerializedIndex(m_state);
263  s << tx << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << mapValueCopy << vOrderForm << fTimeReceivedIsTxTime << nTimeReceived << fFromMe << dummy_bool;
264  }
265 
266  template<typename Stream>
267  void Unserialize(Stream& s)
268  {
269  Init();
270 
271  std::vector<uint256> dummy_vector1;
272  std::vector<CMerkleTx> dummy_vector2;
273  bool dummy_bool;
274  uint256 serialized_block_hash;
275  int serializedIndex;
276  s >> tx >> serialized_block_hash >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >> nTimeReceived >> fFromMe >> dummy_bool;
277 
278  m_state = TxStateInterpretSerialized({serialized_block_hash, serializedIndex});
279 
280  const auto it_op = mapValue.find("n");
281  nOrderPos = (it_op != mapValue.end()) ? LocaleIndependentAtoi<int64_t>(it_op->second) : -1;
282  const auto it_ts = mapValue.find("timesmart");
283  nTimeSmart = (it_ts != mapValue.end()) ? static_cast<unsigned int>(LocaleIndependentAtoi<int64_t>(it_ts->second)) : 0;
284 
285  mapValue.erase("fromaccount");
286  mapValue.erase("spent");
287  mapValue.erase("n");
288  mapValue.erase("timesmart");
289  }
290 
292  {
293  tx = std::move(arg);
294  }
295 
297  void MarkDirty()
298  {
299  m_amounts[DEBIT].Reset();
303  fChangeCached = false;
304  m_is_cache_empty = true;
305  }
306 
308  bool IsEquivalentTo(const CWalletTx& tx) const;
309 
310  bool InMempool() const;
311 
312  int64_t GetTxTime() const;
313 
314  template<typename T> const T* state() const { return std::get_if<T>(&m_state); }
315  template<typename T> T* state() { return std::get_if<T>(&m_state); }
316 
317  bool isAbandoned() const { return state<TxStateInactive>() && state<TxStateInactive>()->abandoned; }
318  bool isConflicted() const { return state<TxStateConflicted>(); }
319  bool isInactive() const { return state<TxStateInactive>(); }
320  bool isUnconfirmed() const { return !isAbandoned() && !isConflicted() && !isConfirmed(); }
321  bool isConfirmed() const { return state<TxStateConfirmed>(); }
322  const uint256& GetHash() const { return tx->GetHash(); }
323  const uint256& GetWitnessHash() const { return tx->GetWitnessHash(); }
324  bool IsCoinBase() const { return tx->IsCoinBase(); }
325 
326  // Disable copying of CWalletTx objects to prevent bugs where instances get
327  // copied in and out of the mapWallet map, and fields are updated in the
328  // wrong copy.
329  CWalletTx(CWalletTx const &) = delete;
330  void operator=(CWalletTx const &x) = delete;
331 };
332 
334  bool operator()(const CWalletTx* a, const CWalletTx* b) const
335  {
336  return a->nOrderPos < b->nOrderPos;
337  }
338 };
339 } // namespace wallet
340 
341 #endif // BITCOIN_WALLET_TRANSACTION_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
256-bit opaque blob.
Definition: uint256.h:105
static const uint256 ONE
Definition: uint256.h:111
static const uint256 ZERO
Definition: uint256.h:110
Legacy class used for deserializing vtxPrev for backwards compatibility.
Definition: transaction.h:142
void Unserialize(Stream &s)
Definition: transaction.h:145
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:161
bool isConfirmed() const
Definition: transaction.h:321
bool IsEquivalentTo(const CWalletTx &tx) const
True if only scriptSigs are different.
Definition: transaction.cpp:8
const T * state() const
Definition: transaction.h:314
std::vector< std::pair< std::string, std::string > > vOrderForm
Definition: transaction.h:189
mapValue_t mapValue
Key/value map with information about the transaction.
Definition: transaction.h:188
CAmount nChangeCached
Definition: transaction.h:222
void Serialize(Stream &s) const
Definition: transaction.h:246
int64_t nOrderPos
position in ordered transaction list
Definition: transaction.h:208
bool isUnconfirmed() const
Definition: transaction.h:320
bool fFromMe
From me flag is set to 1 for transactions that were created by the wallet on this bitcoin node,...
Definition: transaction.h:207
unsigned int nTimeReceived
time received by this node
Definition: transaction.h:191
CTransactionRef tx
Definition: transaction.h:242
bool isConflicted() const
Definition: transaction.h:318
void Unserialize(Stream &s)
Definition: transaction.h:267
void SetTx(CTransactionRef arg)
Definition: transaction.h:291
bool IsCoinBase() const
Definition: transaction.h:324
void operator=(CWalletTx const &x)=delete
unsigned int fTimeReceivedIsTxTime
Definition: transaction.h:190
CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS]
Definition: transaction.h:213
bool InMempool() const
Definition: transaction.cpp:17
bool isAbandoned() const
Definition: transaction.h:317
const uint256 & GetHash() const
Definition: transaction.h:322
const uint256 & GetWitnessHash() const
Definition: transaction.h:323
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
Definition: transaction.h:209
bool isInactive() const
Definition: transaction.h:319
int64_t GetTxTime() const
Definition: transaction.cpp:22
CWalletTx(CTransactionRef tx, const TxState &state)
Definition: transaction.h:224
CWalletTx(CWalletTx const &)=delete
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
Definition: transaction.h:220
unsigned int nTimeSmart
Stable timestamp that never changes, and reflects the order a transaction was added to the wallet.
Definition: transaction.h:201
void MarkDirty()
make sure balances are recalculated
Definition: transaction.h:297
#define T(expected, seed, data)
std::variant< TxStateConfirmed, TxStateInMempool, TxStateInactive > SyncTxState
Subset of states transaction sync logic is implemented to handle.
Definition: transaction.h:71
std::map< std::string, std::string > mapValue_t
Definition: transaction.h:133
static int TxStateSerializedIndex(const TxState &state)
Get TxState serialized block index. Inverse of TxStateInterpretSerialized.
Definition: transaction.h:101
std::underlying_type< isminetype >::type isminefilter
used for bitflags of isminetype
Definition: wallet.h:42
static TxState TxStateInterpretSerialized(TxStateUnrecognized data)
Try to interpret deserialized TxStateUnrecognized data as a recognized state.
Definition: transaction.h:74
@ ISMINE_ENUM_ELEMENTS
Definition: types.h:47
static uint256 TxStateSerializedBlockHash(const TxState &state)
Get TxState serialized block hash. Inverse of TxStateInterpretSerialized.
Definition: transaction.h:89
std::variant< TxStateConfirmed, TxStateInMempool, TxStateConflicted, TxStateInactive, TxStateUnrecognized > TxState
All possible CWalletTx states.
Definition: transaction.h:68
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:109
Overloaded helper for std::visit.
Definition: overloaded.h:16
Cachable amount subdivided into watchonly and spendable parts.
Definition: transaction.h:117
void Set(isminefilter filter, CAmount value)
Definition: transaction.h:125
CAmount m_value[ISMINE_ENUM_ELEMENTS]
Definition: transaction.h:120
std::bitset< ISMINE_ENUM_ELEMENTS > m_cached
Definition: transaction.h:119
State of transaction confirmed in a block.
Definition: transaction.h:26
TxStateConfirmed(const uint256 &block_hash, int height, int index)
Definition: transaction.h:31
State of rejected transaction that conflicts with a confirmed block.
Definition: transaction.h:39
TxStateConflicted(const uint256 &block_hash, int height)
Definition: transaction.h:43
State of transaction added to mempool.
Definition: transaction.h:35
State of transaction not confirmed or conflicting with a known block and not in the mempool.
Definition: transaction.h:50
TxStateInactive(bool abandoned=false)
Definition: transaction.h:53
State of transaction loaded in an unrecognized state with unexpected hash or index values.
Definition: transaction.h:60
TxStateUnrecognized(const uint256 &block_hash, int index)
Definition: transaction.h:64
bool operator()(const CWalletTx *a, const CWalletTx *b) const
Definition: transaction.h:334
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162