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 CBitcoinAddress address;
83 if (ExtractAddress(txout.scriptPubKey, wallet, address))
85 sub.address = address.ToString();
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;
140 CBitcoinAddress address;
141 if (ExtractAddress(txout.scriptPubKey, 0, address))
143 sub.address = address.ToString();
147 int64 nValue = txout.nValue;
148 /* Add fee to first output */
162 // Mixed debit transaction, can't break down payees
164 bool fAllMine = true;
165 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
166 fAllMine = fAllMine && wallet->IsMine(txout);
167 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
168 fAllMine = fAllMine && wallet->IsMine(txin);
170 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
178 void TransactionRecord::updateStatus(const CWalletTx &wtx)
180 // Determine transaction status
182 // Find the block the tx is in
183 CBlockIndex* pindex = NULL;
184 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
185 if (mi != mapBlockIndex.end())
186 pindex = (*mi).second;
188 // Sort order, unrecorded transactions sort to the top
189 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
190 (pindex ? pindex->nHeight : INT_MAX),
191 (wtx.IsCoinBase() ? 1 : 0),
194 status.confirmed = wtx.IsConfirmed();
195 status.depth = wtx.GetDepthInMainChain();
196 status.cur_num_blocks = nBestHeight;
200 if (wtx.nLockTime < LOCKTIME_THRESHOLD)
202 status.status = TransactionStatus::OpenUntilBlock;
203 status.open_for = nBestHeight - wtx.nLockTime;
207 status.status = TransactionStatus::OpenUntilDate;
208 status.open_for = wtx.nLockTime;
213 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
215 status.status = TransactionStatus::Offline;
217 else if (status.depth < NumConfirmations)
219 status.status = TransactionStatus::Unconfirmed;
223 status.status = TransactionStatus::HaveConfirmations;
227 // For generated transactions, determine maturity
228 if(type == TransactionRecord::Generated)
230 int64 nCredit = wtx.GetCredit(true);
233 status.maturity = TransactionStatus::Immature;
235 if (wtx.IsInMainChain())
237 status.matures_in = wtx.GetBlocksToMaturity();
239 // Check if the block was requested by anyone
240 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
241 status.maturity = TransactionStatus::MaturesWarning;
245 status.maturity = TransactionStatus::NotAccepted;
250 status.maturity = TransactionStatus::Mature;
255 bool TransactionRecord::statusUpdateNeeded()
257 return status.cur_num_blocks != nBestHeight;
260 std::string TransactionRecord::getTxID()
262 return hash.ToString() + strprintf("-%03d", idx);