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