You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

walletdb.cpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. #include "walletdb.h"
  6. #include "wallet.h"
  7. #include <boost/filesystem.hpp>
  8. using namespace std;
  9. using namespace boost;
  10. static uint64 nAccountingEntryNumber = 0;
  11. //
  12. // CWalletDB
  13. //
  14. bool CWalletDB::WriteName(const string& strAddress, const string& strName)
  15. {
  16. nWalletDBUpdated++;
  17. return Write(make_pair(string("name"), strAddress), strName);
  18. }
  19. bool CWalletDB::EraseName(const string& strAddress)
  20. {
  21. // This should only be used for sending addresses, never for receiving addresses,
  22. // receiving addresses must always have an address book entry if they're not change return.
  23. nWalletDBUpdated++;
  24. return Erase(make_pair(string("name"), strAddress));
  25. }
  26. bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
  27. {
  28. account.SetNull();
  29. return Read(make_pair(string("acc"), strAccount), account);
  30. }
  31. bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
  32. {
  33. return Write(make_pair(string("acc"), strAccount), account);
  34. }
  35. bool CWalletDB::WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry)
  36. {
  37. return Write(boost::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
  38. }
  39. bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
  40. {
  41. return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
  42. }
  43. int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
  44. {
  45. list<CAccountingEntry> entries;
  46. ListAccountCreditDebit(strAccount, entries);
  47. int64 nCreditDebit = 0;
  48. BOOST_FOREACH (const CAccountingEntry& entry, entries)
  49. nCreditDebit += entry.nCreditDebit;
  50. return nCreditDebit;
  51. }
  52. void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
  53. {
  54. bool fAllAccounts = (strAccount == "*");
  55. Dbc* pcursor = GetCursor();
  56. if (!pcursor)
  57. throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
  58. unsigned int fFlags = DB_SET_RANGE;
  59. loop
  60. {
  61. // Read next record
  62. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  63. if (fFlags == DB_SET_RANGE)
  64. ssKey << boost::make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
  65. CDataStream ssValue(SER_DISK, CLIENT_VERSION);
  66. int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
  67. fFlags = DB_NEXT;
  68. if (ret == DB_NOTFOUND)
  69. break;
  70. else if (ret != 0)
  71. {
  72. pcursor->close();
  73. throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
  74. }
  75. // Unserialize
  76. string strType;
  77. ssKey >> strType;
  78. if (strType != "acentry")
  79. break;
  80. CAccountingEntry acentry;
  81. ssKey >> acentry.strAccount;
  82. if (!fAllAccounts && acentry.strAccount != strAccount)
  83. break;
  84. ssValue >> acentry;
  85. ssKey >> acentry.nEntryNo;
  86. entries.push_back(acentry);
  87. }
  88. pcursor->close();
  89. }
  90. DBErrors
  91. CWalletDB::ReorderTransactions(CWallet* pwallet)
  92. {
  93. LOCK(pwallet->cs_wallet);
  94. // Old wallets didn't have any defined order for transactions
  95. // Probably a bad idea to change the output of this
  96. // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
  97. typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
  98. typedef multimap<int64, TxPair > TxItems;
  99. TxItems txByTime;
  100. for (map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
  101. {
  102. CWalletTx* wtx = &((*it).second);
  103. txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
  104. }
  105. list<CAccountingEntry> acentries;
  106. ListAccountCreditDebit("", acentries);
  107. BOOST_FOREACH(CAccountingEntry& entry, acentries)
  108. {
  109. txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
  110. }
  111. int64& nOrderPosNext = pwallet->nOrderPosNext;
  112. nOrderPosNext = 0;
  113. std::vector<int64> nOrderPosOffsets;
  114. for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
  115. {
  116. CWalletTx *const pwtx = (*it).second.first;
  117. CAccountingEntry *const pacentry = (*it).second.second;
  118. int64& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
  119. if (nOrderPos == -1)
  120. {
  121. nOrderPos = nOrderPosNext++;
  122. nOrderPosOffsets.push_back(nOrderPos);
  123. if (pacentry)
  124. // Have to write accounting regardless, since we don't keep it in memory
  125. if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
  126. return DB_LOAD_FAIL;
  127. }
  128. else
  129. {
  130. int64 nOrderPosOff = 0;
  131. BOOST_FOREACH(const int64& nOffsetStart, nOrderPosOffsets)
  132. {
  133. if (nOrderPos >= nOffsetStart)
  134. ++nOrderPosOff;
  135. }
  136. nOrderPos += nOrderPosOff;
  137. nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
  138. if (!nOrderPosOff)
  139. continue;
  140. // Since we're changing the order, write it back
  141. if (pwtx)
  142. {
  143. if (!WriteTx(pwtx->GetHash(), *pwtx))
  144. return DB_LOAD_FAIL;
  145. }
  146. else
  147. if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
  148. return DB_LOAD_FAIL;
  149. }
  150. }
  151. return DB_LOAD_OK;
  152. }
  153. bool
  154. ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
  155. int& nFileVersion, vector<uint256>& vWalletUpgrade,
  156. bool& fIsEncrypted, bool& fAnyUnordered, string& strType, string& strErr)
  157. {
  158. try {
  159. // Unserialize
  160. // Taking advantage of the fact that pair serialization
  161. // is just the two items serialized one after the other
  162. ssKey >> strType;
  163. if (strType == "name")
  164. {
  165. string strAddress;
  166. ssKey >> strAddress;
  167. ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()];
  168. }
  169. else if (strType == "tx")
  170. {
  171. uint256 hash;
  172. ssKey >> hash;
  173. CWalletTx& wtx = pwallet->mapWallet[hash];
  174. ssValue >> wtx;
  175. CValidationState state;
  176. if (wtx.CheckTransaction(state) && (wtx.GetHash() == hash) && state.IsValid())
  177. wtx.BindWallet(pwallet);
  178. else
  179. {
  180. pwallet->mapWallet.erase(hash);
  181. return false;
  182. }
  183. // Undo serialize changes in 31600
  184. if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
  185. {
  186. if (!ssValue.empty())
  187. {
  188. char fTmp;
  189. char fUnused;
  190. ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
  191. strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
  192. wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
  193. wtx.fTimeReceivedIsTxTime = fTmp;
  194. }
  195. else
  196. {
  197. strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
  198. wtx.fTimeReceivedIsTxTime = 0;
  199. }
  200. vWalletUpgrade.push_back(hash);
  201. }
  202. if (wtx.nOrderPos == -1)
  203. fAnyUnordered = true;
  204. //// debug print
  205. //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
  206. //printf(" %12"PRI64d" %s %s %s\n",
  207. // wtx.vout[0].nValue,
  208. // DateTimeStrFormat("%Y-%m-%d %H:%M:%S", wtx.GetBlockTime()).c_str(),
  209. // wtx.hashBlock.ToString().substr(0,20).c_str(),
  210. // wtx.mapValue["message"].c_str());
  211. }
  212. else if (strType == "acentry")
  213. {
  214. string strAccount;
  215. ssKey >> strAccount;
  216. uint64 nNumber;
  217. ssKey >> nNumber;
  218. if (nNumber > nAccountingEntryNumber)
  219. nAccountingEntryNumber = nNumber;
  220. if (!fAnyUnordered)
  221. {
  222. CAccountingEntry acentry;
  223. ssValue >> acentry;
  224. if (acentry.nOrderPos == -1)
  225. fAnyUnordered = true;
  226. }
  227. }
  228. else if (strType == "key" || strType == "wkey")
  229. {
  230. vector<unsigned char> vchPubKey;
  231. ssKey >> vchPubKey;
  232. CKey key;
  233. if (strType == "key")
  234. {
  235. CPrivKey pkey;
  236. ssValue >> pkey;
  237. key.SetPubKey(vchPubKey);
  238. if (!key.SetPrivKey(pkey))
  239. {
  240. strErr = "Error reading wallet database: CPrivKey corrupt";
  241. return false;
  242. }
  243. if (key.GetPubKey() != vchPubKey)
  244. {
  245. strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
  246. return false;
  247. }
  248. if (!key.IsValid())
  249. {
  250. strErr = "Error reading wallet database: invalid CPrivKey";
  251. return false;
  252. }
  253. }
  254. else
  255. {
  256. CWalletKey wkey;
  257. ssValue >> wkey;
  258. key.SetPubKey(vchPubKey);
  259. if (!key.SetPrivKey(wkey.vchPrivKey))
  260. {
  261. strErr = "Error reading wallet database: CPrivKey corrupt";
  262. return false;
  263. }
  264. if (key.GetPubKey() != vchPubKey)
  265. {
  266. strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
  267. return false;
  268. }
  269. if (!key.IsValid())
  270. {
  271. strErr = "Error reading wallet database: invalid CWalletKey";
  272. return false;
  273. }
  274. }
  275. if (!pwallet->LoadKey(key))
  276. {
  277. strErr = "Error reading wallet database: LoadKey failed";
  278. return false;
  279. }
  280. }
  281. else if (strType == "mkey")
  282. {
  283. unsigned int nID;
  284. ssKey >> nID;
  285. CMasterKey kMasterKey;
  286. ssValue >> kMasterKey;
  287. if(pwallet->mapMasterKeys.count(nID) != 0)
  288. {
  289. strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
  290. return false;
  291. }
  292. pwallet->mapMasterKeys[nID] = kMasterKey;
  293. if (pwallet->nMasterKeyMaxID < nID)
  294. pwallet->nMasterKeyMaxID = nID;
  295. }
  296. else if (strType == "ckey")
  297. {
  298. vector<unsigned char> vchPubKey;
  299. ssKey >> vchPubKey;
  300. vector<unsigned char> vchPrivKey;
  301. ssValue >> vchPrivKey;
  302. if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
  303. {
  304. strErr = "Error reading wallet database: LoadCryptedKey failed";
  305. return false;
  306. }
  307. fIsEncrypted = true;
  308. }
  309. else if (strType == "defaultkey")
  310. {
  311. ssValue >> pwallet->vchDefaultKey;
  312. }
  313. else if (strType == "pool")
  314. {
  315. int64 nIndex;
  316. ssKey >> nIndex;
  317. pwallet->setKeyPool.insert(nIndex);
  318. }
  319. else if (strType == "version")
  320. {
  321. ssValue >> nFileVersion;
  322. if (nFileVersion == 10300)
  323. nFileVersion = 300;
  324. }
  325. else if (strType == "cscript")
  326. {
  327. uint160 hash;
  328. ssKey >> hash;
  329. CScript script;
  330. ssValue >> script;
  331. if (!pwallet->LoadCScript(script))
  332. {
  333. strErr = "Error reading wallet database: LoadCScript failed";
  334. return false;
  335. }
  336. }
  337. else if (strType == "orderposnext")
  338. {
  339. ssValue >> pwallet->nOrderPosNext;
  340. }
  341. } catch (...)
  342. {
  343. return false;
  344. }
  345. return true;
  346. }
  347. static bool IsKeyType(string strType)
  348. {
  349. return (strType== "key" || strType == "wkey" ||
  350. strType == "mkey" || strType == "ckey");
  351. }
  352. DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
  353. {
  354. pwallet->vchDefaultKey = CPubKey();
  355. int nFileVersion = 0;
  356. vector<uint256> vWalletUpgrade;
  357. bool fIsEncrypted = false;
  358. bool fAnyUnordered = false;
  359. bool fNoncriticalErrors = false;
  360. DBErrors result = DB_LOAD_OK;
  361. try {
  362. LOCK(pwallet->cs_wallet);
  363. int nMinVersion = 0;
  364. if (Read((string)"minversion", nMinVersion))
  365. {
  366. if (nMinVersion > CLIENT_VERSION)
  367. return DB_TOO_NEW;
  368. pwallet->LoadMinVersion(nMinVersion);
  369. }
  370. // Get cursor
  371. Dbc* pcursor = GetCursor();
  372. if (!pcursor)
  373. {
  374. printf("Error getting wallet database cursor\n");
  375. return DB_CORRUPT;
  376. }
  377. loop
  378. {
  379. // Read next record
  380. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  381. CDataStream ssValue(SER_DISK, CLIENT_VERSION);
  382. int ret = ReadAtCursor(pcursor, ssKey, ssValue);
  383. if (ret == DB_NOTFOUND)
  384. break;
  385. else if (ret != 0)
  386. {
  387. printf("Error reading next record from wallet database\n");
  388. return DB_CORRUPT;
  389. }
  390. // Try to be tolerant of single corrupt records:
  391. string strType, strErr;
  392. if (!ReadKeyValue(pwallet, ssKey, ssValue, nFileVersion,
  393. vWalletUpgrade, fIsEncrypted, fAnyUnordered, strType, strErr))
  394. {
  395. // losing keys is considered a catastrophic error, anything else
  396. // we assume the user can live with:
  397. if (IsKeyType(strType))
  398. result = DB_CORRUPT;
  399. else
  400. {
  401. // Leave other errors alone, if we try to fix them we might make things worse.
  402. fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
  403. if (strType == "tx")
  404. // Rescan if there is a bad transaction record:
  405. SoftSetBoolArg("-rescan", true);
  406. }
  407. }
  408. if (!strErr.empty())
  409. printf("%s\n", strErr.c_str());
  410. }
  411. pcursor->close();
  412. }
  413. catch (...)
  414. {
  415. result = DB_CORRUPT;
  416. }
  417. if (fNoncriticalErrors && result == DB_LOAD_OK)
  418. result = DB_NONCRITICAL_ERROR;
  419. // Any wallet corruption at all: skip any rewriting or
  420. // upgrading, we don't want to make it worse.
  421. if (result != DB_LOAD_OK)
  422. return result;
  423. printf("nFileVersion = %d\n", nFileVersion);
  424. BOOST_FOREACH(uint256 hash, vWalletUpgrade)
  425. WriteTx(hash, pwallet->mapWallet[hash]);
  426. // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
  427. if (fIsEncrypted && (nFileVersion == 40000 || nFileVersion == 50000))
  428. return DB_NEED_REWRITE;
  429. if (nFileVersion < CLIENT_VERSION) // Update
  430. WriteVersion(CLIENT_VERSION);
  431. if (fAnyUnordered)
  432. result = ReorderTransactions(pwallet);
  433. return result;
  434. }
  435. void ThreadFlushWalletDB(void* parg)
  436. {
  437. // Make this thread recognisable as the wallet flushing thread
  438. RenameThread("bitcoin-wallet");
  439. const string& strFile = ((const string*)parg)[0];
  440. static bool fOneThread;
  441. if (fOneThread)
  442. return;
  443. fOneThread = true;
  444. if (!GetBoolArg("-flushwallet", true))
  445. return;
  446. unsigned int nLastSeen = nWalletDBUpdated;
  447. unsigned int nLastFlushed = nWalletDBUpdated;
  448. int64 nLastWalletUpdate = GetTime();
  449. while (!fShutdown)
  450. {
  451. Sleep(500);
  452. if (nLastSeen != nWalletDBUpdated)
  453. {
  454. nLastSeen = nWalletDBUpdated;
  455. nLastWalletUpdate = GetTime();
  456. }
  457. if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
  458. {
  459. TRY_LOCK(bitdb.cs_db,lockDb);
  460. if (lockDb)
  461. {
  462. // Don't do this if any databases are in use
  463. int nRefCount = 0;
  464. map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
  465. while (mi != bitdb.mapFileUseCount.end())
  466. {
  467. nRefCount += (*mi).second;
  468. mi++;
  469. }
  470. if (nRefCount == 0 && !fShutdown)
  471. {
  472. map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
  473. if (mi != bitdb.mapFileUseCount.end())
  474. {
  475. printf("Flushing wallet.dat\n");
  476. nLastFlushed = nWalletDBUpdated;
  477. int64 nStart = GetTimeMillis();
  478. // Flush wallet.dat so it's self contained
  479. bitdb.CloseDb(strFile);
  480. bitdb.CheckpointLSN(strFile);
  481. bitdb.mapFileUseCount.erase(mi++);
  482. printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
  483. }
  484. }
  485. }
  486. }
  487. }
  488. }
  489. bool BackupWallet(const CWallet& wallet, const string& strDest)
  490. {
  491. if (!wallet.fFileBacked)
  492. return false;
  493. while (!fShutdown)
  494. {
  495. {
  496. LOCK(bitdb.cs_db);
  497. if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
  498. {
  499. // Flush log data to the dat file
  500. bitdb.CloseDb(wallet.strWalletFile);
  501. bitdb.CheckpointLSN(wallet.strWalletFile);
  502. bitdb.mapFileUseCount.erase(wallet.strWalletFile);
  503. // Copy wallet.dat
  504. filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
  505. filesystem::path pathDest(strDest);
  506. if (filesystem::is_directory(pathDest))
  507. pathDest /= wallet.strWalletFile;
  508. try {
  509. #if BOOST_VERSION >= 104000
  510. filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
  511. #else
  512. filesystem::copy_file(pathSrc, pathDest);
  513. #endif
  514. printf("copied wallet.dat to %s\n", pathDest.string().c_str());
  515. return true;
  516. } catch(const filesystem::filesystem_error &e) {
  517. printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
  518. return false;
  519. }
  520. }
  521. }
  522. Sleep(100);
  523. }
  524. return false;
  525. }
  526. //
  527. // Try to (very carefully!) recover wallet.dat if there is a problem.
  528. //
  529. bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
  530. {
  531. // Recovery procedure:
  532. // move wallet.dat to wallet.timestamp.bak
  533. // Call Salvage with fAggressive=true to
  534. // get as much data as possible.
  535. // Rewrite salvaged data to wallet.dat
  536. // Set -rescan so any missing transactions will be
  537. // found.
  538. int64 now = GetTime();
  539. std::string newFilename = strprintf("wallet.%"PRI64d".bak", now);
  540. int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
  541. newFilename.c_str(), DB_AUTO_COMMIT);
  542. if (result == 0)
  543. printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
  544. else
  545. {
  546. printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
  547. return false;
  548. }
  549. std::vector<CDBEnv::KeyValPair> salvagedData;
  550. bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
  551. if (salvagedData.empty())
  552. {
  553. printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
  554. return false;
  555. }
  556. printf("Salvage(aggressive) found %"PRIszu" records\n", salvagedData.size());
  557. bool fSuccess = allOK;
  558. Db* pdbCopy = new Db(&dbenv.dbenv, 0);
  559. int ret = pdbCopy->open(NULL, // Txn pointer
  560. filename.c_str(), // Filename
  561. "main", // Logical db name
  562. DB_BTREE, // Database type
  563. DB_CREATE, // Flags
  564. 0);
  565. if (ret > 0)
  566. {
  567. printf("Cannot create database file %s\n", filename.c_str());
  568. return false;
  569. }
  570. CWallet dummyWallet;
  571. int nFileVersion = 0;
  572. vector<uint256> vWalletUpgrade;
  573. bool fIsEncrypted = false;
  574. bool fAnyUnordered = false;
  575. DbTxn* ptxn = dbenv.TxnBegin();
  576. BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData)
  577. {
  578. if (fOnlyKeys)
  579. {
  580. CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
  581. CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
  582. string strType, strErr;
  583. bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
  584. nFileVersion, vWalletUpgrade,
  585. fIsEncrypted, fAnyUnordered,
  586. strType, strErr);
  587. if (!IsKeyType(strType))
  588. continue;
  589. if (!fReadOK)
  590. {
  591. printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
  592. continue;
  593. }
  594. }
  595. Dbt datKey(&row.first[0], row.first.size());
  596. Dbt datValue(&row.second[0], row.second.size());
  597. int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
  598. if (ret2 > 0)
  599. fSuccess = false;
  600. }
  601. ptxn->commit(0);
  602. pdbCopy->close(0);
  603. delete pdbCopy;
  604. return fSuccess;
  605. }
  606. bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
  607. {
  608. return CWalletDB::Recover(dbenv, filename, false);
  609. }