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.
10 #include <boost/version.hpp>
11 #include <boost/filesystem.hpp>
19 static uint64_t nAccountingEntryNumber = 0;
20 extern bool fWalletUnlockMintOnly;
26 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
29 return Write(make_pair(string("name"), strAddress), strName);
32 bool CWalletDB::EraseName(const string& strAddress)
34 // This should only be used for sending addresses, never for receiving addresses,
35 // receiving addresses must always have an address book entry if they're not change return.
37 return Erase(make_pair(string("name"), strAddress));
40 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
43 return Read(make_pair(string("acc"), strAccount), account);
46 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
48 return Write(make_pair(string("acc"), strAccount), account);
51 bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
53 return Write(std::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
56 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
58 return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
61 int64_t CWalletDB::GetAccountCreditDebit(const string& strAccount)
63 list<CAccountingEntry> entries;
64 ListAccountCreditDebit(strAccount, entries);
66 int64_t nCreditDebit = 0;
67 for (const CAccountingEntry& entry : entries)
68 nCreditDebit += entry.nCreditDebit;
73 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
75 bool fAllAccounts = (strAccount == "*");
77 Dbc* pcursor = GetCursor();
79 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
80 unsigned int fFlags = DB_SET_RANGE;
84 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
85 if (fFlags == DB_SET_RANGE)
86 ssKey << std::make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64_t(0));
87 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
88 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
90 if (ret == DB_NOTFOUND)
95 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
101 if (strType != "acentry")
103 CAccountingEntry acentry;
104 ssKey >> acentry.strAccount;
105 if (!fAllAccounts && acentry.strAccount != strAccount)
109 ssKey >> acentry.nEntryNo;
110 entries.push_back(acentry);
118 CWalletDB::ReorderTransactions(CWallet* pwallet)
120 LOCK(pwallet->cs_wallet);
121 // Old wallets didn't have any defined order for transactions
122 // Probably a bad idea to change the output of this
124 // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
125 typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
126 typedef multimap<int64_t, TxPair > TxItems;
129 for (map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
131 CWalletTx* wtx = &((*it).second);
132 txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
134 list<CAccountingEntry> acentries;
135 ListAccountCreditDebit("", acentries);
136 for (CAccountingEntry& entry : acentries)
138 txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
141 int64_t& nOrderPosNext = pwallet->nOrderPosNext;
143 std::vector<int64_t> nOrderPosOffsets;
144 for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
146 CWalletTx *const pwtx = (*it).second.first;
147 CAccountingEntry *const pacentry = (*it).second.second;
148 int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
152 nOrderPos = nOrderPosNext++;
153 nOrderPosOffsets.push_back(nOrderPos);
156 // Have to write accounting regardless, since we don't keep it in memory
157 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
162 int64_t nOrderPosOff = 0;
163 for (const int64_t& nOffsetStart : nOrderPosOffsets)
165 if (nOrderPos >= nOffsetStart)
168 nOrderPos += nOrderPosOff;
169 nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
174 // Since we're changing the order, write it back
177 if (!WriteTx(pwtx->GetHash(), *pwtx))
181 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
189 class CWalletScanState {
193 unsigned int nKeyMeta;
197 vector<uint256> vWalletUpgrade;
200 nKeys = nCKeys = nKeyMeta = 0;
201 fIsEncrypted = false;
202 fAnyUnordered = false;
208 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
209 CWalletScanState &wss, string& strType, string& strErr)
213 // Taking advantage of the fact that pair serialization
214 // is just the two items serialized one after the other
217 if (strType == "name")
221 ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress)];
223 else if (strType == "tx")
227 CWalletTx& wtx = pwallet->mapWallet[hash];
229 if (wtx.CheckTransaction() && (wtx.GetHash() == hash))
230 wtx.BindWallet(pwallet);
233 pwallet->mapWallet.erase(hash);
237 // Undo serialize changes in 31600
238 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
240 if (!ssValue.empty())
244 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
245 strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
246 wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
247 wtx.fTimeReceivedIsTxTime = fTmp;
251 strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
252 wtx.fTimeReceivedIsTxTime = 0;
254 wss.vWalletUpgrade.push_back(hash);
257 if (wtx.nOrderPos == -1)
258 wss.fAnyUnordered = true;
261 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
262 //printf(" %12"PRId64" %s %s %s\n",
263 // wtx.vout[0].nValue,
264 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
265 // wtx.hashBlock.ToString().substr(0,20).c_str(),
266 // wtx.mapValue["message"].c_str());
268 else if (strType == "acentry")
274 if (nNumber > nAccountingEntryNumber)
275 nAccountingEntryNumber = nNumber;
277 if (!wss.fAnyUnordered)
279 CAccountingEntry acentry;
281 if (acentry.nOrderPos == -1)
282 wss.fAnyUnordered = true;
285 else if (strType == "watchs")
292 pwallet->LoadWatchOnly(script);
294 // Watch-only addresses have no birthday information for now,
295 // so set the wallet birthday to the beginning of time.
296 pwallet->nTimeFirstKey = 1;
298 else if (strType == "malpair")
304 ssValue >> vchSecret;
306 CMalleableKeyView keyView(strKeyView);
307 if (!pwallet->LoadKey(keyView, vchSecret))
309 strErr = "Error reading wallet database: LoadKey failed";
313 else if (strType == "malcpair")
317 std::vector<unsigned char> vchCryptedSecret;
319 ssValue >> vchCryptedSecret;
321 CMalleableKeyView keyView(strKeyView);
322 if (!pwallet->LoadCryptedKey(keyView, vchCryptedSecret))
324 strErr = "Error reading wallet database: LoadCryptedKey failed";
328 else if (strType == "key" || strType == "wkey")
333 if (strType == "key")
338 if (!key.SetPrivKey(pkey))
340 strErr = "Error reading wallet database: CPrivKey corrupt";
343 if (key.GetPubKey() != vchPubKey)
345 strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
348 key.SetCompressedPubKey(vchPubKey.IsCompressed());
351 strErr = "Error reading wallet database: invalid CPrivKey";
359 if (!key.SetPrivKey(wkey.vchPrivKey))
361 strErr = "Error reading wallet database: CPrivKey corrupt";
364 if (key.GetPubKey() != vchPubKey)
366 strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
369 key.SetCompressedPubKey(vchPubKey.IsCompressed());
372 strErr = "Error reading wallet database: invalid CWalletKey";
376 if (!pwallet->LoadKey(key))
378 strErr = "Error reading wallet database: LoadKey failed";
382 else if (strType == "mkey")
386 CMasterKey kMasterKey;
387 ssValue >> kMasterKey;
389 if(pwallet->mapMasterKeys.count(nID) != 0)
391 strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
394 pwallet->mapMasterKeys[nID] = kMasterKey;
395 if (pwallet->nMasterKeyMaxID < nID)
396 pwallet->nMasterKeyMaxID = nID;
398 else if (strType == "ckey")
403 vector<unsigned char> vchPrivKey;
404 ssValue >> vchPrivKey;
405 if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
407 strErr = "Error reading wallet database: LoadCryptedKey failed";
410 wss.fIsEncrypted = true;
412 else if (strType == "malmeta")
417 CMalleableKeyView keyView;
418 keyView.SetString(strKeyView);
420 CKeyMetadata keyMeta;
424 pwallet->LoadKeyMetadata(keyView, keyMeta);
426 else if (strType == "keymeta")
430 CKeyMetadata keyMeta;
434 pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
436 // find earliest key creation time, as wallet birthday
437 if (!pwallet->nTimeFirstKey ||
438 (keyMeta.nCreateTime < pwallet->nTimeFirstKey))
439 pwallet->nTimeFirstKey = keyMeta.nCreateTime;
441 else if (strType == "defaultkey")
443 ssValue >> pwallet->vchDefaultKey;
445 else if (strType == "pool")
451 pwallet->setKeyPool.insert(nIndex);
453 // If no metadata exists yet, create a default with the pool key's
454 // creation time. Note that this may be overwritten by actually
455 // stored metadata for that key later, which is fine.
456 CBitcoinAddress addr = CBitcoinAddress(keypool.vchPubKey.GetID());
457 if (pwallet->mapKeyMetadata.count(addr) == 0)
458 pwallet->mapKeyMetadata[addr] = CKeyMetadata(keypool.nTime);
461 else if (strType == "version")
463 ssValue >> wss.nFileVersion;
464 if (wss.nFileVersion == 10300)
465 wss.nFileVersion = 300;
467 else if (strType == "cscript")
473 if (!pwallet->LoadCScript(script))
475 strErr = "Error reading wallet database: LoadCScript failed";
479 else if (strType == "orderposnext")
481 ssValue >> pwallet->nOrderPosNext;
490 static bool IsKeyType(string strType)
492 return (strType== "key" || strType == "wkey" ||
493 strType == "mkey" || strType == "ckey" || strType == "malpair" || strType == "malcpair");
496 DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
498 pwallet->vchDefaultKey = CPubKey();
499 CWalletScanState wss;
500 bool fNoncriticalErrors = false;
501 DBErrors result = DB_LOAD_OK;
504 LOCK(pwallet->cs_wallet);
506 if (Read((string)"minversion", nMinVersion))
508 if (nMinVersion > CLIENT_VERSION)
510 pwallet->LoadMinVersion(nMinVersion);
514 Dbc* pcursor = GetCursor();
517 printf("Error getting wallet database cursor\n");
524 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
525 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
526 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
527 if (ret == DB_NOTFOUND)
531 printf("Error reading next record from wallet database\n");
535 // Try to be tolerant of single corrupt records:
536 string strType, strErr;
537 if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
539 // losing keys is considered a catastrophic error, anything else
540 // we assume the user can live with:
541 if (IsKeyType(strType))
545 // Leave other errors alone, if we try to fix them we might make things worse.
546 fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
548 // Rescan if there is a bad transaction record:
549 SoftSetBoolArg("-rescan", true);
553 printf("%s\n", strErr.c_str());
562 if (fNoncriticalErrors && result == DB_LOAD_OK)
563 result = DB_NONCRITICAL_ERROR;
565 // Any wallet corruption at all: skip any rewriting or
566 // upgrading, we don't want to make it worse.
567 if (result != DB_LOAD_OK)
570 printf("nFileVersion = %d\n", wss.nFileVersion);
572 printf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total\n",
573 wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys);
575 // nTimeFirstKey is only reliable if all keys have metadata
576 if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
577 pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
580 for (uint256 hash : wss.vWalletUpgrade)
581 WriteTx(hash, pwallet->mapWallet[hash]);
583 // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
584 if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
585 return DB_NEED_REWRITE;
587 if (wss.nFileVersion < CLIENT_VERSION) // Update
588 WriteVersion(CLIENT_VERSION);
590 if (wss.fAnyUnordered)
591 result = ReorderTransactions(pwallet);
596 DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash)
598 pwallet->vchDefaultKey = CPubKey();
599 CWalletScanState wss;
600 bool fNoncriticalErrors = false;
601 DBErrors result = DB_LOAD_OK;
604 LOCK(pwallet->cs_wallet);
606 if (Read((string)"minversion", nMinVersion))
608 if (nMinVersion > CLIENT_VERSION)
610 pwallet->LoadMinVersion(nMinVersion);
614 Dbc* pcursor = GetCursor();
617 printf("Error getting wallet database cursor\n");
624 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
625 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
626 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
627 if (ret == DB_NOTFOUND)
631 printf("Error reading next record from wallet database\n");
637 if (strType == "tx") {
641 vTxHash.push_back(hash);
646 catch (const boost::thread_interrupted&) {
653 if (fNoncriticalErrors && result == DB_LOAD_OK)
654 result = DB_NONCRITICAL_ERROR;
659 DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet)
661 // build list of wallet TXs
662 vector<uint256> vTxHash;
663 DBErrors err = FindWalletTx(pwallet, vTxHash);
664 if (err != DB_LOAD_OK)
667 // erase each wallet TX
668 for (uint256& hash : vTxHash) {
676 void ThreadFlushWalletDB(void* parg)
678 // Make this thread recognisable as the wallet flushing thread
679 RenameThread("novacoin-wallet");
681 const string& strFile = ((const string*)parg)[0];
682 static bool fOneThread;
686 if (!GetBoolArg("-flushwallet", true))
689 unsigned int nLastSeen = nWalletDBUpdated;
690 unsigned int nLastFlushed = nWalletDBUpdated;
691 int64_t nLastWalletUpdate = GetTime();
696 if (nLastSeen != nWalletDBUpdated)
698 nLastSeen = nWalletDBUpdated;
699 nLastWalletUpdate = GetTime();
702 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
704 TRY_LOCK(bitdb.cs_db,lockDb);
707 // Don't do this if any databases are in use
709 map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
710 while (mi != bitdb.mapFileUseCount.end())
712 nRefCount += (*mi).second;
716 if (nRefCount == 0 && !fShutdown)
718 map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
719 if (mi != bitdb.mapFileUseCount.end())
721 printf("Flushing wallet.dat\n");
722 nLastFlushed = nWalletDBUpdated;
723 int64_t nStart = GetTimeMillis();
725 // Flush wallet.dat so it's self contained
726 bitdb.CloseDb(strFile);
727 bitdb.CheckpointLSN(strFile);
729 bitdb.mapFileUseCount.erase(mi++);
730 printf("Flushed wallet.dat %" PRId64 "ms\n", GetTimeMillis() - nStart);
738 bool BackupWallet(const CWallet& wallet, const string& strDest)
740 if (!wallet.fFileBacked)
746 if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
748 // Flush log data to the dat file
749 bitdb.CloseDb(wallet.strWalletFile);
750 bitdb.CheckpointLSN(wallet.strWalletFile);
751 bitdb.mapFileUseCount.erase(wallet.strWalletFile);
754 boost::filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
755 boost::filesystem::path pathDest(strDest);
756 if (boost::filesystem::is_directory(pathDest))
757 pathDest /= wallet.strWalletFile;
760 #if BOOST_VERSION >= 104000
761 boost::filesystem::copy_file(pathSrc, pathDest, boost::filesystem::copy_option::overwrite_if_exists);
763 boost::filesystem::copy_file(pathSrc, pathDest);
765 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
767 } catch(const boost::filesystem::filesystem_error &e) {
768 printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
778 bool DumpWallet(CWallet* pwallet, const string& strDest)
780 if (!pwallet->fFileBacked)
783 std::map<CBitcoinAddress, int64_t> mapAddresses;
784 std::set<CKeyID> setKeyPool;
786 pwallet->GetAddresses(mapAddresses);
787 pwallet->GetAllReserveKeys(setKeyPool);
789 // sort time/key pairs
790 std::vector<std::pair<int64_t, CBitcoinAddress> > vAddresses;
791 for (std::map<CBitcoinAddress, int64_t>::const_iterator it = mapAddresses.begin(); it != mapAddresses.end(); it++) {
792 vAddresses.push_back(std::make_pair(it->second, it->first));
794 mapAddresses.clear();
795 std::sort(vAddresses.begin(), vAddresses.end());
797 // open outputfile as a stream
799 file.open(strDest.c_str());
804 file << strprintf("# Wallet dump created by NovaCoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
805 file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
806 file << strprintf("# * Best block at time of backup was %i (%s),\n", nBestHeight, hashBestChain.ToString().c_str());
807 file << strprintf("# mined on %s\n", EncodeDumpTime(pindexBest->nTime).c_str());
810 for (std::vector<std::pair<int64_t, CBitcoinAddress> >::const_iterator it = vAddresses.begin(); it != vAddresses.end(); it++) {
811 const CBitcoinAddress &addr = it->second;
812 std::string strTime = EncodeDumpTime(it->first);
813 std::string strAddr = addr.ToString();
816 // Pubkey pair address
817 CMalleableKeyView keyView;
818 CMalleablePubKey mPubKey(addr.GetData());
819 if (!pwallet->GetMalleableView(mPubKey, keyView))
822 pwallet->GetMalleableKey(keyView, mKey);
823 file << mKey.ToString();
824 if (pwallet->mapAddressBook.count(addr))
825 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());
827 file << strprintf(" %s # view=%s addr=%s\n", strTime.c_str(), keyView.ToString().c_str(), strAddr.c_str());
830 // Pubkey hash address
832 addr.GetKeyID(keyid);
835 if (!pwallet->GetKey(keyid, key))
837 CSecret secret = key.GetSecret(IsCompressed);
838 file << CBitcoinSecret(secret, IsCompressed).ToString();
839 if (pwallet->mapAddressBook.count(addr))
840 file << strprintf(" %s label=%s # addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), strAddr.c_str());
841 else if (setKeyPool.count(keyid))
842 file << strprintf(" %s reserve=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
844 file << strprintf(" %s change=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
849 file << "# End of dump\n";
855 bool ImportWallet(CWallet *pwallet, const string& strLocation)
858 if (!pwallet->fFileBacked)
861 // open inputfile as stream
863 file.open(strLocation.c_str());
868 int64_t nTimeBegin = pindexBest->nTime;
870 // read through input file checking and importing keys into wallet.
871 while (file.good()) {
873 std::getline(file, line);
874 if (line.empty() || line[0] == '#')
875 continue; // Skip comments and empty lines
877 std::vector<std::string> vstr;
878 istringstream iss(line);
879 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(vstr));
883 int64_t nTime = DecodeDumpTime(vstr[1]);
884 std::string strLabel;
886 for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
887 if (vstr[nStr].compare(0,1, "#") == 0)
889 if (vstr[nStr] == "change=1")
891 if (vstr[nStr] == "reserve=1")
893 if (vstr[nStr].compare(0,6, "label=") == 0) {
894 strLabel = DecodeDumpString(vstr[nStr].substr(6));
899 CBitcoinAddress addr;
900 CBitcoinSecret vchSecret;
901 if (vchSecret.SetString(vstr[0])) {
902 // Simple private key
906 CSecret secret = vchSecret.GetSecret(fCompressed);
907 key.SetSecret(secret, fCompressed);
908 CKeyID keyid = key.GetPubKey().GetID();
909 addr = CBitcoinAddress(keyid);
911 if (pwallet->HaveKey(keyid)) {
912 printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
916 printf("Importing %s...\n", addr.ToString().c_str());
917 if (!pwallet->AddKey(key)) {
922 // A pair of private keys
925 if (!mKey.SetString(vstr[0]))
927 CMalleablePubKey mPubKey = mKey.GetMalleablePubKey();
928 addr = CBitcoinAddress(mPubKey);
930 if (pwallet->CheckOwnership(mPubKey)) {
931 printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
935 printf("Importing %s...\n", addr.ToString().c_str());
936 if (!pwallet->AddKey(mKey)) {
942 pwallet->mapKeyMetadata[addr].nCreateTime = nTime;
944 pwallet->SetAddressBookName(addr, strLabel);
946 nTimeBegin = std::min(nTimeBegin, nTime);
950 // rescan block chain looking for coins from new keys
951 CBlockIndex *pindex = pindexBest;
952 while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200)
953 pindex = pindex->pprev;
955 printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1);
956 pwallet->ScanForWalletTransactions(pindex);
957 pwallet->ReacceptWalletTransactions();
958 pwallet->MarkDirty();
964 // Try to (very carefully!) recover wallet.dat if there is a problem.
966 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
968 // Recovery procedure:
969 // move wallet.dat to wallet.timestamp.bak
970 // Call Salvage with fAggressive=true to
971 // get as much data as possible.
972 // Rewrite salvaged data to wallet.dat
973 // Set -rescan so any missing transactions will be
975 int64_t now = GetTime();
976 std::string newFilename = strprintf("wallet.%" PRId64 ".bak", now);
978 int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
979 newFilename.c_str(), DB_AUTO_COMMIT);
981 printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
984 printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
988 std::vector<CDBEnv::KeyValPair> salvagedData;
989 bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
990 if (salvagedData.empty())
992 printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
995 printf("Salvage(aggressive) found %" PRIszu " records\n", salvagedData.size());
997 bool fSuccess = allOK;
998 Db* pdbCopy = new Db(&dbenv.dbenv, 0);
999 int ret = pdbCopy->open(NULL, // Txn pointer
1000 filename.c_str(), // Filename
1001 "main", // Logical db name
1002 DB_BTREE, // Database type
1007 printf("Cannot create database file %s\n", filename.c_str());
1010 CWallet dummyWallet;
1011 CWalletScanState wss;
1013 DbTxn* ptxn = dbenv.TxnBegin();
1014 for (CDBEnv::KeyValPair& row : salvagedData)
1018 CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
1019 CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
1020 string strType, strErr;
1021 bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
1022 wss, strType, strErr);
1023 if (!IsKeyType(strType))
1027 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
1031 Dbt datKey(&row.first[0], row.first.size());
1032 Dbt datValue(&row.second[0], row.second.size());
1033 int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
1044 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
1046 return CWalletDB::Recover(dbenv, filename, false);