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