1 #include "transactionrecord.h"
4 /* Return positive answer if transaction should be shown in list.
6 bool TransactionRecord::showTransaction(const CWalletTx &wtx)
10 // Don't show generated coin until confirmed by at least one block after it
11 // so we don't get the user's hopes up until it looks like it's probably accepted.
13 // It is not an error when generated blocks are not accepted. By design,
14 // some percentage of blocks, like 10% or more, will end up not accepted.
15 // This is the normal mechanism by which the network copes with latency.
17 // We display regular transactions right away before any confirmation
18 // because they can always get into some block eventually. Generated coins
19 // are special because if their block is not accepted, they are not valid.
21 if (wtx.GetDepthInMainChain() < 2)
30 * Decompose CWallet transaction to model transaction records.
32 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx &wtx)
34 QList<TransactionRecord> parts;
35 int64 nTime = wtx.nTimeDisplayed = wtx.GetTxTime();
36 int64 nCredit = wtx.GetCredit(true);
37 int64 nDebit = wtx.GetDebit();
38 int64 nNet = nCredit - nDebit;
39 uint256 hash = wtx.GetHash();
40 std::map<std::string, std::string> mapValue = wtx.mapValue;
42 if (showTransaction(wtx))
44 if (nNet > 0 || wtx.IsCoinBase())
49 TransactionRecord sub(hash, nTime);
56 sub.type = TransactionRecord::Generated;
61 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
62 nUnmatured += txout.GetCredit();
63 sub.credit = nUnmatured;
66 else if (!mapValue["from"].empty() || !mapValue["message"].empty())
68 // Received by IP connection
69 sub.type = TransactionRecord::RecvFromIP;
70 if (!mapValue["from"].empty())
71 sub.address = mapValue["from"];
75 // Received by Bitcoin Address
76 sub.type = TransactionRecord::RecvWithAddress;
77 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
81 std::vector<unsigned char> vchPubKey;
82 if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey))
84 sub.address = PubKeyToAddress(vchPubKey);
94 bool fAllFromMe = true;
95 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
96 fAllFromMe = fAllFromMe && txin.IsMine();
99 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
100 fAllToMe = fAllToMe && txout.IsMine();
102 if (fAllFromMe && fAllToMe)
105 int64 nChange = wtx.GetChange();
107 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
108 -(nDebit - nChange), nCredit - nChange));
115 int64 nTxFee = nDebit - wtx.GetValueOut();
117 for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
119 const CTxOut& txout = wtx.vout[nOut];
120 TransactionRecord sub(hash, nTime);
121 sub.idx = parts.size();
125 // Ignore parts sent to self, as this is usually the change
126 // from a transaction sent back to our own address.
129 else if (!mapValue["to"].empty())
132 sub.type = TransactionRecord::SendToIP;
133 sub.address = mapValue["to"];
137 // Sent to Bitcoin Address
138 sub.type = TransactionRecord::SendToAddress;
140 if (ExtractHash160(txout.scriptPubKey, hash160))
141 sub.address = Hash160ToAddress(hash160);
144 int64 nValue = txout.nValue;
145 /* Add fee to first output */
159 // Mixed debit transaction, can't break down payees
161 bool fAllMine = true;
162 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
163 fAllMine = fAllMine && txout.IsMine();
164 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
165 fAllMine = fAllMine && txin.IsMine();
167 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
175 void TransactionRecord::updateStatus(const CWalletTx &wtx)
177 // Determine transaction status
179 // Find the block the tx is in
180 CBlockIndex* pindex = NULL;
181 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
182 if (mi != mapBlockIndex.end())
183 pindex = (*mi).second;
185 // Sort order, unrecorded transactions sort to the top
186 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
187 (pindex ? pindex->nHeight : INT_MAX),
188 (wtx.IsCoinBase() ? 1 : 0),
191 status.confirmed = wtx.IsConfirmed();
192 status.depth = wtx.GetDepthInMainChain();
193 status.cur_num_blocks = nBestHeight;
197 if (wtx.nLockTime < 500000000)
199 status.status = TransactionStatus::OpenUntilBlock;
200 status.open_for = nBestHeight - wtx.nLockTime;
204 status.status = TransactionStatus::OpenUntilDate;
205 status.open_for = wtx.nLockTime;
210 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
212 status.status = TransactionStatus::Offline;
214 else if (status.depth < NumConfirmations)
216 status.status = TransactionStatus::Unconfirmed;
220 status.status = TransactionStatus::HaveConfirmations;
224 // For generated transactions, determine maturity
225 if(type == TransactionRecord::Generated)
227 int64 nCredit = wtx.GetCredit(true);
230 status.maturity = TransactionStatus::Immature;
232 if (wtx.IsInMainChain())
234 status.matures_in = wtx.GetBlocksToMaturity();
236 // Check if the block was requested by anyone
237 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
238 status.maturity = TransactionStatus::MaturesWarning;
242 status.maturity = TransactionStatus::NotAccepted;
247 status.maturity = TransactionStatus::Mature;
252 bool TransactionRecord::statusUpdateNeeded()
254 return status.cur_num_blocks != nBestHeight;