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