Use standard C99 (and Qt) types for 64-bit integers
[novacoin.git] / src / qt / transactionrecord.cpp
1 #include <QtGlobal>
2
3 #include "transactionrecord.h"
4
5 #include "headers.h"
6
7 /* Return positive answer if transaction should be shown in list.
8  */
9 bool TransactionRecord::showTransaction(const CWalletTx &wtx)
10 {
11     if (wtx.IsCoinBase())
12     {
13         // Don't show generated coin until confirmed by at least one block after it
14         // so we don't get the user's hopes up until it looks like it's probably accepted.
15         //
16         // It is not an error when generated blocks are not accepted.  By design,
17         // some percentage of blocks, like 10% or more, will end up not accepted.
18         // This is the normal mechanism by which the network copes with latency.
19         //
20         // We display regular transactions right away before any confirmation
21         // because they can always get into some block eventually.  Generated coins
22         // are special because if their block is not accepted, they are not valid.
23         //
24         if (wtx.GetDepthInMainChain() < 2)
25         {
26             return false;
27         }
28     }
29     return true;
30 }
31
32 /*
33  * Decompose CWallet transaction to model transaction records.
34  */
35 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
36 {
37     QList<TransactionRecord> parts;
38     qint64 nTime = wtx.nTimeDisplayed = wtx.GetTxTime();
39     qint64 nCredit = wtx.GetCredit(true);
40     qint64 nDebit = wtx.GetDebit();
41     qint64 nNet = nCredit - nDebit;
42     uint256 hash = wtx.GetHash();
43     std::map<std::string, std::string> mapValue = wtx.mapValue;
44
45     if (showTransaction(wtx))
46     {
47         if (nNet > 0 || wtx.IsCoinBase())
48         {
49             //
50             // Credit
51             //
52             TransactionRecord sub(hash, nTime);
53
54             sub.credit = nNet;
55
56             if (wtx.IsCoinBase())
57             {
58                 // Generated
59                 sub.type = TransactionRecord::Generated;
60
61                 if (nCredit == 0)
62                 {
63                     qint64 nUnmatured = 0;
64                     BOOST_FOREACH(const CTxOut& txout, wtx.vout)
65                         nUnmatured += wallet->GetCredit(txout);
66                     sub.credit = nUnmatured;
67                 }
68             }
69             else if (!mapValue["from"].empty() || !mapValue["message"].empty())
70             {
71                 // Received by IP connection
72                 sub.type = TransactionRecord::RecvFromIP;
73                 if (!mapValue["from"].empty())
74                     sub.address = mapValue["from"];
75             }
76             else
77             {
78                 // Received by Bitcoin Address
79                 sub.type = TransactionRecord::RecvWithAddress;
80                 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
81                 {
82                     if(wallet->IsMine(txout))
83                     {
84                         CBitcoinAddress address;
85                         if (ExtractAddress(txout.scriptPubKey, wallet, address))
86                         {
87                             sub.address = address.ToString();
88                         }
89                         break;
90                     }
91                 }
92             }
93             parts.append(sub);
94         }
95         else
96         {
97             bool fAllFromMe = true;
98             BOOST_FOREACH(const CTxIn& txin, wtx.vin)
99                 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
100
101             bool fAllToMe = true;
102             BOOST_FOREACH(const CTxOut& txout, wtx.vout)
103                 fAllToMe = fAllToMe && wallet->IsMine(txout);
104
105             if (fAllFromMe && fAllToMe)
106             {
107                 // Payment to self
108                 qint64 nChange = wtx.GetChange();
109
110                 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
111                                 -(nDebit - nChange), nCredit - nChange));
112             }
113             else if (fAllFromMe)
114             {
115                 //
116                 // Debit
117                 //
118                 qint64 nTxFee = nDebit - wtx.GetValueOut();
119
120                 for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
121                 {
122                     const CTxOut& txout = wtx.vout[nOut];
123                     TransactionRecord sub(hash, nTime);
124                     sub.idx = parts.size();
125
126                     if(wallet->IsMine(txout))
127                     {
128                         // Ignore parts sent to self, as this is usually the change
129                         // from a transaction sent back to our own address.
130                         continue;
131                     }
132                     else if(!mapValue["to"].empty())
133                     {
134                         // Sent to IP
135                         sub.type = TransactionRecord::SendToIP;
136                         sub.address = mapValue["to"];
137                     }
138                     else
139                     {
140                         // Sent to Bitcoin Address
141                         sub.type = TransactionRecord::SendToAddress;
142                         CBitcoinAddress address;
143                         if (ExtractAddress(txout.scriptPubKey, 0, address))
144                         {
145                             sub.address = address.ToString();
146                         }
147                     }
148
149                     qint64 nValue = txout.nValue;
150                     /* Add fee to first output */
151                     if (nTxFee > 0)
152                     {
153                         nValue += nTxFee;
154                         nTxFee = 0;
155                     }
156                     sub.debit = -nValue;
157
158                     parts.append(sub);
159                 }
160             }
161             else
162             {
163                 //
164                 // Mixed debit transaction, can't break down payees
165                 //
166                 bool fAllMine = true;
167                 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
168                     fAllMine = fAllMine && wallet->IsMine(txout);
169                 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
170                     fAllMine = fAllMine && wallet->IsMine(txin);
171
172                 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
173             }
174         }
175     }
176
177     return parts;
178 }
179
180 void TransactionRecord::updateStatus(const CWalletTx &wtx)
181 {
182     // Determine transaction status
183
184     // Find the block the tx is in
185     CBlockIndex* pindex = NULL;
186     std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
187     if (mi != mapBlockIndex.end())
188         pindex = (*mi).second;
189
190     // Sort order, unrecorded transactions sort to the top
191     status.sortKey = strprintf("%010d-%01d-%010u-%03d",
192         (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
193         (wtx.IsCoinBase() ? 1 : 0),
194         wtx.nTimeReceived,
195         idx);
196     status.confirmed = wtx.IsConfirmed();
197     status.depth = wtx.GetDepthInMainChain();
198     status.cur_num_blocks = nBestHeight;
199
200     if (!wtx.IsFinal())
201     {
202         if (wtx.nLockTime < LOCKTIME_THRESHOLD)
203         {
204             status.status = TransactionStatus::OpenUntilBlock;
205             status.open_for = nBestHeight - wtx.nLockTime;
206         }
207         else
208         {
209             status.status = TransactionStatus::OpenUntilDate;
210             status.open_for = wtx.nLockTime;
211         }
212     }
213     else
214     {
215         if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
216         {
217             status.status = TransactionStatus::Offline;
218         }
219         else if (status.depth < NumConfirmations)
220         {
221             status.status = TransactionStatus::Unconfirmed;
222         }
223         else
224         {
225             status.status = TransactionStatus::HaveConfirmations;
226         }
227     }
228
229     // For generated transactions, determine maturity
230     if(type == TransactionRecord::Generated)
231     {
232         qint64 nCredit = wtx.GetCredit(true);
233         if (nCredit == 0)
234         {
235             status.maturity = TransactionStatus::Immature;
236
237             if (wtx.IsInMainChain())
238             {
239                 status.matures_in = wtx.GetBlocksToMaturity();
240
241                 // Check if the block was requested by anyone
242                 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
243                     status.maturity = TransactionStatus::MaturesWarning;
244             }
245             else
246             {
247                 status.maturity = TransactionStatus::NotAccepted;
248             }
249         }
250         else
251         {
252             status.maturity = TransactionStatus::Mature;
253         }
254     }
255 }
256
257 bool TransactionRecord::statusUpdateNeeded()
258 {
259     return status.cur_num_blocks != nBestHeight;
260 }
261
262 std::string TransactionRecord::getTxID()
263 {
264     return hash.ToString() + strprintf("-%03d", idx);
265 }
266