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/filesystem.hpp>
11 using namespace boost;
14 static uint64 nAccountingEntryNumber = 0;
15 extern bool fWalletUnlockMintOnly;
21 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
24 return Write(make_pair(string("name"), strAddress), strName);
27 bool CWalletDB::EraseName(const string& strAddress)
29 // This should only be used for sending addresses, never for receiving addresses,
30 // receiving addresses must always have an address book entry if they're not change return.
32 return Erase(make_pair(string("name"), strAddress));
35 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
38 return Read(make_pair(string("acc"), strAccount), account);
41 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
43 return Write(make_pair(string("acc"), strAccount), account);
46 bool CWalletDB::WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry)
48 return Write(boost::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
51 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
53 return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
56 int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
58 list<CAccountingEntry> entries;
59 ListAccountCreditDebit(strAccount, entries);
61 int64 nCreditDebit = 0;
62 BOOST_FOREACH (const CAccountingEntry& entry, entries)
63 nCreditDebit += entry.nCreditDebit;
68 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
70 bool fAllAccounts = (strAccount == "*");
72 Dbc* pcursor = GetCursor();
74 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
75 unsigned int fFlags = DB_SET_RANGE;
79 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
80 if (fFlags == DB_SET_RANGE)
81 ssKey << boost::make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
82 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
83 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
85 if (ret == DB_NOTFOUND)
90 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
96 if (strType != "acentry")
98 CAccountingEntry acentry;
99 ssKey >> acentry.strAccount;
100 if (!fAllAccounts && acentry.strAccount != strAccount)
104 ssKey >> acentry.nEntryNo;
105 entries.push_back(acentry);
113 CWalletDB::ReorderTransactions(CWallet* pwallet)
115 LOCK(pwallet->cs_wallet);
116 // Old wallets didn't have any defined order for transactions
117 // Probably a bad idea to change the output of this
119 // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
120 typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
121 typedef multimap<int64, TxPair > TxItems;
124 for (map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
126 CWalletTx* wtx = &((*it).second);
127 txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
129 list<CAccountingEntry> acentries;
130 ListAccountCreditDebit("", acentries);
131 BOOST_FOREACH(CAccountingEntry& entry, acentries)
133 txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
136 int64& nOrderPosNext = pwallet->nOrderPosNext;
138 std::vector<int64> nOrderPosOffsets;
139 for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
141 CWalletTx *const pwtx = (*it).second.first;
142 CAccountingEntry *const pacentry = (*it).second.second;
143 int64& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
147 nOrderPos = nOrderPosNext++;
148 nOrderPosOffsets.push_back(nOrderPos);
151 // Have to write accounting regardless, since we don't keep it in memory
152 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
157 int64 nOrderPosOff = 0;
158 BOOST_FOREACH(const int64& nOffsetStart, nOrderPosOffsets)
160 if (nOrderPos >= nOffsetStart)
163 nOrderPos += nOrderPosOff;
164 nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
169 // Since we're changing the order, write it back
172 if (!WriteTx(pwtx->GetHash(), *pwtx))
176 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
186 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
187 int& nFileVersion, vector<uint256>& vWalletUpgrade,
188 bool& fIsEncrypted, bool& fAnyUnordered, string& strType, string& strErr)
192 // Taking advantage of the fact that pair serialization
193 // is just the two items serialized one after the other
195 if (strType == "name")
199 ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()];
201 else if (strType == "tx")
205 CWalletTx& wtx = pwallet->mapWallet[hash];
207 if (wtx.CheckTransaction() && (wtx.GetHash() == hash))
208 wtx.BindWallet(pwallet);
211 pwallet->mapWallet.erase(hash);
215 // Undo serialize changes in 31600
216 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
218 if (!ssValue.empty())
222 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
223 strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
224 wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
225 wtx.fTimeReceivedIsTxTime = fTmp;
229 strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
230 wtx.fTimeReceivedIsTxTime = 0;
232 vWalletUpgrade.push_back(hash);
235 if (wtx.nOrderPos == -1)
236 fAnyUnordered = true;
239 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
240 //printf(" %12"PRI64d" %s %s %s\n",
241 // wtx.vout[0].nValue,
242 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
243 // wtx.hashBlock.ToString().substr(0,20).c_str(),
244 // wtx.mapValue["message"].c_str());
246 else if (strType == "acentry")
252 if (nNumber > nAccountingEntryNumber)
253 nAccountingEntryNumber = nNumber;
257 CAccountingEntry acentry;
259 if (acentry.nOrderPos == -1)
260 fAnyUnordered = true;
263 else if (strType == "key" || strType == "wkey")
265 vector<unsigned char> vchPubKey;
268 if (strType == "key")
272 key.SetPubKey(vchPubKey);
273 if (!key.SetPrivKey(pkey))
275 strErr = "Error reading wallet database: CPrivKey corrupt";
278 if (key.GetPubKey() != vchPubKey)
280 strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
285 strErr = "Error reading wallet database: invalid CPrivKey";
293 key.SetPubKey(vchPubKey);
294 if (!key.SetPrivKey(wkey.vchPrivKey))
296 strErr = "Error reading wallet database: CPrivKey corrupt";
299 if (key.GetPubKey() != vchPubKey)
301 strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
306 strErr = "Error reading wallet database: invalid CWalletKey";
310 if (!pwallet->LoadKey(key))
312 strErr = "Error reading wallet database: LoadKey failed";
316 else if (strType == "mkey")
320 CMasterKey kMasterKey;
321 ssValue >> kMasterKey;
322 if(pwallet->mapMasterKeys.count(nID) != 0)
324 strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
327 pwallet->mapMasterKeys[nID] = kMasterKey;
328 if (pwallet->nMasterKeyMaxID < nID)
329 pwallet->nMasterKeyMaxID = nID;
331 else if (strType == "ckey")
333 vector<unsigned char> vchPubKey;
335 vector<unsigned char> vchPrivKey;
336 ssValue >> vchPrivKey;
337 if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
339 strErr = "Error reading wallet database: LoadCryptedKey failed";
344 else if (strType == "defaultkey")
346 ssValue >> pwallet->vchDefaultKey;
348 else if (strType == "pool")
352 pwallet->setKeyPool.insert(nIndex);
354 else if (strType == "version")
356 ssValue >> nFileVersion;
357 if (nFileVersion == 10300)
360 else if (strType == "cscript")
366 if (!pwallet->LoadCScript(script))
368 strErr = "Error reading wallet database: LoadCScript failed";
372 else if (strType == "orderposnext")
374 ssValue >> pwallet->nOrderPosNext;
383 static bool IsKeyType(string strType)
385 return (strType== "key" || strType == "wkey" ||
386 strType == "mkey" || strType == "ckey");
389 DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
391 pwallet->vchDefaultKey = CPubKey();
392 int nFileVersion = 0;
393 vector<uint256> vWalletUpgrade;
394 bool fIsEncrypted = false;
395 bool fAnyUnordered = false;
396 bool fNoncriticalErrors = false;
397 DBErrors result = DB_LOAD_OK;
400 LOCK(pwallet->cs_wallet);
402 if (Read((string)"minversion", nMinVersion))
404 if (nMinVersion > CLIENT_VERSION)
406 pwallet->LoadMinVersion(nMinVersion);
410 Dbc* pcursor = GetCursor();
413 printf("Error getting wallet database cursor\n");
420 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
421 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
422 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
423 if (ret == DB_NOTFOUND)
427 printf("Error reading next record from wallet database\n");
431 // Try to be tolerant of single corrupt records:
432 string strType, strErr;
433 if (!ReadKeyValue(pwallet, ssKey, ssValue, nFileVersion,
434 vWalletUpgrade, fIsEncrypted, fAnyUnordered, strType, strErr))
436 // losing keys is considered a catastrophic error, anything else
437 // we assume the user can live with:
438 if (IsKeyType(strType))
442 // Leave other errors alone, if we try to fix them we might make things worse.
443 fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
445 // Rescan if there is a bad transaction record:
446 SoftSetBoolArg("-rescan", true);
450 printf("%s\n", strErr.c_str());
459 if (fNoncriticalErrors && result == DB_LOAD_OK)
460 result = DB_NONCRITICAL_ERROR;
462 // Any wallet corruption at all: skip any rewriting or
463 // upgrading, we don't want to make it worse.
464 if (result != DB_LOAD_OK)
467 printf("nFileVersion = %d\n", nFileVersion);
469 BOOST_FOREACH(uint256 hash, vWalletUpgrade)
470 WriteTx(hash, pwallet->mapWallet[hash]);
472 // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
473 if (fIsEncrypted && (nFileVersion == 40000 || nFileVersion == 50000))
474 return DB_NEED_REWRITE;
476 if (nFileVersion < CLIENT_VERSION) // Update
477 WriteVersion(CLIENT_VERSION);
480 result = ReorderTransactions(pwallet);
485 void ThreadFlushWalletDB(void* parg)
487 // Make this thread recognisable as the wallet flushing thread
488 RenameThread("bitcoin-wallet");
490 const string& strFile = ((const string*)parg)[0];
491 static bool fOneThread;
495 if (!GetBoolArg("-flushwallet", true))
498 unsigned int nLastSeen = nWalletDBUpdated;
499 unsigned int nLastFlushed = nWalletDBUpdated;
500 int64 nLastWalletUpdate = GetTime();
505 if (nLastSeen != nWalletDBUpdated)
507 nLastSeen = nWalletDBUpdated;
508 nLastWalletUpdate = GetTime();
511 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
513 TRY_LOCK(bitdb.cs_db,lockDb);
516 // Don't do this if any databases are in use
518 map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
519 while (mi != bitdb.mapFileUseCount.end())
521 nRefCount += (*mi).second;
525 if (nRefCount == 0 && !fShutdown)
527 map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
528 if (mi != bitdb.mapFileUseCount.end())
530 printf("Flushing wallet.dat\n");
531 nLastFlushed = nWalletDBUpdated;
532 int64 nStart = GetTimeMillis();
534 // Flush wallet.dat so it's self contained
535 bitdb.CloseDb(strFile);
536 bitdb.CheckpointLSN(strFile);
538 bitdb.mapFileUseCount.erase(mi++);
539 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
547 bool BackupWallet(const CWallet& wallet, const string& strDest)
549 if (!wallet.fFileBacked)
555 if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
557 // Flush log data to the dat file
558 bitdb.CloseDb(wallet.strWalletFile);
559 bitdb.CheckpointLSN(wallet.strWalletFile);
560 bitdb.mapFileUseCount.erase(wallet.strWalletFile);
563 filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
564 filesystem::path pathDest(strDest);
565 if (filesystem::is_directory(pathDest))
566 pathDest /= wallet.strWalletFile;
569 #if BOOST_VERSION >= 104000
570 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
572 filesystem::copy_file(pathSrc, pathDest);
574 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
576 } catch(const filesystem::filesystem_error &e) {
577 printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
588 // Try to (very carefully!) recover wallet.dat if there is a problem.
590 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
592 // Recovery procedure:
593 // move wallet.dat to wallet.timestamp.bak
594 // Call Salvage with fAggressive=true to
595 // get as much data as possible.
596 // Rewrite salvaged data to wallet.dat
597 // Set -rescan so any missing transactions will be
599 int64 now = GetTime();
600 std::string newFilename = strprintf("wallet.%"PRI64d".bak", now);
602 int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
603 newFilename.c_str(), DB_AUTO_COMMIT);
605 printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
608 printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
612 std::vector<CDBEnv::KeyValPair> salvagedData;
613 bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
614 if (salvagedData.empty())
616 printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
619 printf("Salvage(aggressive) found %"PRIszu" records\n", salvagedData.size());
621 bool fSuccess = allOK;
622 Db* pdbCopy = new Db(&dbenv.dbenv, 0);
623 int ret = pdbCopy->open(NULL, // Txn pointer
624 filename.c_str(), // Filename
625 "main", // Logical db name
626 DB_BTREE, // Database type
631 printf("Cannot create database file %s\n", filename.c_str());
635 int nFileVersion = 0;
636 vector<uint256> vWalletUpgrade;
637 bool fIsEncrypted = false;
638 bool fAnyUnordered = false;
640 DbTxn* ptxn = dbenv.TxnBegin();
641 BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData)
645 CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
646 CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
647 string strType, strErr;
648 bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
649 nFileVersion, vWalletUpgrade,
650 fIsEncrypted, fAnyUnordered,
652 if (!IsKeyType(strType))
656 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
660 Dbt datKey(&row.first[0], row.first.size());
661 Dbt datValue(&row.second[0], row.second.size());
662 int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
673 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
675 return CWalletDB::Recover(dbenv, filename, false);