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.
5 #include "transactionrecord.h"
13 /* Return positive answer if transaction should be shown in list.
15 bool TransactionRecord::showTransaction(const CWalletTx &wtx)
19 // Ensures we show generated coins / mined transactions at depth 1
20 if (!wtx.IsInMainChain())
29 * Decompose CWallet transaction to model transaction records.
31 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
33 QList<TransactionRecord> parts;
34 int64_t nTime = wtx.GetTxTime();
35 int64_t nCredit = wtx.GetCredit(true);
36 int64_t nDebit = wtx.GetDebit();
37 int64_t nNet = nCredit - nDebit;
38 uint256 hash = wtx.GetHash();
39 std::map<std::string, std::string> mapValue = wtx.mapValue;
41 if (nNet > 0 || wtx.IsCoinBase())
46 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
48 if(wallet->IsMine(txout))
50 TransactionRecord sub(hash, nTime);
51 CTxDestination address;
52 sub.idx = parts.size(); // sequence number
53 sub.credit = txout.nValue;
54 if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
56 // Received by Bitcoin Address
57 sub.type = TransactionRecord::RecvWithAddress;
58 sub.address = CBitcoinAddress(address).ToString();
62 // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
63 sub.type = TransactionRecord::RecvFromOther;
64 sub.address = mapValue["from"];
69 sub.type = TransactionRecord::Generated;
78 bool fAllFromMe = true;
79 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
80 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
83 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
84 fAllToMe = fAllToMe && wallet->IsMine(txout);
86 if (fAllFromMe && fAllToMe)
89 int64_t nChange = wtx.GetChange();
91 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
92 -(nDebit - nChange), nCredit - nChange));
99 int64_t nTxFee = nDebit - wtx.GetValueOut();
101 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
103 const CTxOut& txout = wtx.vout[nOut];
104 TransactionRecord sub(hash, nTime);
105 sub.idx = parts.size();
107 if(wallet->IsMine(txout))
109 // Ignore parts sent to self, as this is usually the change
110 // from a transaction sent back to our own address.
114 CTxDestination address;
115 if (ExtractDestination(txout.scriptPubKey, address))
117 // Sent to Bitcoin Address
118 sub.type = TransactionRecord::SendToAddress;
119 sub.address = CBitcoinAddress(address).ToString();
123 // Sent to IP, or other non-address transaction like OP_EVAL
124 sub.type = TransactionRecord::SendToOther;
125 sub.address = mapValue["to"];
128 int64_t nValue = txout.nValue;
129 /* Add fee to first output */
143 // Mixed debit transaction, can't break down payees
145 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
152 void TransactionRecord::updateStatus(const CWalletTx &wtx)
154 AssertLockHeld(cs_main);
155 // Determine transaction status
157 // Find the block the tx is in
158 CBlockIndex* pindex = NULL;
159 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
160 if (mi != mapBlockIndex.end())
161 pindex = (*mi).second;
163 // Sort order, unrecorded transactions sort to the top
164 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
165 (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
166 (wtx.IsCoinBase() ? 1 : 0),
169 status.countsForBalance = wtx.IsTrusted() && !(wtx.GetBlocksToMaturity() > 0);
170 status.depth = wtx.GetDepthInMainChain();
171 status.cur_num_blocks = chainActive.Height();
173 status.hasConflicting = false;
175 if (!IsFinalTx(wtx, chainActive.Height() + 1))
177 if (wtx.nLockTime < LOCKTIME_THRESHOLD)
179 status.status = TransactionStatus::OpenUntilBlock;
180 status.open_for = wtx.nLockTime - chainActive.Height();
184 status.status = TransactionStatus::OpenUntilDate;
185 status.open_for = wtx.nLockTime;
188 // For generated transactions, determine maturity
189 else if(type == TransactionRecord::Generated)
191 if (wtx.GetBlocksToMaturity() > 0)
193 status.status = TransactionStatus::Immature;
195 if (wtx.IsInMainChain())
197 status.matures_in = wtx.GetBlocksToMaturity();
199 // Check if the block was requested by anyone
200 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
201 status.status = TransactionStatus::MaturesWarning;
205 status.status = TransactionStatus::NotAccepted;
210 status.status = TransactionStatus::Confirmed;
215 if (status.depth < 0)
217 status.status = TransactionStatus::Conflicted;
218 status.hasConflicting = !(wtx.GetConflicts(false).empty());
220 else if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
222 status.status = TransactionStatus::Offline;
224 else if (status.depth == 0)
226 status.status = TransactionStatus::Unconfirmed;
227 status.hasConflicting = !(wtx.GetConflicts(false).empty());
229 else if (status.depth < RecommendedNumConfirmations)
231 status.status = TransactionStatus::Confirming;
235 status.status = TransactionStatus::Confirmed;
240 bool TransactionRecord::statusUpdateNeeded(int64_t nConflictsReceived)
242 AssertLockHeld(cs_main);
243 return (status.cur_num_blocks != chainActive.Height() ||
244 status.cur_num_conflicts != nConflictsReceived);
247 QString TransactionRecord::getTxID() const
249 return formatSubTxId(hash, idx);
252 QString TransactionRecord::formatSubTxId(const uint256 &hash, int vout)
254 return QString::fromStdString(hash.ToString() + strprintf("-%03d", vout));