c_str() isn't needed here
[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 strKeyView;
308
309             CSecret vchSecret;
310             ssKey >> strKeyView;
311             ssValue >> vchSecret;
312
313             CMalleableKeyView keyView(strKeyView);
314             if (!pwallet->LoadMalleableKey(keyView, vchSecret))
315             {
316                 strErr = "Error reading wallet database: LoadMalleableKey failed";
317                 return false;
318             }
319         }
320         else if (strType == "malcpair")
321         {
322             string strKeyView;
323
324             std::vector<unsigned char> vchCryptedSecret;
325             ssKey >> strKeyView;
326             ssValue >> vchCryptedSecret;
327
328             CMalleableKeyView keyView(strKeyView);
329             if (!pwallet->LoadCryptedMalleableKey(keyView, vchCryptedSecret))
330             {
331                 strErr = "Error reading wallet database: LoadCryptedMalleableKey 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" || strType == "malcpair");
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     if (!pwallet->fFileBacked)
788         return false;
789
790     std::map<CBitcoinAddress, int64_t> mapAddresses;
791     std::set<CKeyID> setKeyPool;
792
793     pwallet->GetAddresses(mapAddresses);
794     pwallet->GetAllReserveKeys(setKeyPool);
795
796     // sort time/key pairs
797     std::vector<std::pair<int64_t, CBitcoinAddress> > vAddresses;
798     for (std::map<CBitcoinAddress, int64_t>::const_iterator it = mapAddresses.begin(); it != mapAddresses.end(); it++) {
799         vAddresses.push_back(std::make_pair(it->second, it->first));
800     }
801     mapAddresses.clear();
802     std::sort(vAddresses.begin(), vAddresses.end());
803
804     // open outputfile as a stream
805     ofstream file;
806     file.open(strDest.c_str());
807     if (!file.is_open())
808        return false;
809
810     // produce output
811     file << strprintf("# Wallet dump created by NovaCoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
812     file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
813     file << strprintf("# * Best block at time of backup was %i (%s),\n", nBestHeight, hashBestChain.ToString().c_str());
814     file << strprintf("#   mined on %s\n", EncodeDumpTime(pindexBest->nTime).c_str());
815     file << "\n";
816
817     for (std::vector<std::pair<int64_t, CBitcoinAddress> >::const_iterator it = vAddresses.begin(); it != vAddresses.end(); it++) {
818         const CBitcoinAddress &addr = it->second;
819         std::string strTime = EncodeDumpTime(it->first);
820         std::string strAddr = addr.ToString();
821
822         if (addr.IsPair()) {
823             // Pubkey pair address
824             CMalleableKeyView keyView;
825             CMalleablePubKey mPubKey(addr.GetData());
826             if (!pwallet->GetMalleableView(mPubKey, keyView))
827                 continue;
828             CMalleableKey mKey;
829             pwallet->GetMalleableKey(keyView, mKey);
830             file << strprintf("%s %s # view=%s addr=%s\n", mKey.ToString().c_str(), strTime.c_str(), keyView.ToString().c_str(), strAddr.c_str());
831         }
832         else {
833             // Pubkey hash address
834             CKeyID keyid;
835             addr.GetKeyID(keyid);
836             bool IsCompressed;
837             CKey key;
838             if (!pwallet->GetKey(keyid, key))
839                 continue;
840             CSecret secret = key.GetSecret(IsCompressed);
841             file << CBitcoinSecret(secret, IsCompressed).ToString();
842             if (pwallet->mapAddressBook.count(keyid))
843                 file << strprintf(" %s label=%s # addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[keyid]).c_str(), strAddr.c_str());
844             else if (setKeyPool.count(keyid))
845                 file << strprintf(" %s reserve=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
846             else
847                 file << strprintf(" %s change=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
848         }
849     }
850
851     file << "\n";
852     file << "# End of dump\n";
853     file.close();
854
855     return true;
856 }
857
858 bool ImportWallet(CWallet *pwallet, const string& strLocation)
859 {
860
861    if (!pwallet->fFileBacked)
862        return false;
863    while (!fShutdown)
864    {
865       // open inputfile as stream
866       ifstream file;
867       file.open(strLocation.c_str());
868       if (!file.is_open())
869           return false;
870
871       int64_t nTimeBegin = pindexBest->nTime;
872
873       bool fGood = true;
874
875       // read through input file checking and importing keys into wallet.
876       while (file.good()) {
877           std::string line;
878           std::getline(file, line);
879           if (line.empty() || line[0] == '#')
880               continue;
881
882           std::vector<std::string> vstr;
883           istringstream iss(line);
884           copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(vstr));
885           if (vstr.size() < 2)
886               continue;
887           CBitcoinSecret vchSecret;
888           if (!vchSecret.SetString(vstr[0]))
889               continue;
890
891           bool fCompressed;
892           CKey key;
893           CSecret secret = vchSecret.GetSecret(fCompressed);
894           key.SetSecret(secret, fCompressed);
895           CKeyID keyid = key.GetPubKey().GetID();
896
897           if (pwallet->HaveKey(keyid)) {
898               printf("Skipping import of %s (key already present)\n", CBitcoinAddress(keyid).ToString().c_str());
899              continue;
900           }
901           int64_t nTime = DecodeDumpTime(vstr[1]);
902           std::string strLabel;
903           bool fLabel = true;
904           for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
905               if (boost::algorithm::starts_with(vstr[nStr], "#"))
906                   break;
907               if (vstr[nStr] == "change=1")
908                   fLabel = false;
909               if (vstr[nStr] == "reserve=1")
910                   fLabel = false;
911               if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
912                   strLabel = DecodeDumpString(vstr[nStr].substr(6));
913                   fLabel = true;
914               }
915           }
916           printf("Importing %s...\n", CBitcoinAddress(keyid).ToString().c_str());
917           if (!pwallet->AddKey(key)) {
918               fGood = false;
919               continue;
920           }
921           pwallet->mapKeyMetadata[keyid].nCreateTime = nTime;
922           if (fLabel)
923               pwallet->SetAddressBookName(keyid, strLabel);
924           nTimeBegin = std::min(nTimeBegin, nTime);
925       }
926       file.close();
927
928       // rescan block chain looking for coins from new keys
929       CBlockIndex *pindex = pindexBest;
930       while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200)
931           pindex = pindex->pprev;
932
933       printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1);
934       pwallet->ScanForWalletTransactions(pindex);
935       pwallet->ReacceptWalletTransactions();
936       pwallet->MarkDirty();
937
938       return fGood;
939
940   }
941
942   return false;
943
944 }
945
946
947 //
948 // Try to (very carefully!) recover wallet.dat if there is a problem.
949 //
950 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
951 {
952     // Recovery procedure:
953     // move wallet.dat to wallet.timestamp.bak
954     // Call Salvage with fAggressive=true to
955     // get as much data as possible.
956     // Rewrite salvaged data to wallet.dat
957     // Set -rescan so any missing transactions will be
958     // found.
959     int64_t now = GetTime();
960     std::string newFilename = strprintf("wallet.%" PRId64 ".bak", now);
961
962     int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
963                                       newFilename.c_str(), DB_AUTO_COMMIT);
964     if (result == 0)
965         printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
966     else
967     {
968         printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
969         return false;
970     }
971
972     std::vector<CDBEnv::KeyValPair> salvagedData;
973     bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
974     if (salvagedData.empty())
975     {
976         printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
977         return false;
978     }
979     printf("Salvage(aggressive) found %" PRIszu " records\n", salvagedData.size());
980
981     bool fSuccess = allOK;
982     Db* pdbCopy = new Db(&dbenv.dbenv, 0);
983     int ret = pdbCopy->open(NULL,                 // Txn pointer
984                             filename.c_str(),   // Filename
985                             "main",    // Logical db name
986                             DB_BTREE,  // Database type
987                             DB_CREATE,    // Flags
988                             0);
989     if (ret > 0)
990     {
991         printf("Cannot create database file %s\n", filename.c_str());
992         return false;
993     }
994     CWallet dummyWallet;
995     CWalletScanState wss;
996
997     DbTxn* ptxn = dbenv.TxnBegin();
998     BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData)
999     {
1000         if (fOnlyKeys)
1001         {
1002             CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
1003             CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
1004             string strType, strErr;
1005             bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
1006                                         wss, strType, strErr);
1007             if (!IsKeyType(strType))
1008                 continue;
1009             if (!fReadOK)
1010             {
1011                 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
1012                 continue;
1013             }
1014         }
1015         Dbt datKey(&row.first[0], row.first.size());
1016         Dbt datValue(&row.second[0], row.second.size());
1017         int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
1018         if (ret2 > 0)
1019             fSuccess = false;
1020     }
1021     ptxn->commit(0);
1022     pdbCopy->close(0);
1023     delete pdbCopy;
1024
1025     return fSuccess;
1026 }
1027
1028 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
1029 {
1030     return CWalletDB::Recover(dbenv, filename, false);
1031 }