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.
8 #include <boost/version.hpp>
9 #include <boost/filesystem.hpp>
12 using namespace boost;
15 static uint64 nAccountingEntryNumber = 0;
16 extern bool fWalletUnlockMintOnly;
22 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
25 return Write(make_pair(string("name"), strAddress), strName);
28 bool CWalletDB::EraseName(const string& strAddress)
30 // This should only be used for sending addresses, never for receiving addresses,
31 // receiving addresses must always have an address book entry if they're not change return.
33 return Erase(make_pair(string("name"), strAddress));
36 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
39 return Read(make_pair(string("acc"), strAccount), account);
42 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
44 return Write(make_pair(string("acc"), strAccount), account);
47 bool CWalletDB::WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry)
49 return Write(boost::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
52 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
54 return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
57 int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
59 list<CAccountingEntry> entries;
60 ListAccountCreditDebit(strAccount, entries);
62 int64 nCreditDebit = 0;
63 BOOST_FOREACH (const CAccountingEntry& entry, entries)
64 nCreditDebit += entry.nCreditDebit;
69 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
71 bool fAllAccounts = (strAccount == "*");
73 Dbc* pcursor = GetCursor();
75 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
76 unsigned int fFlags = DB_SET_RANGE;
80 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
81 if (fFlags == DB_SET_RANGE)
82 ssKey << boost::make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
83 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
84 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
86 if (ret == DB_NOTFOUND)
91 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
97 if (strType != "acentry")
99 CAccountingEntry acentry;
100 ssKey >> acentry.strAccount;
101 if (!fAllAccounts && acentry.strAccount != strAccount)
105 ssKey >> acentry.nEntryNo;
106 entries.push_back(acentry);
114 CWalletDB::ReorderTransactions(CWallet* pwallet)
116 LOCK(pwallet->cs_wallet);
117 // Old wallets didn't have any defined order for transactions
118 // Probably a bad idea to change the output of this
120 // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
121 typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
122 typedef multimap<int64, TxPair > TxItems;
125 for (map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
127 CWalletTx* wtx = &((*it).second);
128 txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
130 list<CAccountingEntry> acentries;
131 ListAccountCreditDebit("", acentries);
132 BOOST_FOREACH(CAccountingEntry& entry, acentries)
134 txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
137 int64& nOrderPosNext = pwallet->nOrderPosNext;
139 std::vector<int64> nOrderPosOffsets;
140 for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
142 CWalletTx *const pwtx = (*it).second.first;
143 CAccountingEntry *const pacentry = (*it).second.second;
144 int64& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
148 nOrderPos = nOrderPosNext++;
149 nOrderPosOffsets.push_back(nOrderPos);
152 // Have to write accounting regardless, since we don't keep it in memory
153 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
158 int64 nOrderPosOff = 0;
159 BOOST_FOREACH(const int64& nOffsetStart, nOrderPosOffsets)
161 if (nOrderPos >= nOffsetStart)
164 nOrderPos += nOrderPosOff;
165 nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
170 // Since we're changing the order, write it back
173 if (!WriteTx(pwtx->GetHash(), *pwtx))
177 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
185 class CWalletScanState {
189 unsigned int nKeyMeta;
193 vector<uint256> vWalletUpgrade;
196 nKeys = nCKeys = nKeyMeta = 0;
197 fIsEncrypted = false;
198 fAnyUnordered = false;
204 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
205 CWalletScanState &wss, string& strType, string& strErr)
209 // Taking advantage of the fact that pair serialization
210 // is just the two items serialized one after the other
212 if (strType == "name")
216 ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()];
218 else if (strType == "tx")
224 if (wtx.CheckTransaction() && (wtx.GetHash() == hash))
225 wtx.BindWallet(pwallet);
228 pwallet->mapWallet.erase(hash);
232 // Undo serialize changes in 31600
233 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
235 if (!ssValue.empty())
239 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
240 strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
241 wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
242 wtx.fTimeReceivedIsTxTime = fTmp;
246 strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
247 wtx.fTimeReceivedIsTxTime = 0;
249 wss.vWalletUpgrade.push_back(hash);
252 if (wtx.nOrderPos == -1)
253 wss.fAnyUnordered = true;
255 pwallet->AddToWallet(wtx, true);
258 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
259 //printf(" %12"PRI64d" %s %s %s\n",
260 // wtx.vout[0].nValue,
261 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
262 // wtx.hashBlock.ToString().substr(0,20).c_str(),
263 // wtx.mapValue["message"].c_str());
265 else if (strType == "acentry")
271 if (nNumber > nAccountingEntryNumber)
272 nAccountingEntryNumber = nNumber;
274 if (!wss.fAnyUnordered)
276 CAccountingEntry acentry;
278 if (acentry.nOrderPos == -1)
279 wss.fAnyUnordered = true;
282 else if (strType == "key" || strType == "wkey")
284 vector<unsigned char> vchPubKey;
287 if (strType == "key")
292 key.SetPubKey(vchPubKey);
293 if (!key.SetPrivKey(pkey))
295 strErr = "Error reading wallet database: CPrivKey corrupt";
298 if (key.GetPubKey() != vchPubKey)
300 strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
305 strErr = "Error reading wallet database: invalid CPrivKey";
313 key.SetPubKey(vchPubKey);
314 if (!key.SetPrivKey(wkey.vchPrivKey))
316 strErr = "Error reading wallet database: CPrivKey corrupt";
319 if (key.GetPubKey() != vchPubKey)
321 strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
326 strErr = "Error reading wallet database: invalid CWalletKey";
330 if (!pwallet->LoadKey(key))
332 strErr = "Error reading wallet database: LoadKey failed";
336 else if (strType == "mkey")
340 CMasterKey kMasterKey;
341 ssValue >> kMasterKey;
342 if(pwallet->mapMasterKeys.count(nID) != 0)
344 strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
347 pwallet->mapMasterKeys[nID] = kMasterKey;
348 if (pwallet->nMasterKeyMaxID < nID)
349 pwallet->nMasterKeyMaxID = nID;
351 else if (strType == "ckey")
354 vector<unsigned char> vchPubKey;
356 vector<unsigned char> vchPrivKey;
357 ssValue >> vchPrivKey;
358 if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
360 strErr = "Error reading wallet database: LoadCryptedKey failed";
363 wss.fIsEncrypted = true;
365 else if (strType == "keymeta")
369 CKeyMetadata keyMeta;
373 pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
375 // find earliest key creation time, as wallet birthday
376 if (!pwallet->nTimeFirstKey ||
377 (keyMeta.nCreateTime < pwallet->nTimeFirstKey))
378 pwallet->nTimeFirstKey = keyMeta.nCreateTime;
380 else if (strType == "defaultkey")
382 ssValue >> pwallet->vchDefaultKey;
384 else if (strType == "pool")
390 pwallet->setKeyPool.insert(nIndex);
392 // If no metadata exists yet, create a default with the pool key's
393 // creation time. Note that this may be overwritten by actually
394 // stored metadata for that key later, which is fine.
395 CKeyID keyid = keypool.vchPubKey.GetID();
396 if (pwallet->mapKeyMetadata.count(keyid) == 0)
397 pwallet->mapKeyMetadata[keyid] = CKeyMetadata(keypool.nTime);
400 else if (strType == "version")
402 ssValue >> wss.nFileVersion;
403 if (wss.nFileVersion == 10300)
404 wss.nFileVersion = 300;
406 else if (strType == "cscript")
412 if (!pwallet->LoadCScript(script))
414 strErr = "Error reading wallet database: LoadCScript failed";
418 else if (strType == "orderposnext")
420 ssValue >> pwallet->nOrderPosNext;
429 static bool IsKeyType(string strType)
431 return (strType== "key" || strType == "wkey" ||
432 strType == "mkey" || strType == "ckey");
435 DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
437 pwallet->vchDefaultKey = CPubKey();
438 CWalletScanState wss;
439 bool fNoncriticalErrors = false;
440 DBErrors result = DB_LOAD_OK;
443 LOCK(pwallet->cs_wallet);
445 if (Read((string)"minversion", nMinVersion))
447 if (nMinVersion > CLIENT_VERSION)
449 pwallet->LoadMinVersion(nMinVersion);
453 Dbc* pcursor = GetCursor();
456 printf("Error getting wallet database cursor\n");
463 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
464 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
465 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
466 if (ret == DB_NOTFOUND)
470 printf("Error reading next record from wallet database\n");
474 // Try to be tolerant of single corrupt records:
475 string strType, strErr;
476 if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
478 // losing keys is considered a catastrophic error, anything else
479 // we assume the user can live with:
480 if (IsKeyType(strType))
484 // Leave other errors alone, if we try to fix them we might make things worse.
485 fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
487 // Rescan if there is a bad transaction record:
488 SoftSetBoolArg("-rescan", true);
492 printf("%s\n", strErr.c_str());
501 if (fNoncriticalErrors && result == DB_LOAD_OK)
502 result = DB_NONCRITICAL_ERROR;
504 // Any wallet corruption at all: skip any rewriting or
505 // upgrading, we don't want to make it worse.
506 if (result != DB_LOAD_OK)
509 printf("nFileVersion = %d\n", wss.nFileVersion);
511 printf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total\n",
512 wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys);
514 // nTimeFirstKey is only reliable if all keys have metadata
515 if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
516 pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
519 BOOST_FOREACH(uint256 hash, wss.vWalletUpgrade)
520 WriteTx(hash, pwallet->mapWallet[hash]);
522 // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
523 if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
524 return DB_NEED_REWRITE;
526 if (wss.nFileVersion < CLIENT_VERSION) // Update
527 WriteVersion(CLIENT_VERSION);
529 if (wss.fAnyUnordered)
530 result = ReorderTransactions(pwallet);
535 DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash)
537 pwallet->vchDefaultKey = CPubKey();
538 CWalletScanState wss;
539 bool fNoncriticalErrors = false;
540 DBErrors result = DB_LOAD_OK;
543 LOCK(pwallet->cs_wallet);
545 if (Read((string)"minversion", nMinVersion))
547 if (nMinVersion > CLIENT_VERSION)
549 pwallet->LoadMinVersion(nMinVersion);
553 Dbc* pcursor = GetCursor();
556 printf("Error getting wallet database cursor\n");
563 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
564 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
565 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
566 if (ret == DB_NOTFOUND)
570 printf("Error reading next record from wallet database\n");
576 if (strType == "tx") {
580 vTxHash.push_back(hash);
585 catch (boost::thread_interrupted) {
592 if (fNoncriticalErrors && result == DB_LOAD_OK)
593 result = DB_NONCRITICAL_ERROR;
598 DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet)
600 // build list of wallet TXs
601 vector<uint256> vTxHash;
602 DBErrors err = FindWalletTx(pwallet, vTxHash);
603 if (err != DB_LOAD_OK)
606 // erase each wallet TX
607 BOOST_FOREACH (uint256& hash, vTxHash) {
615 void ThreadFlushWalletDB(void* parg)
617 // Make this thread recognisable as the wallet flushing thread
618 RenameThread("bitcoin-wallet");
620 const string& strFile = ((const string*)parg)[0];
621 static bool fOneThread;
625 if (!GetBoolArg("-flushwallet", true))
628 unsigned int nLastSeen = nWalletDBUpdated;
629 unsigned int nLastFlushed = nWalletDBUpdated;
630 int64 nLastWalletUpdate = GetTime();
635 if (nLastSeen != nWalletDBUpdated)
637 nLastSeen = nWalletDBUpdated;
638 nLastWalletUpdate = GetTime();
641 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
643 TRY_LOCK(bitdb.cs_db,lockDb);
646 // Don't do this if any databases are in use
648 map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
649 while (mi != bitdb.mapFileUseCount.end())
651 nRefCount += (*mi).second;
655 if (nRefCount == 0 && !fShutdown)
657 map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
658 if (mi != bitdb.mapFileUseCount.end())
660 printf("Flushing wallet.dat\n");
661 nLastFlushed = nWalletDBUpdated;
662 int64 nStart = GetTimeMillis();
664 // Flush wallet.dat so it's self contained
665 bitdb.CloseDb(strFile);
666 bitdb.CheckpointLSN(strFile);
668 bitdb.mapFileUseCount.erase(mi++);
669 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
677 bool BackupWallet(const CWallet& wallet, const string& strDest)
679 if (!wallet.fFileBacked)
685 if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
687 // Flush log data to the dat file
688 bitdb.CloseDb(wallet.strWalletFile);
689 bitdb.CheckpointLSN(wallet.strWalletFile);
690 bitdb.mapFileUseCount.erase(wallet.strWalletFile);
693 filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
694 filesystem::path pathDest(strDest);
695 if (filesystem::is_directory(pathDest))
696 pathDest /= wallet.strWalletFile;
699 #if BOOST_VERSION >= 104000
700 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
702 filesystem::copy_file(pathSrc, pathDest);
704 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
706 } catch(const filesystem::filesystem_error &e) {
707 printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
718 // Try to (very carefully!) recover wallet.dat if there is a problem.
720 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
722 // Recovery procedure:
723 // move wallet.dat to wallet.timestamp.bak
724 // Call Salvage with fAggressive=true to
725 // get as much data as possible.
726 // Rewrite salvaged data to wallet.dat
727 // Set -rescan so any missing transactions will be
729 int64 now = GetTime();
730 std::string newFilename = strprintf("wallet.%"PRI64d".bak", now);
732 int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
733 newFilename.c_str(), DB_AUTO_COMMIT);
735 printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
738 printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
742 std::vector<CDBEnv::KeyValPair> salvagedData;
743 bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
744 if (salvagedData.empty())
746 printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
749 printf("Salvage(aggressive) found %"PRIszu" records\n", salvagedData.size());
751 bool fSuccess = allOK;
752 Db* pdbCopy = new Db(&dbenv.dbenv, 0);
753 int ret = pdbCopy->open(NULL, // Txn pointer
754 filename.c_str(), // Filename
755 "main", // Logical db name
756 DB_BTREE, // Database type
761 printf("Cannot create database file %s\n", filename.c_str());
765 CWalletScanState wss;
767 DbTxn* ptxn = dbenv.TxnBegin();
768 BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData)
772 CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
773 CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
774 string strType, strErr;
775 bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
776 wss, strType, strErr);
777 if (!IsKeyType(strType))
781 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
785 Dbt datKey(&row.first[0], row.first.size());
786 Dbt datValue(&row.second[0], row.second.size());
787 int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
798 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
800 return CWalletDB::Recover(dbenv, filename, false);