Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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;
122 std::multimap<int64_t, CWalletTx *>::const_iterator m_it_wtxOrdered;
123
124 // memory only
139 mutable bool m_is_cache_empty{true};
140 mutable bool fChangeCached;
141 mutable bool fInMempool;
143
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
190
192
193 template <typename Stream> void Serialize(Stream &s) const {
195
196 mapValueCopy["fromaccount"] = "";
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;
210 int serializedIndex =
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;
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 */
241 setAbandoned();
242 } else if (serializedIndex == -1) {
244 } else if (!m_confirm.hashBlock.IsNull()) {
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
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 }
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.
bool isAbandoned() const
mapValue_t mapValue
Key/value map with information about the transaction.
Definition transaction.h:99
CTransactionRef tx
bool isUnconfirmed() const
CWalletTx(CWalletTx const &)=delete
void setConflicted()
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.
void SetTx(CTransactionRef arg)
void Serialize(Stream &s) const
CWalletTx(CTransactionRef arg)
bool IsEquivalentTo(const CWalletTx &tx) const
void Unserialize(Stream &s)
bool isConflicted() const
Confirmation m_confirm
TxId GetId() const
@ AMOUNTTYPE_ENUM_ELEMENTS
Amount nChangeCached
void setConfirmed()
std::vector< std::pair< std::string, std::string > > vOrderForm
void Init()
bool fFromMe
From me flag is set to 1 for transactions that were created by the wallet on this bitcoin node,...
void setAbandoned()
void setUnconfirmed()
bool fChangeCached
int64_t GetTxTime() const
bool fInMempool
unsigned int fTimeReceivedIsTxTime
bool isConfirmed() const
CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS]
void MarkDirty()
make sure balances are recalculated
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
bool InMempool() const
bool IsCoinBase() const
unsigned int nTimeReceived
time received by this node
int64_t nOrderPos
position in ordered transaction list
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
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
static std::string ToString(const CService &ip)
Definition db.h:37
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...
Confirmation(Status s=UNCONFIRMED, int b=0, BlockHash h=BlockHash(), int i=0)
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...
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