Bitcoin Core  24.99.0
P2P Digital Currency
transactionrecord.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-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 #include <qt/transactionrecord.h>
6 
7 #include <chain.h>
8 #include <interfaces/wallet.h>
9 #include <key_io.h>
10 #include <wallet/ismine.h>
11 
12 #include <stdint.h>
13 
14 #include <QDateTime>
15 
18 using wallet::isminetype;
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  uint256 hash = wtx.tx->GetHash();
40  std::map<std::string, std::string> mapValue = wtx.value_map;
41 
42  if (nNet > 0 || wtx.is_coinbase)
43  {
44  //
45  // Credit
46  //
47  for(unsigned int i = 0; i < wtx.tx->vout.size(); i++)
48  {
49  const CTxOut& txout = wtx.tx->vout[i];
50  isminetype mine = wtx.txout_is_mine[i];
51  if(mine)
52  {
53  TransactionRecord sub(hash, nTime);
54  sub.idx = i; // vout index
55  sub.credit = txout.nValue;
57  if (wtx.txout_address_is_mine[i])
58  {
59  // Received by Bitcoin Address
62  }
63  else
64  {
65  // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
67  sub.address = mapValue["from"];
68  }
69  if (wtx.is_coinbase)
70  {
71  // Generated
73  }
74 
75  parts.append(sub);
76  }
77  }
78  }
79  else
80  {
81  bool involvesWatchAddress = false;
82  isminetype fAllFromMe = ISMINE_SPENDABLE;
83  for (const isminetype mine : wtx.txin_is_mine)
84  {
85  if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
86  if(fAllFromMe > mine) fAllFromMe = mine;
87  }
88 
89  isminetype fAllToMe = ISMINE_SPENDABLE;
90  for (const isminetype mine : wtx.txout_is_mine)
91  {
92  if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
93  if(fAllToMe > mine) fAllToMe = mine;
94  }
95 
96  if (fAllFromMe && fAllToMe)
97  {
98  // Payment to self
99  std::string address;
100  for (auto it = wtx.txout_address.begin(); it != wtx.txout_address.end(); ++it) {
101  if (it != wtx.txout_address.begin()) address += ", ";
102  address += EncodeDestination(*it);
103  }
104 
105  CAmount nChange = wtx.change;
106  parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, address, -(nDebit - nChange), nCredit - nChange));
107  parts.last().involvesWatchAddress = involvesWatchAddress; // maybe pass to TransactionRecord as constructor argument
108  }
109  else if (fAllFromMe)
110  {
111  //
112  // Debit
113  //
114  CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
115 
116  for (unsigned int nOut = 0; nOut < wtx.tx->vout.size(); nOut++)
117  {
118  const CTxOut& txout = wtx.tx->vout[nOut];
119  TransactionRecord sub(hash, nTime);
120  sub.idx = nOut;
122 
123  if(wtx.txout_is_mine[nOut])
124  {
125  // Ignore parts sent to self, as this is usually the change
126  // from a transaction sent back to our own address.
127  continue;
128  }
129 
130  if (!std::get_if<CNoDestination>(&wtx.txout_address[nOut]))
131  {
132  // Sent to Bitcoin Address
134  sub.address = EncodeDestination(wtx.txout_address[nOut]);
135  }
136  else
137  {
138  // Sent to IP, or other non-address transaction like OP_EVAL
140  sub.address = mapValue["to"];
141  }
142 
143  CAmount nValue = txout.nValue;
144  /* Add fee to first output */
145  if (nTxFee > 0)
146  {
147  nValue += nTxFee;
148  nTxFee = 0;
149  }
150  sub.debit = -nValue;
151 
152  parts.append(sub);
153  }
154  }
155  else
156  {
157  //
158  // Mixed debit transaction, can't break down payees
159  //
160  parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
161  parts.last().involvesWatchAddress = involvesWatchAddress;
162  }
163  }
164 
165  return parts;
166 }
167 
168 void TransactionRecord::updateStatus(const interfaces::WalletTxStatus& wtx, const uint256& block_hash, int numBlocks, int64_t block_time)
169 {
170  // Determine transaction status
171 
172  // Sort order, unrecorded transactions sort to the top
173  status.sortKey = strprintf("%010d-%01d-%010u-%03d",
174  wtx.block_height,
175  wtx.is_coinbase ? 1 : 0,
176  wtx.time_received,
177  idx);
180  status.m_cur_block_hash = block_hash;
181 
182  // For generated transactions, determine maturity
184  if (wtx.blocks_to_maturity > 0)
185  {
187 
188  if (wtx.is_in_main_chain)
189  {
191  }
192  else
193  {
195  }
196  }
197  else
198  {
200  }
201  }
202  else
203  {
204  if (status.depth < 0)
205  {
207  }
208  else if (status.depth == 0)
209  {
211  if (wtx.is_abandoned)
213  }
215  {
217  }
218  else
219  {
221  }
222  }
223  status.needsUpdate = false;
224 }
225 
226 bool TransactionRecord::statusUpdateNeeded(const uint256& block_hash) const
227 {
228  assert(!block_hash.IsNull());
229  return status.m_cur_block_hash != block_hash || status.needsUpdate;
230 }
231 
233 {
234  return QString::fromStdString(hash.ToString());
235 }
236 
238 {
239  return idx;
240 }
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
An output of a transaction.
Definition: transaction.h:157
CAmount nValue
Definition: transaction.h:159
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.
bool involvesWatchAddress
Whether the transaction was sent/received with a watch-only address.
std::string ToString() const
Definition: uint256.cpp:64
bool IsNull() const
Definition: uint256.h:34
256-bit opaque blob.
Definition: uint256.h:119
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:276
isminetype
IsMine() return codes, which depend on ScriptPubKeyMan implementation.
Definition: ismine.h:41
@ ISMINE_SPENDABLE
Definition: ismine.h:44
@ ISMINE_WATCH_ONLY
Definition: ismine.h:43
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:388
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:387
std::map< std::string, std::string > value_map
Definition: wallet.h:396
std::vector< wallet::isminetype > txout_is_mine
Definition: wallet.h:389
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:1164
assert(!tx.IsCoinBase())