Dogecoin Core  1.14.2
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 "transactionrecord.h"
6 
7 #include "base58.h"
8 #include "consensus/consensus.h"
9 #include "validation.h"
10 #include "timedata.h"
11 #include "wallet/wallet.h"
12 
13 #include <stdint.h>
14 
15 #include <boost/foreach.hpp>
16 
17 /* Return positive answer if transaction should be shown in list.
18  */
20 {
21  if (wtx.IsCoinBase())
22  {
23  // Ensures we show generated coins / mined transactions at depth 1
24  if (!wtx.IsInMainChain())
25  {
26  return false;
27  }
28  }
29  return true;
30 }
31 
32 /*
33  * Decompose CWallet transaction to model transaction records.
34  */
35 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
36 {
37  QList<TransactionRecord> parts;
38  int64_t nTime = wtx.GetTxTime();
39  CAmount nCredit = wtx.GetCredit(ISMINE_ALL);
40  CAmount nDebit = wtx.GetDebit(ISMINE_ALL);
41  CAmount nNet = nCredit - nDebit;
42  uint256 hash = wtx.GetHash();
43  std::map<std::string, std::string> mapValue = wtx.mapValue;
44 
45  if (nNet > 0 || wtx.IsCoinBase())
46  {
47  //
48  // Credit
49  //
50  for(unsigned int i = 0; i < wtx.tx->vout.size(); i++)
51  {
52  const CTxOut& txout = wtx.tx->vout[i];
53  isminetype mine = wallet->IsMine(txout);
54  if(mine)
55  {
56  TransactionRecord sub(hash, nTime);
58  sub.idx = i; // vout index
59  sub.credit = txout.nValue;
61  if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
62  {
63  // Received by Bitcoin Address
66  }
67  else
68  {
69  // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
71  sub.address = mapValue["from"];
72  }
73  if (wtx.IsCoinBase())
74  {
75  // Generated
77  }
78 
79  parts.append(sub);
80  }
81  }
82  }
83  else
84  {
85  bool involvesWatchAddress = false;
86  isminetype fAllFromMe = ISMINE_SPENDABLE;
87  BOOST_FOREACH(const CTxIn& txin, wtx.tx->vin)
88  {
89  isminetype mine = wallet->IsMine(txin);
90  if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
91  if(fAllFromMe > mine) fAllFromMe = mine;
92  }
93 
94  isminetype fAllToMe = ISMINE_SPENDABLE;
95  BOOST_FOREACH(const CTxOut& txout, wtx.tx->vout)
96  {
97  isminetype mine = wallet->IsMine(txout);
98  if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
99  if(fAllToMe > mine) fAllToMe = mine;
100  }
101 
102  if (fAllFromMe && fAllToMe)
103  {
104  // Payment to self
105  CAmount nChange = wtx.GetChange();
106 
107  parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
108  -(nDebit - nChange), nCredit - nChange));
109  parts.last().involvesWatchAddress = involvesWatchAddress; // maybe pass to TransactionRecord as constructor argument
110  }
111  else if (fAllFromMe)
112  {
113  //
114  // Debit
115  //
116  CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
117 
118  for (unsigned int nOut = 0; nOut < wtx.tx->vout.size(); nOut++)
119  {
120  const CTxOut& txout = wtx.tx->vout[nOut];
121  TransactionRecord sub(hash, nTime);
122  sub.idx = nOut;
124 
125  if(wallet->IsMine(txout))
126  {
127  // Ignore parts sent to self, as this is usually the change
128  // from a transaction sent back to our own address.
129  continue;
130  }
131 
134  {
135  // Sent to Bitcoin Address
138  }
139  else
140  {
141  // Sent to IP, or other non-address transaction like OP_EVAL
143  sub.address = mapValue["to"];
144  }
145 
146  CAmount nValue = txout.nValue;
147  /* Add fee to first output */
148  if (nTxFee > 0)
149  {
150  nValue += nTxFee;
151  nTxFee = 0;
152  }
153  sub.debit = -nValue;
154 
155  parts.append(sub);
156  }
157  }
158  else
159  {
160  //
161  // Mixed debit transaction, can't break down payees
162  //
163  parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
164  parts.last().involvesWatchAddress = involvesWatchAddress;
165  }
166  }
167 
168  return parts;
169 }
170 
172 {
174  // Determine transaction status
175 
176  // Find the block the tx is in
177  CBlockIndex* pindex = NULL;
178  BlockMap::iterator mi = mapBlockIndex.find(wtx.hashBlock);
179  if (mi != mapBlockIndex.end())
180  pindex = (*mi).second;
181 
182  // Sort order, unrecorded transactions sort to the top
183  status.sortKey = strprintf("%010d-%01d-%010u-%03d",
184  (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
185  (wtx.IsCoinBase() ? 1 : 0),
186  wtx.nTimeReceived,
187  idx);
188  status.countsForBalance = wtx.IsTrusted() && !(wtx.GetBlocksToMaturity() > 0);
191 
192  if (!CheckFinalTx(wtx))
193  {
194  if (wtx.tx->nLockTime < LOCKTIME_THRESHOLD)
195  {
197  status.open_for = wtx.tx->nLockTime - chainActive.Height();
198  }
199  else
200  {
202  status.open_for = wtx.tx->nLockTime;
203  }
204  }
205  // For generated transactions, determine maturity
207  {
208  if (wtx.GetBlocksToMaturity() > 0)
209  {
211 
212  if (wtx.IsInMainChain())
213  {
215 
216  // Check if the block was requested by anyone
217  if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
219  }
220  else
221  {
223  }
224  }
225  else
226  {
228  }
229  }
230  else
231  {
232  if (status.depth < 0)
233  {
235  }
236  else if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
237  {
239  }
240  else if (status.depth == 0)
241  {
243  if (wtx.isAbandoned())
245  }
247  {
249  }
250  else
251  {
253  }
254  }
255 
256 }
257 
259 {
262 }
263 
265 {
266  return QString::fromStdString(hash.ToString());
267 }
268 
270 {
271  return idx;
272 }
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:15
std::string ToString() const
Definition: base58.cpp:193
base58-encoded Bitcoin addresses.
Definition: base58.h:104
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:158
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:170
int Height() const
Return the maximal height in the chain.
Definition: chain.h:474
int GetDepthInMainChain(const CBlockIndex *&pindexRet) const
Return depth of transaction in blockchain: <0 : conflicts with a transaction this deep in the blockch...
Definition: auxpow.cpp:45
bool isAbandoned() const
Definition: auxpow.h:110
const uint256 & GetHash() const
Definition: auxpow.h:113
bool IsCoinBase() const
Definition: auxpow.h:114
int GetBlocksToMaturity() const
Definition: auxpow.cpp:64
uint256 hashBlock
Definition: auxpow.h:38
bool IsInMainChain() const
Definition: auxpow.h:105
CTransactionRef tx
Definition: auxpow.h:37
An input of a transaction.
Definition: transaction.h:63
An output of a transaction.
Definition: transaction.h:133
CScript scriptPubKey
Definition: transaction.h:136
CAmount nValue
Definition: transaction.h:135
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:493
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:177
mapValue_t mapValue
Definition: wallet.h:182
unsigned int nTimeReceived
time received by this node
Definition: wallet.h:185
UI model for a transaction.
int idx
Subtransaction index, for sort key.
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.
TransactionStatus status
Status: can change with block chain update.
int getOutputIndex() const
Return the output index of the subtransaction
static bool showTransaction(const CWalletTx &wtx)
Decompose CWallet transaction to model transaction records.
QString getTxID() const
Return the unique identifier for this transaction (part)
void updateStatus(const CWalletTx &wtx)
Update status from core wallet tx.
static QList< TransactionRecord > decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
bool statusUpdateNeeded()
Return whether a status update is needed.
bool involvesWatchAddress
Whether the transaction was sent/received with a watch-only address.
bool countsForBalance
Transaction counts towards available balance.
int cur_num_blocks
Current number of blocks (to know whether cached status is still valid)
qint64 open_for
Timestamp if status==OpenUntilDate, otherwise number of additional blocks that need to be mined befor...
@ MaturesWarning
Transaction will likely not mature because no nodes have confirmed.
@ Confirmed
Have 6 or more confirmations (normal tx) or fully mature (mined tx)
@ OpenUntilDate
Normal (sent/received) transactions.
@ Unconfirmed
Not yet mined into a block.
@ Immature
Generated (mined) transactions.
@ Confirming
Confirmed, but waiting for the recommended number of confirmations.
@ NotAccepted
Mined but not accepted.
@ OpenUntilBlock
Transaction not yet final, waiting for block.
@ Conflicted
Conflicts with other transaction or mempool.
@ Abandoned
Abandoned from the wallet.
@ Offline
Not sent to any other nodes.
std::string sortKey
Sorting key based on status.
std::string ToString() const
Definition: uint256.cpp:65
256-bit opaque blob.
Definition: uint256.h:123
CAmount GetCredit(const isminefilter &filter) const
Definition: wallet.cpp:1695
isminetype IsMine(const CTxIn &txin) const
Definition: wallet.cpp:1201
CAmount GetChange() const
Definition: wallet.cpp:1815
bool IsTrusted() const
Definition: wallet.cpp:1833
CAmount GetDebit(const isminefilter &filter) const
filter decides which addresses will count towards the debit
Definition: wallet.cpp:1664
int64_t GetTxTime() const
Definition: wallet.cpp:1407
int GetRequestCount() const
Definition: wallet.cpp:1413
isminetype IsMine(const CKeyStore &keystore, const CScript &scriptPubKey, SigVersion sigversion)
Definition: ismine.cpp:32
isminetype
IsMine() return codes.
Definition: ismine.h:18
@ ISMINE_ALL
Definition: ismine.h:26
@ ISMINE_SPENDABLE
Definition: ismine.h:25
@ ISMINE_WATCH_ONLY
Definition: ismine.h:24
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Definition: standard.cpp:182
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:71
#define AssertLockHeld(cs)
Definition: sync.h:86
int64_t GetAdjustedTime()
Definition: timedata.cpp:36
#define strprintf
Definition: tinyformat.h:1047
CCriticalSection cs_main
Global state.
Definition: validation.cpp:61
bool CheckFinalTx(const CTransaction &tx, int flags)
Check if transaction will be final in the next block to be created.
Definition: validation.cpp:250
BlockMap mapBlockIndex
Definition: validation.cpp:63
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
Definition: validation.cpp:64