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