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