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.
13 #include <boost/version.hpp>
14 #include <boost/filesystem.hpp>
18 static uint64_t nAccountingEntryNumber = 0;
19 extern bool fWalletUnlockMintOnly;
25 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
28 return Write(make_pair(string("name"), strAddress), strName);
31 bool CWalletDB::EraseName(const string& strAddress)
33 // This should only be used for sending addresses, never for receiving addresses,
34 // receiving addresses must always have an address book entry if they're not change return.
36 return Erase(make_pair(string("name"), strAddress));
39 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
42 return Read(make_pair(string("acc"), strAccount), account);
45 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
47 return Write(make_pair(string("acc"), strAccount), account);
50 bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
52 return Write(make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
55 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
57 return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
60 int64_t CWalletDB::GetAccountCreditDebit(const string& strAccount)
62 list<CAccountingEntry> entries;
63 ListAccountCreditDebit(strAccount, entries);
65 int64_t nCreditDebit = 0;
66 for(const auto& entry : entries)
67 nCreditDebit += entry.nCreditDebit;
72 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
74 bool fAllAccounts = (strAccount == "*");
76 Dbc* pcursor = GetCursor();
78 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
79 unsigned int fFlags = DB_SET_RANGE;
83 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
84 if (fFlags == DB_SET_RANGE)
85 ssKey << make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64_t(0));
86 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
87 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
89 if (ret == DB_NOTFOUND)
94 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
100 if (strType != "acentry")
102 CAccountingEntry acentry;
103 ssKey >> acentry.strAccount;
104 if (!fAllAccounts && acentry.strAccount != strAccount)
108 ssKey >> acentry.nEntryNo;
109 entries.push_back(acentry);
117 CWalletDB::ReorderTransactions(CWallet* pwallet)
119 LOCK(pwallet->cs_wallet);
120 // Old wallets didn't have any defined order for transactions
121 // Probably a bad idea to change the output of this
123 // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
124 typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
125 typedef multimap<int64_t, TxPair > TxItems;
128 for (auto it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
130 CWalletTx* wtx = &((*it).second);
131 txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
133 list<CAccountingEntry> acentries;
134 ListAccountCreditDebit("", acentries);
135 for(auto& entry : acentries)
137 txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
140 int64_t& nOrderPosNext = pwallet->nOrderPosNext;
142 vector<int64_t> nOrderPosOffsets;
143 for (auto it = txByTime.begin(); it != txByTime.end(); ++it)
145 CWalletTx *const pwtx = (*it).second.first;
146 CAccountingEntry *const pacentry = (*it).second.second;
147 int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
151 nOrderPos = nOrderPosNext++;
152 nOrderPosOffsets.push_back(nOrderPos);
155 // Have to write accounting regardless, since we don't keep it in memory
156 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
161 int64_t nOrderPosOff = 0;
162 for(const int64_t& nOffsetStart : nOrderPosOffsets)
164 if (nOrderPos >= nOffsetStart)
167 nOrderPos += nOrderPosOff;
168 nOrderPosNext = max(nOrderPosNext, nOrderPos + 1);
173 // Since we're changing the order, write it back
176 if (!WriteTx(pwtx->GetHash(), *pwtx))
180 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
188 class CWalletScanState {
192 unsigned int nKeyMeta;
196 vector<uint256> vWalletUpgrade;
199 nKeys = nCKeys = nKeyMeta = 0;
200 fIsEncrypted = false;
201 fAnyUnordered = false;
207 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
208 CWalletScanState &wss, string& strType, string& strErr)
212 // Taking advantage of the fact that pair serialization
213 // is just the two items serialized one after the other
216 if (strType == "name")
220 ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress)];
222 else if (strType == "tx")
226 CWalletTx& wtx = pwallet->mapWallet[hash];
228 if (wtx.CheckTransaction() && (wtx.GetHash() == hash))
229 wtx.BindWallet(pwallet);
232 pwallet->mapWallet.erase(hash);
236 // Undo serialize changes in 31600
237 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
239 if (!ssValue.empty())
243 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
244 strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
245 wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
246 wtx.fTimeReceivedIsTxTime = fTmp;
250 strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
251 wtx.fTimeReceivedIsTxTime = 0;
253 wss.vWalletUpgrade.push_back(hash);
256 if (wtx.nOrderPos == -1)
257 wss.fAnyUnordered = true;
260 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
261 //printf(" %12"PRId64" %s %s %s\n",
262 // wtx.vout[0].nValue,
263 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
264 // wtx.hashBlock.ToString().substr(0,20).c_str(),
265 // wtx.mapValue["message"].c_str());
267 else if (strType == "acentry")
273 if (nNumber > nAccountingEntryNumber)
274 nAccountingEntryNumber = nNumber;
276 if (!wss.fAnyUnordered)
278 CAccountingEntry acentry;
280 if (acentry.nOrderPos == -1)
281 wss.fAnyUnordered = true;
284 else if (strType == "watchs")
291 pwallet->LoadWatchOnly(script);
293 // Watch-only addresses have no birthday information for now,
294 // so set the wallet birthday to the beginning of time.
295 pwallet->nTimeFirstKey = 1;
297 else if (strType == "malpair")
303 ssValue >> vchSecret;
305 CMalleableKeyView keyView(strKeyView);
306 if (!pwallet->LoadKey(keyView, vchSecret))
308 strErr = "Error reading wallet database: LoadKey failed";
312 else if (strType == "malcpair")
316 vector<unsigned char> vchCryptedSecret;
318 ssValue >> vchCryptedSecret;
320 CMalleableKeyView keyView(strKeyView);
321 if (!pwallet->LoadCryptedKey(keyView, vchCryptedSecret))
323 strErr = "Error reading wallet database: LoadCryptedKey failed";
327 else if (strType == "key" || strType == "wkey")
332 if (strType == "key")
337 if (!key.SetPrivKey(pkey))
339 strErr = "Error reading wallet database: CPrivKey corrupt";
342 if (key.GetPubKey() != vchPubKey)
344 strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
347 key.SetCompressedPubKey(vchPubKey.IsCompressed());
350 strErr = "Error reading wallet database: invalid CPrivKey";
358 if (!key.SetPrivKey(wkey.vchPrivKey))
360 strErr = "Error reading wallet database: CPrivKey corrupt";
363 if (key.GetPubKey() != vchPubKey)
365 strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
368 key.SetCompressedPubKey(vchPubKey.IsCompressed());
371 strErr = "Error reading wallet database: invalid CWalletKey";
375 if (!pwallet->LoadKey(key))
377 strErr = "Error reading wallet database: LoadKey failed";
381 else if (strType == "mkey")
385 CMasterKey kMasterKey;
386 ssValue >> kMasterKey;
388 if(pwallet->mapMasterKeys.count(nID) != 0)
390 strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
393 pwallet->mapMasterKeys[nID] = kMasterKey;
394 if (pwallet->nMasterKeyMaxID < nID)
395 pwallet->nMasterKeyMaxID = nID;
397 else if (strType == "ckey")
402 vector<unsigned char> vchPrivKey;
403 ssValue >> vchPrivKey;
404 if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
406 strErr = "Error reading wallet database: LoadCryptedKey failed";
409 wss.fIsEncrypted = true;
411 else if (strType == "malmeta")
416 CMalleableKeyView keyView;
417 keyView.SetString(strKeyView);
419 CKeyMetadata keyMeta;
423 pwallet->LoadKeyMetadata(keyView, keyMeta);
425 else if (strType == "keymeta")
429 CKeyMetadata keyMeta;
433 pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
435 // find earliest key creation time, as wallet birthday
436 if (!pwallet->nTimeFirstKey ||
437 (keyMeta.nCreateTime < pwallet->nTimeFirstKey))
438 pwallet->nTimeFirstKey = keyMeta.nCreateTime;
440 else if (strType == "defaultkey")
442 ssValue >> pwallet->vchDefaultKey;
444 else if (strType == "pool")
450 pwallet->setKeyPool.insert(nIndex);
452 // If no metadata exists yet, create a default with the pool key's
453 // creation time. Note that this may be overwritten by actually
454 // stored metadata for that key later, which is fine.
455 CBitcoinAddress addr = CBitcoinAddress(keypool.vchPubKey.GetID());
456 if (pwallet->mapKeyMetadata.count(addr) == 0)
457 pwallet->mapKeyMetadata[addr] = CKeyMetadata(keypool.nTime);
460 else if (strType == "version")
462 ssValue >> wss.nFileVersion;
463 if (wss.nFileVersion == 10300)
464 wss.nFileVersion = 300;
466 else if (strType == "cscript")
472 if (!pwallet->LoadCScript(script))
474 strErr = "Error reading wallet database: LoadCScript failed";
478 else if (strType == "orderposnext")
480 ssValue >> pwallet->nOrderPosNext;
489 static bool IsKeyType(string strType)
491 return (strType== "key" || strType == "wkey" ||
492 strType == "mkey" || strType == "ckey" || strType == "malpair" || strType == "malcpair");
495 DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
497 pwallet->vchDefaultKey = CPubKey();
498 CWalletScanState wss;
499 bool fNoncriticalErrors = false;
500 DBErrors result = DB_LOAD_OK;
503 LOCK(pwallet->cs_wallet);
505 if (Read((string)"minversion", nMinVersion))
507 if (nMinVersion > CLIENT_VERSION)
509 pwallet->LoadMinVersion(nMinVersion);
513 Dbc* pcursor = GetCursor();
516 printf("Error getting wallet database cursor\n");
523 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
524 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
525 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
526 if (ret == DB_NOTFOUND)
530 printf("Error reading next record from wallet database\n");
534 // Try to be tolerant of single corrupt records:
535 string strType, strErr;
536 if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
538 // losing keys is considered a catastrophic error, anything else
539 // we assume the user can live with:
540 if (IsKeyType(strType))
544 // Leave other errors alone, if we try to fix them we might make things worse.
545 fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
547 // Rescan if there is a bad transaction record:
548 SoftSetBoolArg("-rescan", true);
552 printf("%s\n", strErr.c_str());
561 if (fNoncriticalErrors && result == DB_LOAD_OK)
562 result = DB_NONCRITICAL_ERROR;
564 // Any wallet corruption at all: skip any rewriting or
565 // upgrading, we don't want to make it worse.
566 if (result != DB_LOAD_OK)
569 printf("nFileVersion = %d\n", wss.nFileVersion);
571 printf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total\n",
572 wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys);
574 // nTimeFirstKey is only reliable if all keys have metadata
575 if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
576 pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
579 for(uint256 hash : wss.vWalletUpgrade)
580 WriteTx(hash, pwallet->mapWallet[hash]);
582 // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
583 if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
584 return DB_NEED_REWRITE;
586 if (wss.nFileVersion < CLIENT_VERSION) // Update
587 WriteVersion(CLIENT_VERSION);
589 if (wss.fAnyUnordered)
590 result = ReorderTransactions(pwallet);
595 DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash)
597 pwallet->vchDefaultKey = CPubKey();
598 CWalletScanState wss;
599 bool fNoncriticalErrors = false;
600 DBErrors result = DB_LOAD_OK;
603 LOCK(pwallet->cs_wallet);
605 if (Read((string)"minversion", nMinVersion))
607 if (nMinVersion > CLIENT_VERSION)
609 pwallet->LoadMinVersion(nMinVersion);
613 Dbc* pcursor = GetCursor();
616 printf("Error getting wallet database cursor\n");
623 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
624 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
625 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
626 if (ret == DB_NOTFOUND)
630 printf("Error reading next record from wallet database\n");
636 if (strType == "tx") {
640 vTxHash.push_back(hash);
645 catch (const boost::thread_interrupted&) {
652 if (fNoncriticalErrors && result == DB_LOAD_OK)
653 result = DB_NONCRITICAL_ERROR;
658 DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet)
660 // build list of wallet TXs
661 vector<uint256> vTxHash;
662 DBErrors err = FindWalletTx(pwallet, vTxHash);
663 if (err != DB_LOAD_OK)
666 // erase each wallet TX
667 for(uint256& hash : vTxHash) {
675 void ThreadFlushWalletDB(void* parg)
677 // Make this thread recognisable as the wallet flushing thread
678 RenameThread("novacoin-wallet");
680 const string& strFile = ((const string*)parg)[0];
681 static bool fOneThread;
685 if (!GetBoolArg("-flushwallet", true))
688 unsigned int nLastSeen = nWalletDBUpdated;
689 unsigned int nLastFlushed = nWalletDBUpdated;
690 int64_t nLastWalletUpdate = GetTime();
695 if (nLastSeen != nWalletDBUpdated)
697 nLastSeen = nWalletDBUpdated;
698 nLastWalletUpdate = GetTime();
701 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
703 TRY_LOCK(bitdb.cs_db,lockDb);
706 // Don't do this if any databases are in use
708 auto mi = bitdb.mapFileUseCount.begin();
709 while (mi != bitdb.mapFileUseCount.end())
711 nRefCount += (*mi).second;
715 if (nRefCount == 0 && !fShutdown)
717 auto mi = bitdb.mapFileUseCount.find(strFile);
718 if (mi != bitdb.mapFileUseCount.end())
720 printf("Flushing wallet.dat\n");
721 nLastFlushed = nWalletDBUpdated;
722 int64_t nStart = GetTimeMillis();
724 // Flush wallet.dat so it's self contained
725 bitdb.CloseDb(strFile);
726 bitdb.CheckpointLSN(strFile);
728 bitdb.mapFileUseCount.erase(mi++);
729 printf("Flushed wallet.dat %" PRId64 "ms\n", GetTimeMillis() - nStart);
737 bool BackupWallet(const CWallet& wallet, const string& strDest)
739 if (!wallet.fFileBacked)
745 if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
747 // Flush log data to the dat file
748 bitdb.CloseDb(wallet.strWalletFile);
749 bitdb.CheckpointLSN(wallet.strWalletFile);
750 bitdb.mapFileUseCount.erase(wallet.strWalletFile);
753 boost::filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
754 boost::filesystem::path pathDest(strDest);
755 if (boost::filesystem::is_directory(pathDest))
756 pathDest /= wallet.strWalletFile;
759 #if BOOST_VERSION >= 104000
760 boost::filesystem::copy_file(pathSrc, pathDest, boost::filesystem::copy_option::overwrite_if_exists);
762 boost::filesystem::copy_file(pathSrc, pathDest);
764 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
766 } catch(const boost::filesystem::filesystem_error &e) {
767 printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
777 bool DumpWallet(CWallet* pwallet, const string& strDest)
779 if (!pwallet->fFileBacked)
782 map<CBitcoinAddress, int64_t> mapAddresses;
783 set<CKeyID> setKeyPool;
785 pwallet->GetAddresses(mapAddresses);
786 pwallet->GetAllReserveKeys(setKeyPool);
788 // sort time/key pairs
789 vector<pair<int64_t, CBitcoinAddress> > vAddresses;
790 for (auto it = mapAddresses.begin(); it != mapAddresses.end(); it++) {
791 vAddresses.push_back({ it->second, it->first });
793 mapAddresses.clear();
794 sort(vAddresses.begin(), vAddresses.end());
796 // open outputfile as a stream
798 file.open(strDest.c_str());
803 file << strprintf("# Wallet dump created by NovaCoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
804 file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
805 file << strprintf("# * Best block at time of backup was %i (%s),\n", nBestHeight, hashBestChain.ToString().c_str());
806 file << strprintf("# mined on %s\n", EncodeDumpTime(pindexBest->nTime).c_str());
809 for (const auto& addrItem : vAddresses) {
810 const auto &addr = addrItem.second;
811 const auto strTime = EncodeDumpTime(addrItem.first);
812 const auto strAddr = addr.ToString();
815 // Pubkey pair address
816 CMalleableKeyView keyView;
817 CMalleablePubKey mPubKey(addr.GetData());
818 if (!pwallet->GetMalleableView(mPubKey, keyView))
821 pwallet->GetMalleableKey(keyView, mKey);
822 file << mKey.ToString();
823 if (pwallet->mapAddressBook.count(addr))
824 file << strprintf(" %s label=%s # view=%s addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), keyView.ToString().c_str(), strAddr.c_str());
826 file << strprintf(" %s # view=%s addr=%s\n", strTime.c_str(), keyView.ToString().c_str(), strAddr.c_str());
829 // Pubkey hash address
831 addr.GetKeyID(keyid);
834 if (!pwallet->GetKey(keyid, key))
836 auto secret = key.GetSecret(IsCompressed);
837 file << CBitcoinSecret(secret, IsCompressed).ToString();
838 if (pwallet->mapAddressBook.count(addr))
839 file << strprintf(" %s label=%s # addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), strAddr.c_str());
840 else if (setKeyPool.count(keyid))
841 file << strprintf(" %s reserve=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
843 file << strprintf(" %s change=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
848 file << "# End of dump\n";
854 bool ImportWallet(CWallet *pwallet, const string& strLocation)
857 if (!pwallet->fFileBacked)
860 // open inputfile as stream
862 file.open(strLocation.c_str());
867 int64_t nTimeBegin = pindexBest->nTime;
869 // read through input file checking and importing keys into wallet.
870 while (file.good()) {
873 if (line.empty() || line[0] == '#')
874 continue; // Skip comments and empty lines
877 istringstream iss(line);
878 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(vstr));
882 int64_t nTime = DecodeDumpTime(vstr[1]);
885 for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
886 if (vstr[nStr].compare(0,1, "#") == 0)
888 if (vstr[nStr] == "change=1")
890 if (vstr[nStr] == "reserve=1")
892 if (vstr[nStr].compare(0,6, "label=") == 0) {
893 strLabel = DecodeDumpString(vstr[nStr].substr(6));
898 CBitcoinAddress addr;
899 CBitcoinSecret vchSecret;
900 if (vchSecret.SetString(vstr[0])) {
901 // Simple private key
905 auto secret = vchSecret.GetSecret(fCompressed);
906 key.SetSecret(secret, fCompressed);
907 auto keyid = key.GetPubKey().GetID();
908 addr = CBitcoinAddress(keyid);
910 if (pwallet->HaveKey(keyid)) {
911 printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
915 printf("Importing %s...\n", addr.ToString().c_str());
916 if (!pwallet->AddKey(key)) {
921 // A pair of private keys
924 if (!mKey.SetString(vstr[0]))
926 auto mPubKey = mKey.GetMalleablePubKey();
927 addr = CBitcoinAddress(mPubKey);
929 if (pwallet->CheckOwnership(mPubKey)) {
930 printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
934 printf("Importing %s...\n", addr.ToString().c_str());
935 if (!pwallet->AddKey(mKey)) {
941 pwallet->mapKeyMetadata[addr].nCreateTime = nTime;
943 pwallet->SetAddressBookName(addr, strLabel);
945 nTimeBegin = min(nTimeBegin, nTime);
949 // rescan block chain looking for coins from new keys
950 CBlockIndex *pindex = pindexBest;
951 while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200)
952 pindex = pindex->pprev;
954 printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1);
955 pwallet->ScanForWalletTransactions(pindex);
956 pwallet->ReacceptWalletTransactions();
957 pwallet->MarkDirty();
963 // Try to (very carefully!) recover wallet.dat if there is a problem.
965 bool CWalletDB::Recover(CDBEnv& dbenv, string filename, bool fOnlyKeys)
967 // Recovery procedure:
968 // move wallet.dat to wallet.timestamp.bak
969 // Call Salvage with fAggressive=true to
970 // get as much data as possible.
971 // Rewrite salvaged data to wallet.dat
972 // Set -rescan so any missing transactions will be
974 int64_t now = GetTime();
975 string newFilename = strprintf("wallet.%" PRId64 ".bak", now);
977 int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
978 newFilename.c_str(), DB_AUTO_COMMIT);
980 printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
983 printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
987 vector<CDBEnv::KeyValPair> salvagedData;
988 bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
989 if (salvagedData.empty())
991 printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
994 printf("Salvage(aggressive) found %" PRIszu " records\n", salvagedData.size());
996 bool fSuccess = allOK;
997 unique_ptr<Db> pdbCopy(new Db(&dbenv.dbenv, 0));
998 int ret = pdbCopy->open(NULL, // Txn pointer
999 filename.c_str(), // Filename
1000 "main", // Logical db name
1001 DB_BTREE, // Database type
1006 printf("Cannot create database file %s\n", filename.c_str());
1009 CWallet dummyWallet;
1010 CWalletScanState wss;
1012 DbTxn* ptxn = dbenv.TxnBegin();
1013 for(CDBEnv::KeyValPair& row : salvagedData)
1017 CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
1018 CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
1019 string strType, strErr;
1020 bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
1021 wss, strType, strErr);
1022 if (!IsKeyType(strType))
1026 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
1030 Dbt datKey(&row.first[0], row.first.size());
1031 Dbt datValue(&row.second[0], row.second.size());
1032 int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
1042 bool CWalletDB::Recover(CDBEnv& dbenv, string filename)
1044 return CWalletDB::Recover(dbenv, filename, false);