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>
15 #include <boost/algorithm/string.hpp>
17 #include <boost/date_time/posix_time/posix_time.hpp>
18 #include <boost/lexical_cast.hpp>
19 #include <boost/variant/get.hpp>
20 #include <boost/algorithm/string.hpp>
23 using namespace boost;
26 static uint64_t nAccountingEntryNumber = 0;
27 extern bool fWalletUnlockMintOnly;
33 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
36 return Write(make_pair(string("name"), strAddress), strName);
39 bool CWalletDB::EraseName(const string& strAddress)
41 // This should only be used for sending addresses, never for receiving addresses,
42 // receiving addresses must always have an address book entry if they're not change return.
44 return Erase(make_pair(string("name"), strAddress));
47 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
50 return Read(make_pair(string("acc"), strAccount), account);
53 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
55 return Write(make_pair(string("acc"), strAccount), account);
58 bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
60 return Write(boost::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
63 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
65 return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
68 int64_t CWalletDB::GetAccountCreditDebit(const string& strAccount)
70 list<CAccountingEntry> entries;
71 ListAccountCreditDebit(strAccount, entries);
73 int64_t nCreditDebit = 0;
74 BOOST_FOREACH (const CAccountingEntry& entry, entries)
75 nCreditDebit += entry.nCreditDebit;
80 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
82 bool fAllAccounts = (strAccount == "*");
84 Dbc* pcursor = GetCursor();
86 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
87 unsigned int fFlags = DB_SET_RANGE;
91 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
92 if (fFlags == DB_SET_RANGE)
93 ssKey << boost::make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64_t(0));
94 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
95 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
97 if (ret == DB_NOTFOUND)
102 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
108 if (strType != "acentry")
110 CAccountingEntry acentry;
111 ssKey >> acentry.strAccount;
112 if (!fAllAccounts && acentry.strAccount != strAccount)
116 ssKey >> acentry.nEntryNo;
117 entries.push_back(acentry);
125 CWalletDB::ReorderTransactions(CWallet* pwallet)
127 LOCK(pwallet->cs_wallet);
128 // Old wallets didn't have any defined order for transactions
129 // Probably a bad idea to change the output of this
131 // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
132 typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
133 typedef multimap<int64_t, TxPair > TxItems;
136 for (map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
138 CWalletTx* wtx = &((*it).second);
139 txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
141 list<CAccountingEntry> acentries;
142 ListAccountCreditDebit("", acentries);
143 BOOST_FOREACH(CAccountingEntry& entry, acentries)
145 txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
148 int64_t& nOrderPosNext = pwallet->nOrderPosNext;
150 std::vector<int64_t> nOrderPosOffsets;
151 for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
153 CWalletTx *const pwtx = (*it).second.first;
154 CAccountingEntry *const pacentry = (*it).second.second;
155 int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
159 nOrderPos = nOrderPosNext++;
160 nOrderPosOffsets.push_back(nOrderPos);
163 // Have to write accounting regardless, since we don't keep it in memory
164 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
169 int64_t nOrderPosOff = 0;
170 BOOST_FOREACH(const int64_t& nOffsetStart, nOrderPosOffsets)
172 if (nOrderPos >= nOffsetStart)
175 nOrderPos += nOrderPosOff;
176 nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
181 // Since we're changing the order, write it back
184 if (!WriteTx(pwtx->GetHash(), *pwtx))
188 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
196 class CWalletScanState {
200 unsigned int nKeyMeta;
204 vector<uint256> vWalletUpgrade;
207 nKeys = nCKeys = nKeyMeta = 0;
208 fIsEncrypted = false;
209 fAnyUnordered = false;
215 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
216 CWalletScanState &wss, string& strType, string& strErr)
220 // Taking advantage of the fact that pair serialization
221 // is just the two items serialized one after the other
224 if (strType == "name")
228 ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress)];
230 else if (strType == "tx")
234 CWalletTx& wtx = pwallet->mapWallet[hash];
236 if (wtx.CheckTransaction() && (wtx.GetHash() == hash))
237 wtx.BindWallet(pwallet);
240 pwallet->mapWallet.erase(hash);
244 // Undo serialize changes in 31600
245 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
247 if (!ssValue.empty())
251 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
252 strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
253 wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
254 wtx.fTimeReceivedIsTxTime = fTmp;
258 strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
259 wtx.fTimeReceivedIsTxTime = 0;
261 wss.vWalletUpgrade.push_back(hash);
264 if (wtx.nOrderPos == -1)
265 wss.fAnyUnordered = true;
268 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
269 //printf(" %12"PRId64" %s %s %s\n",
270 // wtx.vout[0].nValue,
271 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
272 // wtx.hashBlock.ToString().substr(0,20).c_str(),
273 // wtx.mapValue["message"].c_str());
275 else if (strType == "acentry")
281 if (nNumber > nAccountingEntryNumber)
282 nAccountingEntryNumber = nNumber;
284 if (!wss.fAnyUnordered)
286 CAccountingEntry acentry;
288 if (acentry.nOrderPos == -1)
289 wss.fAnyUnordered = true;
292 else if (strType == "watchs")
299 pwallet->LoadWatchOnly(script);
301 // Watch-only addresses have no birthday information for now,
302 // so set the wallet birthday to the beginning of time.
303 pwallet->nTimeFirstKey = 1;
305 else if (strType == "malpair")
311 ssValue >> vchSecret;
313 CMalleableKeyView keyView(strKeyView);
314 if (!pwallet->LoadKey(keyView, vchSecret))
316 strErr = "Error reading wallet database: LoadKey failed";
320 else if (strType == "malcpair")
324 std::vector<unsigned char> vchCryptedSecret;
326 ssValue >> vchCryptedSecret;
328 CMalleableKeyView keyView(strKeyView);
329 if (!pwallet->LoadCryptedKey(keyView, vchCryptedSecret))
331 strErr = "Error reading wallet database: LoadCryptedKey failed";
335 else if (strType == "key" || strType == "wkey")
340 if (strType == "key")
345 if (!key.SetPrivKey(pkey))
347 strErr = "Error reading wallet database: CPrivKey corrupt";
350 if (key.GetPubKey() != vchPubKey)
352 strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
355 if (vchPubKey.size() == 33) {
356 key.SetCompressedPubKey();
360 strErr = "Error reading wallet database: invalid CPrivKey";
368 if (!key.SetPrivKey(wkey.vchPrivKey))
370 strErr = "Error reading wallet database: CPrivKey corrupt";
373 if (key.GetPubKey() != vchPubKey)
375 strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
378 if (vchPubKey.size() == 33) {
379 key.SetCompressedPubKey();
383 strErr = "Error reading wallet database: invalid CWalletKey";
387 if (!pwallet->LoadKey(key))
389 strErr = "Error reading wallet database: LoadKey failed";
393 else if (strType == "mkey")
397 CMasterKey kMasterKey;
398 ssValue >> kMasterKey;
400 if(pwallet->mapMasterKeys.count(nID) != 0)
402 strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
405 pwallet->mapMasterKeys[nID] = kMasterKey;
406 if (pwallet->nMasterKeyMaxID < nID)
407 pwallet->nMasterKeyMaxID = nID;
409 else if (strType == "ckey")
414 vector<unsigned char> vchPrivKey;
415 ssValue >> vchPrivKey;
416 if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
418 strErr = "Error reading wallet database: LoadCryptedKey failed";
421 wss.fIsEncrypted = true;
423 else if (strType == "malmeta")
428 CMalleableKeyView keyView;
429 keyView.SetString(strKeyView);
431 CKeyMetadata keyMeta;
435 pwallet->LoadKeyMetadata(keyView, keyMeta);
437 else if (strType == "keymeta")
441 CKeyMetadata keyMeta;
445 pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
447 // find earliest key creation time, as wallet birthday
448 if (!pwallet->nTimeFirstKey ||
449 (keyMeta.nCreateTime < pwallet->nTimeFirstKey))
450 pwallet->nTimeFirstKey = keyMeta.nCreateTime;
452 else if (strType == "defaultkey")
454 ssValue >> pwallet->vchDefaultKey;
456 else if (strType == "pool")
462 pwallet->setKeyPool.insert(nIndex);
464 // If no metadata exists yet, create a default with the pool key's
465 // creation time. Note that this may be overwritten by actually
466 // stored metadata for that key later, which is fine.
467 CBitcoinAddress addr = CBitcoinAddress(keypool.vchPubKey.GetID());
468 if (pwallet->mapKeyMetadata.count(addr) == 0)
469 pwallet->mapKeyMetadata[addr] = CKeyMetadata(keypool.nTime);
472 else if (strType == "version")
474 ssValue >> wss.nFileVersion;
475 if (wss.nFileVersion == 10300)
476 wss.nFileVersion = 300;
478 else if (strType == "cscript")
484 if (!pwallet->LoadCScript(script))
486 strErr = "Error reading wallet database: LoadCScript failed";
490 else if (strType == "orderposnext")
492 ssValue >> pwallet->nOrderPosNext;
501 static bool IsKeyType(string strType)
503 return (strType== "key" || strType == "wkey" ||
504 strType == "mkey" || strType == "ckey" || strType == "malpair" || strType == "malcpair");
507 DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
509 pwallet->vchDefaultKey = CPubKey();
510 CWalletScanState wss;
511 bool fNoncriticalErrors = false;
512 DBErrors result = DB_LOAD_OK;
515 LOCK(pwallet->cs_wallet);
517 if (Read((string)"minversion", nMinVersion))
519 if (nMinVersion > CLIENT_VERSION)
521 pwallet->LoadMinVersion(nMinVersion);
525 Dbc* pcursor = GetCursor();
528 printf("Error getting wallet database cursor\n");
535 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
536 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
537 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
538 if (ret == DB_NOTFOUND)
542 printf("Error reading next record from wallet database\n");
546 // Try to be tolerant of single corrupt records:
547 string strType, strErr;
548 if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
550 // losing keys is considered a catastrophic error, anything else
551 // we assume the user can live with:
552 if (IsKeyType(strType))
556 // Leave other errors alone, if we try to fix them we might make things worse.
557 fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
559 // Rescan if there is a bad transaction record:
560 SoftSetBoolArg("-rescan", true);
564 printf("%s\n", strErr.c_str());
573 if (fNoncriticalErrors && result == DB_LOAD_OK)
574 result = DB_NONCRITICAL_ERROR;
576 // Any wallet corruption at all: skip any rewriting or
577 // upgrading, we don't want to make it worse.
578 if (result != DB_LOAD_OK)
581 printf("nFileVersion = %d\n", wss.nFileVersion);
583 printf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total\n",
584 wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys);
586 // nTimeFirstKey is only reliable if all keys have metadata
587 if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
588 pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
591 BOOST_FOREACH(uint256 hash, wss.vWalletUpgrade)
592 WriteTx(hash, pwallet->mapWallet[hash]);
594 // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
595 if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
596 return DB_NEED_REWRITE;
598 if (wss.nFileVersion < CLIENT_VERSION) // Update
599 WriteVersion(CLIENT_VERSION);
601 if (wss.fAnyUnordered)
602 result = ReorderTransactions(pwallet);
607 DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash)
609 pwallet->vchDefaultKey = CPubKey();
610 CWalletScanState wss;
611 bool fNoncriticalErrors = false;
612 DBErrors result = DB_LOAD_OK;
615 LOCK(pwallet->cs_wallet);
617 if (Read((string)"minversion", nMinVersion))
619 if (nMinVersion > CLIENT_VERSION)
621 pwallet->LoadMinVersion(nMinVersion);
625 Dbc* pcursor = GetCursor();
628 printf("Error getting wallet database cursor\n");
635 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
636 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
637 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
638 if (ret == DB_NOTFOUND)
642 printf("Error reading next record from wallet database\n");
648 if (strType == "tx") {
652 vTxHash.push_back(hash);
657 catch (const boost::thread_interrupted&) {
664 if (fNoncriticalErrors && result == DB_LOAD_OK)
665 result = DB_NONCRITICAL_ERROR;
670 DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet)
672 // build list of wallet TXs
673 vector<uint256> vTxHash;
674 DBErrors err = FindWalletTx(pwallet, vTxHash);
675 if (err != DB_LOAD_OK)
678 // erase each wallet TX
679 BOOST_FOREACH (uint256& hash, vTxHash) {
687 void ThreadFlushWalletDB(void* parg)
689 // Make this thread recognisable as the wallet flushing thread
690 RenameThread("novacoin-wallet");
692 const string& strFile = ((const string*)parg)[0];
693 static bool fOneThread;
697 if (!GetBoolArg("-flushwallet", true))
700 unsigned int nLastSeen = nWalletDBUpdated;
701 unsigned int nLastFlushed = nWalletDBUpdated;
702 int64_t nLastWalletUpdate = GetTime();
707 if (nLastSeen != nWalletDBUpdated)
709 nLastSeen = nWalletDBUpdated;
710 nLastWalletUpdate = GetTime();
713 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
715 TRY_LOCK(bitdb.cs_db,lockDb);
718 // Don't do this if any databases are in use
720 map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
721 while (mi != bitdb.mapFileUseCount.end())
723 nRefCount += (*mi).second;
727 if (nRefCount == 0 && !fShutdown)
729 map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
730 if (mi != bitdb.mapFileUseCount.end())
732 printf("Flushing wallet.dat\n");
733 nLastFlushed = nWalletDBUpdated;
734 int64_t nStart = GetTimeMillis();
736 // Flush wallet.dat so it's self contained
737 bitdb.CloseDb(strFile);
738 bitdb.CheckpointLSN(strFile);
740 bitdb.mapFileUseCount.erase(mi++);
741 printf("Flushed wallet.dat %" PRId64 "ms\n", GetTimeMillis() - nStart);
749 bool BackupWallet(const CWallet& wallet, const string& strDest)
751 if (!wallet.fFileBacked)
757 if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
759 // Flush log data to the dat file
760 bitdb.CloseDb(wallet.strWalletFile);
761 bitdb.CheckpointLSN(wallet.strWalletFile);
762 bitdb.mapFileUseCount.erase(wallet.strWalletFile);
765 filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
766 filesystem::path pathDest(strDest);
767 if (filesystem::is_directory(pathDest))
768 pathDest /= wallet.strWalletFile;
771 #if BOOST_VERSION >= 104000
772 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
774 filesystem::copy_file(pathSrc, pathDest);
776 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
778 } catch(const filesystem::filesystem_error &e) {
779 printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
789 bool DumpWallet(CWallet* pwallet, const string& strDest)
791 if (!pwallet->fFileBacked)
794 std::map<CBitcoinAddress, int64_t> mapAddresses;
795 std::set<CKeyID> setKeyPool;
797 pwallet->GetAddresses(mapAddresses);
798 pwallet->GetAllReserveKeys(setKeyPool);
800 // sort time/key pairs
801 std::vector<std::pair<int64_t, CBitcoinAddress> > vAddresses;
802 for (std::map<CBitcoinAddress, int64_t>::const_iterator it = mapAddresses.begin(); it != mapAddresses.end(); it++) {
803 vAddresses.push_back(std::make_pair(it->second, it->first));
805 mapAddresses.clear();
806 std::sort(vAddresses.begin(), vAddresses.end());
808 // open outputfile as a stream
810 file.open(strDest.c_str());
815 file << strprintf("# Wallet dump created by NovaCoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
816 file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
817 file << strprintf("# * Best block at time of backup was %i (%s),\n", nBestHeight, hashBestChain.ToString().c_str());
818 file << strprintf("# mined on %s\n", EncodeDumpTime(pindexBest->nTime).c_str());
821 for (std::vector<std::pair<int64_t, CBitcoinAddress> >::const_iterator it = vAddresses.begin(); it != vAddresses.end(); it++) {
822 const CBitcoinAddress &addr = it->second;
823 std::string strTime = EncodeDumpTime(it->first);
824 std::string strAddr = addr.ToString();
827 // Pubkey pair address
828 CMalleableKeyView keyView;
829 CMalleablePubKey mPubKey(addr.GetData());
830 if (!pwallet->GetMalleableView(mPubKey, keyView))
833 pwallet->GetMalleableKey(keyView, mKey);
834 file << mKey.ToString();
835 if (pwallet->mapAddressBook.count(addr))
836 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());
838 file << strprintf(" %s # view=%s addr=%s\n", strTime.c_str(), keyView.ToString().c_str(), strAddr.c_str());
841 // Pubkey hash address
843 addr.GetKeyID(keyid);
846 if (!pwallet->GetKey(keyid, key))
848 CSecret secret = key.GetSecret(IsCompressed);
849 file << CBitcoinSecret(secret, IsCompressed).ToString();
850 if (pwallet->mapAddressBook.count(addr))
851 file << strprintf(" %s label=%s # addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), strAddr.c_str());
852 else if (setKeyPool.count(keyid))
853 file << strprintf(" %s reserve=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
855 file << strprintf(" %s change=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
860 file << "# End of dump\n";
866 bool ImportWallet(CWallet *pwallet, const string& strLocation)
869 if (!pwallet->fFileBacked)
872 // open inputfile as stream
874 file.open(strLocation.c_str());
879 int64_t nTimeBegin = pindexBest->nTime;
881 // read through input file checking and importing keys into wallet.
882 while (file.good()) {
884 std::getline(file, line);
885 if (line.empty() || line[0] == '#')
886 continue; // Skip comments and empty lines
888 std::vector<std::string> vstr;
889 istringstream iss(line);
890 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(vstr));
894 int64_t nTime = DecodeDumpTime(vstr[1]);
895 std::string strLabel;
897 for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
898 if (boost::algorithm::starts_with(vstr[nStr], "#"))
900 if (vstr[nStr] == "change=1")
902 if (vstr[nStr] == "reserve=1")
904 if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
905 strLabel = DecodeDumpString(vstr[nStr].substr(6));
910 CBitcoinAddress addr;
911 CBitcoinSecret vchSecret;
912 if (vchSecret.SetString(vstr[0])) {
913 // Simple private key
917 CSecret secret = vchSecret.GetSecret(fCompressed);
918 key.SetSecret(secret, fCompressed);
919 CKeyID keyid = key.GetPubKey().GetID();
920 addr = CBitcoinAddress(keyid);
922 if (pwallet->HaveKey(keyid)) {
923 printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
927 printf("Importing %s...\n", addr.ToString().c_str());
928 if (!pwallet->AddKey(key)) {
933 // A pair of private keys
936 if (!mKey.SetString(vstr[0]))
938 CMalleablePubKey mPubKey = mKey.GetMalleablePubKey();
939 addr = CBitcoinAddress(mPubKey);
941 if (pwallet->CheckOwnership(mPubKey)) {
942 printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
946 printf("Importing %s...\n", addr.ToString().c_str());
947 if (!pwallet->AddKey(mKey)) {
953 pwallet->mapKeyMetadata[addr].nCreateTime = nTime;
955 pwallet->SetAddressBookName(addr, strLabel);
957 nTimeBegin = std::min(nTimeBegin, nTime);
961 // rescan block chain looking for coins from new keys
962 CBlockIndex *pindex = pindexBest;
963 while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200)
964 pindex = pindex->pprev;
966 printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1);
967 pwallet->ScanForWalletTransactions(pindex);
968 pwallet->ReacceptWalletTransactions();
969 pwallet->MarkDirty();
975 // Try to (very carefully!) recover wallet.dat if there is a problem.
977 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
979 // Recovery procedure:
980 // move wallet.dat to wallet.timestamp.bak
981 // Call Salvage with fAggressive=true to
982 // get as much data as possible.
983 // Rewrite salvaged data to wallet.dat
984 // Set -rescan so any missing transactions will be
986 int64_t now = GetTime();
987 std::string newFilename = strprintf("wallet.%" PRId64 ".bak", now);
989 int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
990 newFilename.c_str(), DB_AUTO_COMMIT);
992 printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
995 printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
999 std::vector<CDBEnv::KeyValPair> salvagedData;
1000 bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
1001 if (salvagedData.empty())
1003 printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
1006 printf("Salvage(aggressive) found %" PRIszu " records\n", salvagedData.size());
1008 bool fSuccess = allOK;
1009 Db* pdbCopy = new Db(&dbenv.dbenv, 0);
1010 int ret = pdbCopy->open(NULL, // Txn pointer
1011 filename.c_str(), // Filename
1012 "main", // Logical db name
1013 DB_BTREE, // Database type
1018 printf("Cannot create database file %s\n", filename.c_str());
1021 CWallet dummyWallet;
1022 CWalletScanState wss;
1024 DbTxn* ptxn = dbenv.TxnBegin();
1025 BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData)
1029 CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
1030 CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
1031 string strType, strErr;
1032 bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
1033 wss, strType, strErr);
1034 if (!IsKeyType(strType))
1038 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
1042 Dbt datKey(&row.first[0], row.first.size());
1043 Dbt datValue(&row.second[0], row.second.size());
1044 int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
1055 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
1057 return CWalletDB::Recover(dbenv, filename, false);