Bitcoin Core  0.18.99
P2P Digital Currency
transactionrecord.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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 <stdint.h>
12 
13 #include <QDateTime>
14 
15 /* Return positive answer if transaction should be shown in list.
16  */
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  */
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  uint256 hash = wtx.tx->GetHash();
35  std::map<std::string, std::string> mapValue = wtx.value_map;
36 
37  if (nNet > 0 || wtx.is_coinbase)
38  {
39  //
40  // Credit
41  //
42  for(unsigned int i = 0; i < wtx.tx->vout.size(); i++)
43  {
44  const CTxOut& txout = wtx.tx->vout[i];
45  isminetype mine = wtx.txout_is_mine[i];
46  if(mine)
47  {
48  TransactionRecord sub(hash, nTime);
50  sub.idx = i; // vout index
51  sub.credit = txout.nValue;
53  if (wtx.txout_address_is_mine[i])
54  {
55  // Received by Bitcoin Address
58  }
59  else
60  {
61  // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
63  sub.address = mapValue["from"];
64  }
65  if (wtx.is_coinbase)
66  {
67  // Generated
69  }
70 
71  parts.append(sub);
72  }
73  }
74  }
75  else
76  {
77  bool involvesWatchAddress = false;
78  isminetype fAllFromMe = ISMINE_SPENDABLE;
79  for (const isminetype mine : wtx.txin_is_mine)
80  {
81  if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
82  if(fAllFromMe > mine) fAllFromMe = mine;
83  }
84 
85  isminetype fAllToMe = ISMINE_SPENDABLE;
86  for (const isminetype mine : wtx.txout_is_mine)
87  {
88  if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
89  if(fAllToMe > mine) fAllToMe = mine;
90  }
91 
92  if (fAllFromMe && fAllToMe)
93  {
94  // Payment to self
95  CAmount nChange = wtx.change;
96 
97  parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
98  -(nDebit - nChange), nCredit - nChange));
99  parts.last().involvesWatchAddress = involvesWatchAddress; // maybe pass to TransactionRecord as constructor argument
100  }
101  else if (fAllFromMe)
102  {
103  //
104  // Debit
105  //
106  CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
107 
108  for (unsigned int nOut = 0; nOut < wtx.tx->vout.size(); nOut++)
109  {
110  const CTxOut& txout = wtx.tx->vout[nOut];
111  TransactionRecord sub(hash, nTime);
112  sub.idx = nOut;
114 
115  if(wtx.txout_is_mine[nOut])
116  {
117  // Ignore parts sent to self, as this is usually the change
118  // from a transaction sent back to our own address.
119  continue;
120  }
121 
122  if (!boost::get<CNoDestination>(&wtx.txout_address[nOut]))
123  {
124  // Sent to Bitcoin Address
126  sub.address = EncodeDestination(wtx.txout_address[nOut]);
127  }
128  else
129  {
130  // Sent to IP, or other non-address transaction like OP_EVAL
132  sub.address = mapValue["to"];
133  }
134 
135  CAmount nValue = txout.nValue;
136  /* Add fee to first output */
137  if (nTxFee > 0)
138  {
139  nValue += nTxFee;
140  nTxFee = 0;
141  }
142  sub.debit = -nValue;
143 
144  parts.append(sub);
145  }
146  }
147  else
148  {
149  //
150  // Mixed debit transaction, can't break down payees
151  //
152  parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
153  parts.last().involvesWatchAddress = involvesWatchAddress;
154  }
155  }
156 
157  return parts;
158 }
159 
160 void TransactionRecord::updateStatus(const interfaces::WalletTxStatus& wtx, int numBlocks, int64_t block_time)
161 {
162  // Determine transaction status
163 
164  // Sort order, unrecorded transactions sort to the top
165  status.sortKey = strprintf("%010d-%01d-%010u-%03d",
166  wtx.block_height,
167  wtx.is_coinbase ? 1 : 0,
168  wtx.time_received,
169  idx);
172  status.cur_num_blocks = numBlocks;
173 
174  const bool up_to_date = ((int64_t)QDateTime::currentMSecsSinceEpoch() / 1000 - block_time < MAX_BLOCK_TIME_GAP);
175  if (up_to_date && !wtx.is_final) {
176  if (wtx.lock_time < LOCKTIME_THRESHOLD) {
178  status.open_for = wtx.lock_time - numBlocks;
179  }
180  else
181  {
183  status.open_for = wtx.lock_time;
184  }
185  }
186  // For generated transactions, determine maturity
188  {
189  if (wtx.blocks_to_maturity > 0)
190  {
192 
193  if (wtx.is_in_main_chain)
194  {
196  }
197  else
198  {
200  }
201  }
202  else
203  {
205  }
206  }
207  else
208  {
209  if (status.depth < 0)
210  {
212  }
213  else if (status.depth == 0)
214  {
216  if (wtx.is_abandoned)
218  }
220  {
222  }
223  else
224  {
226  }
227  }
228  status.needsUpdate = false;
229 }
230 
231 bool TransactionRecord::statusUpdateNeeded(int numBlocks) const
232 {
233  return status.cur_num_blocks != numBlocks || status.needsUpdate;
234 }
235 
237 {
238  return QString::fromStdString(hash.ToString());
239 }
240 
242 {
243  return idx;
244 }
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:344
Transaction not yet final, waiting for block.
int idx
Subtransaction index, for sort key.
unsigned int time_received
Definition: wallet.h:360
QString getTxHash() const
Return the unique identifier for this transaction (part)
Generated (mined) transactions.
#define strprintf
Definition: tinyformat.h:1066
std::vector< isminetype > txin_is_mine
Definition: wallet.h:342
std::vector< isminetype > txout_is_mine
Definition: wallet.h:343
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:341
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.
isminetype
IsMine() return codes.
Definition: ismine.h:18
bool countsForBalance
Transaction counts towards available balance.
An output of a transaction.
Definition: transaction.h:133
std::string ToString() const
Definition: uint256.cpp:62
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:121
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:345
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:211
std::map< std::string, std::string > value_map
Definition: wallet.h:350
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:139
Abandoned from the wallet.
Updated transaction status.
Definition: wallet.h:355
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.