Bitcoin Core 29.99.0
P2P Digital Currency
transactionrecord.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-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
6
7#include <chain.h>
8#include <interfaces/wallet.h>
9#include <key_io.h>
10#include <wallet/types.h>
11
12#include <stdint.h>
13
14#include <QDateTime>
15
19
20/* Return positive answer if transaction should be shown in list.
21 */
23{
24 // There are currently no cases where we hide transactions, but
25 // we may want to use this in the future for things like RBF.
26 return true;
27}
28
29/*
30 * Decompose CWallet transaction to model transaction records.
31 */
33{
34 QList<TransactionRecord> parts;
35 int64_t nTime = wtx.time;
36 CAmount nCredit = wtx.credit;
37 CAmount nDebit = wtx.debit;
38 CAmount nNet = nCredit - nDebit;
39 Txid hash = wtx.tx->GetHash();
40 std::map<std::string, std::string> mapValue = wtx.value_map;
41
42 isminetype fAllFromMe = ISMINE_SPENDABLE;
43 bool any_from_me = false;
44 if (wtx.is_coinbase) {
45 fAllFromMe = ISMINE_NO;
46 } else {
47 for (const isminetype mine : wtx.txin_is_mine)
48 {
49 if(fAllFromMe > mine) fAllFromMe = mine;
50 if (mine) any_from_me = true;
51 }
52 }
53
54 if (fAllFromMe || !any_from_me) {
55 CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
56
57 for(unsigned int i = 0; i < wtx.tx->vout.size(); i++)
58 {
59 const CTxOut& txout = wtx.tx->vout[i];
60
61 if (fAllFromMe) {
62 // Change is only really possible if we're the sender
63 // Otherwise, someone just sent bitcoins to a change address, which should be shown
64 if (wtx.txout_is_change[i]) {
65 continue;
66 }
67
68 //
69 // Debit
70 //
71
72 TransactionRecord sub(hash, nTime);
73 sub.idx = i;
74
75 if (!std::get_if<CNoDestination>(&wtx.txout_address[i]))
76 {
77 // Sent to Bitcoin Address
80 }
81 else
82 {
83 // Sent to IP, or other non-address transaction like OP_EVAL
85 sub.address = mapValue["to"];
86 }
87
88 CAmount nValue = txout.nValue;
89 /* Add fee to first output */
90 if (nTxFee > 0)
91 {
92 nValue += nTxFee;
93 nTxFee = 0;
94 }
95 sub.debit = -nValue;
96
97 parts.append(sub);
98 }
99
100 isminetype mine = wtx.txout_is_mine[i];
101 if(mine)
102 {
103 //
104 // Credit
105 //
106
107 TransactionRecord sub(hash, nTime);
108 sub.idx = i; // vout index
109 sub.credit = txout.nValue;
110 if (wtx.txout_address_is_mine[i])
111 {
112 // Received by Bitcoin Address
115 }
116 else
117 {
118 // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
120 sub.address = mapValue["from"];
121 }
122 if (wtx.is_coinbase)
123 {
124 // Generated
126 }
127
128 parts.append(sub);
129 }
130 }
131 } else {
132 //
133 // Mixed debit transaction, can't break down payees
134 //
135 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
136 }
137
138 return parts;
139}
140
141void TransactionRecord::updateStatus(const interfaces::WalletTxStatus& wtx, const uint256& block_hash, int numBlocks, int64_t block_time)
142{
143 // Determine transaction status
144
145 // Sort order, unrecorded transactions sort to the top
146 int typesort;
147 switch (type) {
148 case SendToAddress: case SendToOther:
149 typesort = 2; break;
151 typesort = 3; break;
152 default:
153 typesort = 9;
154 }
155 status.sortKey = strprintf("%010d-%01d-%010u-%03d-%d",
156 wtx.block_height,
157 wtx.is_coinbase ? 1 : 0,
158 wtx.time_received,
159 idx,
160 typesort);
163 status.m_cur_block_hash = block_hash;
164
165 // For generated transactions, determine maturity
167 if (wtx.blocks_to_maturity > 0)
168 {
170
171 if (wtx.is_in_main_chain)
172 {
174 }
175 else
176 {
178 }
179 }
180 else
181 {
183 }
184 }
185 else
186 {
187 if (status.depth < 0)
188 {
190 }
191 else if (status.depth == 0)
192 {
194 if (wtx.is_abandoned)
196 }
198 {
200 }
201 else
202 {
204 }
205 }
206 status.needsUpdate = false;
207}
208
210{
211 assert(!block_hash.IsNull());
212 return status.m_cur_block_hash != block_hash || status.needsUpdate;
213}
214
216{
217 return QString::fromStdString(hash.ToString());
218}
219
221{
222 return idx;
223}
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
An output of a transaction.
Definition: transaction.h:150
CAmount nValue
Definition: transaction.h:152
UI model for a transaction.
int idx
Subtransaction index, for sort key.
static QList< TransactionRecord > decomposeTransaction(const interfaces::WalletTx &wtx)
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.
static bool showTransaction()
Decompose CWallet transaction to model transaction records.
TransactionStatus status
Status: can change with block chain update.
int getOutputIndex() const
Return the output index of the subtransaction
QString getTxHash() const
Return the unique identifier for this transaction (part)
bool statusUpdateNeeded(const uint256 &block_hash) const
Return whether a status update is needed.
void updateStatus(const interfaces::WalletTxStatus &wtx, const uint256 &block_hash, int numBlocks, int64_t block_time)
Update status from core wallet tx.
constexpr bool IsNull() const
Definition: uint256.h:48
std::string ToString() const
256-bit opaque blob.
Definition: uint256.h:196
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:294
isminetype
IsMine() return codes, which depend on ScriptPubKeyMan implementation.
Definition: types.h:41
@ ISMINE_NO
Definition: types.h:42
@ ISMINE_SPENDABLE
Definition: types.h:44
bool countsForBalance
Transaction counts towards available balance.
uint256 m_cur_block_hash
Current block hash (to know whether cached status is still valid)
@ Confirmed
Have 6 or more confirmations (normal tx) or fully mature (mined tx)
@ Unconfirmed
Normal (sent/received) transactions.
@ Immature
Generated (mined) transactions.
@ Confirming
Confirmed, but waiting for the recommended number of confirmations.
@ NotAccepted
Mined but not accepted.
@ Conflicted
Conflicts with other transaction or mempool.
@ Abandoned
Abandoned from the wallet.
std::string sortKey
Sorting key based on status.
std::vector< wallet::isminetype > txin_is_mine
Definition: wallet.h:387
std::vector< CTxDestination > txout_address
Definition: wallet.h:390
std::vector< wallet::isminetype > txout_address_is_mine
Definition: wallet.h:391
CTransactionRef tx
Definition: wallet.h:386
std::vector< bool > txout_is_change
Definition: wallet.h:389
std::map< std::string, std::string > value_map
Definition: wallet.h:396
std::vector< wallet::isminetype > txout_is_mine
Definition: wallet.h:388
Updated transaction status.
Definition: wallet.h:404
unsigned int time_received
Definition: wallet.h:408
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
assert(!tx.IsCoinBase())
is a home for public enum and struct type definitions that are used by internally by wallet code,...