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 #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  CAmount nChange = wtx.change;
97 
98  parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
99  -(nDebit - nChange), nCredit - nChange));
100  parts.last().involvesWatchAddress = involvesWatchAddress; // maybe pass to TransactionRecord as constructor argument
101  }
102  else if (fAllFromMe)
103  {
104  //
105  // Debit
106  //
107  CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
108 
109  for (unsigned int nOut = 0; nOut < wtx.tx->vout.size(); nOut++)
110  {
111  const CTxOut& txout = wtx.tx->vout[nOut];
112  TransactionRecord sub(hash, nTime);
113  sub.idx = nOut;
115 
116  if(wtx.txout_is_mine[nOut])
117  {
118  // Ignore parts sent to self, as this is usually the change
119  // from a transaction sent back to our own address.
120  continue;
121  }
122 
123  if (!boost::get<CNoDestination>(&wtx.txout_address[nOut]))
124  {
125  // Sent to Bitcoin Address
127  sub.address = EncodeDestination(wtx.txout_address[nOut]);
128  }
129  else
130  {
131  // Sent to IP, or other non-address transaction like OP_EVAL
133  sub.address = mapValue["to"];
134  }
135 
136  CAmount nValue = txout.nValue;
137  /* Add fee to first output */
138  if (nTxFee > 0)
139  {
140  nValue += nTxFee;
141  nTxFee = 0;
142  }
143  sub.debit = -nValue;
144 
145  parts.append(sub);
146  }
147  }
148  else
149  {
150  //
151  // Mixed debit transaction, can't break down payees
152  //
153  parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
154  parts.last().involvesWatchAddress = involvesWatchAddress;
155  }
156  }
157 
158  return parts;
159 }
160 
161 void TransactionRecord::updateStatus(const interfaces::WalletTxStatus& wtx, int numBlocks, int64_t block_time)
162 {
163  // Determine transaction status
164 
165  // Sort order, unrecorded transactions sort to the top
166  status.sortKey = strprintf("%010d-%01d-%010u-%03d",
167  wtx.block_height,
168  wtx.is_coinbase ? 1 : 0,
169  wtx.time_received,
170  idx);
173  status.cur_num_blocks = numBlocks;
174 
175  const bool up_to_date = ((int64_t)QDateTime::currentMSecsSinceEpoch() / 1000 - block_time < MAX_BLOCK_TIME_GAP);
176  if (up_to_date && !wtx.is_final) {
177  if (wtx.lock_time < LOCKTIME_THRESHOLD) {
179  status.open_for = wtx.lock_time - numBlocks;
180  }
181  else
182  {
184  status.open_for = wtx.lock_time;
185  }
186  }
187  // For generated transactions, determine maturity
189  {
190  if (wtx.blocks_to_maturity > 0)
191  {
193 
194  if (wtx.is_in_main_chain)
195  {
197  }
198  else
199  {
201  }
202  }
203  else
204  {
206  }
207  }
208  else
209  {
210  if (status.depth < 0)
211  {
213  }
214  else if (status.depth == 0)
215  {
217  if (wtx.is_abandoned)
219  }
221  {
223  }
224  else
225  {
227  }
228  }
229  status.needsUpdate = false;
230 }
231 
232 bool TransactionRecord::statusUpdateNeeded(int numBlocks) const
233 {
234  return status.cur_num_blocks != numBlocks || status.needsUpdate;
235 }
236 
238 {
239  return QString::fromStdString(hash.ToString());
240 }
241 
243 {
244  return idx;
245 }
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:336
Transaction not yet final, waiting for block.
int idx
Subtransaction index, for sort key.
unsigned int time_received
Definition: wallet.h:352
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:1067
std::vector< isminetype > txin_is_mine
Definition: wallet.h:334
std::vector< isminetype > txout_is_mine
Definition: wallet.h:335
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:333
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:61
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:337
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:342
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:139
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:347
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.