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)
29 /* Decompose CWallet transaction to model transaction records.
31 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx &wtx)
33 QList<TransactionRecord> parts;
34 int64 nTime = wtx.nTimeDisplayed = wtx.GetTxTime();
35 int64 nCredit = wtx.GetCredit(true);
36 int64 nDebit = wtx.GetDebit();
37 int64 nNet = nCredit - nDebit;
38 uint256 hash = wtx.GetHash();
39 std::map<std::string, std::string> mapValue = wtx.mapValue;
41 if (showTransaction(wtx))
43 if (nNet > 0 || wtx.IsCoinBase())
48 TransactionRecord sub(hash, nTime);
55 sub.type = TransactionRecord::Generated;
60 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
61 nUnmatured += txout.GetCredit();
62 sub.credit = nUnmatured;
65 else if (!mapValue["from"].empty() || !mapValue["message"].empty())
67 // Received by IP connection
68 sub.type = TransactionRecord::RecvFromIP;
69 if (!mapValue["from"].empty())
70 sub.address = mapValue["from"];
74 // Received by Bitcoin Address
75 sub.type = TransactionRecord::RecvWithAddress;
76 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
80 std::vector<unsigned char> vchPubKey;
81 if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey))
83 sub.address = PubKeyToAddress(vchPubKey);
93 bool fAllFromMe = true;
94 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
95 fAllFromMe = fAllFromMe && txin.IsMine();
98 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
99 fAllToMe = fAllToMe && txout.IsMine();
101 if (fAllFromMe && fAllToMe)
104 int64 nChange = wtx.GetChange();
106 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
107 -(nDebit - nChange), nCredit - nChange));
114 int64 nTxFee = nDebit - wtx.GetValueOut();
116 for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
118 const CTxOut& txout = wtx.vout[nOut];
119 TransactionRecord sub(hash, nTime);
120 sub.idx = parts.size();
124 // Ignore parts sent to self, as this is usually the change
125 // from a transaction sent back to our own address.
128 else if (!mapValue["to"].empty())
131 sub.type = TransactionRecord::SendToIP;
132 sub.address = mapValue["to"];
136 // Sent to Bitcoin Address
137 sub.type = TransactionRecord::SendToAddress;
139 if (ExtractHash160(txout.scriptPubKey, hash160))
140 sub.address = Hash160ToAddress(hash160);
143 int64 nValue = txout.nValue;
144 /* Add fee to first output */
158 // Mixed debit transaction, can't break down payees
160 bool fAllMine = true;
161 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
162 fAllMine = fAllMine && txout.IsMine();
163 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
164 fAllMine = fAllMine && txin.IsMine();
166 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
174 void TransactionRecord::updateStatus(const CWalletTx &wtx)
176 // Determine transaction status
178 // Find the block the tx is in
179 CBlockIndex* pindex = NULL;
180 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
181 if (mi != mapBlockIndex.end())
182 pindex = (*mi).second;
184 // Sort order, unrecorded transactions sort to the top
185 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
186 (pindex ? pindex->nHeight : INT_MAX),
187 (wtx.IsCoinBase() ? 1 : 0),
190 status.confirmed = wtx.IsConfirmed();
191 status.depth = wtx.GetDepthInMainChain();
192 status.cur_num_blocks = nBestHeight;
196 if (wtx.nLockTime < 500000000)
198 status.status = TransactionStatus::OpenUntilBlock;
199 status.open_for = nBestHeight - wtx.nLockTime;
203 status.status = TransactionStatus::OpenUntilDate;
204 status.open_for = wtx.nLockTime;
209 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
211 status.status = TransactionStatus::Offline;
213 else if (status.depth < 6)
215 status.status = TransactionStatus::Unconfirmed;
219 status.status = TransactionStatus::HaveConfirmations;
223 // For generated transactions, determine maturity
224 if(type == TransactionRecord::Generated)
226 int64 nCredit = wtx.GetCredit(true);
229 status.maturity = TransactionStatus::Immature;
231 if (wtx.IsInMainChain())
233 status.matures_in = wtx.GetBlocksToMaturity();
235 // Check if the block was requested by anyone
236 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
237 status.maturity = TransactionStatus::MaturesWarning;
241 status.maturity = TransactionStatus::NotAccepted;
246 status.maturity = TransactionStatus::Mature;
251 bool TransactionRecord::statusUpdateNeeded()
253 return status.cur_num_blocks != nBestHeight;