Bitcoin Core  0.19.99
P2P Digital Currency
transactionrecord.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2019 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 
16 /* Return positive answer if transaction should be shown in list.
17  */
19 {
20  // There are currently no cases where we hide transactions, but
21  // we may want to use this in the future for things like RBF.
22  return true;
23 }
24 
25 /*
26  * Decompose CWallet transaction to model transaction records.
27  */
29 {
30  QList<TransactionRecord> parts;
31  int64_t nTime = wtx.time;
32  CAmount nCredit = wtx.credit;
33  CAmount nDebit = wtx.debit;
34  CAmount nNet = nCredit - nDebit;
35  uint256 hash = wtx.tx->GetHash();
36  std::map<std::string, std::string> mapValue = wtx.value_map;
37 
38  if (nNet > 0 || wtx.is_coinbase)
39  {
40  //
41  // Credit
42  //
43  for(unsigned int i = 0; i < wtx.tx->vout.size(); i++)
44  {
45  const CTxOut& txout = wtx.tx->vout[i];
46  isminetype mine = wtx.txout_is_mine[i];
47  if(mine)
48  {
49  TransactionRecord sub(hash, nTime);
51  sub.idx = i; // vout index
52  sub.credit = txout.nValue;
54  if (wtx.txout_address_is_mine[i])
55  {
56  // Received by Bitcoin Address
59  }
60  else
61  {
62  // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
64  sub.address = mapValue["from"];
65  }
66  if (wtx.is_coinbase)
67  {
68  // Generated
70  }
71 
72  parts.append(sub);
73  }
74  }
75  }
76  else
77  {
78  bool involvesWatchAddress = false;
79  isminetype fAllFromMe = ISMINE_SPENDABLE;
80  for (const isminetype mine : wtx.txin_is_mine)
81  {
82  if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
83  if(fAllFromMe > mine) fAllFromMe = mine;
84  }
85 
86  isminetype fAllToMe = ISMINE_SPENDABLE;
87  for (const isminetype mine : wtx.txout_is_mine)
88  {
89  if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
90  if(fAllToMe > mine) fAllToMe = mine;
91  }
92 
93  if (fAllFromMe && fAllToMe)
94  {
95  // Payment to self
96  std::string address;
97  for (auto it = wtx.txout_address.begin(); it != wtx.txout_address.end(); ++it) {
98  if (it != wtx.txout_address.begin()) address += ", ";
99  address += EncodeDestination(*it);
100  }
101 
102  CAmount nChange = wtx.change;
103  parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, address, -(nDebit - nChange), nCredit - nChange));
104  parts.last().involvesWatchAddress = involvesWatchAddress; // maybe pass to TransactionRecord as constructor argument
105  }
106  else if (fAllFromMe)
107  {
108  //
109  // Debit
110  //
111  CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
112 
113  for (unsigned int nOut = 0; nOut < wtx.tx->vout.size(); nOut++)
114  {
115  const CTxOut& txout = wtx.tx->vout[nOut];
116  TransactionRecord sub(hash, nTime);
117  sub.idx = nOut;
119 
120  if(wtx.txout_is_mine[nOut])
121  {
122  // Ignore parts sent to self, as this is usually the change
123  // from a transaction sent back to our own address.
124  continue;
125  }
126 
127  if (!boost::get<CNoDestination>(&wtx.txout_address[nOut]))
128  {
129  // Sent to Bitcoin Address
131  sub.address = EncodeDestination(wtx.txout_address[nOut]);
132  }
133  else
134  {
135  // Sent to IP, or other non-address transaction like OP_EVAL
137  sub.address = mapValue["to"];
138  }
139 
140  CAmount nValue = txout.nValue;
141  /* Add fee to first output */
142  if (nTxFee > 0)
143  {
144  nValue += nTxFee;
145  nTxFee = 0;
146  }
147  sub.debit = -nValue;
148 
149  parts.append(sub);
150  }
151  }
152  else
153  {
154  //
155  // Mixed debit transaction, can't break down payees
156  //
157  parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
158  parts.last().involvesWatchAddress = involvesWatchAddress;
159  }
160  }
161 
162  return parts;
163 }
164 
165 void TransactionRecord::updateStatus(const interfaces::WalletTxStatus& wtx, int numBlocks, int64_t block_time)
166 {
167  // Determine transaction status
168 
169  // Sort order, unrecorded transactions sort to the top
170  status.sortKey = strprintf("%010d-%01d-%010u-%03d",
171  wtx.block_height,
172  wtx.is_coinbase ? 1 : 0,
173  wtx.time_received,
174  idx);
177  status.cur_num_blocks = numBlocks;
178 
179  const bool up_to_date = ((int64_t)QDateTime::currentMSecsSinceEpoch() / 1000 - block_time < MAX_BLOCK_TIME_GAP);
180  if (up_to_date && !wtx.is_final) {
181  if (wtx.lock_time < LOCKTIME_THRESHOLD) {
183  status.open_for = wtx.lock_time - numBlocks;
184  }
185  else
186  {
188  status.open_for = wtx.lock_time;
189  }
190  }
191  // For generated transactions, determine maturity
193  {
194  if (wtx.blocks_to_maturity > 0)
195  {
197 
198  if (wtx.is_in_main_chain)
199  {
201  }
202  else
203  {
205  }
206  }
207  else
208  {
210  }
211  }
212  else
213  {
214  if (status.depth < 0)
215  {
217  }
218  else if (status.depth == 0)
219  {
221  if (wtx.is_abandoned)
223  }
225  {
227  }
228  else
229  {
231  }
232  }
233  status.needsUpdate = false;
234 }
235 
236 bool TransactionRecord::statusUpdateNeeded(int numBlocks) const
237 {
238  return status.cur_num_blocks != numBlocks || status.needsUpdate;
239 }
240 
242 {
243  return QString::fromStdString(hash.ToString());
244 }
245 
247 {
248  return idx;
249 }
CAmount nValue
Definition: transaction.h:136
bool statusUpdateNeeded(int numBlocks) const
Return whether a status update is needed.
Confirmed, but waiting for the recommended number of confirmations.
std::vector< CTxDestination > txout_address
Definition: wallet.h:339
Transaction not yet final, waiting for block.
int idx
Subtransaction index, for sort key.
unsigned int time_received
Definition: wallet.h:355
QString getTxHash() const
Return the unique identifier for this transaction (part)
Generated (mined) transactions.
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
std::vector< isminetype > txin_is_mine
Definition: wallet.h:337
std::vector< isminetype > txout_is_mine
Definition: wallet.h:338
Have 6 or more confirmations (normal tx) or fully mature (mined tx)
std::string sortKey
Sorting key based on status.
static QList< TransactionRecord > decomposeTransaction(const interfaces::WalletTx &wtx)
Mined but not accepted.
Not yet mined into a block.
CTransactionRef tx
Definition: wallet.h:336
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
int getOutputIndex() const
Return the output index of the subtransaction.
UI model for a transaction.
TransactionStatus status
Status: can change with block chain update.
bool countsForBalance
Transaction counts towards available balance.
isminetype
IsMine() return codes.
Definition: ismine.h:18
An output of a transaction.
Definition: transaction.h:133
std::string ToString() const
Definition: uint256.cpp:60
int cur_num_blocks
Current number of blocks (to know whether cached status is still valid)
Normal (sent/received) transactions.
static bool showTransaction()
Decompose CWallet transaction to model transaction records.
256-bit opaque blob.
Definition: uint256.h:120
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:340
void updateStatus(const interfaces::WalletTxStatus &wtx, int numBlocks, int64_t block_time)
Update status from core wallet tx.
Conflicts with other transaction or mempool.
bool involvesWatchAddress
Whether the transaction was sent/received with a watch-only address.
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:210
std::map< std::string, std::string > value_map
Definition: wallet.h:345
static const unsigned int LOCKTIME_THRESHOLD
Definition: script.h:39
qint64 open_for
Timestamp if status==OpenUntilDate, otherwise number of additional blocks that need to be mined befor...
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:143
auto it
Definition: validation.cpp:362
Abandoned from the wallet.
static constexpr int64_t MAX_BLOCK_TIME_GAP
Maximum gap between node time and block time used for the "Catching up..." mode in GUI...
Definition: chain.h:38
Updated transaction status.
Definition: wallet.h:350
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.