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