Get rid of fTrickle parameter.
[novacoin.git] / src / walletdb.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 "walletdb.h"
7 #include "wallet.h"
8 #include "base58.h"
9
10 #include <iostream>
11 #include <fstream>
12
13 #include <boost/version.hpp>
14 #include <boost/filesystem.hpp>
15 #include <boost/algorithm/string.hpp>
16
17 #include <boost/date_time/posix_time/posix_time.hpp>
18 #include <boost/lexical_cast.hpp>
19 #include <boost/variant/get.hpp>
20 #include <boost/algorithm/string.hpp>
21
22 using namespace std;
23 using namespace boost;
24
25
26 static uint64_t nAccountingEntryNumber = 0;
27 extern bool fWalletUnlockMintOnly;
28
29 //
30 // CWalletDB
31 //
32
33 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
34 {
35     nWalletDBUpdated++;
36     return Write(make_pair(string("name"), strAddress), strName);
37 }
38
39 bool CWalletDB::EraseName(const string& strAddress)
40 {
41     // This should only be used for sending addresses, never for receiving addresses,
42     // receiving addresses must always have an address book entry if they're not change return.
43     nWalletDBUpdated++;
44     return Erase(make_pair(string("name"), strAddress));
45 }
46
47 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
48 {
49     account.SetNull();
50     return Read(make_pair(string("acc"), strAccount), account);
51 }
52
53 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
54 {
55     return Write(make_pair(string("acc"), strAccount), account);
56 }
57
58 bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
59 {
60     return Write(boost::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
61 }
62
63 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
64 {
65     return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
66 }
67
68 int64_t CWalletDB::GetAccountCreditDebit(const string& strAccount)
69 {
70     list<CAccountingEntry> entries;
71     ListAccountCreditDebit(strAccount, entries);
72
73     int64_t nCreditDebit = 0;
74     BOOST_FOREACH (const CAccountingEntry& entry, entries)
75         nCreditDebit += entry.nCreditDebit;
76
77     return nCreditDebit;
78 }
79
80 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
81 {
82     bool fAllAccounts = (strAccount == "*");
83
84     Dbc* pcursor = GetCursor();
85     if (!pcursor)
86         throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
87     unsigned int fFlags = DB_SET_RANGE;
88     for ( ; ; )
89     {
90         // Read next record
91         CDataStream ssKey(SER_DISK, CLIENT_VERSION);
92         if (fFlags == DB_SET_RANGE)
93             ssKey << boost::make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64_t(0));
94         CDataStream ssValue(SER_DISK, CLIENT_VERSION);
95         int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
96         fFlags = DB_NEXT;
97         if (ret == DB_NOTFOUND)
98             break;
99         else if (ret != 0)
100         {
101             pcursor->close();
102             throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
103         }
104
105         // Unserialize
106         string strType;
107         ssKey >> strType;
108         if (strType != "acentry")
109             break;
110         CAccountingEntry acentry;
111         ssKey >> acentry.strAccount;
112         if (!fAllAccounts && acentry.strAccount != strAccount)
113             break;
114
115         ssValue >> acentry;
116         ssKey >> acentry.nEntryNo;
117         entries.push_back(acentry);
118     }
119
120     pcursor->close();
121 }
122
123
124 DBErrors
125 CWalletDB::ReorderTransactions(CWallet* pwallet)
126 {
127     LOCK(pwallet->cs_wallet);
128     // Old wallets didn't have any defined order for transactions
129     // Probably a bad idea to change the output of this
130
131     // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
132     typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
133     typedef multimap<int64_t, TxPair > TxItems;
134     TxItems txByTime;
135
136     for (map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
137     {
138         CWalletTx* wtx = &((*it).second);
139         txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
140     }
141     list<CAccountingEntry> acentries;
142     ListAccountCreditDebit("", acentries);
143     BOOST_FOREACH(CAccountingEntry& entry, acentries)
144     {
145         txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
146     }
147
148     int64_t& nOrderPosNext = pwallet->nOrderPosNext;
149     nOrderPosNext = 0;
150     std::vector<int64_t> nOrderPosOffsets;
151     for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
152     {
153         CWalletTx *const pwtx = (*it).second.first;
154         CAccountingEntry *const pacentry = (*it).second.second;
155         int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
156
157         if (nOrderPos == -1)
158         {
159             nOrderPos = nOrderPosNext++;
160             nOrderPosOffsets.push_back(nOrderPos);
161
162             if (pacentry)
163                 // Have to write accounting regardless, since we don't keep it in memory
164                 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
165                     return DB_LOAD_FAIL;
166         }
167         else
168         {
169             int64_t nOrderPosOff = 0;
170             BOOST_FOREACH(const int64_t& nOffsetStart, nOrderPosOffsets)
171             {
172                 if (nOrderPos >= nOffsetStart)
173                     ++nOrderPosOff;
174             }
175             nOrderPos += nOrderPosOff;
176             nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
177
178             if (!nOrderPosOff)
179                 continue;
180
181             // Since we're changing the order, write it back
182             if (pwtx)
183             {
184                 if (!WriteTx(pwtx->GetHash(), *pwtx))
185                     return DB_LOAD_FAIL;
186             }
187             else
188                 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
189                     return DB_LOAD_FAIL;
190         }
191     }
192
193     return DB_LOAD_OK;
194 }
195
196 class CWalletScanState {
197 public:
198     unsigned int nKeys;
199     unsigned int nCKeys;
200     unsigned int nKeyMeta;
201     bool fIsEncrypted;
202     bool fAnyUnordered;
203     int nFileVersion;
204     vector<uint256> vWalletUpgrade;
205
206     CWalletScanState() {
207         nKeys = nCKeys = nKeyMeta = 0;
208         fIsEncrypted = false;
209         fAnyUnordered = false;
210         nFileVersion = 0;
211     }
212 };
213
214 bool
215 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
216              CWalletScanState &wss, string& strType, string& strErr)
217 {
218     try {
219         // Unserialize
220         // Taking advantage of the fact that pair serialization
221         // is just the two items serialized one after the other
222         ssKey >> strType;
223
224         if (strType == "name")
225         {
226             string strAddress;
227             ssKey >> strAddress;
228             ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress)];
229         }
230         else if (strType == "tx")
231         {
232             uint256 hash;
233             ssKey >> hash;
234             CWalletTx& wtx = pwallet->mapWallet[hash];
235             ssValue >> wtx;
236             if (wtx.CheckTransaction() && (wtx.GetHash() == hash))
237                 wtx.BindWallet(pwallet);
238             else
239             {
240                 pwallet->mapWallet.erase(hash);
241                 return false;
242             }
243
244             // Undo serialize changes in 31600
245             if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
246             {
247                 if (!ssValue.empty())
248                 {
249                     char fTmp;
250                     char fUnused;
251                     ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
252                     strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
253                                        wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
254                     wtx.fTimeReceivedIsTxTime = fTmp;
255                 }
256                 else
257                 {
258                     strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
259                     wtx.fTimeReceivedIsTxTime = 0;
260                 }
261                 wss.vWalletUpgrade.push_back(hash);
262             }
263
264             if (wtx.nOrderPos == -1)
265                 wss.fAnyUnordered = true;
266
267             //// debug print
268             //printf("LoadWallet  %s\n", wtx.GetHash().ToString().c_str());
269             //printf(" %12"PRId64"  %s  %s  %s\n",
270             //    wtx.vout[0].nValue,
271             //    DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
272             //    wtx.hashBlock.ToString().substr(0,20).c_str(),
273             //    wtx.mapValue["message"].c_str());
274         }
275         else if (strType == "acentry")
276         {
277             string strAccount;
278             ssKey >> strAccount;
279             uint64_t nNumber;
280             ssKey >> nNumber;
281             if (nNumber > nAccountingEntryNumber)
282                 nAccountingEntryNumber = nNumber;
283
284             if (!wss.fAnyUnordered)
285             {
286                 CAccountingEntry acentry;
287                 ssValue >> acentry;
288                 if (acentry.nOrderPos == -1)
289                     wss.fAnyUnordered = true;
290             }
291         }
292         else if (strType == "watchs")
293         {
294             CScript script;
295             ssKey >> script;
296             char fYes;
297             ssValue >> fYes;
298             if (fYes == '1')
299                 pwallet->LoadWatchOnly(script);
300
301             // Watch-only addresses have no birthday information for now,
302             // so set the wallet birthday to the beginning of time.
303             pwallet->nTimeFirstKey = 1;
304         }
305         else if (strType == "malpair")
306         {
307             string strKeyView;
308
309             CSecret vchSecret;
310             ssKey >> strKeyView;
311             ssValue >> vchSecret;
312
313             CMalleableKeyView keyView(strKeyView);
314             if (!pwallet->LoadKey(keyView, vchSecret))
315             {
316                 strErr = "Error reading wallet database: LoadKey failed";
317                 return false;
318             }
319         }
320         else if (strType == "malcpair")
321         {
322             string strKeyView;
323
324             std::vector<unsigned char> vchCryptedSecret;
325             ssKey >> strKeyView;
326             ssValue >> vchCryptedSecret;
327
328             CMalleableKeyView keyView(strKeyView);
329             if (!pwallet->LoadCryptedKey(keyView, vchCryptedSecret))
330             {
331                 strErr = "Error reading wallet database: LoadCryptedKey failed";
332                 return false;
333             }
334         }
335         else if (strType == "key" || strType == "wkey")
336         {
337             CKey key;
338             CPubKey vchPubKey;
339             ssKey >> vchPubKey;
340             if (strType == "key")
341             {
342                 wss.nKeys++;
343                 CPrivKey pkey;
344                 ssValue >> pkey;
345                 if (!key.SetPrivKey(pkey))
346                 {
347                     strErr = "Error reading wallet database: CPrivKey corrupt";
348                     return false;
349                 }
350                 if (key.GetPubKey() != vchPubKey)
351                 {
352                     strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
353                     return false;
354                 }
355                 if (vchPubKey.size() == 33) {
356                     key.SetCompressedPubKey();
357                 }
358                 if (!key.IsValid())
359                 {
360                     strErr = "Error reading wallet database: invalid CPrivKey";
361                     return false;
362                 }
363             }
364             else
365             {
366                 CWalletKey wkey;
367                 ssValue >> wkey;
368                 if (!key.SetPrivKey(wkey.vchPrivKey))
369                 {
370                     strErr = "Error reading wallet database: CPrivKey corrupt";
371                     return false;
372                 }
373                 if (key.GetPubKey() != vchPubKey)
374                 {
375                     strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
376                     return false;
377                 }
378                 if (vchPubKey.size() == 33) {
379                     key.SetCompressedPubKey();
380                 }
381                 if (!key.IsValid())
382                 {
383                     strErr = "Error reading wallet database: invalid CWalletKey";
384                     return false;
385                 }
386             }
387             if (!pwallet->LoadKey(key))
388             {
389                 strErr = "Error reading wallet database: LoadKey failed";
390                 return false;
391             }
392         }
393         else if (strType == "mkey")
394         {
395             unsigned int nID;
396             ssKey >> nID;
397             CMasterKey kMasterKey;
398             ssValue >> kMasterKey;
399
400             if(pwallet->mapMasterKeys.count(nID) != 0)
401             {
402                 strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
403                 return false;
404             }
405             pwallet->mapMasterKeys[nID] = kMasterKey;
406             if (pwallet->nMasterKeyMaxID < nID)
407                 pwallet->nMasterKeyMaxID = nID;
408         }
409         else if (strType == "ckey")
410         {
411             wss.nCKeys++;
412             CPubKey vchPubKey;
413             ssKey >> vchPubKey;
414             vector<unsigned char> vchPrivKey;
415             ssValue >> vchPrivKey;
416             if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
417             {
418                 strErr = "Error reading wallet database: LoadCryptedKey failed";
419                 return false;
420             }
421             wss.fIsEncrypted = true;
422         }
423         else if (strType == "malmeta")
424         {
425             string strKeyView;
426             ssKey >> strKeyView;
427
428             CMalleableKeyView keyView;
429             keyView.SetString(strKeyView);
430
431             CKeyMetadata keyMeta;
432             ssValue >> keyMeta;
433             wss.nKeyMeta++;
434
435             pwallet->LoadKeyMetadata(keyView, keyMeta);
436         }
437         else if (strType == "keymeta")
438         {
439             CPubKey vchPubKey;
440             ssKey >> vchPubKey;
441             CKeyMetadata keyMeta;
442             ssValue >> keyMeta;
443             wss.nKeyMeta++;
444
445             pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
446
447             // find earliest key creation time, as wallet birthday
448             if (!pwallet->nTimeFirstKey ||
449                 (keyMeta.nCreateTime < pwallet->nTimeFirstKey))
450                 pwallet->nTimeFirstKey = keyMeta.nCreateTime;
451         }
452         else if (strType == "defaultkey")
453         {
454             ssValue >> pwallet->vchDefaultKey;
455         }
456         else if (strType == "pool")
457         {
458             int64_t nIndex;
459             ssKey >> nIndex;
460             CKeyPool keypool;
461             ssValue >> keypool;
462             pwallet->setKeyPool.insert(nIndex);
463
464             // If no metadata exists yet, create a default with the pool key's
465             // creation time. Note that this may be overwritten by actually
466             // stored metadata for that key later, which is fine.
467             CBitcoinAddress addr = CBitcoinAddress(keypool.vchPubKey.GetID());
468             if (pwallet->mapKeyMetadata.count(addr) == 0)
469                 pwallet->mapKeyMetadata[addr] = CKeyMetadata(keypool.nTime);
470
471         }
472         else if (strType == "version")
473         {
474             ssValue >> wss.nFileVersion;
475             if (wss.nFileVersion == 10300)
476                 wss.nFileVersion = 300;
477         }
478         else if (strType == "cscript")
479         {
480             uint160 hash;
481             ssKey >> hash;
482             CScript script;
483             ssValue >> script;
484             if (!pwallet->LoadCScript(script))
485             {
486                 strErr = "Error reading wallet database: LoadCScript failed";
487                 return false;
488             }
489         }
490         else if (strType == "orderposnext")
491         {
492             ssValue >> pwallet->nOrderPosNext;
493         }
494     } catch (...)
495     {
496         return false;
497     }
498     return true;
499 }
500
501 static bool IsKeyType(string strType)
502 {
503     return (strType== "key" || strType == "wkey" ||
504             strType == "mkey" || strType == "ckey" || strType == "malpair" || strType == "malcpair");
505 }
506
507 DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
508 {
509     pwallet->vchDefaultKey = CPubKey();
510     CWalletScanState wss;
511     bool fNoncriticalErrors = false;
512     DBErrors result = DB_LOAD_OK;
513
514     try {
515         LOCK(pwallet->cs_wallet);
516         int nMinVersion = 0;
517         if (Read((string)"minversion", nMinVersion))
518         {
519             if (nMinVersion > CLIENT_VERSION)
520                 return DB_TOO_NEW;
521             pwallet->LoadMinVersion(nMinVersion);
522         }
523
524         // Get cursor
525         Dbc* pcursor = GetCursor();
526         if (!pcursor)
527         {
528             printf("Error getting wallet database cursor\n");
529             return DB_CORRUPT;
530         }
531
532         for ( ; ; )
533         {
534             // Read next record
535             CDataStream ssKey(SER_DISK, CLIENT_VERSION);
536             CDataStream ssValue(SER_DISK, CLIENT_VERSION);
537             int ret = ReadAtCursor(pcursor, ssKey, ssValue);
538             if (ret == DB_NOTFOUND)
539                 break;
540             else if (ret != 0)
541             {
542                 printf("Error reading next record from wallet database\n");
543                 return DB_CORRUPT;
544             }
545
546             // Try to be tolerant of single corrupt records:
547             string strType, strErr;
548             if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
549             {
550                 // losing keys is considered a catastrophic error, anything else
551                 // we assume the user can live with:
552                 if (IsKeyType(strType))
553                     result = DB_CORRUPT;
554                 else
555                 {
556                     // Leave other errors alone, if we try to fix them we might make things worse.
557                     fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
558                     if (strType == "tx")
559                         // Rescan if there is a bad transaction record:
560                         SoftSetBoolArg("-rescan", true);
561                 }
562             }
563             if (!strErr.empty())
564                 printf("%s\n", strErr.c_str());
565         }
566         pcursor->close();
567     }
568     catch (...)
569     {
570         result = DB_CORRUPT;
571     }
572
573     if (fNoncriticalErrors && result == DB_LOAD_OK)
574         result = DB_NONCRITICAL_ERROR;
575
576     // Any wallet corruption at all: skip any rewriting or
577     // upgrading, we don't want to make it worse.
578     if (result != DB_LOAD_OK)
579         return result;
580
581     printf("nFileVersion = %d\n", wss.nFileVersion);
582
583     printf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total\n",
584            wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys);
585
586     // nTimeFirstKey is only reliable if all keys have metadata
587     if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
588         pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
589
590
591     BOOST_FOREACH(uint256 hash, wss.vWalletUpgrade)
592         WriteTx(hash, pwallet->mapWallet[hash]);
593
594     // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
595     if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
596         return DB_NEED_REWRITE;
597
598     if (wss.nFileVersion < CLIENT_VERSION) // Update
599         WriteVersion(CLIENT_VERSION);
600
601     if (wss.fAnyUnordered)
602         result = ReorderTransactions(pwallet);
603
604     return result;
605 }
606
607 DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash)
608 {
609     pwallet->vchDefaultKey = CPubKey();
610     CWalletScanState wss;
611     bool fNoncriticalErrors = false;
612     DBErrors result = DB_LOAD_OK;
613
614     try {
615         LOCK(pwallet->cs_wallet);
616         int nMinVersion = 0;
617         if (Read((string)"minversion", nMinVersion))
618         {
619             if (nMinVersion > CLIENT_VERSION)
620                 return DB_TOO_NEW;
621             pwallet->LoadMinVersion(nMinVersion);
622         }
623
624         // Get cursor
625         Dbc* pcursor = GetCursor();
626         if (!pcursor)
627         {
628             printf("Error getting wallet database cursor\n");
629             return DB_CORRUPT;
630         }
631
632         for ( ; ; )
633         {
634             // Read next record
635             CDataStream ssKey(SER_DISK, CLIENT_VERSION);
636             CDataStream ssValue(SER_DISK, CLIENT_VERSION);
637             int ret = ReadAtCursor(pcursor, ssKey, ssValue);
638             if (ret == DB_NOTFOUND)
639                 break;
640             else if (ret != 0)
641             {
642                 printf("Error reading next record from wallet database\n");
643                 return DB_CORRUPT;
644             }
645
646             string strType;
647             ssKey >> strType;
648             if (strType == "tx") {
649                 uint256 hash;
650                 ssKey >> hash;
651
652                 vTxHash.push_back(hash);
653             }
654         }
655         pcursor->close();
656     }
657     catch (boost::thread_interrupted) {
658         throw;
659     }
660     catch (...) {
661         result = DB_CORRUPT;
662     }
663
664     if (fNoncriticalErrors && result == DB_LOAD_OK)
665         result = DB_NONCRITICAL_ERROR;
666
667     return result;
668 }
669
670 DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet)
671 {
672     // build list of wallet TXs
673     vector<uint256> vTxHash;
674     DBErrors err = FindWalletTx(pwallet, vTxHash);
675     if (err != DB_LOAD_OK)
676         return err;
677
678     // erase each wallet TX
679     BOOST_FOREACH (uint256& hash, vTxHash) {
680         if (!EraseTx(hash))
681             return DB_CORRUPT;
682     }
683
684     return DB_LOAD_OK;
685 }
686
687 void ThreadFlushWalletDB(void* parg)
688 {
689     // Make this thread recognisable as the wallet flushing thread
690     RenameThread("novacoin-wallet");
691
692     const string& strFile = ((const string*)parg)[0];
693     static bool fOneThread;
694     if (fOneThread)
695         return;
696     fOneThread = true;
697     if (!GetBoolArg("-flushwallet", true))
698         return;
699
700     unsigned int nLastSeen = nWalletDBUpdated;
701     unsigned int nLastFlushed = nWalletDBUpdated;
702     int64_t nLastWalletUpdate = GetTime();
703     while (!fShutdown)
704     {
705         Sleep(500);
706
707         if (nLastSeen != nWalletDBUpdated)
708         {
709             nLastSeen = nWalletDBUpdated;
710             nLastWalletUpdate = GetTime();
711         }
712
713         if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
714         {
715             TRY_LOCK(bitdb.cs_db,lockDb);
716             if (lockDb)
717             {
718                 // Don't do this if any databases are in use
719                 int nRefCount = 0;
720                 map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
721                 while (mi != bitdb.mapFileUseCount.end())
722                 {
723                     nRefCount += (*mi).second;
724                     mi++;
725                 }
726
727                 if (nRefCount == 0 && !fShutdown)
728                 {
729                     map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
730                     if (mi != bitdb.mapFileUseCount.end())
731                     {
732                         printf("Flushing wallet.dat\n");
733                         nLastFlushed = nWalletDBUpdated;
734                         int64_t nStart = GetTimeMillis();
735
736                         // Flush wallet.dat so it's self contained
737                         bitdb.CloseDb(strFile);
738                         bitdb.CheckpointLSN(strFile);
739
740                         bitdb.mapFileUseCount.erase(mi++);
741                         printf("Flushed wallet.dat %" PRId64 "ms\n", GetTimeMillis() - nStart);
742                     }
743                 }
744             }
745         }
746     }
747 }
748
749 bool BackupWallet(const CWallet& wallet, const string& strDest)
750 {
751     if (!wallet.fFileBacked)
752         return false;
753     while (!fShutdown)
754     {
755         {
756             LOCK(bitdb.cs_db);
757             if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
758             {
759                 // Flush log data to the dat file
760                 bitdb.CloseDb(wallet.strWalletFile);
761                 bitdb.CheckpointLSN(wallet.strWalletFile);
762                 bitdb.mapFileUseCount.erase(wallet.strWalletFile);
763
764                 // Copy wallet.dat
765                 filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
766                 filesystem::path pathDest(strDest);
767                 if (filesystem::is_directory(pathDest))
768                     pathDest /= wallet.strWalletFile;
769
770                 try {
771 #if BOOST_VERSION >= 104000
772                     filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
773 #else
774                     filesystem::copy_file(pathSrc, pathDest);
775 #endif
776                     printf("copied wallet.dat to %s\n", pathDest.string().c_str());
777                     return true;
778                 } catch(const filesystem::filesystem_error &e) {
779                     printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
780                     return false;
781                 }
782             }
783         }
784         Sleep(100);
785     }
786     return false;
787 }
788
789 bool DumpWallet(CWallet* pwallet, const string& strDest)
790 {
791     if (!pwallet->fFileBacked)
792         return false;
793
794     std::map<CBitcoinAddress, int64_t> mapAddresses;
795     std::set<CKeyID> setKeyPool;
796
797     pwallet->GetAddresses(mapAddresses);
798     pwallet->GetAllReserveKeys(setKeyPool);
799
800     // sort time/key pairs
801     std::vector<std::pair<int64_t, CBitcoinAddress> > vAddresses;
802     for (std::map<CBitcoinAddress, int64_t>::const_iterator it = mapAddresses.begin(); it != mapAddresses.end(); it++) {
803         vAddresses.push_back(std::make_pair(it->second, it->first));
804     }
805     mapAddresses.clear();
806     std::sort(vAddresses.begin(), vAddresses.end());
807
808     // open outputfile as a stream
809     ofstream file;
810     file.open(strDest.c_str());
811     if (!file.is_open())
812        return false;
813
814     // produce output
815     file << strprintf("# Wallet dump created by NovaCoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
816     file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
817     file << strprintf("# * Best block at time of backup was %i (%s),\n", nBestHeight, hashBestChain.ToString().c_str());
818     file << strprintf("#   mined on %s\n", EncodeDumpTime(pindexBest->nTime).c_str());
819     file << "\n";
820
821     for (std::vector<std::pair<int64_t, CBitcoinAddress> >::const_iterator it = vAddresses.begin(); it != vAddresses.end(); it++) {
822         const CBitcoinAddress &addr = it->second;
823         std::string strTime = EncodeDumpTime(it->first);
824         std::string strAddr = addr.ToString();
825
826         if (addr.IsPair()) {
827             // Pubkey pair address
828             CMalleableKeyView keyView;
829             CMalleablePubKey mPubKey(addr.GetData());
830             if (!pwallet->GetMalleableView(mPubKey, keyView))
831                 continue;
832             CMalleableKey mKey;
833             pwallet->GetMalleableKey(keyView, mKey);
834             file << mKey.ToString();
835             if (pwallet->mapAddressBook.count(addr))
836                 file << strprintf(" %s label=%s # view=%s addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), keyView.ToString().c_str(), strAddr.c_str());
837             else
838                 file << strprintf(" %s # view=%s addr=%s\n", strTime.c_str(), keyView.ToString().c_str(), strAddr.c_str());
839         }
840         else {
841             // Pubkey hash address
842             CKeyID keyid;
843             addr.GetKeyID(keyid);
844             bool IsCompressed;
845             CKey key;
846             if (!pwallet->GetKey(keyid, key))
847                 continue;
848             CSecret secret = key.GetSecret(IsCompressed);
849             file << CBitcoinSecret(secret, IsCompressed).ToString();
850             if (pwallet->mapAddressBook.count(addr))
851                 file << strprintf(" %s label=%s # addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), strAddr.c_str());
852             else if (setKeyPool.count(keyid))
853                 file << strprintf(" %s reserve=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
854             else
855                 file << strprintf(" %s change=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
856         }
857     }
858
859     file << "\n";
860     file << "# End of dump\n";
861     file.close();
862
863     return true;
864 }
865
866 bool ImportWallet(CWallet *pwallet, const string& strLocation)
867 {
868
869    if (!pwallet->fFileBacked)
870        return false;
871
872    // open inputfile as stream
873    ifstream file;
874    file.open(strLocation.c_str());
875    if (!file.is_open())
876        return false;
877
878    bool fGood = true;
879    int64_t nTimeBegin = pindexBest->nTime;
880
881    // read through input file checking and importing keys into wallet.
882    while (file.good()) {
883        std::string line;
884        std::getline(file, line);
885        if (line.empty() || line[0] == '#')
886            continue; // Skip comments and empty lines
887
888        std::vector<std::string> vstr;
889        istringstream iss(line);
890        copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(vstr));
891        if (vstr.size() < 2)
892            continue;
893
894        int64_t nTime = DecodeDumpTime(vstr[1]);
895        std::string strLabel;
896        bool fLabel = true;
897        for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
898            if (boost::algorithm::starts_with(vstr[nStr], "#"))
899                break;
900            if (vstr[nStr] == "change=1")
901                fLabel = false;
902            if (vstr[nStr] == "reserve=1")
903                fLabel = false;
904            if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
905                strLabel = DecodeDumpString(vstr[nStr].substr(6));
906                fLabel = true;
907            }
908        }
909
910        CBitcoinAddress addr;
911        CBitcoinSecret vchSecret;
912        if (vchSecret.SetString(vstr[0])) {
913            // Simple private key
914
915            bool fCompressed;
916            CKey key;
917            CSecret secret = vchSecret.GetSecret(fCompressed);
918            key.SetSecret(secret, fCompressed);
919            CKeyID keyid = key.GetPubKey().GetID();
920            addr = CBitcoinAddress(keyid);
921
922            if (pwallet->HaveKey(keyid)) {
923                printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
924                continue;
925            }
926
927            printf("Importing %s...\n", addr.ToString().c_str());
928            if (!pwallet->AddKey(key)) {
929                fGood = false;
930                continue;
931            }
932        } else {
933            // A pair of private keys
934
935            CMalleableKey mKey;
936            if (!mKey.SetString(vstr[0]))
937                continue;
938            CMalleablePubKey mPubKey = mKey.GetMalleablePubKey();
939            addr = CBitcoinAddress(mPubKey);
940
941            if (pwallet->CheckOwnership(mPubKey)) {
942                printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
943                continue;
944            }
945
946            printf("Importing %s...\n", addr.ToString().c_str());
947            if (!pwallet->AddKey(mKey)) {
948                fGood = false;
949                continue;
950            }
951        }
952
953        pwallet->mapKeyMetadata[addr].nCreateTime = nTime;
954        if (fLabel)
955            pwallet->SetAddressBookName(addr, strLabel);
956
957        nTimeBegin = std::min(nTimeBegin, nTime);
958    }
959    file.close();
960
961    // rescan block chain looking for coins from new keys
962    CBlockIndex *pindex = pindexBest;
963    while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200)
964        pindex = pindex->pprev;
965
966    printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1);
967    pwallet->ScanForWalletTransactions(pindex);
968    pwallet->ReacceptWalletTransactions();
969    pwallet->MarkDirty();
970
971    return fGood;
972 }
973
974 //
975 // Try to (very carefully!) recover wallet.dat if there is a problem.
976 //
977 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
978 {
979     // Recovery procedure:
980     // move wallet.dat to wallet.timestamp.bak
981     // Call Salvage with fAggressive=true to
982     // get as much data as possible.
983     // Rewrite salvaged data to wallet.dat
984     // Set -rescan so any missing transactions will be
985     // found.
986     int64_t now = GetTime();
987     std::string newFilename = strprintf("wallet.%" PRId64 ".bak", now);
988
989     int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
990                                       newFilename.c_str(), DB_AUTO_COMMIT);
991     if (result == 0)
992         printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
993     else
994     {
995         printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
996         return false;
997     }
998
999     std::vector<CDBEnv::KeyValPair> salvagedData;
1000     bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
1001     if (salvagedData.empty())
1002     {
1003         printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
1004         return false;
1005     }
1006     printf("Salvage(aggressive) found %" PRIszu " records\n", salvagedData.size());
1007
1008     bool fSuccess = allOK;
1009     Db* pdbCopy = new Db(&dbenv.dbenv, 0);
1010     int ret = pdbCopy->open(NULL,                 // Txn pointer
1011                             filename.c_str(),   // Filename
1012                             "main",    // Logical db name
1013                             DB_BTREE,  // Database type
1014                             DB_CREATE,    // Flags
1015                             0);
1016     if (ret > 0)
1017     {
1018         printf("Cannot create database file %s\n", filename.c_str());
1019         return false;
1020     }
1021     CWallet dummyWallet;
1022     CWalletScanState wss;
1023
1024     DbTxn* ptxn = dbenv.TxnBegin();
1025     BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData)
1026     {
1027         if (fOnlyKeys)
1028         {
1029             CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
1030             CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
1031             string strType, strErr;
1032             bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
1033                                         wss, strType, strErr);
1034             if (!IsKeyType(strType))
1035                 continue;
1036             if (!fReadOK)
1037             {
1038                 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
1039                 continue;
1040             }
1041         }
1042         Dbt datKey(&row.first[0], row.first.size());
1043         Dbt datValue(&row.second[0], row.second.size());
1044         int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
1045         if (ret2 > 0)
1046             fSuccess = false;
1047     }
1048     ptxn->commit(0);
1049     pdbCopy->close(0);
1050     delete pdbCopy;
1051
1052     return fSuccess;
1053 }
1054
1055 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
1056 {
1057     return CWalletDB::Recover(dbenv, filename, false);
1058 }