Bitcoin ABC  0.24.7
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);
49  sub.idx = i; // vout index
50  sub.credit = txout.nValue;
52  if (wtx.txout_address_is_mine[i]) {
53  // Received by Bitcoin Address
55  sub.address =
57  } else {
58  // Received by IP connection (deprecated features), or a
59  // multisignature or other non-simple transaction
61  sub.address = mapValue["from"];
62  }
63  if (wtx.is_coinbase) {
64  // Generated
66  }
67 
68  parts.append(sub);
69  }
70  }
71  } else {
72  bool involvesWatchAddress = false;
73  isminetype fAllFromMe = ISMINE_SPENDABLE;
74  for (const isminetype mine : wtx.txin_is_mine) {
75  if (mine & ISMINE_WATCH_ONLY) {
76  involvesWatchAddress = true;
77  }
78  if (fAllFromMe > mine) {
79  fAllFromMe = mine;
80  }
81  }
82 
83  isminetype fAllToMe = ISMINE_SPENDABLE;
84  for (const isminetype mine : wtx.txout_is_mine) {
85  if (mine & ISMINE_WATCH_ONLY) {
86  involvesWatchAddress = true;
87  }
88  if (fAllToMe > mine) {
89  fAllToMe = mine;
90  }
91  }
92 
93  if (fAllFromMe && fAllToMe) {
94  // Payment to self
95  std::string address;
96  for (auto it = wtx.txout_address.begin();
97  it != wtx.txout_address.end(); ++it) {
98  if (it != wtx.txout_address.begin()) {
99  address += ", ";
100  }
101  address += EncodeCashAddr(*it, Params());
102  }
103  Amount nChange = wtx.change;
104  parts.append(TransactionRecord(
106  -(nDebit - nChange), nCredit - nChange));
107 
108  // maybe pass to TransactionRecord as constructor argument
109  parts.last().involvesWatchAddress = involvesWatchAddress;
110  } else if (fAllFromMe) {
111  //
112  // Debit
113  //
114  Amount nTxFee = nDebit - wtx.tx->GetValueOut();
115 
116  for (size_t nOut = 0; nOut < wtx.tx->vout.size(); nOut++) {
117  const CTxOut &txout = wtx.tx->vout[nOut];
118  TransactionRecord sub(txid, nTime);
119  sub.idx = nOut;
121 
122  if (wtx.txout_is_mine[nOut]) {
123  // Ignore parts sent to self, as this is usually the change
124  // from a transaction sent back to our own address.
125  continue;
126  }
127 
128  if (!boost::get<CNoDestination>(&wtx.txout_address[nOut])) {
129  // Sent to Bitcoin Address
131  sub.address =
132  EncodeCashAddr(wtx.txout_address[nOut], Params());
133  } else {
134  // Sent to IP, or other non-address transaction like OP_EVAL
136  sub.address = mapValue["to"];
137  }
138 
139  Amount nValue = txout.nValue;
140  /* Add fee to first output */
141  if (nTxFee > Amount::zero()) {
142  nValue += nTxFee;
143  nTxFee = Amount::zero();
144  }
145  sub.debit = -1 * nValue;
146 
147  parts.append(sub);
148  }
149  } else {
150  //
151  // Mixed debit transaction, can't break down payees
152  //
153  parts.append(TransactionRecord(txid, nTime,
154  TransactionRecord::Other, "", nNet,
155  Amount::zero()));
156  parts.last().involvesWatchAddress = involvesWatchAddress;
157  }
158  }
159 
160  return parts;
161 }
162 
164  const BlockHash &block_hash, int numBlocks,
165  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.m_cur_block_hash = block_hash;
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(const BlockHash &block_hash) const {
216  assert(!block_hash.IsNull());
217  return status.m_cur_block_hash != block_hash;
218 }
219 
220 QString TransactionRecord::getTxID() const {
221  return QString::fromStdString(txid.ToString());
222 }
223 
225  return idx;
226 }
TransactionRecord::txid
TxId txid
Definition: transactionrecord.h:128
CTxOut::nValue
Amount nValue
Definition: transaction.h:132
TransactionRecord::idx
int idx
Subtransaction index, for sort key.
Definition: transactionrecord.h:137
interfaces::WalletTx::txout_address_is_mine
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:377
TransactionRecord::decomposeTransaction
static QList< TransactionRecord > decomposeTransaction(const interfaces::WalletTx &wtx)
Decompose CWallet transaction to model transaction records.
Definition: transactionrecord.cpp:31
TransactionStatus::sortKey
std::string sortKey
Sorting key based on status.
Definition: transactionrecord.h:62
TransactionStatus::OpenUntilDate
@ OpenUntilDate
Normal (sent/received) transactions.
Definition: transactionrecord.h:39
LOCKTIME_THRESHOLD
static const unsigned int LOCKTIME_THRESHOLD
Definition: script.h:39
interfaces::WalletTx::change
Amount change
Definition: wallet.h:380
interfaces::WalletTxStatus::depth_in_main_chain
int depth_in_main_chain
Definition: wallet.h:390
MAX_BLOCK_TIME_GAP
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
key_io.h
TransactionRecord::status
TransactionStatus status
Status: can change with block chain update.
Definition: transactionrecord.h:140
TransactionRecord::RecvWithAddress
@ RecvWithAddress
Definition: transactionrecord.h:98
ISMINE_WATCH_ONLY
@ ISMINE_WATCH_ONLY
Definition: ismine.h:20
interfaces::WalletTxStatus::is_final
bool is_final
Definition: wallet.h:393
interfaces::WalletTx::is_coinbase
bool is_coinbase
Definition: wallet.h:383
interfaces::WalletTxStatus::block_height
int block_height
Definition: wallet.h:388
TransactionRecord::debit
Amount debit
Definition: transactionrecord.h:132
TransactionStatus::Conflicted
@ Conflicted
Conflicts with other transaction or mempool.
Definition: transactionrecord.h:49
TransactionStatus::m_cur_block_hash
BlockHash m_cur_block_hash
Current block hash (to know whether cached status is still valid)
Definition: transactionrecord.h:84
chainparams.h
interfaces::WalletTx::time
int64_t time
Definition: wallet.h:381
interfaces::WalletTx::credit
Amount credit
Definition: wallet.h:378
TransactionRecord::RecommendedNumConfirmations
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.
Definition: transactionrecord.h:104
TransactionRecord::RecvFromOther
@ RecvFromOther
Definition: transactionrecord.h:99
TransactionRecord
UI model for a transaction.
Definition: transactionrecord.h:91
interfaces::WalletTxStatus::is_coinbase
bool is_coinbase
Definition: wallet.h:396
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
ismine.h
interfaces::WalletTx::value_map
std::map< std::string, std::string > value_map
Definition: wallet.h:382
isminetype
isminetype
IsMine() return codes.
Definition: ismine.h:18
TransactionStatus::open_for
qint64 open_for
Timestamp if status==OpenUntilDate, otherwise number of additional blocks that need to be mined befor...
Definition: transactionrecord.h:77
CTxOut
An output of a transaction.
Definition: transaction.h:130
TransactionRecord::SendToOther
@ SendToOther
Definition: transactionrecord.h:97
EncodeCashAddr
std::string EncodeCashAddr(const CTxDestination &dst, const CChainParams &params)
Definition: cashaddrenc.cpp:91
TransactionRecord::involvesWatchAddress
bool involvesWatchAddress
Whether the transaction was sent/received with a watch-only address.
Definition: transactionrecord.h:143
TransactionRecord::statusUpdateNeeded
bool statusUpdateNeeded(const BlockHash &block_hash) const
Return whether a status update is needed.
Definition: transactionrecord.cpp:215
interfaces::WalletTx
Definition: wallet.h:372
TransactionRecord::Generated
@ Generated
Definition: transactionrecord.h:95
base_blob::ToString
std::string ToString() const
Definition: uint256.h:78
interfaces::WalletTxStatus
Updated transaction status.
Definition: wallet.h:387
TxId
A TxId is the identifier of a transaction.
Definition: txid.h:14
TransactionStatus::Abandoned
@ Abandoned
Abandoned from the wallet.
Definition: transactionrecord.h:51
TransactionRecord::SendToSelf
@ SendToSelf
Definition: transactionrecord.h:100
Amount
Definition: amount.h:19
interfaces::WalletTxStatus::is_in_main_chain
bool is_in_main_chain
Definition: wallet.h:397
TransactionStatus::Immature
@ Immature
Generated (mined) transactions.
Definition: transactionrecord.h:54
interfaces::WalletTxStatus::blocks_to_maturity
int blocks_to_maturity
Definition: wallet.h:389
TransactionStatus::OpenUntilBlock
@ OpenUntilBlock
Transaction not yet final, waiting for block.
Definition: transactionrecord.h:41
interfaces::WalletTx::txin_is_mine
std::vector< isminetype > txin_is_mine
Definition: wallet.h:374
TransactionRecord::type
Type type
Definition: transactionrecord.h:130
BlockHash
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
TransactionStatus::Confirmed
@ Confirmed
Have 6 or more confirmations (normal tx) or fully mature (mined tx)
Definition: transactionrecord.h:36
transactionrecord.h
ISMINE_SPENDABLE
@ ISMINE_SPENDABLE
Definition: ismine.h:21
TransactionRecord::address
std::string address
Definition: transactionrecord.h:131
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
interfaces::WalletTxStatus::is_trusted
bool is_trusted
Definition: wallet.h:394
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:30
interfaces::WalletTx::txout_is_mine
std::vector< isminetype > txout_is_mine
Definition: wallet.h:375
TransactionStatus::Confirming
@ Confirming
Confirmed, but waiting for the recommended number of confirmations.
Definition: transactionrecord.h:47
TransactionStatus::status
Status status
Definition: transactionrecord.h:71
TransactionStatus::matures_in
int matures_in
Definition: transactionrecord.h:66
interfaces::WalletTxStatus::lock_time
uint32_t lock_time
Definition: wallet.h:392
TransactionRecord::credit
Amount credit
Definition: transactionrecord.h:133
TransactionRecord::getOutputIndex
int getOutputIndex() const
Return the output index of the subtransaction
Definition: transactionrecord.cpp:224
interfaces::WalletTxStatus::is_abandoned
bool is_abandoned
Definition: wallet.h:395
interfaces::WalletTx::txout_address
std::vector< CTxDestination > txout_address
Definition: wallet.h:376
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:508
TransactionRecord::SendToAddress
@ SendToAddress
Definition: transactionrecord.h:96
interfaces::WalletTx::debit
Amount debit
Definition: wallet.h:379
wallet.h
TransactionStatus::depth
qint64 depth
Definition: transactionrecord.h:72
TransactionStatus::NotAccepted
@ NotAccepted
Mined but not accepted.
Definition: transactionrecord.h:56
interfaces::WalletTxStatus::time_received
unsigned int time_received
Definition: wallet.h:391
TransactionStatus::countsForBalance
bool countsForBalance
Transaction counts towards available balance.
Definition: transactionrecord.h:60
TransactionStatus::Unconfirmed
@ Unconfirmed
Not yet mined into a block.
Definition: transactionrecord.h:43
TransactionRecord::getTxID
QString getTxID() const
Return the unique identifier for this transaction (part)
Definition: transactionrecord.cpp:220
TransactionRecord::TransactionRecord
TransactionRecord()
Definition: transactionrecord.h:106
TransactionRecord::updateStatus
void updateStatus(const interfaces::WalletTxStatus &wtx, const BlockHash &block_hash, int numBlocks, int64_t block_time)
Update status from core wallet tx.
Definition: transactionrecord.cpp:163
interfaces::WalletTx::tx
CTransactionRef tx
Definition: wallet.h:373
cashaddrenc.h
TransactionRecord::Other
@ Other
Definition: transactionrecord.h:94
TransactionRecord::showTransaction
static bool showTransaction()
Decompose CWallet transaction to model transaction records.
Definition: transactionrecord.cpp:21