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