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