1 #include "transactionrecord.h"
5 /* Return positive answer if transaction should be shown in list.
7 bool TransactionRecord::showTransaction(const CWalletTx &wtx)
11 // Don't show generated coin until confirmed by at least one block after it
12 // so we don't get the user's hopes up until it looks like it's probably accepted.
14 // It is not an error when generated blocks are not accepted. By design,
15 // some percentage of blocks, like 10% or more, will end up not accepted.
16 // This is the normal mechanism by which the network copes with latency.
18 // We display regular transactions right away before any confirmation
19 // because they can always get into some block eventually. Generated coins
20 // are special because if their block is not accepted, they are not valid.
22 if (wtx.GetDepthInMainChain() < 2)
31 * Decompose CWallet transaction to model transaction records.
33 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
35 QList<TransactionRecord> parts;
36 int64 nTime = wtx.nTimeDisplayed = wtx.GetTxTime();
37 int64 nCredit = wtx.GetCredit(true);
38 int64 nDebit = wtx.GetDebit();
39 int64 nNet = nCredit - nDebit;
40 uint256 hash = wtx.GetHash();
41 std::map<std::string, std::string> mapValue = wtx.mapValue;
43 if (showTransaction(wtx))
45 if (nNet > 0 || wtx.IsCoinBase())
50 TransactionRecord sub(hash, nTime);
57 sub.type = TransactionRecord::Generated;
62 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
63 nUnmatured += wallet->GetCredit(txout);
64 sub.credit = nUnmatured;
67 else if (!mapValue["from"].empty() || !mapValue["message"].empty())
69 // Received by IP connection
70 sub.type = TransactionRecord::RecvFromIP;
71 if (!mapValue["from"].empty())
72 sub.address = mapValue["from"];
76 // Received by Bitcoin Address
77 sub.type = TransactionRecord::RecvWithAddress;
78 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
80 if(wallet->IsMine(txout))
82 std::vector<unsigned char> vchPubKey;
83 if (ExtractPubKey(txout.scriptPubKey, wallet, vchPubKey))
85 sub.address = PubKeyToAddress(vchPubKey);
95 bool fAllFromMe = true;
96 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
97 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
100 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
101 fAllToMe = fAllToMe && wallet->IsMine(txout);
103 if (fAllFromMe && fAllToMe)
106 int64 nChange = wtx.GetChange();
108 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
109 -(nDebit - nChange), nCredit - nChange));
116 int64 nTxFee = nDebit - wtx.GetValueOut();
118 for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
120 const CTxOut& txout = wtx.vout[nOut];
121 TransactionRecord sub(hash, nTime);
122 sub.idx = parts.size();
124 if(wallet->IsMine(txout))
126 // Ignore parts sent to self, as this is usually the change
127 // from a transaction sent back to our own address.
130 else if(!mapValue["to"].empty())
133 sub.type = TransactionRecord::SendToIP;
134 sub.address = mapValue["to"];
138 // Sent to Bitcoin Address
139 sub.type = TransactionRecord::SendToAddress;
141 if (ExtractHash160(txout.scriptPubKey, hash160))
142 sub.address = Hash160ToAddress(hash160);
145 int64 nValue = txout.nValue;
146 /* Add fee to first output */
160 // Mixed debit transaction, can't break down payees
162 bool fAllMine = true;
163 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
164 fAllMine = fAllMine && wallet->IsMine(txout);
165 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
166 fAllMine = fAllMine && wallet->IsMine(txin);
168 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
176 void TransactionRecord::updateStatus(const CWalletTx &wtx)
178 // Determine transaction status
180 // Find the block the tx is in
181 CBlockIndex* pindex = NULL;
182 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
183 if (mi != mapBlockIndex.end())
184 pindex = (*mi).second;
186 // Sort order, unrecorded transactions sort to the top
187 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
188 (pindex ? pindex->nHeight : INT_MAX),
189 (wtx.IsCoinBase() ? 1 : 0),
192 status.confirmed = wtx.IsConfirmed();
193 status.depth = wtx.GetDepthInMainChain();
194 status.cur_num_blocks = nBestHeight;
198 if (wtx.nLockTime < 500000000)
200 status.status = TransactionStatus::OpenUntilBlock;
201 status.open_for = nBestHeight - wtx.nLockTime;
205 status.status = TransactionStatus::OpenUntilDate;
206 status.open_for = wtx.nLockTime;
211 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
213 status.status = TransactionStatus::Offline;
215 else if (status.depth < NumConfirmations)
217 status.status = TransactionStatus::Unconfirmed;
221 status.status = TransactionStatus::HaveConfirmations;
225 // For generated transactions, determine maturity
226 if(type == TransactionRecord::Generated)
228 int64 nCredit = wtx.GetCredit(true);
231 status.maturity = TransactionStatus::Immature;
233 if (wtx.IsInMainChain())
235 status.matures_in = wtx.GetBlocksToMaturity();
237 // Check if the block was requested by anyone
238 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
239 status.maturity = TransactionStatus::MaturesWarning;
243 status.maturity = TransactionStatus::NotAccepted;
248 status.maturity = TransactionStatus::Mature;
253 bool TransactionRecord::statusUpdateNeeded()
255 return status.cur_num_blocks != nBestHeight;