BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
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 <consensus/consensus.h>
8 #include <interfaces/wallet.h>
9 #include <key_io.h>
10 #include <timedata.h>
11 #include <validation.h>
12 
13 #include <stdint.h>
14 
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 adjustedTime)
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  if (!wtx.is_final)
176  {
177  if (wtx.lock_time < LOCKTIME_THRESHOLD)
178  {
180  status.open_for = wtx.lock_time - numBlocks;
181  }
182  else
183  {
185  status.open_for = wtx.lock_time;
186  }
187  }
188  // For generated transactions, determine maturity
190  {
191  if (wtx.blocks_to_maturity > 0)
192  {
194 
195  if (wtx.is_in_main_chain)
196  {
198  }
199  else
200  {
202  }
203  }
204  else
205  {
207  }
208  }
209  else
210  {
211  if (status.depth < 0)
212  {
214  }
215  else if (status.depth == 0)
216  {
218  if (wtx.is_abandoned)
220  }
222  {
224  }
225  else
226  {
228  }
229  }
230  status.needsUpdate = false;
231 }
232 
233 bool TransactionRecord::statusUpdateNeeded(int numBlocks) const
234 {
235  return status.cur_num_blocks != numBlocks || status.needsUpdate;
236 }
237 
239 {
240  return QString::fromStdString(hash.ToString());
241 }
242 
244 {
245  return idx;
246 }
CAmount nValue
Definition: transaction.h:134
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:335
Transaction not yet final, waiting for block.
int idx
Subtransaction index, for sort key.
unsigned int time_received
Definition: wallet.h:351
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:333
std::vector< isminetype > txout_is_mine
Definition: wallet.h:334
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:332
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
void updateStatus(const interfaces::WalletTxStatus &wtx, int numBlocks, int64_t adjustedTime)
Update status from core wallet tx.
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:17
boost::variant< CNoDestination, CKeyID, CScriptID, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:123
bool countsForBalance
Transaction counts towards available balance.
An output of a transaction.
Definition: transaction.h:131
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:122
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:336
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:209
std::map< std::string, std::string > value_map
Definition: wallet.h:341
qint64 open_for
Timestamp if status==OpenUntilDate, otherwise number of additional blocks that need to be mined befor...
Abandoned from the wallet.
Updated transaction status.
Definition: wallet.h:346
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.