1 // Copyright (c) 2011-2014 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.
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 CAmount nCredit = wtx.GetCredit(ISMINE_ALL);
36 CAmount nDebit = wtx.GetDebit(ISMINE_ALL);
37 CAmount 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 isminetype mine = wallet->IsMine(txout);
51 TransactionRecord sub(hash, nTime);
52 CTxDestination address;
53 sub.idx = parts.size(); // sequence number
54 sub.credit = txout.nValue;
55 sub.involvesWatchAddress = mine == ISMINE_WATCH_ONLY;
56 if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
58 // Received by Bitcoin Address
59 sub.type = TransactionRecord::RecvWithAddress;
60 sub.address = CBitcoinAddress(address).ToString();
64 // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
65 sub.type = TransactionRecord::RecvFromOther;
66 sub.address = mapValue["from"];
71 sub.type = TransactionRecord::Generated;
80 bool involvesWatchAddress = false;
81 isminetype fAllFromMe = ISMINE_SPENDABLE;
82 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
84 isminetype mine = wallet->IsMine(txin);
85 if(mine == ISMINE_WATCH_ONLY) involvesWatchAddress = true;
86 if(fAllFromMe > mine) fAllFromMe = mine;
89 isminetype fAllToMe = ISMINE_SPENDABLE;
90 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
92 isminetype mine = wallet->IsMine(txout);
93 if(mine == ISMINE_WATCH_ONLY) involvesWatchAddress = true;
94 if(fAllToMe > mine) fAllToMe = mine;
97 if (fAllFromMe && fAllToMe)
100 CAmount nChange = wtx.GetChange();
102 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
103 -(nDebit - nChange), nCredit - nChange));
104 parts.last().involvesWatchAddress = involvesWatchAddress; // maybe pass to TransactionRecord as constructor argument
111 CAmount nTxFee = nDebit - wtx.GetValueOut();
113 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
115 const CTxOut& txout = wtx.vout[nOut];
116 TransactionRecord sub(hash, nTime);
117 sub.idx = parts.size();
118 sub.involvesWatchAddress = involvesWatchAddress;
120 if(wallet->IsMine(txout))
122 // Ignore parts sent to self, as this is usually the change
123 // from a transaction sent back to our own address.
127 CTxDestination address;
128 if (ExtractDestination(txout.scriptPubKey, address))
130 // Sent to Bitcoin Address
131 sub.type = TransactionRecord::SendToAddress;
132 sub.address = CBitcoinAddress(address).ToString();
136 // Sent to IP, or other non-address transaction like OP_EVAL
137 sub.type = TransactionRecord::SendToOther;
138 sub.address = mapValue["to"];
141 CAmount nValue = txout.nValue;
142 /* Add fee to first output */
156 // Mixed debit transaction, can't break down payees
158 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
159 parts.last().involvesWatchAddress = involvesWatchAddress;
166 void TransactionRecord::updateStatus(const CWalletTx &wtx)
168 AssertLockHeld(cs_main);
169 // Determine transaction status
171 // Find the block the tx is in
172 CBlockIndex* pindex = NULL;
173 BlockMap::iterator mi = mapBlockIndex.find(wtx.hashBlock);
174 if (mi != mapBlockIndex.end())
175 pindex = (*mi).second;
177 // Sort order, unrecorded transactions sort to the top
178 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
179 (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
180 (wtx.IsCoinBase() ? 1 : 0),
183 status.countsForBalance = wtx.IsTrusted() && !(wtx.GetBlocksToMaturity() > 0);
184 status.depth = wtx.GetDepthInMainChain();
185 status.cur_num_blocks = chainActive.Height();
187 if (!IsFinalTx(wtx, chainActive.Height() + 1))
189 if (wtx.nLockTime < LOCKTIME_THRESHOLD)
191 status.status = TransactionStatus::OpenUntilBlock;
192 status.open_for = wtx.nLockTime - chainActive.Height();
196 status.status = TransactionStatus::OpenUntilDate;
197 status.open_for = wtx.nLockTime;
200 // For generated transactions, determine maturity
201 else if(type == TransactionRecord::Generated)
203 if (wtx.GetBlocksToMaturity() > 0)
205 status.status = TransactionStatus::Immature;
207 if (wtx.IsInMainChain())
209 status.matures_in = wtx.GetBlocksToMaturity();
211 // Check if the block was requested by anyone
212 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
213 status.status = TransactionStatus::MaturesWarning;
217 status.status = TransactionStatus::NotAccepted;
222 status.status = TransactionStatus::Confirmed;
227 if (status.depth < 0)
229 status.status = TransactionStatus::Conflicted;
231 else if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
233 status.status = TransactionStatus::Offline;
235 else if (status.depth == 0)
237 status.status = TransactionStatus::Unconfirmed;
239 else if (status.depth < RecommendedNumConfirmations)
241 status.status = TransactionStatus::Confirming;
245 status.status = TransactionStatus::Confirmed;
251 bool TransactionRecord::statusUpdateNeeded()
253 AssertLockHeld(cs_main);
254 return status.cur_num_blocks != chainActive.Height();
257 QString TransactionRecord::getTxID() const
259 return formatSubTxId(hash, idx);
262 QString TransactionRecord::formatSubTxId(const uint256 &hash, int vout)
264 return QString::fromStdString(hash.ToString() + strprintf("-%03d", vout));