Bitcoin ABC  0.22.13
P2P Digital Currency
transactionrecord.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 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 <cashaddrenc.h>
8 #include <chain.h> // For MAX_BLOCK_TIME_GAP
9 #include <chainparams.h> // For Params()
10 #include <interfaces/wallet.h>
11 #include <key_io.h>
12 #include <wallet/ismine.h>
13 
14 #include <QDateTime>
15 
16 #include <cstdint>
17 
22  // There are currently no cases where we hide transactions, but we may want
23  // to use this in the future for things like RBF.
24  return true;
25 }
26 
30 QList<TransactionRecord>
32  QList<TransactionRecord> parts;
33  int64_t nTime = wtx.time;
34  Amount nCredit = wtx.credit;
35  Amount nDebit = wtx.debit;
36  Amount nNet = nCredit - nDebit;
37  const TxId &txid = wtx.tx->GetId();
38  std::map<std::string, std::string> mapValue = wtx.value_map;
39 
40  if (nNet > Amount::zero() || wtx.is_coinbase) {
41  //
42  // Credit
43  //
44  for (size_t i = 0; i < wtx.tx->vout.size(); i++) {
45  const CTxOut &txout = wtx.tx->vout[i];
46  isminetype mine = wtx.txout_is_mine[i];
47  if (mine) {
48  TransactionRecord sub(txid, nTime);
50  sub.idx = i; // vout index
51  sub.credit = txout.nValue;
53  if (wtx.txout_address_is_mine[i]) {
54  // Received by Bitcoin Address
56  sub.address =
58  } else {
59  // Received by IP connection (deprecated features), or a
60  // multisignature or other non-simple transaction
62  sub.address = mapValue["from"];
63  }
64  if (wtx.is_coinbase) {
65  // Generated
67  }
68 
69  parts.append(sub);
70  }
71  }
72  } else {
73  bool involvesWatchAddress = false;
74  isminetype fAllFromMe = ISMINE_SPENDABLE;
75  for (const isminetype mine : wtx.txin_is_mine) {
76  if (mine & ISMINE_WATCH_ONLY) {
77  involvesWatchAddress = true;
78  }
79  if (fAllFromMe > mine) {
80  fAllFromMe = mine;
81  }
82  }
83 
84  isminetype fAllToMe = ISMINE_SPENDABLE;
85  for (const isminetype mine : wtx.txout_is_mine) {
86  if (mine & ISMINE_WATCH_ONLY) {
87  involvesWatchAddress = true;
88  }
89  if (fAllToMe > mine) {
90  fAllToMe = mine;
91  }
92  }
93 
94  if (fAllFromMe && fAllToMe) {
95  // Payment to self
96  std::string address;
97  for (auto it = wtx.txout_address.begin();
98  it != wtx.txout_address.end(); ++it) {
99  if (it != wtx.txout_address.begin()) {
100  address += ", ";
101  }
102  address += EncodeCashAddr(*it, Params());
103  }
104  Amount nChange = wtx.change;
105  parts.append(TransactionRecord(
106  txid, nTime, TransactionRecord::SendToSelf, address,
107  -(nDebit - nChange), nCredit - nChange));
108 
109  // maybe pass to TransactionRecord as constructor argument
110  parts.last().involvesWatchAddress = involvesWatchAddress;
111  } else if (fAllFromMe) {
112  //
113  // Debit
114  //
115  Amount nTxFee = nDebit - wtx.tx->GetValueOut();
116 
117  for (size_t nOut = 0; nOut < wtx.tx->vout.size(); nOut++) {
118  const CTxOut &txout = wtx.tx->vout[nOut];
119  TransactionRecord sub(txid, nTime);
120  sub.idx = nOut;
122 
123  if (wtx.txout_is_mine[nOut]) {
124  // Ignore parts sent to self, as this is usually the change
125  // from a transaction sent back to our own address.
126  continue;
127  }
128 
129  if (!boost::get<CNoDestination>(&wtx.txout_address[nOut])) {
130  // Sent to Bitcoin Address
132  sub.address =
133  EncodeCashAddr(wtx.txout_address[nOut], Params());
134  } else {
135  // Sent to IP, or other non-address transaction like OP_EVAL
137  sub.address = mapValue["to"];
138  }
139 
140  Amount nValue = txout.nValue;
141  /* Add fee to first output */
142  if (nTxFee > Amount::zero()) {
143  nValue += nTxFee;
144  nTxFee = Amount::zero();
145  }
146  sub.debit = -1 * nValue;
147 
148  parts.append(sub);
149  }
150  } else {
151  //
152  // Mixed debit transaction, can't break down payees
153  //
154  parts.append(TransactionRecord(txid, nTime,
155  TransactionRecord::Other, "", nNet,
156  Amount::zero()));
157  parts.last().involvesWatchAddress = involvesWatchAddress;
158  }
159  }
160 
161  return parts;
162 }
163 
165  int numBlocks, int64_t block_time) {
166  // Determine transaction status
167 
168  // Sort order, unrecorded transactions sort to the top
169  status.sortKey = strprintf("%010d-%01d-%010u-%03d", wtx.block_height,
170  wtx.is_coinbase ? 1 : 0, wtx.time_received, idx);
173  status.cur_num_blocks = numBlocks;
174 
175  const bool up_to_date =
176  (int64_t(QDateTime::currentMSecsSinceEpoch()) / 1000 - block_time <
178  if (up_to_date && !wtx.is_final) {
179  if (wtx.lock_time < LOCKTIME_THRESHOLD) {
181  status.open_for = wtx.lock_time - numBlocks;
182  } else {
184  status.open_for = wtx.lock_time;
185  }
186  } else if (type == TransactionRecord::Generated) {
187  // For generated transactions, determine maturity
188  if (wtx.blocks_to_maturity > 0) {
190 
191  if (wtx.is_in_main_chain) {
193  } else {
195  }
196  } else {
198  }
199  } else {
200  if (status.depth < 0) {
202  } else if (status.depth == 0) {
204  if (wtx.is_abandoned) {
206  }
209  } else {
211  }
212  }
213 }
214 
215 bool TransactionRecord::statusUpdateNeeded(int numBlocks) const {
216  return status.cur_num_blocks != numBlocks;
217 }
218 
219 QString TransactionRecord::getTxID() const {
220  return QString::fromStdString(txid.ToString());
221 }
222 
224  return idx;
225 }
bool statusUpdateNeeded(int numBlocks) const
Return whether a status update is needed.
Confirmed, but waiting for the recommended number of confirmations.
static constexpr Amount zero()
Definition: amount.h:35
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:359
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:1201
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.
Definition: amount.h:17
static QList< TransactionRecord > decomposeTransaction(const interfaces::WalletTx &wtx)
Decompose CWallet transaction to model transaction records.
Mined but not accepted.
Not yet mined into a block.
CTransactionRef tx
Definition: wallet.h:341
int getOutputIndex() const
Return the output index of the subtransaction.
UI model for a transaction.
TransactionStatus status
Status: can change with block chain update.
QString getTxID() const
Return the unique identifier for this transaction (part)
bool countsForBalance
Transaction counts towards available balance.
isminetype
IsMine() return codes.
Definition: ismine.h:18
An output of a transaction.
Definition: transaction.h:141
std::string ToString() const
Definition: uint256.h:74
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.
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.
const CChainParams & Params()
Return the currently selected parameters.
A TxId is the identifier of a transaction.
Definition: txid.h:14
bool involvesWatchAddress
Whether the transaction was sent/received with a watch-only address.
std::string EncodeCashAddr(const CTxDestination &dst, const CChainParams &params)
Definition: cashaddrenc.cpp:91
std::map< std::string, std::string > value_map
Definition: wallet.h:350
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...
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:44
Updated transaction status.
Definition: wallet.h:355
Amount nValue
Definition: transaction.h:143
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:87
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.