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