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