Bitcoin Core  22.99.0
P2P Digital Currency
transaction.h
Go to the documentation of this file.
1 // Copyright (c) 2021 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 <amount.h>
10 #include <serialize.h>
11 #include <wallet/ismine.h>
12 #include <threadsafety.h>
13 #include <tinyformat.h>
14 #include <util/strencodings.h>
15 #include <util/string.h>
16 
17 #include <list>
18 #include <vector>
19 
20 typedef std::map<std::string, std::string> mapValue_t;
21 
27 class CMerkleTx
28 {
29 public:
30  template<typename Stream>
31  void Unserialize(Stream& s)
32  {
33  CTransactionRef tx;
34  uint256 hashBlock;
35  std::vector<uint256> vMerkleBranch;
36  int nIndex;
37 
38  s >> tx >> hashBlock >> vMerkleBranch >> nIndex;
39  }
40 };
41 
46 class CWalletTx
47 {
48 private:
52  static constexpr const uint256& ABANDON_HASH = uint256::ONE;
53 
54 public:
81  std::vector<std::pair<std::string, std::string> > vOrderForm;
82  unsigned int fTimeReceivedIsTxTime;
83  unsigned int nTimeReceived;
84 
93  unsigned int nTimeSmart;
99  bool fFromMe;
100  int64_t nOrderPos;
101  std::multimap<int64_t, CWalletTx*>::const_iterator m_it_wtxOrdered;
102 
103  // memory only
112  mutable bool m_is_cache_empty{true};
113  mutable bool fChangeCached;
114  mutable bool fInMempool;
116 
118  : tx(std::move(arg))
119  {
120  Init();
121  }
122 
123  void Init()
124  {
125  mapValue.clear();
126  vOrderForm.clear();
127  fTimeReceivedIsTxTime = false;
128  nTimeReceived = 0;
129  nTimeSmart = 0;
130  fFromMe = false;
131  fChangeCached = false;
132  fInMempool = false;
133  nChangeCached = 0;
134  nOrderPos = -1;
136  }
137 
139 
148  enum Status {
153  };
154 
160  struct Confirmation {
164  int nIndex;
165  Confirmation(Status s = UNCONFIRMED, int b = 0, uint256 h = uint256(), int i = 0) : status(s), block_height(b), hashBlock(h), nIndex(i) {}
166  };
167 
169 
170  template<typename Stream>
171  void Serialize(Stream& s) const
172  {
173  mapValue_t mapValueCopy = mapValue;
174 
175  mapValueCopy["fromaccount"] = "";
176  if (nOrderPos != -1) {
177  mapValueCopy["n"] = ToString(nOrderPos);
178  }
179  if (nTimeSmart) {
180  mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart);
181  }
182 
183  std::vector<uint8_t> dummy_vector1;
184  std::vector<uint8_t> dummy_vector2;
185  bool dummy_bool = false;
186  uint256 serializedHash = isAbandoned() ? ABANDON_HASH : m_confirm.hashBlock;
187  int serializedIndex = isAbandoned() || isConflicted() ? -1 : m_confirm.nIndex;
188  s << tx << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << mapValueCopy << vOrderForm << fTimeReceivedIsTxTime << nTimeReceived << fFromMe << dummy_bool;
189  }
190 
191  template<typename Stream>
192  void Unserialize(Stream& s)
193  {
194  Init();
195 
196  std::vector<uint256> dummy_vector1;
197  std::vector<CMerkleTx> dummy_vector2;
198  bool dummy_bool;
199  int serializedIndex;
200  s >> tx >> m_confirm.hashBlock >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >> nTimeReceived >> fFromMe >> dummy_bool;
201 
202  /* At serialization/deserialization, an nIndex == -1 means that hashBlock refers to
203  * the earliest block in the chain we know this or any in-wallet ancestor conflicts
204  * with. If nIndex == -1 and hashBlock is ABANDON_HASH, it means transaction is abandoned.
205  * In same context, an nIndex >= 0 refers to a confirmed transaction (if hashBlock set) or
206  * unconfirmed one. Older clients interpret nIndex == -1 as unconfirmed for backward
207  * compatibility (pre-commit 9ac63d6).
208  */
209  if (serializedIndex == -1 && m_confirm.hashBlock == ABANDON_HASH) {
210  setAbandoned();
211  } else if (serializedIndex == -1) {
212  setConflicted();
213  } else if (!m_confirm.hashBlock.IsNull()) {
214  m_confirm.nIndex = serializedIndex;
215  setConfirmed();
216  }
217 
218  const auto it_op = mapValue.find("n");
219  nOrderPos = (it_op != mapValue.end()) ? atoi64(it_op->second) : -1;
220  const auto it_ts = mapValue.find("timesmart");
221  nTimeSmart = (it_ts != mapValue.end()) ? static_cast<unsigned int>(atoi64(it_ts->second)) : 0;
222 
223  mapValue.erase("fromaccount");
224  mapValue.erase("spent");
225  mapValue.erase("n");
226  mapValue.erase("timesmart");
227  }
228 
230  {
231  tx = std::move(arg);
232  }
233 
235  void MarkDirty()
236  {
237  m_amounts[DEBIT].Reset();
241  fChangeCached = false;
242  m_is_cache_empty = true;
243  }
244 
246  bool IsEquivalentTo(const CWalletTx& tx) const;
247 
248  bool InMempool() const;
249 
250  int64_t GetTxTime() const;
251 
252  bool isAbandoned() const { return m_confirm.status == CWalletTx::ABANDONED; }
254  {
258  m_confirm.nIndex = 0;
259  }
264  bool isConfirmed() const { return m_confirm.status == CWalletTx::CONFIRMED; }
266  const uint256& GetHash() const { return tx->GetHash(); }
267  bool IsCoinBase() const { return tx->IsCoinBase(); }
268 
269  // Disable copying of CWalletTx objects to prevent bugs where instances get
270  // copied in and out of the mapWallet map, and fields are updated in the
271  // wrong copy.
272  CWalletTx(CWalletTx const &) = delete;
273  void operator=(CWalletTx const &x) = delete;
274 };
275 
276 #endif // BITCOIN_WALLET_TRANSACTION_H
atoi64
int64_t atoi64(const std::string &str)
Definition: strencodings.cpp:440
CWalletTx::CONFLICTED
@ CONFLICTED
Definition: transaction.h:151
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
CWalletTx::setConflicted
void setConflicted()
Definition: transaction.h:261
CWalletTx::fTimeReceivedIsTxTime
unsigned int fTimeReceivedIsTxTime
Definition: transaction.h:82
CWalletTx::AVAILABLE_CREDIT
@ AVAILABLE_CREDIT
Definition: transaction.h:104
transaction.h
CWalletTx::Serialize
void Serialize(Stream &s) const
Definition: transaction.h:171
string.h
CWalletTx::mapValue
mapValue_t mapValue
Key/value map with information about the transaction.
Definition: transaction.h:80
CWalletTx::ABANDONED
@ ABANDONED
Definition: transaction.h:152
CWalletTx::setConfirmed
void setConfirmed()
Definition: transaction.h:265
CWalletTx::Init
void Init()
Definition: transaction.h:123
CWalletTx::setUnconfirmed
void setUnconfirmed()
Definition: transaction.h:263
CWalletTx::AmountType
AmountType
Definition: transaction.h:104
CWalletTx::isConfirmed
bool isConfirmed() const
Definition: transaction.h:264
CWalletTx::m_confirm
Confirmation m_confirm
Definition: transaction.h:168
CWalletTx::m_is_cache_empty
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
Definition: transaction.h:112
CWalletTx::Unserialize
void Unserialize(Stream &s)
Definition: transaction.h:192
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
CWalletTx::Confirmation::block_height
int block_height
Definition: transaction.h:162
CWalletTx::GetTxTime
int64_t GetTxTime() const
Definition: transaction.cpp:21
CWalletTx::CWalletTx
CWalletTx(CTransactionRef arg)
Definition: transaction.h:117
CWalletTx::nChangeCached
CAmount nChangeCached
Definition: transaction.h:115
CWalletTx::nTimeSmart
unsigned int nTimeSmart
Stable timestamp that never changes, and reflects the order a transaction was added to the wallet.
Definition: transaction.h:93
tinyformat.h
CWalletTx::SetTx
void SetTx(CTransactionRef arg)
Definition: transaction.h:229
ismine.h
strencodings.h
CWalletTx::IsEquivalentTo
bool IsEquivalentTo(const CWalletTx &tx) const
True if only scriptSigs are different.
Definition: transaction.cpp:7
CWalletTx::nTimeReceived
unsigned int nTimeReceived
time received by this node
Definition: transaction.h:83
CMerkleTx
Legacy class used for deserializing vtxPrev for backwards compatibility.
Definition: transaction.h:27
CWalletTx::isAbandoned
bool isAbandoned() const
Definition: transaction.h:252
CWalletTx::Confirmation::status
Status status
Definition: transaction.h:161
threadsafety.h
CWalletTx::nOrderPos
int64_t nOrderPos
position in ordered transaction list
Definition: transaction.h:100
CWalletTx::fChangeCached
bool fChangeCached
Definition: transaction.h:113
CWalletTx::ABANDON_HASH
static constexpr const uint256 & ABANDON_HASH
Constant used in hashBlock to indicate tx has been abandoned, only used at serialization/deserializat...
Definition: transaction.h:52
CWalletTx::tx
CTransactionRef tx
Definition: transaction.h:138
CWalletTx::isUnconfirmed
bool isUnconfirmed() const
Definition: transaction.h:262
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CWalletTx::vOrderForm
std::vector< std::pair< std::string, std::string > > vOrderForm
Definition: transaction.h:81
uint256
256-bit opaque blob.
Definition: uint256.h:124
CWalletTx::CONFIRMED
@ CONFIRMED
Definition: transaction.h:150
CWalletTx::Confirmation::Confirmation
Confirmation(Status s=UNCONFIRMED, int b=0, uint256 h=uint256(), int i=0)
Definition: transaction.h:165
CWalletTx::Confirmation
Confirmation includes tx status and a triplet of {block height/block hash/tx index in block} at which...
Definition: transaction.h:160
CWalletTx::Confirmation::hashBlock
uint256 hashBlock
Definition: transaction.h:163
mapValue_t
std::map< std::string, std::string > mapValue_t
Definition: transaction.h:20
CWalletTx::Confirmation::nIndex
int nIndex
Definition: transaction.h:164
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
CWalletTx::setAbandoned
void setAbandoned()
Definition: transaction.h:253
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:31
CWalletTx::m_it_wtxOrdered
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
Definition: transaction.h:101
CWalletTx::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:267
CWalletTx::CREDIT
@ CREDIT
Definition: transaction.h:104
CWalletTx::isConflicted
bool isConflicted() const
Definition: transaction.h:260
CWalletTx::GetHash
const uint256 & GetHash() const
Definition: transaction.h:266
CWalletTx
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:46
CWalletTx::Status
Status
New transactions start as UNCONFIRMED.
Definition: transaction.h:148
serialize.h
CWalletTx::fFromMe
bool fFromMe
From me flag is set to 1 for transactions that were created by the wallet on this bitcoin node,...
Definition: transaction.h:99
CWalletTx::fInMempool
bool fInMempool
Definition: transaction.h:114
CachableAmount::Reset
void Reset()
Definition: ismine.h:59
CWalletTx::MarkDirty
void MarkDirty()
make sure balances are recalculated
Definition: transaction.h:235
CWalletTx::operator=
void operator=(CWalletTx const &x)=delete
CWalletTx::IMMATURE_CREDIT
@ IMMATURE_CREDIT
Definition: transaction.h:104
CachableAmount
Cachable amount subdivided into watchonly and spendable parts.
Definition: ismine.h:54
CWalletTx::DEBIT
@ DEBIT
Definition: transaction.h:104
CWalletTx::m_amounts
CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS]
Definition: transaction.h:105
CMerkleTx::Unserialize
void Unserialize(Stream &s)
Definition: transaction.h:31
amount.h
CWalletTx::UNCONFIRMED
@ UNCONFIRMED
Definition: transaction.h:149
CWalletTx::AMOUNTTYPE_ENUM_ELEMENTS
@ AMOUNTTYPE_ENUM_ELEMENTS
Definition: transaction.h:104
CWalletTx::InMempool
bool InMempool() const
Definition: transaction.cpp:16
uint256::ONE
static const uint256 ONE
Definition: uint256.h:130