Calculate actual kernel and coin*day rates on the fly.
[novacoin.git] / src / wallet.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include "txdb.h"
7 #include "wallet.h"
8 #include "walletdb.h"
9 #include "crypter.h"
10 #include "ui_interface.h"
11 #include "base58.h"
12 #include "kernel.h"
13 #include "coincontrol.h"
14 #include <boost/algorithm/string/replace.hpp>
15
16 #include "main.h"
17
18 using namespace std;
19
20
21 bool fCoinsDataActual;
22
23 //////////////////////////////////////////////////////////////////////////////
24 //
25 // mapWallet
26 //
27
28 struct CompareValueOnly
29 {
30     bool operator()(const pair<int64, pair<const CWalletTx*, unsigned int> >& t1,
31                     const pair<int64, pair<const CWalletTx*, unsigned int> >& t2) const
32     {
33         return t1.first < t2.first;
34     }
35 };
36
37 CPubKey CWallet::GenerateNewKey()
38 {
39     bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
40
41     RandAddSeedPerfmon();
42     CKey key;
43     key.MakeNewKey(fCompressed);
44
45     // Compressed public keys were introduced in version 0.6.0
46     if (fCompressed)
47         SetMinVersion(FEATURE_COMPRPUBKEY);
48
49     CPubKey pubkey = key.GetPubKey();
50
51     // Create new metadata
52     int64 nCreationTime = GetTime();
53     mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime);
54     if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
55         nTimeFirstKey = nCreationTime;
56
57     if (!AddKey(key))
58         throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
59     return key.GetPubKey();
60 }
61
62 bool CWallet::AddKey(const CKey& key)
63 {
64     CPubKey pubkey = key.GetPubKey();
65
66     if (!CCryptoKeyStore::AddKey(key))
67         return false;
68     if (!fFileBacked)
69         return true;
70     if (!IsCrypted())
71         return CWalletDB(strWalletFile).WriteKey(pubkey, key.GetPrivKey(), mapKeyMetadata[pubkey.GetID()]);
72     return true;
73 }
74
75 bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector<unsigned char> &vchCryptedSecret)
76 {
77     if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
78         return false;
79     if (!fFileBacked)
80         return true;
81     {
82         LOCK(cs_wallet);
83         if (pwalletdbEncryption)
84             return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
85         else
86             return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
87     }
88     return false;
89 }
90
91 bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
92 {
93     if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
94         nTimeFirstKey = meta.nCreateTime;
95
96     mapKeyMetadata[pubkey.GetID()] = meta;
97     return true;
98 }
99
100 bool CWallet::AddCScript(const CScript& redeemScript)
101 {
102     if (!CCryptoKeyStore::AddCScript(redeemScript))
103         return false;
104     if (!fFileBacked)
105         return true;
106     return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
107 }
108
109 bool CWallet::AddWatchOnly(const CScript &dest)
110 {
111     if (!CCryptoKeyStore::AddWatchOnly(dest))
112         return false;
113     nTimeFirstKey = 1; // No birthday information for watch-only keys.
114     if (!fFileBacked)
115         return true;
116     return CWalletDB(strWalletFile).WriteWatchOnly(dest);
117 }
118
119 bool CWallet::LoadWatchOnly(const CScript &dest)
120 {
121     return CCryptoKeyStore::AddWatchOnly(dest);
122 }
123
124 // ppcoin: optional setting to unlock wallet for block minting only;
125 //         serves to disable the trivial sendmoney when OS account compromised
126 bool fWalletUnlockMintOnly = false;
127
128 bool CWallet::Unlock(const SecureString& strWalletPassphrase)
129 {
130     if (!IsLocked())
131         return false;
132
133     CCrypter crypter;
134     CKeyingMaterial vMasterKey;
135
136     {
137         LOCK(cs_wallet);
138         BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
139         {
140             if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
141                 return false;
142             if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
143                 return false;
144             if (CCryptoKeyStore::Unlock(vMasterKey))
145                 return true;
146         }
147     }
148     return false;
149 }
150
151 bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
152 {
153     bool fWasLocked = IsLocked();
154
155     {
156         LOCK(cs_wallet);
157         Lock();
158
159         CCrypter crypter;
160         CKeyingMaterial vMasterKey;
161         BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
162         {
163             if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
164                 return false;
165             if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
166                 return false;
167             if (CCryptoKeyStore::Unlock(vMasterKey))
168             {
169                 int64 nStartTime = GetTimeMillis();
170                 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
171                 pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
172
173                 nStartTime = GetTimeMillis();
174                 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
175                 pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
176
177                 if (pMasterKey.second.nDeriveIterations < 25000)
178                     pMasterKey.second.nDeriveIterations = 25000;
179
180                 printf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
181
182                 if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
183                     return false;
184                 if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
185                     return false;
186                 CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second);
187                 if (fWasLocked)
188                     Lock();
189                 return true;
190             }
191         }
192     }
193
194     return false;
195 }
196
197 void CWallet::SetBestChain(const CBlockLocator& loc)
198 {
199     CWalletDB walletdb(strWalletFile);
200     walletdb.WriteBestBlock(loc);
201 }
202
203 // This class implements an addrIncoming entry that causes pre-0.4
204 // clients to crash on startup if reading a private-key-encrypted wallet.
205 class CCorruptAddress
206 {
207 public:
208     IMPLEMENT_SERIALIZE
209     (
210         if (nType & SER_DISK)
211             READWRITE(nVersion);
212     )
213 };
214
215 bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
216 {
217     if (nWalletVersion >= nVersion)
218         return true;
219
220     // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
221     if (fExplicit && nVersion > nWalletMaxVersion)
222             nVersion = FEATURE_LATEST;
223
224     nWalletVersion = nVersion;
225
226     if (nVersion > nWalletMaxVersion)
227         nWalletMaxVersion = nVersion;
228
229     if (fFileBacked)
230     {
231         CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
232         if (nWalletVersion >= 40000)
233         {
234             // Versions prior to 0.4.0 did not support the "minversion" record.
235             // Use a CCorruptAddress to make them crash instead.
236             CCorruptAddress corruptAddress;
237             pwalletdb->WriteSetting("addrIncoming", corruptAddress);
238         }
239         if (nWalletVersion > 40000)
240             pwalletdb->WriteMinVersion(nWalletVersion);
241         if (!pwalletdbIn)
242             delete pwalletdb;
243     }
244
245     return true;
246 }
247
248 bool CWallet::SetMaxVersion(int nVersion)
249 {
250     // cannot downgrade below current version
251     if (nWalletVersion > nVersion)
252         return false;
253
254     nWalletMaxVersion = nVersion;
255
256     return true;
257 }
258
259 bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
260 {
261     if (IsCrypted())
262         return false;
263
264     CKeyingMaterial vMasterKey;
265     RandAddSeedPerfmon();
266
267     vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
268     RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
269
270     CMasterKey kMasterKey(nDerivationMethodIndex);
271
272     RandAddSeedPerfmon();
273     kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
274     RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
275
276     CCrypter crypter;
277     int64 nStartTime = GetTimeMillis();
278     crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
279     kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime));
280
281     nStartTime = GetTimeMillis();
282     crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
283     kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
284
285     if (kMasterKey.nDeriveIterations < 25000)
286         kMasterKey.nDeriveIterations = 25000;
287
288     printf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
289
290     if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
291         return false;
292     if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey))
293         return false;
294
295     {
296         LOCK(cs_wallet);
297         mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
298         if (fFileBacked)
299         {
300             pwalletdbEncryption = new CWalletDB(strWalletFile);
301             if (!pwalletdbEncryption->TxnBegin())
302                 return false;
303             pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
304         }
305
306         if (!EncryptKeys(vMasterKey))
307         {
308             if (fFileBacked)
309                 pwalletdbEncryption->TxnAbort();
310             exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet.
311         }
312
313         // Encryption was introduced in version 0.4.0
314         SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
315
316         if (fFileBacked)
317         {
318             if (!pwalletdbEncryption->TxnCommit())
319                 exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet.
320
321             delete pwalletdbEncryption;
322             pwalletdbEncryption = NULL;
323         }
324
325         Lock();
326         Unlock(strWalletPassphrase);
327         NewKeyPool();
328         Lock();
329
330         // Need to completely rewrite the wallet file; if we don't, bdb might keep
331         // bits of the unencrypted private key in slack space in the database file.
332         CDB::Rewrite(strWalletFile);
333
334     }
335     NotifyStatusChanged(this);
336
337     return true;
338 }
339
340 int64 CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
341 {
342     int64 nRet = nOrderPosNext++;
343     if (pwalletdb) {
344         pwalletdb->WriteOrderPosNext(nOrderPosNext);
345     } else {
346         CWalletDB(strWalletFile).WriteOrderPosNext(nOrderPosNext);
347     }
348     return nRet;
349 }
350
351 CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount)
352 {
353     CWalletDB walletdb(strWalletFile);
354
355     // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap.
356     TxItems txOrdered;
357
358     // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry
359     // would make this much faster for applications that do this a lot.
360     for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
361     {
362         CWalletTx* wtx = &((*it).second);
363         txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0)));
364     }
365     acentries.clear();
366     walletdb.ListAccountCreditDebit(strAccount, acentries);
367     BOOST_FOREACH(CAccountingEntry& entry, acentries)
368     {
369         txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry)));
370     }
371
372     return txOrdered;
373 }
374
375 void CWallet::WalletUpdateSpent(const CTransaction &tx, bool fBlock)
376 {
377     // Anytime a signature is successfully verified, it's proof the outpoint is spent.
378     // Update the wallet spent flag if it doesn't know due to wallet.dat being
379     // restored from backup or the user making copies of wallet.dat.
380     {
381         LOCK(cs_wallet);
382         BOOST_FOREACH(const CTxIn& txin, tx.vin)
383         {
384             map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
385             if (mi != mapWallet.end())
386             {
387                 CWalletTx& wtx = (*mi).second;
388                 if (txin.prevout.n >= wtx.vout.size())
389                     printf("WalletUpdateSpent: bad wtx %s\n", wtx.GetHash().ToString().c_str());
390                 else if (!wtx.IsSpent(txin.prevout.n) && IsMine(wtx.vout[txin.prevout.n]))
391                 {
392                     printf("WalletUpdateSpent found spent coin %snvc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
393                     wtx.MarkSpent(txin.prevout.n);
394                     wtx.WriteToDisk();
395                     NotifyTransactionChanged(this, txin.prevout.hash, CT_UPDATED);
396                     vMintingWalletUpdated.push_back(txin.prevout.hash);
397                 }
398             }
399         }
400
401         if (fBlock)
402         {
403             uint256 hash = tx.GetHash();
404             map<uint256, CWalletTx>::iterator mi = mapWallet.find(hash);
405             CWalletTx& wtx = (*mi).second;
406
407             BOOST_FOREACH(const CTxOut& txout, tx.vout)
408             {
409                 if (IsMine(txout))
410                 {
411                     wtx.MarkUnspent(&txout - &tx.vout[0]);
412                     wtx.WriteToDisk();
413                     NotifyTransactionChanged(this, hash, CT_UPDATED);
414                     vMintingWalletUpdated.push_back(hash);
415                 }
416             }
417         }
418
419     }
420 }
421
422 void CWallet::MarkDirty()
423 {
424     {
425         LOCK(cs_wallet);
426         BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
427             item.second.MarkDirty();
428     }
429 }
430
431 bool CWallet::AddToWallet(const CWalletTx& wtxIn)
432 {
433     uint256 hash = wtxIn.GetHash();
434     {
435         LOCK(cs_wallet);
436         // Inserts only if not already there, returns tx inserted or tx found
437         pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
438         CWalletTx& wtx = (*ret.first).second;
439         wtx.BindWallet(this);
440         bool fInsertedNew = ret.second;
441         if (fInsertedNew)
442         {
443             wtx.nTimeReceived = GetAdjustedTime();
444             wtx.nOrderPos = IncOrderPosNext();
445
446             wtx.nTimeSmart = wtx.nTimeReceived;
447             if (wtxIn.hashBlock != 0)
448             {
449                 if (mapBlockIndex.count(wtxIn.hashBlock))
450                 {
451                     unsigned int latestNow = wtx.nTimeReceived;
452                     unsigned int latestEntry = 0;
453                     {
454                         // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
455                         int64 latestTolerated = latestNow + 300;
456                         std::list<CAccountingEntry> acentries;
457                         TxItems txOrdered = OrderedTxItems(acentries);
458                         for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
459                         {
460                             CWalletTx *const pwtx = (*it).second.first;
461                             if (pwtx == &wtx)
462                                 continue;
463                             CAccountingEntry *const pacentry = (*it).second.second;
464                             int64 nSmartTime;
465                             if (pwtx)
466                             {
467                                 nSmartTime = pwtx->nTimeSmart;
468                                 if (!nSmartTime)
469                                     nSmartTime = pwtx->nTimeReceived;
470                             }
471                             else
472                                 nSmartTime = pacentry->nTime;
473                             if (nSmartTime <= latestTolerated)
474                             {
475                                 latestEntry = nSmartTime;
476                                 if (nSmartTime > latestNow)
477                                     latestNow = nSmartTime;
478                                 break;
479                             }
480                         }
481                     }
482
483                     unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime;
484                     wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
485                 }
486                 else
487                     printf("AddToWallet() : found %s in block %s not in index\n",
488                            wtxIn.GetHash().ToString().substr(0,10).c_str(),
489                            wtxIn.hashBlock.ToString().c_str());
490             }
491         }
492
493         bool fUpdated = false;
494         if (!fInsertedNew)
495         {
496             // Merge
497             if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
498             {
499                 wtx.hashBlock = wtxIn.hashBlock;
500                 fUpdated = true;
501             }
502             if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
503             {
504                 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
505                 wtx.nIndex = wtxIn.nIndex;
506                 fUpdated = true;
507             }
508             if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
509             {
510                 wtx.fFromMe = wtxIn.fFromMe;
511                 fUpdated = true;
512             }
513             fUpdated |= wtx.UpdateSpent(wtxIn.vfSpent);
514         }
515
516         //// debug print
517         printf("AddToWallet %s  %s%s\n", wtxIn.GetHash().ToString().substr(0,10).c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
518
519         // Write to disk
520         if (fInsertedNew || fUpdated)
521             if (!wtx.WriteToDisk())
522                 return false;
523 #ifndef QT_GUI
524         // If default receiving address gets used, replace it with a new one
525         CScript scriptDefaultKey;
526         scriptDefaultKey.SetDestination(vchDefaultKey.GetID());
527         BOOST_FOREACH(const CTxOut& txout, wtx.vout)
528         {
529             if (txout.scriptPubKey == scriptDefaultKey)
530             {
531                 CPubKey newDefaultKey;
532                 if (GetKeyFromPool(newDefaultKey, false))
533                 {
534                     SetDefaultKey(newDefaultKey);
535                     SetAddressBookName(vchDefaultKey.GetID(), "");
536                 }
537             }
538         }
539 #endif
540         // since AddToWallet is called directly for self-originating transactions, check for consumption of own coins
541         WalletUpdateSpent(wtx, (wtxIn.hashBlock != 0));
542
543         // Notify UI of new or updated transaction
544         NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
545         vMintingWalletUpdated.push_back(hash);
546         // notify an external script when a wallet transaction comes in or is updated
547         std::string strCmd = GetArg("-walletnotify", "");
548
549         if ( !strCmd.empty())
550         {
551             boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
552             boost::thread t(runCommand, strCmd); // thread runs free
553         }
554
555     }
556     return true;
557 }
558
559 // Add a transaction to the wallet, or update it.
560 // pblock is optional, but should be provided if the transaction is known to be in a block.
561 // If fUpdate is true, existing transactions will be updated.
562 bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate, bool fFindBlock)
563 {
564     uint256 hash = tx.GetHash();
565     {
566         LOCK(cs_wallet);
567         bool fExisted = mapWallet.count(hash);
568         if (fExisted && !fUpdate) return false;
569         if (fExisted || IsMine(tx) || IsFromMe(tx))
570         {
571             CWalletTx wtx(this,tx);
572             // Get merkle branch if transaction was found in a block
573             if (pblock)
574                 wtx.SetMerkleBranch(pblock);
575             return AddToWallet(wtx);
576         }
577         else
578             WalletUpdateSpent(tx);
579     }
580     return false;
581 }
582
583 bool CWallet::EraseFromWallet(uint256 hash)
584 {
585     if (!fFileBacked)
586         return false;
587     {
588         LOCK(cs_wallet);
589         if (mapWallet.erase(hash))
590             CWalletDB(strWalletFile).EraseTx(hash);
591     }
592     return true;
593 }
594
595
596 isminetype CWallet::IsMine(const CTxIn &txin) const
597 {
598     {
599         LOCK(cs_wallet);
600         map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
601         if (mi != mapWallet.end())
602         {
603             const CWalletTx& prev = (*mi).second;
604             if (txin.prevout.n < prev.vout.size())
605                 return IsMine(prev.vout[txin.prevout.n]);
606         }
607     }
608     return MINE_NO;
609 }
610
611 int64 CWallet::GetDebit(const CTxIn &txin, const isminefilter& filter) const
612 {
613     {
614         LOCK(cs_wallet);
615         map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
616         if (mi != mapWallet.end())
617         {
618             const CWalletTx& prev = (*mi).second;
619             if (txin.prevout.n < prev.vout.size())
620                 if (IsMine(prev.vout[txin.prevout.n]) & filter)
621                     return prev.vout[txin.prevout.n].nValue;
622         }
623     }
624     return 0;
625 }
626
627 bool CWallet::IsChange(const CTxOut& txout) const
628 {
629     // TODO: fix handling of 'change' outputs. The assumption is that any
630     // payment to a script that is ours, but isn't in the address book
631     // is change. That assumption is likely to break when we implement multisignature
632     // wallets that return change back into a multi-signature-protected address;
633     // a better way of identifying which outputs are 'the send' and which are
634     // 'the change' will need to be implemented (maybe extend CWalletTx to remember
635     // which output, if any, was change).
636     if (::IsMine(*this, txout.scriptPubKey))
637     {
638         CTxDestination address;
639         if (!ExtractDestination(txout.scriptPubKey, address))
640             return true;
641
642         LOCK(cs_wallet);
643         if (!mapAddressBook.count(address))
644             return true;
645     }
646     return false;
647 }
648
649 int64 CWalletTx::GetTxTime() const
650 {
651     int64 n = nTimeSmart;
652     return n ? n : nTimeReceived;
653 }
654
655 int CWalletTx::GetRequestCount() const
656 {
657     // Returns -1 if it wasn't being tracked
658     int nRequests = -1;
659     {
660         LOCK(pwallet->cs_wallet);
661         if (IsCoinBase() || IsCoinStake())
662         {
663             // Generated block
664             if (hashBlock != 0)
665             {
666                 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
667                 if (mi != pwallet->mapRequestCount.end())
668                     nRequests = (*mi).second;
669             }
670         }
671         else
672         {
673             // Did anyone request this transaction?
674             map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
675             if (mi != pwallet->mapRequestCount.end())
676             {
677                 nRequests = (*mi).second;
678
679                 // How about the block it's in?
680                 if (nRequests == 0 && hashBlock != 0)
681                 {
682                     map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
683                     if (mi != pwallet->mapRequestCount.end())
684                         nRequests = (*mi).second;
685                     else
686                         nRequests = 1; // If it's in someone else's block it must have got out
687                 }
688             }
689         }
690     }
691     return nRequests;
692 }
693
694 void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, list<pair<CTxDestination, int64> >& listReceived,
695                            list<pair<CTxDestination, int64> >& listSent, int64& nFee, string& strSentAccount, const isminefilter& filter) const
696 {
697     nGeneratedImmature = nGeneratedMature = nFee = 0;
698     listReceived.clear();
699     listSent.clear();
700     strSentAccount = strFromAccount;
701
702     if (IsCoinBase() || IsCoinStake())
703     {
704         if (GetBlocksToMaturity() > 0)
705             nGeneratedImmature = pwallet->GetCredit(*this, filter);
706         else
707             nGeneratedMature = GetCredit(filter);
708         return;
709     }
710
711     // Compute fee:
712     int64 nDebit = GetDebit(filter);
713     if (nDebit > 0) // debit>0 means we signed/sent this transaction
714     {
715         int64 nValueOut = GetValueOut();
716         nFee = nDebit - nValueOut;
717     }
718
719     // Sent/received.
720     BOOST_FOREACH(const CTxOut& txout, vout)
721     {
722         isminetype fIsMine = pwallet->IsMine(txout);
723         // Only need to handle txouts if AT LEAST one of these is true:
724         //   1) they debit from us (sent)
725         //   2) the output is to us (received)
726         if (nDebit > 0)
727         {
728             // Don't report 'change' txouts
729             if (pwallet->IsChange(txout))
730                 continue;
731         }
732         else if (!(fIsMine & filter))
733             continue;
734
735         // In either case, we need to get the destination address
736         CTxDestination address;
737         if (!ExtractDestination(txout.scriptPubKey, address))
738         {
739             printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
740                    this->GetHash().ToString().c_str());
741             address = CNoDestination();
742         }
743
744         // If we are debited by the transaction, add the output as a "sent" entry
745         if (nDebit > 0)
746             listSent.push_back(make_pair(address, txout.nValue));
747
748         // If we are receiving the output, add it as a "received" entry
749         if (fIsMine & filter)
750             listReceived.push_back(make_pair(address, txout.nValue));
751     }
752
753 }
754
755 void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, int64& nReceived,
756                                   int64& nSent, int64& nFee, const isminefilter& filter) const
757 {
758     nGenerated = nReceived = nSent = nFee = 0;
759
760     int64 allGeneratedImmature, allGeneratedMature, allFee;
761     allGeneratedImmature = allGeneratedMature = allFee = 0;
762     string strSentAccount;
763     list<pair<CTxDestination, int64> > listReceived;
764     list<pair<CTxDestination, int64> > listSent;
765     GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount, filter);
766
767     if (strAccount == "")
768         nGenerated = allGeneratedMature;
769     if (strAccount == strSentAccount)
770     {
771         BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& s, listSent)
772             nSent += s.second;
773         nFee = allFee;
774     }
775     {
776         LOCK(pwallet->cs_wallet);
777         BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& r, listReceived)
778         {
779             if (pwallet->mapAddressBook.count(r.first))
780             {
781                 map<CTxDestination, string>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
782                 if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount)
783                     nReceived += r.second;
784             }
785             else if (strAccount.empty())
786             {
787                 nReceived += r.second;
788             }
789         }
790     }
791 }
792
793 void CWalletTx::AddSupportingTransactions(CTxDB& txdb)
794 {
795     vtxPrev.clear();
796
797     const int COPY_DEPTH = 3;
798     if (SetMerkleBranch() < COPY_DEPTH)
799     {
800         vector<uint256> vWorkQueue;
801         BOOST_FOREACH(const CTxIn& txin, vin)
802             vWorkQueue.push_back(txin.prevout.hash);
803
804         // This critsect is OK because txdb is already open
805         {
806             LOCK(pwallet->cs_wallet);
807             map<uint256, const CMerkleTx*> mapWalletPrev;
808             set<uint256> setAlreadyDone;
809             for (unsigned int i = 0; i < vWorkQueue.size(); i++)
810             {
811                 uint256 hash = vWorkQueue[i];
812                 if (setAlreadyDone.count(hash))
813                     continue;
814                 setAlreadyDone.insert(hash);
815
816                 CMerkleTx tx;
817                 map<uint256, CWalletTx>::const_iterator mi = pwallet->mapWallet.find(hash);
818                 if (mi != pwallet->mapWallet.end())
819                 {
820                     tx = (*mi).second;
821                     BOOST_FOREACH(const CMerkleTx& txWalletPrev, (*mi).second.vtxPrev)
822                         mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
823                 }
824                 else if (mapWalletPrev.count(hash))
825                 {
826                     tx = *mapWalletPrev[hash];
827                 }
828                 else if (!fClient && txdb.ReadDiskTx(hash, tx))
829                 {
830                     ;
831                 }
832                 else
833                 {
834                     printf("ERROR: AddSupportingTransactions() : unsupported transaction\n");
835                     continue;
836                 }
837
838                 int nDepth = tx.SetMerkleBranch();
839                 vtxPrev.push_back(tx);
840
841                 if (nDepth < COPY_DEPTH)
842                 {
843                     BOOST_FOREACH(const CTxIn& txin, tx.vin)
844                         vWorkQueue.push_back(txin.prevout.hash);
845                 }
846             }
847         }
848     }
849
850     reverse(vtxPrev.begin(), vtxPrev.end());
851 }
852
853 bool CWalletTx::WriteToDisk()
854 {
855     return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
856 }
857
858 // Scan the block chain (starting in pindexStart) for transactions
859 // from or to us. If fUpdate is true, found transactions that already
860 // exist in the wallet will be updated.
861 int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
862 {
863     int ret = 0;
864
865     CBlockIndex* pindex = pindexStart;
866     {
867         LOCK(cs_wallet);
868         while (pindex)
869         {
870             CBlock block;
871             block.ReadFromDisk(pindex, true);
872             BOOST_FOREACH(CTransaction& tx, block.vtx)
873             {
874                 if (AddToWalletIfInvolvingMe(tx, &block, fUpdate))
875                     ret++;
876             }
877             pindex = pindex->pnext;
878         }
879     }
880     return ret;
881 }
882
883 int CWallet::ScanForWalletTransaction(const uint256& hashTx)
884 {
885     CTransaction tx;
886     tx.ReadFromDisk(COutPoint(hashTx, 0));
887     if (AddToWalletIfInvolvingMe(tx, NULL, true, true))
888         return 1;
889     return 0;
890 }
891
892 void CWallet::ReacceptWalletTransactions()
893 {
894     CTxDB txdb("r");
895     bool fRepeat = true;
896     while (fRepeat)
897     {
898         LOCK(cs_wallet);
899         fRepeat = false;
900         vector<CDiskTxPos> vMissingTx;
901         BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
902         {
903             CWalletTx& wtx = item.second;
904             if ((wtx.IsCoinBase() && wtx.IsSpent(0)) || (wtx.IsCoinStake() && wtx.IsSpent(1)))
905                 continue;
906
907             CTxIndex txindex;
908             bool fUpdated = false;
909             if (txdb.ReadTxIndex(wtx.GetHash(), txindex))
910             {
911                 // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
912                 if (txindex.vSpent.size() != wtx.vout.size())
913                 {
914                     printf("ERROR: ReacceptWalletTransactions() : txindex.vSpent.size() %"PRIszu" != wtx.vout.size() %"PRIszu"\n", txindex.vSpent.size(), wtx.vout.size());
915                     continue;
916                 }
917                 for (unsigned int i = 0; i < txindex.vSpent.size(); i++)
918                 {
919                     if (wtx.IsSpent(i))
920                         continue;
921                     if (!txindex.vSpent[i].IsNull() && IsMine(wtx.vout[i]))
922                     {
923                         wtx.MarkSpent(i);
924                         fUpdated = true;
925                         vMissingTx.push_back(txindex.vSpent[i]);
926                     }
927                 }
928                 if (fUpdated)
929                 {
930                     printf("ReacceptWalletTransactions found spent coin %snvc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
931                     wtx.MarkDirty();
932                     wtx.WriteToDisk();
933                 }
934             }
935             else
936             {
937                 // Re-accept any txes of ours that aren't already in a block
938                 if (!(wtx.IsCoinBase() || wtx.IsCoinStake()))
939                     wtx.AcceptWalletTransaction(txdb, false);
940             }
941         }
942         if (!vMissingTx.empty())
943         {
944             // TODO: optimize this to scan just part of the block chain?
945             if (ScanForWalletTransactions(pindexGenesisBlock))
946                 fRepeat = true;  // Found missing transactions: re-do re-accept.
947         }
948     }
949 }
950
951 void CWalletTx::RelayWalletTransaction(CTxDB& txdb)
952 {
953     BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
954     {
955         if (!(tx.IsCoinBase() || tx.IsCoinStake()))
956         {
957             uint256 hash = tx.GetHash();
958             if (!txdb.ContainsTx(hash))
959                 RelayTransaction((CTransaction)tx, hash);
960         }
961     }
962     if (!(IsCoinBase() || IsCoinStake()))
963     {
964         uint256 hash = GetHash();
965         if (!txdb.ContainsTx(hash))
966         {
967             printf("Relaying wtx %s\n", hash.ToString().substr(0,10).c_str());
968             RelayTransaction((CTransaction)*this, hash);
969         }
970     }
971 }
972
973 void CWalletTx::RelayWalletTransaction()
974 {
975    CTxDB txdb("r");
976    RelayWalletTransaction(txdb);
977 }
978
979 void CWallet::ResendWalletTransactions()
980 {
981     // Do this infrequently and randomly to avoid giving away
982     // that these are our transactions.
983     static int64 nNextTime;
984     if (GetTime() < nNextTime)
985         return;
986     bool fFirst = (nNextTime == 0);
987     nNextTime = GetTime() + GetRand(30 * 60);
988     if (fFirst)
989         return;
990
991     // Only do it if there's been a new block since last time
992     static int64 nLastTime;
993     if (nTimeBestReceived < nLastTime)
994         return;
995     nLastTime = GetTime();
996
997     // Rebroadcast any of our txes that aren't in a block yet
998     printf("ResendWalletTransactions()\n");
999     CTxDB txdb("r");
1000     {
1001         LOCK(cs_wallet);
1002         // Sort them in chronological order
1003         multimap<unsigned int, CWalletTx*> mapSorted;
1004         BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
1005         {
1006             CWalletTx& wtx = item.second;
1007             // Don't rebroadcast until it's had plenty of time that
1008             // it should have gotten in already by now.
1009             if (nTimeBestReceived - (int64)wtx.nTimeReceived > 5 * 60)
1010                 mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
1011         }
1012         BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
1013         {
1014             CWalletTx& wtx = *item.second;
1015             if (wtx.CheckTransaction())
1016                 wtx.RelayWalletTransaction(txdb);
1017             else
1018                 printf("ResendWalletTransactions() : CheckTransaction failed for transaction %s\n", wtx.GetHash().ToString().c_str());
1019         }
1020     }
1021 }
1022
1023
1024
1025
1026
1027
1028 //////////////////////////////////////////////////////////////////////////////
1029 //
1030 // Actions
1031 //
1032
1033
1034 int64 CWallet::GetBalance() const
1035 {
1036     int64 nTotal = 0;
1037     {
1038         LOCK(cs_wallet);
1039         for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1040         {
1041             const CWalletTx* pcoin = &(*it).second;
1042             if (pcoin->IsTrusted())
1043                 nTotal += pcoin->GetAvailableCredit();
1044         }
1045     }
1046
1047     return nTotal;
1048 }
1049
1050 int64 CWallet::GetWatchOnlyBalance() const
1051 {
1052     int64 nTotal = 0;
1053     {
1054         LOCK(cs_wallet);
1055         for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1056         {
1057             const CWalletTx* pcoin = &(*it).second;
1058             if (pcoin->IsTrusted())
1059                 nTotal += pcoin->GetAvailableWatchCredit();
1060         }
1061     }
1062
1063     return nTotal;
1064 }
1065
1066 int64 CWallet::GetUnconfirmedBalance() const
1067 {
1068     int64 nTotal = 0;
1069     {
1070         LOCK(cs_wallet);
1071         for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1072         {
1073             const CWalletTx* pcoin = &(*it).second;
1074             if (!pcoin->IsFinal() || !pcoin->IsTrusted())
1075                 nTotal += pcoin->GetAvailableCredit();
1076         }
1077     }
1078     return nTotal;
1079 }
1080
1081 int64 CWallet::GetUnconfirmedWatchOnlyBalance() const
1082 {
1083     int64 nTotal = 0;
1084     {
1085         LOCK(cs_wallet);
1086         for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1087         {
1088             const CWalletTx* pcoin = &(*it).second;
1089             if (!pcoin->IsFinal() || !pcoin->IsTrusted())
1090                 nTotal += pcoin->GetAvailableWatchCredit();
1091         }
1092     }
1093     return nTotal;
1094 }
1095
1096 int64 CWallet::GetImmatureBalance() const
1097 {
1098     int64 nTotal = 0;
1099     {
1100         LOCK(cs_wallet);
1101         for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1102         {
1103             const CWalletTx* pcoin = &(*it).second;
1104             nTotal += pcoin->GetImmatureCredit();
1105         }
1106     }
1107     return nTotal;
1108 }
1109
1110 int64 CWallet::GetImmatureWatchOnlyBalance() const
1111 {
1112     int64 nTotal = 0;
1113     {
1114         LOCK(cs_wallet);
1115         for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1116         {
1117             const CWalletTx* pcoin = &(*it).second;
1118             nTotal += pcoin->GetImmatureWatchOnlyCredit();
1119         }
1120     }
1121     return nTotal;
1122 }
1123
1124 // populate vCoins with vector of spendable COutputs
1125 void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const CCoinControl *coinControl) const
1126 {
1127     vCoins.clear();
1128
1129     {
1130         LOCK(cs_wallet);
1131         for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1132         {
1133             const CWalletTx* pcoin = &(*it).second;
1134
1135             if (!pcoin->IsFinal())
1136                 continue;
1137
1138             if (fOnlyConfirmed && !pcoin->IsTrusted())
1139                 continue;
1140
1141             if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
1142                 continue;
1143
1144             if(pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0)
1145                 continue;
1146
1147             for (unsigned int i = 0; i < pcoin->vout.size(); i++) {
1148                 isminetype mine = IsMine(pcoin->vout[i]);
1149                 if (!(pcoin->IsSpent(i)) && mine != MINE_NO && 
1150                     pcoin->vout[i].nValue >= nMinimumInputValue &&
1151                     (!coinControl || !coinControl->HasSelected() || coinControl->IsSelected((*it).first, i)))
1152                 {
1153                     vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain(), mine == MINE_SPENDABLE));
1154                 }
1155             }
1156         }
1157     }
1158 }
1159
1160 void CWallet::AvailableCoinsMinConf(vector<COutput>& vCoins, int nConf, int64 nMinValue, int64 nMaxValue) const
1161 {
1162     vCoins.clear();
1163
1164     {
1165         LOCK(cs_wallet);
1166         for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1167         {
1168             const CWalletTx* pcoin = &(*it).second;
1169
1170             if (!pcoin->IsFinal())
1171                 continue;
1172
1173             if(pcoin->GetDepthInMainChain() < nConf)
1174                 continue;
1175
1176             for (unsigned int i = 0; i < pcoin->vout.size(); i++) {
1177                 isminetype mine = IsMine(pcoin->vout[i]);
1178
1179                 // ignore coin if it was already spent or we don't own it
1180                 if (pcoin->IsSpent(i) || mine == MINE_NO)
1181                     continue;
1182
1183                 // if coin value is between required limits then add new item to vector
1184                 if (pcoin->vout[i].nValue >= nMinValue && pcoin->vout[i].nValue < nMaxValue)
1185                     vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain(), mine == MINE_SPENDABLE));
1186             }
1187         }
1188     }
1189 }
1190
1191 static void ApproximateBestSubset(vector<pair<int64, pair<const CWalletTx*,unsigned int> > >vValue, int64 nTotalLower, int64 nTargetValue,
1192                                   vector<char>& vfBest, int64& nBest, int iterations = 1000)
1193 {
1194     vector<char> vfIncluded;
1195
1196     vfBest.assign(vValue.size(), true);
1197     nBest = nTotalLower;
1198
1199     for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
1200     {
1201         vfIncluded.assign(vValue.size(), false);
1202         int64 nTotal = 0;
1203         bool fReachedTarget = false;
1204         for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
1205         {
1206             for (unsigned int i = 0; i < vValue.size(); i++)
1207             {
1208                 if (nPass == 0 ? rand() % 2 : !vfIncluded[i])
1209                 {
1210                     nTotal += vValue[i].first;
1211                     vfIncluded[i] = true;
1212                     if (nTotal >= nTargetValue)
1213                     {
1214                         fReachedTarget = true;
1215                         if (nTotal < nBest)
1216                         {
1217                             nBest = nTotal;
1218                             vfBest = vfIncluded;
1219                         }
1220                         nTotal -= vValue[i].first;
1221                         vfIncluded[i] = false;
1222                     }
1223                 }
1224             }
1225         }
1226     }
1227 }
1228
1229 int64 CWallet::GetStake() const
1230 {
1231     int64 nTotal = 0;
1232     LOCK(cs_wallet);
1233     for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1234     {
1235         const CWalletTx* pcoin = &(*it).second;
1236         if (pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1237             nTotal += CWallet::GetCredit(*pcoin, MINE_ALL);
1238     }
1239     return nTotal;
1240 }
1241
1242 int64 CWallet::GetWatchOnlyStake() const
1243 {
1244     int64 nTotal = 0;
1245     LOCK(cs_wallet);
1246     for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1247     {
1248         const CWalletTx* pcoin = &(*it).second;
1249         if (pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1250             nTotal += CWallet::GetCredit(*pcoin, MINE_WATCH_ONLY);
1251     }
1252     return nTotal;
1253 }
1254
1255 int64 CWallet::GetNewMint() const
1256 {
1257     int64 nTotal = 0;
1258     LOCK(cs_wallet);
1259     for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1260     {
1261         const CWalletTx* pcoin = &(*it).second;
1262         if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1263             nTotal += CWallet::GetCredit(*pcoin, MINE_ALL);
1264     }
1265     return nTotal;
1266 }
1267
1268 int64 CWallet::GetWatchOnlyNewMint() const
1269 {
1270     int64 nTotal = 0;
1271     LOCK(cs_wallet);
1272     for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1273     {
1274         const CWalletTx* pcoin = &(*it).second;
1275         if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1276             nTotal += CWallet::GetCredit(*pcoin, MINE_WATCH_ONLY);
1277     }
1278     return nTotal;
1279 }
1280
1281 bool CWallet::SelectCoinsMinConf(int64 nTargetValue, unsigned int nSpendTime, int nConfMine, int nConfTheirs, vector<COutput> vCoins, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
1282 {
1283     setCoinsRet.clear();
1284     nValueRet = 0;
1285
1286     // List of values less than target
1287     pair<int64, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
1288     coinLowestLarger.first = std::numeric_limits<int64>::max();
1289     coinLowestLarger.second.first = NULL;
1290     vector<pair<int64, pair<const CWalletTx*,unsigned int> > > vValue;
1291     int64 nTotalLower = 0;
1292
1293     random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
1294
1295     BOOST_FOREACH(const COutput &output, vCoins)
1296     {
1297         if (!output.fSpendable)
1298             continue;
1299
1300         const CWalletTx *pcoin = output.tx;
1301
1302         if (output.nDepth < (pcoin->IsFromMe(MINE_ALL) ? nConfMine : nConfTheirs))
1303             continue;
1304
1305         int i = output.i;
1306
1307         // Follow the timestamp rules
1308         if (pcoin->nTime > nSpendTime)
1309             continue;
1310
1311         int64 n = pcoin->vout[i].nValue;
1312
1313         pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
1314
1315         if (n == nTargetValue)
1316         {
1317             setCoinsRet.insert(coin.second);
1318             nValueRet += coin.first;
1319             return true;
1320         }
1321         else if (n < nTargetValue + CENT)
1322         {
1323             vValue.push_back(coin);
1324             nTotalLower += n;
1325         }
1326         else if (n < coinLowestLarger.first)
1327         {
1328             coinLowestLarger = coin;
1329         }
1330     }
1331
1332     if (nTotalLower == nTargetValue)
1333     {
1334         for (unsigned int i = 0; i < vValue.size(); ++i)
1335         {
1336             setCoinsRet.insert(vValue[i].second);
1337             nValueRet += vValue[i].first;
1338         }
1339         return true;
1340     }
1341
1342     if (nTotalLower < nTargetValue)
1343     {
1344         if (coinLowestLarger.second.first == NULL)
1345             return false;
1346         setCoinsRet.insert(coinLowestLarger.second);
1347         nValueRet += coinLowestLarger.first;
1348         return true;
1349     }
1350
1351     // Solve subset sum by stochastic approximation
1352     sort(vValue.rbegin(), vValue.rend(), CompareValueOnly());
1353     vector<char> vfBest;
1354     int64 nBest;
1355
1356     ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000);
1357     if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT)
1358         ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000);
1359
1360     // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
1361     //                                   or the next bigger coin is closer), return the bigger coin
1362     if (coinLowestLarger.second.first &&
1363         ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest))
1364     {
1365         setCoinsRet.insert(coinLowestLarger.second);
1366         nValueRet += coinLowestLarger.first;
1367     }
1368     else {
1369         for (unsigned int i = 0; i < vValue.size(); i++)
1370             if (vfBest[i])
1371             {
1372                 setCoinsRet.insert(vValue[i].second);
1373                 nValueRet += vValue[i].first;
1374             }
1375
1376         if (fDebug && GetBoolArg("-printpriority"))
1377         {
1378             //// debug print
1379             printf("SelectCoins() best subset: ");
1380             for (unsigned int i = 0; i < vValue.size(); i++)
1381                 if (vfBest[i])
1382                     printf("%s ", FormatMoney(vValue[i].first).c_str());
1383             printf("total %s\n", FormatMoney(nBest).c_str());
1384         }
1385     }
1386
1387     return true;
1388 }
1389
1390 bool CWallet::SelectCoins(int64 nTargetValue, unsigned int nSpendTime, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet, const CCoinControl* coinControl) const
1391 {
1392     vector<COutput> vCoins;
1393     AvailableCoins(vCoins, true, coinControl);
1394
1395     // coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
1396     if (coinControl && coinControl->HasSelected())
1397     {
1398         BOOST_FOREACH(const COutput& out, vCoins)
1399         {
1400             if(!out.fSpendable)
1401                 continue;
1402             nValueRet += out.tx->vout[out.i].nValue;
1403             setCoinsRet.insert(make_pair(out.tx, out.i));
1404         }
1405         return (nValueRet >= nTargetValue);
1406     }
1407
1408     return (SelectCoinsMinConf(nTargetValue, nSpendTime, 1, 6, vCoins, setCoinsRet, nValueRet) ||
1409             SelectCoinsMinConf(nTargetValue, nSpendTime, 1, 1, vCoins, setCoinsRet, nValueRet) ||
1410             SelectCoinsMinConf(nTargetValue, nSpendTime, 0, 1, vCoins, setCoinsRet, nValueRet));
1411 }
1412
1413 // Select some coins without random shuffle or best subset approximation
1414 bool CWallet::SelectCoinsSimple(int64 nTargetValue, int64 nMinValue, int64 nMaxValue, unsigned int nSpendTime, int nMinConf, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
1415 {
1416     vector<COutput> vCoins;
1417     AvailableCoinsMinConf(vCoins, nMinConf, nMinValue, nMaxValue);
1418
1419     setCoinsRet.clear();
1420     nValueRet = 0;
1421
1422     BOOST_FOREACH(COutput output, vCoins)
1423     {
1424         if(!output.fSpendable)
1425             continue;
1426         const CWalletTx *pcoin = output.tx;
1427         int i = output.i;
1428
1429         // Ignore immature coins
1430         if (pcoin->GetBlocksToMaturity() > 0)
1431             continue;
1432
1433         // Stop if we've chosen enough inputs
1434         if (nValueRet >= nTargetValue)
1435             break;
1436
1437         // Follow the timestamp rules
1438         if (pcoin->nTime > nSpendTime)
1439             continue;
1440
1441         int64 n = pcoin->vout[i].nValue;
1442
1443         pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
1444
1445         if (n >= nTargetValue)
1446         {
1447             // If input value is greater or equal to target then simply insert
1448             //    it into the current subset and exit
1449             setCoinsRet.insert(coin.second);
1450             nValueRet += coin.first;
1451             break;
1452         }
1453         else if (n < nTargetValue + CENT)
1454         {
1455             setCoinsRet.insert(coin.second);
1456             nValueRet += coin.first;
1457         }
1458     }
1459
1460     return true;
1461 }
1462
1463 bool CWallet::CreateTransaction(const vector<pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, const CCoinControl* coinControl)
1464 {
1465     int64 nValue = 0;
1466     BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1467     {
1468         if (nValue < 0)
1469             return false;
1470         nValue += s.second;
1471     }
1472     if (vecSend.empty() || nValue < 0)
1473         return false;
1474
1475     wtxNew.BindWallet(this);
1476
1477     {
1478         LOCK2(cs_main, cs_wallet);
1479         // txdb must be opened before the mapWallet lock
1480         CTxDB txdb("r");
1481         {
1482             nFeeRet = nTransactionFee;
1483             while (true)
1484             {
1485                 wtxNew.vin.clear();
1486                 wtxNew.vout.clear();
1487                 wtxNew.fFromMe = true;
1488
1489                 int64 nTotalValue = nValue + nFeeRet;
1490                 double dPriority = 0;
1491                 // vouts to the payees
1492                 BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1493                     wtxNew.vout.push_back(CTxOut(s.second, s.first));
1494
1495                 // Choose coins to use
1496                 set<pair<const CWalletTx*,unsigned int> > setCoins;
1497                 int64 nValueIn = 0;
1498                 if (!SelectCoins(nTotalValue, wtxNew.nTime, setCoins, nValueIn, coinControl))
1499                     return false;
1500                 BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1501                 {
1502                     int64 nCredit = pcoin.first->vout[pcoin.second].nValue;
1503                     dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain();
1504                 }
1505
1506                 int64 nChange = nValueIn - nValue - nFeeRet;
1507                 // if sub-cent change is required, the fee must be raised to at least MIN_TX_FEE
1508                 // or until nChange becomes zero
1509                 // NOTE: this depends on the exact behaviour of GetMinFee
1510                 if (wtxNew.nTime < FEE_SWITCH_TIME && !fTestNet)
1511                 {
1512                     if (nFeeRet < MIN_TX_FEE && nChange > 0 && nChange < CENT)
1513                     {
1514                         int64 nMoveToFee = min(nChange, MIN_TX_FEE - nFeeRet);
1515                         nChange -= nMoveToFee;
1516                         nFeeRet += nMoveToFee;
1517                     }
1518
1519                     // sub-cent change is moved to fee
1520                     if (nChange > 0 && nChange < CENT)
1521                     {
1522                         nFeeRet += nChange;
1523                         nChange = 0;
1524                     }
1525                 }
1526
1527                 if (nChange > 0)
1528                 {
1529                     // Fill a vout to ourself
1530                     // TODO: pass in scriptChange instead of reservekey so
1531                     // change transaction isn't always pay-to-bitcoin-address
1532                     CScript scriptChange;
1533
1534                     // coin control: send change to custom address
1535                     if (coinControl && !boost::get<CNoDestination>(&coinControl->destChange))
1536                         scriptChange.SetDestination(coinControl->destChange);
1537
1538                     // no coin control: send change to newly generated address
1539                     else
1540                     {
1541                         // Note: We use a new key here to keep it from being obvious which side is the change.
1542                         //  The drawback is that by not reusing a previous key, the change may be lost if a
1543                         //  backup is restored, if the backup doesn't have the new private key for the change.
1544                         //  If we reused the old key, it would be possible to add code to look for and
1545                         //  rediscover unknown transactions that were written with keys of ours to recover
1546                         //  post-backup change.
1547
1548                         // Reserve a new key pair from key pool
1549                         CPubKey vchPubKey = reservekey.GetReservedKey();
1550
1551                         scriptChange.SetDestination(vchPubKey.GetID());
1552                     }
1553
1554                     // Insert change txn at random position:
1555                     vector<CTxOut>::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size());
1556                     wtxNew.vout.insert(position, CTxOut(nChange, scriptChange));
1557                 }
1558                 else
1559                     reservekey.ReturnKey();
1560
1561                 // Fill vin
1562                 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1563                     wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
1564
1565                 // Sign
1566                 int nIn = 0;
1567                 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1568                     if (!SignSignature(*this, *coin.first, wtxNew, nIn++))
1569                         return false;
1570
1571                 // Limit size
1572                 unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
1573                 if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
1574                     return false;
1575                 dPriority /= nBytes;
1576
1577                 // Check that enough fee is included
1578                 int64 nPayFee = nTransactionFee * (1 + (int64)nBytes / 1000);
1579                 bool fAllowFree = CTransaction::AllowFree(dPriority);
1580
1581                 // Disable free transactions until 1 July 2014
1582                 if (!fTestNet && wtxNew.nTime < FEE_SWITCH_TIME)
1583                 {
1584                     fAllowFree = false;
1585                 }
1586
1587                 int64 nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND, nBytes);
1588
1589                 if (nFeeRet < max(nPayFee, nMinFee))
1590                 {
1591                     nFeeRet = max(nPayFee, nMinFee);
1592                     continue;
1593                 }
1594
1595                 // Fill vtxPrev by copying from previous transactions vtxPrev
1596                 wtxNew.AddSupportingTransactions(txdb);
1597                 wtxNew.fTimeReceivedIsTxTime = true;
1598
1599                 break;
1600             }
1601         }
1602     }
1603     return true;
1604 }
1605
1606 bool CWallet::CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, const CCoinControl* coinControl)
1607 {
1608     vector< pair<CScript, int64> > vecSend;
1609     vecSend.push_back(make_pair(scriptPubKey, nValue));
1610     return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, coinControl);
1611 }
1612
1613 void CWallet::GetStakeWeightFromValue(const int64& nTime, const int64& nValue, uint64& nWeight)
1614 {
1615     int64 nTimeWeight = GetWeight(nTime, (int64)GetTime());
1616
1617     // If time weight is lower or equal to zero then weight is zero.
1618     if (nTimeWeight <= 0)
1619     {
1620         nWeight = 0;
1621         return;
1622     }
1623
1624     CBigNum bnCoinDayWeight = CBigNum(nValue) * nTimeWeight / COIN / (24 * 60 * 60);
1625     nWeight = bnCoinDayWeight.getuint64();
1626 }
1627
1628
1629 // NovaCoin: get current stake miner statistics
1630 void CWallet::GetStakeStats(float &nKernelsRate, float &nCoinDaysRate)
1631 {
1632     static uint64 nLastKernels = 0, nLastCoinDays = 0;
1633     static float nLastKernelsRate = 0, nLastCoinDaysRate = 0;
1634     static unsigned int nLastTime = GetTime();
1635
1636
1637     if (nKernelsTried < nLastKernels)
1638     {
1639         nLastKernels = 0;
1640         nLastCoinDays = 0;
1641
1642         nLastTime = GetTime();
1643     }
1644
1645     unsigned int nInterval = GetTime() - nLastTime;
1646     if (nKernelsTried > 1000 && nInterval > 5)
1647     {
1648         nKernelsRate = nLastKernelsRate = ( nKernelsTried - nLastKernels ) / (float) nInterval;
1649         nCoinDaysRate = nLastCoinDaysRate = ( nCoinDaysTried - nLastCoinDays ) / (float) nInterval;
1650
1651         nLastKernels = nKernelsTried;
1652         nLastCoinDays = nCoinDaysTried;
1653         nLastTime = GetTime();
1654     }
1655     else
1656     {
1657         nKernelsRate = nLastKernelsRate;
1658         nCoinDaysRate = nLastCoinDaysRate;
1659     }
1660 }
1661
1662 bool CWallet::MergeCoins(const int64& nAmount, const int64& nMinValue, const int64& nOutputValue, list<uint256>& listMerged)
1663 {
1664     int64 nBalance = GetBalance();
1665
1666     if (nAmount > nBalance)
1667         return false;
1668
1669     listMerged.clear();
1670     int64 nValueIn = 0;
1671     set<pair<const CWalletTx*,unsigned int> > setCoins;
1672
1673     // Simple coins selection - no randomization
1674     if (!SelectCoinsSimple(nAmount, nMinValue, nOutputValue, GetTime(), 1, setCoins, nValueIn))
1675         return false;
1676
1677     if (setCoins.empty())
1678         return false;
1679
1680     CWalletTx wtxNew;
1681     vector<const CWalletTx*> vwtxPrev;
1682
1683     // Reserve a new key pair from key pool
1684     CReserveKey reservekey(this);
1685     CPubKey vchPubKey = reservekey.GetReservedKey();
1686
1687     // Output script
1688     CScript scriptOutput;
1689     scriptOutput.SetDestination(vchPubKey.GetID());
1690
1691     // Insert output
1692     wtxNew.vout.push_back(CTxOut(0, scriptOutput));
1693
1694     double dWeight = 0;
1695     BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1696     {
1697         int64 nCredit = pcoin.first->vout[pcoin.second].nValue;
1698
1699         // Add current coin to inputs list and add its credit to transaction output
1700         wtxNew.vin.push_back(CTxIn(pcoin.first->GetHash(), pcoin.second));
1701         wtxNew.vout[0].nValue += nCredit;
1702         vwtxPrev.push_back(pcoin.first);
1703
1704 /*
1705         // Replaced with estimation for performance purposes
1706
1707         for (unsigned int i = 0; i < wtxNew.vin.size(); i++) {
1708             const CWalletTx *txin = vwtxPrev[i];
1709
1710             // Sign scripts to get actual transaction size for fee calculation
1711             if (!SignSignature(*this, *txin, wtxNew, i))
1712                 return false;
1713         }
1714 */
1715
1716         // Assuming that average scriptsig size is 110 bytes
1717         int64 nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION) + wtxNew.vin.size() * 110;
1718         dWeight += (double)nCredit * pcoin.first->GetDepthInMainChain();
1719
1720         double dFinalPriority = dWeight /= nBytes;
1721         bool fAllowFree = CTransaction::AllowFree(dFinalPriority);
1722
1723         // Get actual transaction fee according to its estimated size and priority
1724         int64 nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND, nBytes);
1725
1726         // Prepare transaction for commit if sum is enough ot its size is too big
1727         if (nBytes >= MAX_BLOCK_SIZE_GEN/6 || wtxNew.vout[0].nValue >= nOutputValue)
1728         {
1729             wtxNew.vout[0].nValue -= nMinFee; // Set actual fee
1730
1731             for (unsigned int i = 0; i < wtxNew.vin.size(); i++) {
1732                 const CWalletTx *txin = vwtxPrev[i];
1733
1734                 // Sign all scripts
1735                 if (!SignSignature(*this, *txin, wtxNew, i))
1736                     return false;
1737             }
1738
1739             // Try to commit, return false on failure
1740             if (!CommitTransaction(wtxNew, reservekey))
1741                 return false;
1742
1743             listMerged.push_back(wtxNew.GetHash()); // Add to hashes list
1744
1745             dWeight = 0;  // Reset all temporary values
1746             vwtxPrev.clear();
1747             wtxNew.SetNull();
1748             wtxNew.vout.push_back(CTxOut(0, scriptOutput));
1749         }
1750     }
1751
1752     // Create transactions if there are some unhandled coins left
1753     if (wtxNew.vout[0].nValue > 0) {
1754         int64 nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION) + wtxNew.vin.size() * 110;
1755
1756         double dFinalPriority = dWeight /= nBytes;
1757         bool fAllowFree = CTransaction::AllowFree(dFinalPriority);
1758
1759         // Get actual transaction fee according to its size and priority
1760         int64 nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND, nBytes);
1761
1762         wtxNew.vout[0].nValue -= nMinFee; // Set actual fee
1763
1764         if (wtxNew.vout[0].nValue <= 0)
1765             return false;
1766
1767         for (unsigned int i = 0; i < wtxNew.vin.size(); i++) {
1768             const CWalletTx *txin = vwtxPrev[i];
1769
1770             // Sign all scripts again
1771             if (!SignSignature(*this, *txin, wtxNew, i))
1772                 return false;
1773         }
1774
1775         // Try to commit, return false on failure
1776         if (!CommitTransaction(wtxNew, reservekey))
1777             return false;
1778
1779         listMerged.push_back(wtxNew.GetHash()); // Add to hashes list
1780     }
1781
1782     return true;
1783 }
1784
1785
1786 bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64 nSearchInterval, CTransaction& txNew, CKey& key)
1787 {
1788     // The following combine threshold is important to security
1789     // Should not be adjusted if you don't understand the consequences
1790     int64 nCombineThreshold = GetProofOfWorkReward(GetLastBlockIndex(pindexBest, false)->nBits) / 3;
1791
1792     CBigNum bnTargetPerCoinDay;
1793     bnTargetPerCoinDay.SetCompact(nBits);
1794
1795     txNew.vin.clear();
1796     txNew.vout.clear();
1797
1798     // Mark coin stake transaction
1799     CScript scriptEmpty;
1800     scriptEmpty.clear();
1801     txNew.vout.push_back(CTxOut(0, scriptEmpty));
1802
1803     // Choose coins to use
1804     int64 nBalance = GetBalance();
1805     int64 nReserveBalance = 0;
1806
1807     if (mapArgs.count("-reservebalance") && !ParseMoney(mapArgs["-reservebalance"], nReserveBalance))
1808         return error("CreateCoinStake : invalid reserve balance amount");
1809
1810     if (nBalance <= nReserveBalance)
1811         return false;
1812
1813     vector<const CWalletTx*> vwtxPrev;
1814
1815     CTxDB txdb("r");
1816     {
1817         LOCK2(cs_main, cs_wallet);
1818         // Cache outputs unless best block or wallet transaction set changed
1819         if (!fCoinsDataActual)
1820         {
1821             mapMeta.clear();
1822             int64 nValueIn = 0;
1823             CoinsSet setCoins;
1824             if (!SelectCoinsSimple(nBalance - nReserveBalance, MIN_TX_FEE, MAX_MONEY, txNew.nTime, nCoinbaseMaturity * 10, setCoins, nValueIn))
1825                 return false;
1826
1827             if (setCoins.empty())
1828                 return false;
1829
1830             {
1831                 CTxIndex txindex;
1832                 CBlock block;
1833                 for(CoinsSet::iterator pcoin = setCoins.begin(); pcoin != setCoins.end(); pcoin++)
1834                 {
1835                     // Load transaction index item
1836                     if (!txdb.ReadTxIndex(pcoin->first->GetHash(), txindex))
1837                         continue;
1838
1839                     // Read block header
1840                     if (!block.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, false))
1841                         continue;
1842
1843                     uint64 nStakeModifier = 0;
1844                     if (!GetKernelStakeModifier(block.GetHash(), nStakeModifier))
1845                         continue;
1846
1847                     // Add meta record
1848                     // txid => ((txindex, (tx, vout.n)), (block, modifier))
1849                     mapMeta[make_pair(pcoin->first->GetHash(), pcoin->second)] = make_pair(make_pair(txindex, *pcoin), make_pair(block, nStakeModifier));
1850
1851                     if (fDebug)
1852                         printf("Load coin: %s\n", pcoin->first->GetHash().GetHex().c_str());
1853                 }
1854             }
1855
1856             if (fDebug)
1857                 printf("Stake miner: %"PRIszu" meta items loaded for %"PRIszu" coins\n", mapMeta.size(), setCoins.size());
1858
1859             fCoinsDataActual = true;
1860             nKernelsTried = 0;
1861             nCoinDaysTried = 0;
1862         }
1863     }
1864
1865     int64 nCredit = 0;
1866     CScript scriptPubKeyKernel;
1867
1868     KernelSearchSettings settings;
1869     settings.nBits = nBits;
1870     settings.nTime = txNew.nTime;
1871     settings.nOffset = 0;
1872     settings.nLimit = mapMeta.size();
1873     settings.nSearchInterval = nSearchInterval;
1874
1875     unsigned int nTimeTx, nBlockTime;
1876     COutPoint prevoutStake;
1877     CoinsSet::value_type kernelcoin;
1878
1879     if (ScanForStakeKernelHash(mapMeta, settings, kernelcoin, nTimeTx, nBlockTime, nKernelsTried, nCoinDaysTried))
1880     {
1881         // Found a kernel
1882         if (fDebug && GetBoolArg("-printcoinstake"))
1883             printf("CreateCoinStake : kernel found\n");
1884         vector<valtype> vSolutions;
1885         txnouttype whichType;
1886         CScript scriptPubKeyOut;
1887         scriptPubKeyKernel = kernelcoin.first->vout[kernelcoin.second].scriptPubKey;
1888         if (!Solver(scriptPubKeyKernel, whichType, vSolutions))
1889         {
1890             if (fDebug && GetBoolArg("-printcoinstake"))
1891                 printf("CreateCoinStake : failed to parse kernel\n");
1892             return false;
1893         }
1894         if (fDebug && GetBoolArg("-printcoinstake"))
1895             printf("CreateCoinStake : parsed kernel type=%d\n", whichType);
1896         if (whichType != TX_PUBKEY && whichType != TX_PUBKEYHASH)
1897         {
1898             if (fDebug && GetBoolArg("-printcoinstake"))
1899                 printf("CreateCoinStake : no support for kernel type=%d\n", whichType);
1900             return false;  // only support pay to public key and pay to address
1901         }
1902         if (whichType == TX_PUBKEYHASH) // pay to address type
1903         {
1904             // convert to pay to public key type
1905             if (!keystore.GetKey(uint160(vSolutions[0]), key))
1906             {
1907                 if (fDebug && GetBoolArg("-printcoinstake"))
1908                     printf("CreateCoinStake : failed to get key for kernel type=%d\n", whichType);
1909                 return false;  // unable to find corresponding public key
1910             }
1911             scriptPubKeyOut << key.GetPubKey() << OP_CHECKSIG;
1912         }
1913         if (whichType == TX_PUBKEY)
1914         {
1915             valtype& vchPubKey = vSolutions[0];
1916             if (!keystore.GetKey(Hash160(vchPubKey), key))
1917             {
1918                 if (fDebug && GetBoolArg("-printcoinstake"))
1919                     printf("CreateCoinStake : failed to get key for kernel type=%d\n", whichType);
1920                 return false;  // unable to find corresponding public key
1921             }
1922             if (key.GetPubKey() != vchPubKey)
1923             {
1924                 if (fDebug && GetBoolArg("-printcoinstake"))
1925                     printf("CreateCoinStake : invalid key for kernel type=%d\n", whichType);
1926                 return false; // keys mismatch
1927             }
1928
1929             scriptPubKeyOut = scriptPubKeyKernel;
1930         }
1931
1932         txNew.nTime = nTimeTx;
1933         txNew.vin.push_back(CTxIn(kernelcoin.first->GetHash(), kernelcoin.second));
1934         nCredit += kernelcoin.first->vout[kernelcoin.second].nValue;
1935         vwtxPrev.push_back(kernelcoin.first);
1936         txNew.vout.push_back(CTxOut(0, scriptPubKeyOut));
1937
1938         if (GetWeight((int64)nBlockTime, (int64)txNew.nTime) < nStakeMaxAge)
1939             txNew.vout.push_back(CTxOut(0, scriptPubKeyOut)); //split stake
1940         if (fDebug && GetBoolArg("-printcoinstake"))
1941             printf("CreateCoinStake : added kernel type=%d\n", whichType);
1942     }
1943
1944     if (nCredit == 0 || nCredit > nBalance - nReserveBalance)
1945         return false;
1946
1947     // (txid, vout.n) => ((txindex, (tx, vout.n)), (block, modifier))
1948     for(MetaMap::const_iterator meta_item = mapMeta.begin(); meta_item != mapMeta.end(); meta_item++)
1949     {
1950         // Get coin
1951         CoinsSet::value_type pcoin = meta_item->second.first.second;
1952
1953         // Attempt to add more inputs
1954         // Only add coins of the same key/address as kernel
1955         if (txNew.vout.size() == 2 && ((pcoin.first->vout[pcoin.second].scriptPubKey == scriptPubKeyKernel || pcoin.first->vout[pcoin.second].scriptPubKey == txNew.vout[1].scriptPubKey))
1956             && pcoin.first->GetHash() != txNew.vin[0].prevout.hash)
1957         {
1958             int64 nTimeWeight = GetWeight((int64)pcoin.first->nTime, (int64)txNew.nTime);
1959
1960             // Stop adding more inputs if already too many inputs
1961             if (txNew.vin.size() >= 100)
1962                 break;
1963             // Stop adding more inputs if value is already pretty significant
1964             if (nCredit > nCombineThreshold)
1965                 break;
1966             // Stop adding inputs if reached reserve limit
1967             if (nCredit + pcoin.first->vout[pcoin.second].nValue > nBalance - nReserveBalance)
1968                 break;
1969             // Do not add additional significant input
1970             if (pcoin.first->vout[pcoin.second].nValue > nCombineThreshold)
1971                 continue;
1972             // Do not add input that is still too young
1973             if (nTimeWeight < nStakeMaxAge)
1974                 continue;
1975
1976             txNew.vin.push_back(CTxIn(pcoin.first->GetHash(), pcoin.second));
1977             nCredit += pcoin.first->vout[pcoin.second].nValue;
1978             vwtxPrev.push_back(pcoin.first);
1979         }
1980     }
1981
1982     // Calculate coin age reward
1983     {
1984         uint64 nCoinAge;
1985         CTxDB txdb("r");
1986         if (!txNew.GetCoinAge(txdb, nCoinAge))
1987             return error("CreateCoinStake : failed to calculate coin age");
1988         
1989         int64 nReward = GetProofOfStakeReward(nCoinAge, nBits, txNew.nTime);
1990         // Refuse to create mint that has zero or negative reward
1991         if(nReward <= 0)
1992             return false;
1993     
1994         nCredit += nReward;
1995     }
1996
1997     int64 nMinFee = 0;
1998     while (true)
1999     {
2000         // Set output amount
2001         if (txNew.vout.size() == 3)
2002         {
2003             txNew.vout[1].nValue = ((nCredit - nMinFee) / 2 / CENT) * CENT;
2004             txNew.vout[2].nValue = nCredit - nMinFee - txNew.vout[1].nValue;
2005         }
2006         else
2007             txNew.vout[1].nValue = nCredit - nMinFee;
2008
2009         // Sign
2010         int nIn = 0;
2011         BOOST_FOREACH(const CWalletTx* pcoin, vwtxPrev)
2012         {
2013             if (!SignSignature(*this, *pcoin, txNew, nIn++))
2014                 return error("CreateCoinStake : failed to sign coinstake");
2015         }
2016
2017         // Limit size
2018         unsigned int nBytes = ::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION);
2019         if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
2020             return error("CreateCoinStake : exceeded coinstake size limit");
2021
2022         // Check enough fee is paid
2023         if (nMinFee < txNew.GetMinFee(1, false, GMF_BLOCK, nBytes) - CENT)
2024         {
2025             nMinFee = txNew.GetMinFee(1, false, GMF_BLOCK, nBytes) - CENT;
2026             continue; // try signing again
2027         }
2028         else
2029         {
2030             if (fDebug && GetBoolArg("-printfee"))
2031                 printf("CreateCoinStake : fee for coinstake %s\n", FormatMoney(nMinFee).c_str());
2032             break;
2033         }
2034     }
2035
2036     // Successfully generated coinstake
2037     return true;
2038 }
2039
2040
2041 // Call after CreateTransaction unless you want to abort
2042 bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
2043 {
2044     {
2045         LOCK2(cs_main, cs_wallet);
2046         printf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
2047         {
2048             // This is only to keep the database open to defeat the auto-flush for the
2049             // duration of this scope.  This is the only place where this optimization
2050             // maybe makes sense; please don't do it anywhere else.
2051             CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
2052
2053             // Take key pair from key pool so it won't be used again
2054             reservekey.KeepKey();
2055
2056             // Add tx to wallet, because if it has change it's also ours,
2057             // otherwise just for transaction history.
2058             AddToWallet(wtxNew);
2059
2060             // Mark old coins as spent
2061             set<CWalletTx*> setCoins;
2062             BOOST_FOREACH(const CTxIn& txin, wtxNew.vin)
2063             {
2064                 CWalletTx &coin = mapWallet[txin.prevout.hash];
2065                 coin.BindWallet(this);
2066                 coin.MarkSpent(txin.prevout.n);
2067                 coin.WriteToDisk();
2068                 NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
2069                 vMintingWalletUpdated.push_back(coin.GetHash());
2070             }
2071
2072             if (fFileBacked)
2073                 delete pwalletdb;
2074         }
2075
2076         // Track how many getdata requests our transaction gets
2077         mapRequestCount[wtxNew.GetHash()] = 0;
2078
2079         // Broadcast
2080         if (!wtxNew.AcceptToMemoryPool())
2081         {
2082             // This must not fail. The transaction has already been signed and recorded.
2083             printf("CommitTransaction() : Error: Transaction not valid");
2084             return false;
2085         }
2086         wtxNew.RelayWalletTransaction();
2087     }
2088     return true;
2089 }
2090
2091
2092
2093
2094 string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
2095 {
2096     CReserveKey reservekey(this);
2097     int64 nFeeRequired;
2098
2099     if (IsLocked())
2100     {
2101         string strError = _("Error: Wallet locked, unable to create transaction  ");
2102         printf("SendMoney() : %s", strError.c_str());
2103         return strError;
2104     }
2105     if (fWalletUnlockMintOnly)
2106     {
2107         string strError = _("Error: Wallet unlocked for block minting only, unable to create transaction.");
2108         printf("SendMoney() : %s", strError.c_str());
2109         return strError;
2110     }
2111     if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired))
2112     {
2113         string strError;
2114         if (nValue + nFeeRequired > GetBalance())
2115             strError = strprintf(_("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds  "), FormatMoney(nFeeRequired).c_str());
2116         else
2117             strError = _("Error: Transaction creation failed  ");
2118         printf("SendMoney() : %s", strError.c_str());
2119         return strError;
2120     }
2121
2122     if (fAskFee && !uiInterface.ThreadSafeAskFee(nFeeRequired, _("Sending...")))
2123         return "ABORTED";
2124
2125     if (!CommitTransaction(wtxNew, reservekey))
2126         return _("Error: The transaction was rejected.  This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
2127
2128     return "";
2129 }
2130
2131
2132
2133 string CWallet::SendMoneyToDestination(const CTxDestination& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
2134 {
2135     // Check amount
2136     if (nValue <= 0)
2137         return _("Invalid amount");
2138     if (nValue + nTransactionFee > GetBalance())
2139         return _("Insufficient funds");
2140
2141     // Parse Bitcoin address
2142     CScript scriptPubKey;
2143     scriptPubKey.SetDestination(address);
2144
2145     return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
2146 }
2147
2148
2149
2150
2151 DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
2152 {
2153     if (!fFileBacked)
2154         return DB_LOAD_OK;
2155     fFirstRunRet = false;
2156     DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this);
2157     if (nLoadWalletRet == DB_NEED_REWRITE)
2158     {
2159         if (CDB::Rewrite(strWalletFile, "\x04pool"))
2160         {
2161             setKeyPool.clear();
2162             // Note: can't top-up keypool here, because wallet is locked.
2163             // User will be prompted to unlock wallet the next operation
2164             // the requires a new key.
2165         }
2166     }
2167
2168     if (nLoadWalletRet != DB_LOAD_OK)
2169         return nLoadWalletRet;
2170     fFirstRunRet = !vchDefaultKey.IsValid();
2171
2172     NewThread(ThreadFlushWalletDB, &strWalletFile);
2173     return DB_LOAD_OK;
2174 }
2175
2176
2177 bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName)
2178 {
2179     std::map<CTxDestination, std::string>::iterator mi = mapAddressBook.find(address);
2180     mapAddressBook[address] = strName;
2181     NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED);
2182     if (!fFileBacked)
2183         return false;
2184     return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName);
2185 }
2186
2187 bool CWallet::DelAddressBookName(const CTxDestination& address)
2188 {
2189     mapAddressBook.erase(address);
2190     NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), CT_DELETED);
2191     if (!fFileBacked)
2192         return false;
2193     return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString());
2194 }
2195
2196
2197 void CWallet::PrintWallet(const CBlock& block)
2198 {
2199     {
2200         LOCK(cs_wallet);
2201         if (block.IsProofOfWork() && mapWallet.count(block.vtx[0].GetHash()))
2202         {
2203             CWalletTx& wtx = mapWallet[block.vtx[0].GetHash()];
2204             printf("    mine:  %d  %d  %"PRI64d"", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
2205         }
2206         if (block.IsProofOfStake() && mapWallet.count(block.vtx[1].GetHash()))
2207         {
2208             CWalletTx& wtx = mapWallet[block.vtx[1].GetHash()];
2209             printf("    stake: %d  %d  %"PRI64d"", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
2210          }
2211
2212     }
2213     printf("\n");
2214 }
2215
2216 bool CWallet::GetTransaction(const uint256 &hashTx, CWalletTx& wtx)
2217 {
2218     {
2219         LOCK(cs_wallet);
2220         map<uint256, CWalletTx>::iterator mi = mapWallet.find(hashTx);
2221         if (mi != mapWallet.end())
2222         {
2223             wtx = (*mi).second;
2224             return true;
2225         }
2226     }
2227     return false;
2228 }
2229
2230 bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
2231 {
2232     if (fFileBacked)
2233     {
2234         if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey))
2235             return false;
2236     }
2237     vchDefaultKey = vchPubKey;
2238     return true;
2239 }
2240
2241 bool GetWalletFile(CWallet* pwallet, string &strWalletFileOut)
2242 {
2243     if (!pwallet->fFileBacked)
2244         return false;
2245     strWalletFileOut = pwallet->strWalletFile;
2246     return true;
2247 }
2248
2249 //
2250 // Mark old keypool keys as used,
2251 // and generate all new keys
2252 //
2253 bool CWallet::NewKeyPool()
2254 {
2255     {
2256         LOCK(cs_wallet);
2257         CWalletDB walletdb(strWalletFile);
2258         BOOST_FOREACH(int64 nIndex, setKeyPool)
2259             walletdb.ErasePool(nIndex);
2260         setKeyPool.clear();
2261
2262         if (IsLocked())
2263             return false;
2264
2265         int64 nKeys = max(GetArg("-keypool", 100), (int64)0);
2266         for (int i = 0; i < nKeys; i++)
2267         {
2268             int64 nIndex = i+1;
2269             walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey()));
2270             setKeyPool.insert(nIndex);
2271         }
2272         printf("CWallet::NewKeyPool wrote %"PRI64d" new keys\n", nKeys);
2273     }
2274     return true;
2275 }
2276
2277 bool CWallet::TopUpKeyPool(unsigned int nSize)
2278 {
2279     {
2280         LOCK(cs_wallet);
2281
2282         if (IsLocked())
2283             return false;
2284
2285         CWalletDB walletdb(strWalletFile);
2286
2287         // Top up key pool
2288         unsigned int nTargetSize;
2289         if (nSize > 0)
2290             nTargetSize = nSize;
2291         else
2292             nTargetSize = max(GetArg("-keypool", 100), 0LL);
2293
2294         while (setKeyPool.size() < (nTargetSize + 1))
2295         {
2296             int64 nEnd = 1;
2297             if (!setKeyPool.empty())
2298                 nEnd = *(--setKeyPool.end()) + 1;
2299             if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
2300                 throw runtime_error("TopUpKeyPool() : writing generated key failed");
2301             setKeyPool.insert(nEnd);
2302             printf("keypool added key %"PRI64d", size=%"PRIszu"\n", nEnd, setKeyPool.size());
2303         }
2304     }
2305     return true;
2306 }
2307
2308 void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
2309 {
2310     nIndex = -1;
2311     keypool.vchPubKey = CPubKey();
2312     {
2313         LOCK(cs_wallet);
2314
2315         if (!IsLocked())
2316             TopUpKeyPool();
2317
2318         // Get the oldest key
2319         if(setKeyPool.empty())
2320             return;
2321
2322         CWalletDB walletdb(strWalletFile);
2323
2324         nIndex = *(setKeyPool.begin());
2325         setKeyPool.erase(setKeyPool.begin());
2326         if (!walletdb.ReadPool(nIndex, keypool))
2327             throw runtime_error("ReserveKeyFromKeyPool() : read failed");
2328         if (!HaveKey(keypool.vchPubKey.GetID()))
2329             throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
2330         assert(keypool.vchPubKey.IsValid());
2331         if (fDebug && GetBoolArg("-printkeypool"))
2332             printf("keypool reserve %"PRI64d"\n", nIndex);
2333     }
2334 }
2335
2336 int64 CWallet::AddReserveKey(const CKeyPool& keypool)
2337 {
2338     {
2339         LOCK2(cs_main, cs_wallet);
2340         CWalletDB walletdb(strWalletFile);
2341
2342         int64 nIndex = 1 + *(--setKeyPool.end());
2343         if (!walletdb.WritePool(nIndex, keypool))
2344             throw runtime_error("AddReserveKey() : writing added key failed");
2345         setKeyPool.insert(nIndex);
2346         return nIndex;
2347     }
2348     return -1;
2349 }
2350
2351 void CWallet::KeepKey(int64 nIndex)
2352 {
2353     // Remove from key pool
2354     if (fFileBacked)
2355     {
2356         CWalletDB walletdb(strWalletFile);
2357         walletdb.ErasePool(nIndex);
2358     }
2359     if(fDebug)
2360         printf("keypool keep %"PRI64d"\n", nIndex);
2361 }
2362
2363 void CWallet::ReturnKey(int64 nIndex)
2364 {
2365     // Return to key pool
2366     {
2367         LOCK(cs_wallet);
2368         setKeyPool.insert(nIndex);
2369     }
2370     if(fDebug)
2371         printf("keypool return %"PRI64d"\n", nIndex);
2372 }
2373
2374 bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse)
2375 {
2376     int64 nIndex = 0;
2377     CKeyPool keypool;
2378     {
2379         LOCK(cs_wallet);
2380         ReserveKeyFromKeyPool(nIndex, keypool);
2381         if (nIndex == -1)
2382         {
2383             if (fAllowReuse && vchDefaultKey.IsValid())
2384             {
2385                 result = vchDefaultKey;
2386                 return true;
2387             }
2388             if (IsLocked()) return false;
2389             result = GenerateNewKey();
2390             return true;
2391         }
2392         KeepKey(nIndex);
2393         result = keypool.vchPubKey;
2394     }
2395     return true;
2396 }
2397
2398 int64 CWallet::GetOldestKeyPoolTime()
2399 {
2400     int64 nIndex = 0;
2401     CKeyPool keypool;
2402     ReserveKeyFromKeyPool(nIndex, keypool);
2403     if (nIndex == -1)
2404         return GetTime();
2405     ReturnKey(nIndex);
2406     return keypool.nTime;
2407 }
2408
2409 std::map<CTxDestination, int64> CWallet::GetAddressBalances()
2410 {
2411     map<CTxDestination, int64> balances;
2412
2413     {
2414         LOCK(cs_wallet);
2415         BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
2416         {
2417             CWalletTx *pcoin = &walletEntry.second;
2418
2419             if (!pcoin->IsFinal() || !pcoin->IsTrusted())
2420                 continue;
2421
2422             if ((pcoin->IsCoinBase() || pcoin->IsCoinStake()) && pcoin->GetBlocksToMaturity() > 0)
2423                 continue;
2424
2425             int nDepth = pcoin->GetDepthInMainChain();
2426             if (nDepth < (pcoin->IsFromMe(MINE_ALL) ? 0 : 1))
2427                 continue;
2428
2429             for (unsigned int i = 0; i < pcoin->vout.size(); i++)
2430             {
2431                 CTxDestination addr;
2432                 if (!IsMine(pcoin->vout[i]))
2433                     continue;
2434                 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, addr))
2435                     continue;
2436
2437                 int64 n = pcoin->IsSpent(i) ? 0 : pcoin->vout[i].nValue;
2438
2439                 if (!balances.count(addr))
2440                     balances[addr] = 0;
2441                 balances[addr] += n;
2442             }
2443         }
2444     }
2445
2446     return balances;
2447 }
2448
2449 set< set<CTxDestination> > CWallet::GetAddressGroupings()
2450 {
2451     set< set<CTxDestination> > groupings;
2452     set<CTxDestination> grouping;
2453
2454     BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
2455     {
2456         CWalletTx *pcoin = &walletEntry.second;
2457
2458         if (pcoin->vin.size() > 0 && IsMine(pcoin->vin[0]))
2459         {
2460             // group all input addresses with each other
2461             BOOST_FOREACH(CTxIn txin, pcoin->vin)
2462             {
2463                 CTxDestination address;
2464                 if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address))
2465                     continue;
2466                 grouping.insert(address);
2467             }
2468
2469             // group change with input addresses
2470             BOOST_FOREACH(CTxOut txout, pcoin->vout)
2471                 if (IsChange(txout))
2472                 {
2473                     CWalletTx tx = mapWallet[pcoin->vin[0].prevout.hash];
2474                     CTxDestination txoutAddr;
2475                     if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
2476                         continue;
2477                     grouping.insert(txoutAddr);
2478                 }
2479             groupings.insert(grouping);
2480             grouping.clear();
2481         }
2482
2483         // group lone addrs by themselves
2484         for (unsigned int i = 0; i < pcoin->vout.size(); i++)
2485             if (IsMine(pcoin->vout[i]))
2486             {
2487                 CTxDestination address;
2488                 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address))
2489                     continue;
2490                 grouping.insert(address);
2491                 groupings.insert(grouping);
2492                 grouping.clear();
2493             }
2494     }
2495
2496     set< set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
2497     map< CTxDestination, set<CTxDestination>* > setmap;  // map addresses to the unique group containing it
2498     BOOST_FOREACH(set<CTxDestination> grouping, groupings)
2499     {
2500         // make a set of all the groups hit by this new group
2501         set< set<CTxDestination>* > hits;
2502         map< CTxDestination, set<CTxDestination>* >::iterator it;
2503         BOOST_FOREACH(CTxDestination address, grouping)
2504             if ((it = setmap.find(address)) != setmap.end())
2505                 hits.insert((*it).second);
2506
2507         // merge all hit groups into a new single group and delete old groups
2508         set<CTxDestination>* merged = new set<CTxDestination>(grouping);
2509         BOOST_FOREACH(set<CTxDestination>* hit, hits)
2510         {
2511             merged->insert(hit->begin(), hit->end());
2512             uniqueGroupings.erase(hit);
2513             delete hit;
2514         }
2515         uniqueGroupings.insert(merged);
2516
2517         // update setmap
2518         BOOST_FOREACH(CTxDestination element, *merged)
2519             setmap[element] = merged;
2520     }
2521
2522     set< set<CTxDestination> > ret;
2523     BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings)
2524     {
2525         ret.insert(*uniqueGrouping);
2526         delete uniqueGrouping;
2527     }
2528
2529     return ret;
2530 }
2531
2532 // ppcoin: check 'spent' consistency between wallet and txindex
2533 // ppcoin: fix wallet spent state according to txindex
2534 void CWallet::FixSpentCoins(int& nMismatchFound, int64& nBalanceInQuestion, bool fCheckOnly)
2535 {
2536     nMismatchFound = 0;
2537     nBalanceInQuestion = 0;
2538
2539     LOCK(cs_wallet);
2540     vector<CWalletTx*> vCoins;
2541     vCoins.reserve(mapWallet.size());
2542     for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
2543         vCoins.push_back(&(*it).second);
2544
2545     CTxDB txdb("r");
2546     BOOST_FOREACH(CWalletTx* pcoin, vCoins)
2547     {
2548         // Find the corresponding transaction index
2549         CTxIndex txindex;
2550         if (!txdb.ReadTxIndex(pcoin->GetHash(), txindex))
2551             continue;
2552         for (unsigned int n=0; n < pcoin->vout.size(); n++)
2553         {
2554             if (IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && (txindex.vSpent.size() <= n || txindex.vSpent[n].IsNull()))
2555             {
2556                 printf("FixSpentCoins found lost coin %sppc %s[%d], %s\n",
2557                     FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
2558                 nMismatchFound++;
2559                 nBalanceInQuestion += pcoin->vout[n].nValue;
2560                 if (!fCheckOnly)
2561                 {
2562                     pcoin->MarkUnspent(n);
2563                     pcoin->WriteToDisk();
2564                 }
2565             }
2566             else if (IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && (txindex.vSpent.size() > n && !txindex.vSpent[n].IsNull()))
2567             {
2568                 printf("FixSpentCoins found spent coin %sppc %s[%d], %s\n",
2569                     FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
2570                 nMismatchFound++;
2571                 nBalanceInQuestion += pcoin->vout[n].nValue;
2572                 if (!fCheckOnly)
2573                 {
2574                     pcoin->MarkSpent(n);
2575                     pcoin->WriteToDisk();
2576                 }
2577             }
2578
2579         }
2580
2581         if(IsMine((CTransaction)*pcoin) && (pcoin->IsCoinBase() || pcoin->IsCoinStake()) && pcoin->GetDepthInMainChain() == 0)
2582         {
2583             printf("FixSpentCoins %s tx %s\n", fCheckOnly ? "found" : "removed", pcoin->GetHash().ToString().c_str());
2584             if (!fCheckOnly)
2585             {
2586                 EraseFromWallet(pcoin->GetHash());
2587             }
2588         }
2589     }
2590 }
2591
2592 // ppcoin: disable transaction (only for coinstake)
2593 void CWallet::DisableTransaction(const CTransaction &tx)
2594 {
2595     if (!tx.IsCoinStake() || !IsFromMe(tx))
2596         return; // only disconnecting coinstake requires marking input unspent
2597
2598     LOCK(cs_wallet);
2599     BOOST_FOREACH(const CTxIn& txin, tx.vin)
2600     {
2601         map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
2602         if (mi != mapWallet.end())
2603         {
2604             CWalletTx& prev = (*mi).second;
2605             if (txin.prevout.n < prev.vout.size() && IsMine(prev.vout[txin.prevout.n]))
2606             {
2607                 prev.MarkUnspent(txin.prevout.n);
2608                 prev.WriteToDisk();
2609             }
2610         }
2611     }
2612 }
2613
2614 CPubKey CReserveKey::GetReservedKey()
2615 {
2616     if (nIndex == -1)
2617     {
2618         CKeyPool keypool;
2619         pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
2620         if (nIndex != -1)
2621             vchPubKey = keypool.vchPubKey;
2622         else
2623         {
2624             printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
2625             vchPubKey = pwallet->vchDefaultKey;
2626         }
2627     }
2628     assert(vchPubKey.IsValid());
2629     return vchPubKey;
2630 }
2631
2632 void CReserveKey::KeepKey()
2633 {
2634     if (nIndex != -1)
2635         pwallet->KeepKey(nIndex);
2636     nIndex = -1;
2637     vchPubKey = CPubKey();
2638 }
2639
2640 void CReserveKey::ReturnKey()
2641 {
2642     if (nIndex != -1)
2643         pwallet->ReturnKey(nIndex);
2644     nIndex = -1;
2645     vchPubKey = CPubKey();
2646 }
2647
2648 void CWallet::GetAllReserveKeys(set<CKeyID>& setAddress) const
2649 {
2650     setAddress.clear();
2651
2652     CWalletDB walletdb(strWalletFile);
2653
2654     LOCK2(cs_main, cs_wallet);
2655     BOOST_FOREACH(const int64& id, setKeyPool)
2656     {
2657         CKeyPool keypool;
2658         if (!walletdb.ReadPool(id, keypool))
2659             throw runtime_error("GetAllReserveKeyHashes() : read failed");
2660         assert(keypool.vchPubKey.IsValid());
2661         CKeyID keyID = keypool.vchPubKey.GetID();
2662         if (!HaveKey(keyID))
2663             throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool");
2664         setAddress.insert(keyID);
2665     }
2666 }
2667
2668 void CWallet::UpdatedTransaction(const uint256 &hashTx)
2669 {
2670     {
2671         LOCK(cs_wallet);
2672         // Only notify UI if this transaction is in this wallet
2673         map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(hashTx);
2674         if (mi != mapWallet.end())
2675         {
2676             NotifyTransactionChanged(this, hashTx, CT_UPDATED);
2677             vMintingWalletUpdated.push_back(hashTx);
2678         }
2679     }
2680 }
2681
2682 void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64> &mapKeyBirth) const {
2683     mapKeyBirth.clear();
2684
2685     // get birth times for keys with metadata
2686     for (std::map<CKeyID, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++)
2687         if (it->second.nCreateTime)
2688             mapKeyBirth[it->first] = it->second.nCreateTime;
2689
2690     // map in which we'll infer heights of other keys
2691     CBlockIndex *pindexMax = FindBlockByHeight(std::max(0, nBestHeight - 144)); // the tip can be reorganised; use a 144-block safety margin
2692     std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
2693     std::set<CKeyID> setKeys;
2694     GetKeys(setKeys);
2695     BOOST_FOREACH(const CKeyID &keyid, setKeys) {
2696         if (mapKeyBirth.count(keyid) == 0)
2697             mapKeyFirstBlock[keyid] = pindexMax;
2698     }
2699     setKeys.clear();
2700
2701     // if there are no such keys, we're done
2702     if (mapKeyFirstBlock.empty())
2703         return;
2704
2705     // find first block that affects those keys, if there are any left
2706     std::vector<CKeyID> vAffected;
2707     for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
2708         // iterate over all wallet transactions...
2709         const CWalletTx &wtx = (*it).second;
2710         std::map<uint256, CBlockIndex*>::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
2711         if (blit != mapBlockIndex.end() && blit->second->IsInMainChain()) {
2712             // ... which are already in a block
2713             int nHeight = blit->second->nHeight;
2714             BOOST_FOREACH(const CTxOut &txout, wtx.vout) {
2715                 // iterate over all their outputs
2716                 ::ExtractAffectedKeys(*this, txout.scriptPubKey, vAffected);
2717                 BOOST_FOREACH(const CKeyID &keyid, vAffected) {
2718                     // ... and all their affected keys
2719                     std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find(keyid);
2720                     if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight)
2721                         rit->second = blit->second;
2722                 }
2723                 vAffected.clear();
2724             }
2725         }
2726     }
2727
2728     // Extract block timestamps for those keys
2729     for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
2730         mapKeyBirth[it->first] = it->second->nTime - 7200; // block times can be 2h off
2731 }
2732
2733 void CWallet::ClearOrphans()
2734 {
2735     list<uint256> orphans;
2736
2737     LOCK(cs_wallet);
2738     for(map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
2739     {
2740         const CWalletTx *wtx = &(*it).second;
2741         if((wtx->IsCoinBase() || wtx->IsCoinStake()) && !wtx->IsInMainChain())
2742         {
2743             orphans.push_back(wtx->GetHash());
2744         }
2745     }
2746
2747     for(list<uint256>::const_iterator it = orphans.begin(); it != orphans.end(); ++it)
2748         EraseFromWallet(*it);
2749 }