Bitcoin ABC 0.30.7
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 <consensus/amount.h>
11#include <serialize.h>
12#include <threadsafety.h>
13#include <tinyformat.h>
14#include <util/strencodings.h>
15#include <util/string.h>
16#include <wallet/ismine.h>
17
18#include <list>
19#include <vector>
20
21typedef std::map<std::string, std::string> mapValue_t;
22
23static inline void ReadOrderPos(int64_t &nOrderPos, mapValue_t &mapValue) {
24 if (!mapValue.count("n")) {
25 // TODO: calculate elsewhere
26 nOrderPos = -1;
27 return;
28 }
29
30 nOrderPos = atoi64(mapValue["n"]);
31}
32
33static inline void WriteOrderPos(const int64_t &nOrderPos,
34 mapValue_t &mapValue) {
35 if (nOrderPos == -1) {
36 return;
37 }
38 mapValue["n"] = ToString(nOrderPos);
39}
40
48class CMerkleTx {
49public:
50 template <typename Stream> void Unserialize(Stream &s) {
52 BlockHash hashBlock;
53 std::vector<uint256> vMerkleBranch;
54 int nIndex = 0;
55
56 s >> tx >> hashBlock >> vMerkleBranch >> nIndex;
57 }
58};
59
65class CWalletTx {
66private:
71 static constexpr const uint256 &ABANDON_HASH = uint256::ONE;
72
73public:
100 std::vector<std::pair<std::string, std::string>> vOrderForm;
103 unsigned int nTimeReceived;
113 unsigned int nTimeSmart;
121 int64_t nOrderPos;
122 std::multimap<int64_t, CWalletTx *>::const_iterator m_it_wtxOrdered;
123
124 // memory only
131 };
139 mutable bool m_is_cache_empty{true};
140 mutable bool fChangeCached;
141 mutable bool fInMempool;
143
144 CWalletTx(CTransactionRef arg) : tx(std::move(arg)) { Init(); }
145
146 void Init() {
147 mapValue.clear();
148 vOrderForm.clear();
149 fTimeReceivedIsTxTime = false;
150 nTimeReceived = 0;
151 nTimeSmart = 0;
152 fFromMe = false;
153 fChangeCached = false;
154 fInMempool = false;
156 nOrderPos = -1;
158 }
159
161
173
187 BlockHash h = BlockHash(), int i = 0)
188 : status(s), block_height(b), hashBlock(h), nIndex(i) {}
189 };
190
192
193 template <typename Stream> void Serialize(Stream &s) const {
194 mapValue_t mapValueCopy = mapValue;
195
196 mapValueCopy["fromaccount"] = "";
197 WriteOrderPos(nOrderPos, mapValueCopy);
198 if (nTimeSmart) {
199 mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart);
200 }
201
203 std::vector<uint8_t> dummy_vector1;
205 std::vector<uint8_t> dummy_vector2;
207 bool dummy_bool = false;
208 uint256 serializedHash =
210 int serializedIndex =
212 s << tx << serializedHash << dummy_vector1 << serializedIndex
213 << dummy_vector2 << mapValueCopy << vOrderForm
214 << fTimeReceivedIsTxTime << nTimeReceived << fFromMe << dummy_bool;
215 }
216
217 template <typename Stream> void Unserialize(Stream &s) {
218 Init();
219
221 std::vector<uint256> dummy_vector1;
223 std::vector<CMerkleTx> dummy_vector2;
225 bool dummy_bool;
226 int serializedIndex;
227 s >> tx >> m_confirm.hashBlock >> dummy_vector1 >> serializedIndex >>
228 dummy_vector2 >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >>
229 nTimeReceived >> fFromMe >> dummy_bool;
230
231 /*
232 * At serialization/deserialization, an nIndex == -1 means that
233 * hashBlock refers to the earliest block in the chain we know this or
234 * any in-wallet ancestor conflicts with. If nIndex == -1 and hashBlock
235 * is ABANDON_HASH, it means transaction is abandoned. In same context,
236 * an nIndex >= 0 refers to a confirmed transaction (if hashBlock set)
237 * or unconfirmed one. Older clients interpret nIndex == -1 as
238 * unconfirmed for backward compatibility (pre-commit 9ac63d6).
239 */
240 if (serializedIndex == -1 && m_confirm.hashBlock == ABANDON_HASH) {
241 setAbandoned();
242 } else if (serializedIndex == -1) {
244 } else if (!m_confirm.hashBlock.IsNull()) {
245 m_confirm.nIndex = serializedIndex;
246 setConfirmed();
247 }
248
250 nTimeSmart = mapValue.count("timesmart")
251 ? (unsigned int)atoi64(mapValue["timesmart"])
252 : 0;
253
254 mapValue.erase("fromaccount");
255 mapValue.erase("spent");
256 mapValue.erase("n");
257 mapValue.erase("timesmart");
258 }
259
260 void SetTx(CTransactionRef arg) { tx = std::move(arg); }
261
263 void MarkDirty() {
268 fChangeCached = false;
269 m_is_cache_empty = true;
270 }
271
272 // True if only scriptSigs are different
273 bool IsEquivalentTo(const CWalletTx &tx) const;
274
275 bool InMempool() const;
276
277 int64_t GetTxTime() const;
278
279 bool isAbandoned() const {
281 }
286 m_confirm.nIndex = 0;
287 }
288 bool isConflicted() const {
290 }
292 bool isUnconfirmed() const {
294 }
296 bool isConfirmed() const {
298 }
300 TxId GetId() const { return tx->GetId(); }
301 bool IsCoinBase() const { return tx->IsCoinBase(); }
302
303 // Disable copying of CWalletTx objects to prevent bugs where instances get
304 // copied in and out of the mapWallet map, and fields are updated in the
305 // wrong copy.
306 CWalletTx(CWalletTx const &) = delete;
307 void operator=(CWalletTx const &x) = delete;
308};
309
310#endif // BITCOIN_WALLET_TRANSACTION_H
Legacy class used for deserializing vtxPrev for backwards compatibility.
Definition: transaction.h:48
void Unserialize(Stream &s)
Definition: transaction.h:50
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:65
Status
New transactions start as UNCONFIRMED.
Definition: transaction.h:172
bool isAbandoned() const
Definition: transaction.h:279
mapValue_t mapValue
Key/value map with information about the transaction.
Definition: transaction.h:99
CTransactionRef tx
Definition: transaction.h:160
bool isUnconfirmed() const
Definition: transaction.h:292
CWalletTx(CWalletTx const &)=delete
void setConflicted()
Definition: transaction.h:291
void operator=(CWalletTx const &x)=delete
static constexpr const uint256 & ABANDON_HASH
Constant used in hashBlock to indicate tx has been abandoned, only used at serialization/deserializat...
Definition: transaction.h:71
unsigned int nTimeSmart
Stable timestamp that never changes, and reflects the order a transaction was added to the wallet.
Definition: transaction.h:113
void SetTx(CTransactionRef arg)
Definition: transaction.h:260
void Serialize(Stream &s) const
Definition: transaction.h:193
CWalletTx(CTransactionRef arg)
Definition: transaction.h:144
bool IsEquivalentTo(const CWalletTx &tx) const
Definition: transaction.cpp:7
void Unserialize(Stream &s)
Definition: transaction.h:217
bool isConflicted() const
Definition: transaction.h:288
Confirmation m_confirm
Definition: transaction.h:191
TxId GetId() const
Definition: transaction.h:300
@ AVAILABLE_CREDIT
Definition: transaction.h:129
@ IMMATURE_CREDIT
Definition: transaction.h:128
@ AMOUNTTYPE_ENUM_ELEMENTS
Definition: transaction.h:130
Amount nChangeCached
Definition: transaction.h:142
void setConfirmed()
Definition: transaction.h:299
std::vector< std::pair< std::string, std::string > > vOrderForm
Definition: transaction.h:100
void Init()
Definition: transaction.h:146
bool fFromMe
From me flag is set to 1 for transactions that were created by the wallet on this bitcoin node,...
Definition: transaction.h:119
void setAbandoned()
Definition: transaction.h:282
void setUnconfirmed()
Definition: transaction.h:295
bool fChangeCached
Definition: transaction.h:140
int64_t GetTxTime() const
Definition: transaction.cpp:25
bool fInMempool
Definition: transaction.h:141
unsigned int fTimeReceivedIsTxTime
Definition: transaction.h:101
bool isConfirmed() const
Definition: transaction.h:296
CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS]
Definition: transaction.h:132
void MarkDirty()
make sure balances are recalculated
Definition: transaction.h:263
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
Definition: transaction.h:139
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
Definition: transaction.h:122
bool InMempool() const
Definition: transaction.cpp:21
bool IsCoinBase() const
Definition: transaction.h:301
unsigned int nTimeReceived
time received by this node
Definition: transaction.h:103
int64_t nOrderPos
position in ordered transaction list
Definition: transaction.h:121
bool IsNull() const
Definition: uint256.h:32
256-bit opaque blob.
Definition: uint256.h:129
static const uint256 ONE
Definition: uint256.h:135
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:100
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Confirmation includes tx status and a triplet of {block height/block hash/tx index in block} at which...
Definition: transaction.h:181
Confirmation(Status s=UNCONFIRMED, int b=0, BlockHash h=BlockHash(), int i=0)
Definition: transaction.h:186
Cachable amount subdivided into watchonly and spendable parts.
Definition: ismine.h:34
void Reset()
Definition: ismine.h:38
A TxId is the identifier of a transaction.
Definition: txid.h:14
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
int64_t atoi64(const std::string &str)
static void ReadOrderPos(int64_t &nOrderPos, mapValue_t &mapValue)
Definition: transaction.h:23
std::map< std::string, std::string > mapValue_t
Definition: transaction.h:21
static void WriteOrderPos(const int64_t &nOrderPos, mapValue_t &mapValue)
Definition: transaction.h:33