Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
transactionrecord.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Distributed under the MIT/X11 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 "wallet.h"
9 
10 #include <stdint.h>
11 
12 /* Return positive answer if transaction should be shown in list.
13  */
15 {
16  if (wtx.IsCoinBase())
17  {
18  // Ensures we show generated coins / mined transactions at depth 1
19  if (!wtx.IsInMainChain())
20  {
21  return false;
22  }
23  }
24  return true;
25 }
26 
27 /*
28  * Decompose CWallet transaction to model transaction records.
29  */
30 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
31 {
32  QList<TransactionRecord> parts;
33  int64_t nTime = wtx.GetTxTime();
34  int64_t nCredit = wtx.GetCredit(true);
35  int64_t nDebit = wtx.GetDebit();
36  int64_t nNet = nCredit - nDebit;
37  uint256 hash = wtx.GetHash();
38  std::map<std::string, std::string> mapValue = wtx.mapValue;
39 
40  if (nNet > 0 || wtx.IsCoinBase())
41  {
42  //
43  // Credit
44  //
45  BOOST_FOREACH(const CTxOut& txout, wtx.vout)
46  {
47  if(wallet->IsMine(txout))
48  {
49  TransactionRecord sub(hash, nTime);
51  sub.idx = parts.size(); // sequence number
52  sub.credit = txout.nValue;
53  if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
54  {
55  // Received by Bitcoin Address
57  sub.address = CBitcoinAddress(address).ToString();
58  }
59  else
60  {
61  // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
63  sub.address = mapValue["from"];
64  }
65  if (wtx.IsCoinBase())
66  {
67  // Generated
69  }
70 
71  parts.append(sub);
72  }
73  }
74  }
75  else
76  {
77  bool fAllFromMe = true;
78  BOOST_FOREACH(const CTxIn& txin, wtx.vin)
79  fAllFromMe = fAllFromMe && wallet->IsMine(txin);
80 
81  bool fAllToMe = true;
82  BOOST_FOREACH(const CTxOut& txout, wtx.vout)
83  fAllToMe = fAllToMe && wallet->IsMine(txout);
84 
85  if (fAllFromMe && fAllToMe)
86  {
87  // Payment to self
88  int64_t nChange = wtx.GetChange();
89 
90  parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
91  -(nDebit - nChange), nCredit - nChange));
92  }
93  else if (fAllFromMe)
94  {
95  //
96  // Debit
97  //
98  int64_t nTxFee = nDebit - wtx.GetValueOut();
99 
100  for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
101  {
102  const CTxOut& txout = wtx.vout[nOut];
103  TransactionRecord sub(hash, nTime);
104  sub.idx = parts.size();
105 
106  if(wallet->IsMine(txout))
107  {
108  // Ignore parts sent to self, as this is usually the change
109  // from a transaction sent back to our own address.
110  continue;
111  }
112 
114  if (ExtractDestination(txout.scriptPubKey, address))
115  {
116  // Sent to Bitcoin Address
118  sub.address = CBitcoinAddress(address).ToString();
119  }
120  else
121  {
122  // Sent to IP, or other non-address transaction like OP_EVAL
124  sub.address = mapValue["to"];
125  }
126 
127  int64_t nValue = txout.nValue;
128  /* Add fee to first output */
129  if (nTxFee > 0)
130  {
131  nValue += nTxFee;
132  nTxFee = 0;
133  }
134  sub.debit = -nValue;
135 
136  parts.append(sub);
137  }
138  }
139  else
140  {
141  //
142  // Mixed debit transaction, can't break down payees
143  //
144  parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
145  }
146  }
147 
148  return parts;
149 }
150 
152 {
154  // Determine transaction status
155 
156  // Find the block the tx is in
157  CBlockIndex* pindex = NULL;
158  std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
159  if (mi != mapBlockIndex.end())
160  pindex = (*mi).second;
161 
162  // Sort order, unrecorded transactions sort to the top
163  status.sortKey = strprintf("%010d-%01d-%010u-%03d",
164  (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
165  (wtx.IsCoinBase() ? 1 : 0),
166  wtx.nTimeReceived,
167  idx);
168  status.countsForBalance = wtx.IsTrusted() && !(wtx.GetBlocksToMaturity() > 0);
171 
172  if (!IsFinalTx(wtx, chainActive.Height() + 1))
173  {
174  if (wtx.nLockTime < LOCKTIME_THRESHOLD)
175  {
178  }
179  else
180  {
182  status.open_for = wtx.nLockTime;
183  }
184  }
185  // For generated transactions, determine maturity
187  {
188  if (wtx.GetBlocksToMaturity() > 0)
189  {
191 
192  if (wtx.IsInMainChain())
193  {
195 
196  // Check if the block was requested by anyone
197  if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
199  }
200  else
201  {
203  }
204  }
205  else
206  {
208  }
209  }
210  else
211  {
212  if (status.depth < 0)
213  {
215  }
216  else if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
217  {
219  }
220  else if (status.depth == 0)
221  {
223  }
225  {
227  }
228  else
229  {
231  }
232  }
233 
234 }
235 
237 {
240 }
241 
243 {
244  return formatSubTxId(hash, idx);
245 }
246 
247 QString TransactionRecord::formatSubTxId(const uint256 &hash, int vout)
248 {
249  return QString::fromStdString(hash.ToString() + strprintf("-%03d", vout));
250 }
251 
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Definition: main.cpp:594
int64_t GetValueOut() const
Definition: core.cpp:109
Confirmed, but waiting for the recommended number of confirmations.
Transaction not yet final, waiting for block.
int idx
Subtransaction index, for sort key.
bool IsMine(const CTxIn &txin) const
Definition: wallet.cpp:653
CScript scriptPubKey
Definition: core.h:123
int64_t GetChange() const
Definition: wallet.h:649
QString getTxID() const
Return the unique identifier for this transaction (part)
Not sent to any other nodes.
Generated (mined) transactions.
Have 6 or more confirmations (normal tx) or fully mature (mined tx)
std::string sortKey
Sorting key based on status.
uint256 hashBlock
Definition: main.h:432
CCriticalSection cs_main
Definition: main.cpp:43
int64_t GetDebit() const
Definition: wallet.h:579
Mined but not accepted.
uint256 GetHash() const
Definition: core.cpp:75
Not yet mined into a block.
#define strprintf
Definition: util.h:116
static bool showTransaction(const CWalletTx &wtx)
Decompose CWallet transaction to model transaction records.
base58-encoded Bitcoin addresses.
Definition: base58.h:101
bool IsTrusted() const
Definition: wallet.h:669
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:48
mapValue_t mapValue
Definition: wallet.h:457
#define AssertLockHeld(cs)
Definition: sync.h:97
int GetBlocksToMaturity() const
Definition: main.cpp:1033
int GetRequestCount() const
Definition: wallet.cpp:711
int Height() const
Return the maximal height in the chain.
Definition: main.h:1043
bool IsInMainChain() const
Definition: main.h:476
UI model for a transaction.
int64_t GetAdjustedTime()
Definition: util.cpp:1236
TransactionStatus status
Status: can change with block chain update.
unsigned int nLockTime
Definition: core.h:192
static QList< TransactionRecord > decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
An input of a transaction.
Definition: core.h:70
std::vector< CTxOut > vout
Definition: core.h:191
static QString formatSubTxId(const uint256 &hash, int vout)
Format subtransaction id.
std::vector< CTxIn > vin
Definition: core.h:190
bool countsForBalance
Transaction counts towards available balance.
void updateStatus(const CWalletTx &wtx)
Update status from core wallet tx.
std::string ToString() const
Definition: base58.cpp:174
An output of a transaction.
Definition: core.h:119
int cur_num_blocks
Current number of blocks (to know whether cached status is still valid)
Normal (sent/received) transactions.
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:451
int64_t GetTxTime() const
Definition: wallet.cpp:705
256-bit unsigned integer
Definition: uint256.h:531
Conflicts with other transaction or mempool.
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:688
std::string ToString() const
Definition: uint256.h:340
static const unsigned int LOCKTIME_THRESHOLD
Threshold for nLockTime: below this value it is interpreted as block number, otherwise as UNIX timest...
Definition: main.h:60
bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
Definition: script.cpp:1448
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Definition: script.cpp:1493
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:100
int GetDepthInMainChain(CBlockIndex *&pindexRet) const
Definition: main.cpp:1023
bool statusUpdateNeeded()
Return whether a status update is needed.
bool IsCoinBase() const
Definition: core.h:232
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: script.h:218
unsigned int nTimeReceived
Definition: wallet.h:460
int nHeight
Definition: main.h:698
qint64 open_for
Timestamp if status==OpenUntilDate, otherwise number of additional blocks that need to be mined befor...
map< uint256, CBlockIndex * > mapBlockIndex
Definition: main.cpp:47
int64_t GetCredit(bool fUseCache=true) const
Definition: wallet.h:590
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.
Transaction will likely not mature because no nodes have confirmed.
int64_t nValue
Definition: core.h:122