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.
12 #include <boost/version.hpp>
13 #include <boost/filesystem.hpp>
14 #include <boost/algorithm/string.hpp>
16 #include <boost/date_time/posix_time/posix_time.hpp>
17 #include <boost/lexical_cast.hpp>
18 #include <boost/variant/get.hpp>
19 #include <boost/algorithm/string.hpp>
22 using namespace boost;
25 static uint64_t nAccountingEntryNumber = 0;
26 extern bool fWalletUnlockMintOnly;
32 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
35 return Write(make_pair(string("name"), strAddress), strName);
38 bool CWalletDB::EraseName(const string& strAddress)
40 // This should only be used for sending addresses, never for receiving addresses,
41 // receiving addresses must always have an address book entry if they're not change return.
43 return Erase(make_pair(string("name"), strAddress));
46 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
49 return Read(make_pair(string("acc"), strAccount), account);
52 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
54 return Write(make_pair(string("acc"), strAccount), account);
57 bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
59 return Write(boost::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
62 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
64 return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
67 int64_t CWalletDB::GetAccountCreditDebit(const string& strAccount)
69 list<CAccountingEntry> entries;
70 ListAccountCreditDebit(strAccount, entries);
72 int64_t nCreditDebit = 0;
73 BOOST_FOREACH (const CAccountingEntry& entry, entries)
74 nCreditDebit += entry.nCreditDebit;
79 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
81 bool fAllAccounts = (strAccount == "*");
83 Dbc* pcursor = GetCursor();
85 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
86 unsigned int fFlags = DB_SET_RANGE;
90 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
91 if (fFlags == DB_SET_RANGE)
92 ssKey << boost::make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64_t(0));
93 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
94 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
96 if (ret == DB_NOTFOUND)
101 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
107 if (strType != "acentry")
109 CAccountingEntry acentry;
110 ssKey >> acentry.strAccount;
111 if (!fAllAccounts && acentry.strAccount != strAccount)
115 ssKey >> acentry.nEntryNo;
116 entries.push_back(acentry);
124 CWalletDB::ReorderTransactions(CWallet* pwallet)
126 LOCK(pwallet->cs_wallet);
127 // Old wallets didn't have any defined order for transactions
128 // Probably a bad idea to change the output of this
130 // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
131 typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
132 typedef multimap<int64_t, TxPair > TxItems;
135 for (map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
137 CWalletTx* wtx = &((*it).second);
138 txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
140 list<CAccountingEntry> acentries;
141 ListAccountCreditDebit("", acentries);
142 BOOST_FOREACH(CAccountingEntry& entry, acentries)
144 txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
147 int64_t& nOrderPosNext = pwallet->nOrderPosNext;
149 std::vector<int64_t> nOrderPosOffsets;
150 for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
152 CWalletTx *const pwtx = (*it).second.first;
153 CAccountingEntry *const pacentry = (*it).second.second;
154 int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
158 nOrderPos = nOrderPosNext++;
159 nOrderPosOffsets.push_back(nOrderPos);
162 // Have to write accounting regardless, since we don't keep it in memory
163 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
168 int64_t nOrderPosOff = 0;
169 BOOST_FOREACH(const int64_t& nOffsetStart, nOrderPosOffsets)
171 if (nOrderPos >= nOffsetStart)
174 nOrderPos += nOrderPosOff;
175 nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
180 // Since we're changing the order, write it back
183 if (!WriteTx(pwtx->GetHash(), *pwtx))
187 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
195 class CWalletScanState {
199 unsigned int nKeyMeta;
203 vector<uint256> vWalletUpgrade;
206 nKeys = nCKeys = nKeyMeta = 0;
207 fIsEncrypted = false;
208 fAnyUnordered = false;
214 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
215 CWalletScanState &wss, string& strType, string& strErr)
219 // Taking advantage of the fact that pair serialization
220 // is just the two items serialized one after the other
222 if (strType == "name")
226 ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()];
228 else if (strType == "tx")
232 CWalletTx& wtx = pwallet->mapWallet[hash];
234 if (wtx.CheckTransaction() && (wtx.GetHash() == hash))
235 wtx.BindWallet(pwallet);
238 pwallet->mapWallet.erase(hash);
242 // Undo serialize changes in 31600
243 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
245 if (!ssValue.empty())
249 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
250 strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
251 wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
252 wtx.fTimeReceivedIsTxTime = fTmp;
256 strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
257 wtx.fTimeReceivedIsTxTime = 0;
259 wss.vWalletUpgrade.push_back(hash);
262 if (wtx.nOrderPos == -1)
263 wss.fAnyUnordered = true;
266 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
267 //printf(" %12"PRId64" %s %s %s\n",
268 // wtx.vout[0].nValue,
269 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
270 // wtx.hashBlock.ToString().substr(0,20).c_str(),
271 // wtx.mapValue["message"].c_str());
273 else if (strType == "acentry")
279 if (nNumber > nAccountingEntryNumber)
280 nAccountingEntryNumber = nNumber;
282 if (!wss.fAnyUnordered)
284 CAccountingEntry acentry;
286 if (acentry.nOrderPos == -1)
287 wss.fAnyUnordered = true;
290 else if (strType == "watchs")
297 pwallet->LoadWatchOnly(script);
299 // Watch-only addresses have no birthday information for now,
300 // so set the wallet birthday to the beginning of time.
301 pwallet->nTimeFirstKey = 1;
303 else if (strType == "key" || strType == "wkey")
305 vector<unsigned char> vchPubKey;
308 if (strType == "key")
313 key.SetPubKey(vchPubKey);
314 if (!key.SetPrivKey(pkey))
316 strErr = "Error reading wallet database: CPrivKey corrupt";
319 if (key.GetPubKey() != vchPubKey)
321 strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
326 strErr = "Error reading wallet database: invalid CPrivKey";
334 key.SetPubKey(vchPubKey);
335 if (!key.SetPrivKey(wkey.vchPrivKey))
337 strErr = "Error reading wallet database: CPrivKey corrupt";
340 if (key.GetPubKey() != vchPubKey)
342 strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
347 strErr = "Error reading wallet database: invalid CWalletKey";
351 if (!pwallet->LoadKey(key))
353 strErr = "Error reading wallet database: LoadKey failed";
357 else if (strType == "mkey")
361 CMasterKey kMasterKey;
362 ssValue >> kMasterKey;
364 if(pwallet->mapMasterKeys.count(nID) != 0)
366 strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
369 pwallet->mapMasterKeys[nID] = kMasterKey;
370 if (pwallet->nMasterKeyMaxID < nID)
371 pwallet->nMasterKeyMaxID = nID;
373 else if (strType == "ckey")
376 vector<unsigned char> vchPubKey;
378 vector<unsigned char> vchPrivKey;
379 ssValue >> vchPrivKey;
380 if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
382 strErr = "Error reading wallet database: LoadCryptedKey failed";
385 wss.fIsEncrypted = true;
387 else if (strType == "keymeta")
391 CKeyMetadata keyMeta;
395 pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
397 // find earliest key creation time, as wallet birthday
398 if (!pwallet->nTimeFirstKey ||
399 (keyMeta.nCreateTime < pwallet->nTimeFirstKey))
400 pwallet->nTimeFirstKey = keyMeta.nCreateTime;
402 else if (strType == "defaultkey")
404 ssValue >> pwallet->vchDefaultKey;
406 else if (strType == "pool")
412 pwallet->setKeyPool.insert(nIndex);
414 // If no metadata exists yet, create a default with the pool key's
415 // creation time. Note that this may be overwritten by actually
416 // stored metadata for that key later, which is fine.
417 CKeyID keyid = keypool.vchPubKey.GetID();
418 if (pwallet->mapKeyMetadata.count(keyid) == 0)
419 pwallet->mapKeyMetadata[keyid] = CKeyMetadata(keypool.nTime);
422 else if (strType == "version")
424 ssValue >> wss.nFileVersion;
425 if (wss.nFileVersion == 10300)
426 wss.nFileVersion = 300;
428 else if (strType == "cscript")
434 if (!pwallet->LoadCScript(script))
436 strErr = "Error reading wallet database: LoadCScript failed";
440 else if (strType == "orderposnext")
442 ssValue >> pwallet->nOrderPosNext;
451 static bool IsKeyType(string strType)
453 return (strType== "key" || strType == "wkey" ||
454 strType == "mkey" || strType == "ckey");
457 DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
459 pwallet->vchDefaultKey = CPubKey();
460 CWalletScanState wss;
461 bool fNoncriticalErrors = false;
462 DBErrors result = DB_LOAD_OK;
465 LOCK(pwallet->cs_wallet);
467 if (Read((string)"minversion", nMinVersion))
469 if (nMinVersion > CLIENT_VERSION)
471 pwallet->LoadMinVersion(nMinVersion);
475 Dbc* pcursor = GetCursor();
478 printf("Error getting wallet database cursor\n");
485 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
486 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
487 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
488 if (ret == DB_NOTFOUND)
492 printf("Error reading next record from wallet database\n");
496 // Try to be tolerant of single corrupt records:
497 string strType, strErr;
498 if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
500 // losing keys is considered a catastrophic error, anything else
501 // we assume the user can live with:
502 if (IsKeyType(strType))
506 // Leave other errors alone, if we try to fix them we might make things worse.
507 fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
509 // Rescan if there is a bad transaction record:
510 SoftSetBoolArg("-rescan", true);
514 printf("%s\n", strErr.c_str());
523 if (fNoncriticalErrors && result == DB_LOAD_OK)
524 result = DB_NONCRITICAL_ERROR;
526 // Any wallet corruption at all: skip any rewriting or
527 // upgrading, we don't want to make it worse.
528 if (result != DB_LOAD_OK)
531 printf("nFileVersion = %d\n", wss.nFileVersion);
533 printf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total\n",
534 wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys);
536 // nTimeFirstKey is only reliable if all keys have metadata
537 if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
538 pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
541 BOOST_FOREACH(uint256 hash, wss.vWalletUpgrade)
542 WriteTx(hash, pwallet->mapWallet[hash]);
544 // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
545 if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
546 return DB_NEED_REWRITE;
548 if (wss.nFileVersion < CLIENT_VERSION) // Update
549 WriteVersion(CLIENT_VERSION);
551 if (wss.fAnyUnordered)
552 result = ReorderTransactions(pwallet);
557 void ThreadFlushWalletDB(void* parg)
559 // Make this thread recognisable as the wallet flushing thread
560 RenameThread("novacoin-wallet");
562 const string& strFile = ((const string*)parg)[0];
563 static bool fOneThread;
567 if (!GetBoolArg("-flushwallet", true))
570 unsigned int nLastSeen = nWalletDBUpdated;
571 unsigned int nLastFlushed = nWalletDBUpdated;
572 int64_t nLastWalletUpdate = GetTime();
577 if (nLastSeen != nWalletDBUpdated)
579 nLastSeen = nWalletDBUpdated;
580 nLastWalletUpdate = GetTime();
583 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
585 TRY_LOCK(bitdb.cs_db,lockDb);
588 // Don't do this if any databases are in use
590 map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
591 while (mi != bitdb.mapFileUseCount.end())
593 nRefCount += (*mi).second;
597 if (nRefCount == 0 && !fShutdown)
599 map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
600 if (mi != bitdb.mapFileUseCount.end())
602 printf("Flushing wallet.dat\n");
603 nLastFlushed = nWalletDBUpdated;
604 int64_t nStart = GetTimeMillis();
606 // Flush wallet.dat so it's self contained
607 bitdb.CloseDb(strFile);
608 bitdb.CheckpointLSN(strFile);
610 bitdb.mapFileUseCount.erase(mi++);
611 printf("Flushed wallet.dat %" PRId64 "ms\n", GetTimeMillis() - nStart);
619 bool BackupWallet(const CWallet& wallet, const string& strDest)
621 if (!wallet.fFileBacked)
627 if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
629 // Flush log data to the dat file
630 bitdb.CloseDb(wallet.strWalletFile);
631 bitdb.CheckpointLSN(wallet.strWalletFile);
632 bitdb.mapFileUseCount.erase(wallet.strWalletFile);
635 filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
636 filesystem::path pathDest(strDest);
637 if (filesystem::is_directory(pathDest))
638 pathDest /= wallet.strWalletFile;
641 #if BOOST_VERSION >= 104000
642 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
644 filesystem::copy_file(pathSrc, pathDest);
646 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
648 } catch(const filesystem::filesystem_error &e) {
649 printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
659 bool DumpWallet(CWallet* pwallet, const string& strDest)
662 if (!pwallet->fFileBacked)
667 std::map<CKeyID, int64_t> mapKeyBirth;
668 std::set<CKeyID> setKeyPool;
669 pwallet->GetKeyBirthTimes(mapKeyBirth);
670 pwallet->GetAllReserveKeys(setKeyPool);
672 // sort time/key pairs
673 std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
674 for (std::map<CKeyID, int64_t>::const_iterator it = mapKeyBirth.begin(); it != mapKeyBirth.end(); it++) {
675 vKeyBirth.push_back(std::make_pair(it->second, it->first));
678 std::sort(vKeyBirth.begin(), vKeyBirth.end());
680 // open outputfile as a stream
682 file.open(strDest.c_str());
687 file << strprintf("# Wallet dump created by NovaCoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
688 file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
689 file << strprintf("# * Best block at time of backup was %i (%s),\n", nBestHeight, hashBestChain.ToString().c_str());
690 file << strprintf("# mined on %s\n", EncodeDumpTime(pindexBest->nTime).c_str());
692 for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
693 const CKeyID &keyid = it->second;
694 std::string strTime = EncodeDumpTime(it->first);
695 std::string strAddr = CBitcoinAddress(keyid).ToString();
699 if (pwallet->GetKey(keyid, key)) {
700 if (pwallet->mapAddressBook.count(keyid)) {
701 CSecret secret = key.GetSecret(IsCompressed);
702 file << strprintf("%s %s label=%s # addr=%s\n",
703 CBitcoinSecret(secret, IsCompressed).ToString().c_str(),
705 EncodeDumpString(pwallet->mapAddressBook[keyid]).c_str(),
707 } else if (setKeyPool.count(keyid)) {
708 CSecret secret = key.GetSecret(IsCompressed);
709 file << strprintf("%s %s reserve=1 # addr=%s\n",
710 CBitcoinSecret(secret, IsCompressed).ToString().c_str(),
714 CSecret secret = key.GetSecret(IsCompressed);
715 file << strprintf("%s %s change=1 # addr=%s\n",
716 CBitcoinSecret(secret, IsCompressed).ToString().c_str(),
723 file << "# End of dump\n";
731 bool ImportWallet(CWallet *pwallet, const string& strLocation)
734 if (!pwallet->fFileBacked)
738 // open inputfile as stream
740 file.open(strLocation.c_str());
744 int64_t nTimeBegin = pindexBest->nTime;
748 // read through input file checking and importing keys into wallet.
749 while (file.good()) {
751 std::getline(file, line);
752 if (line.empty() || line[0] == '#')
755 std::vector<std::string> vstr;
756 boost::split(vstr, line, boost::is_any_of(" "));
759 CBitcoinSecret vchSecret;
760 if (!vchSecret.SetString(vstr[0]))
765 CSecret secret = vchSecret.GetSecret(fCompressed);
766 key.SetSecret(secret, fCompressed);
767 CKeyID keyid = key.GetPubKey().GetID();
769 if (pwallet->HaveKey(keyid)) {
770 printf("Skipping import of %s (key already present)\n", CBitcoinAddress(keyid).ToString().c_str());
773 int64_t nTime = DecodeDumpTime(vstr[1]);
774 std::string strLabel;
776 for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
777 if (boost::algorithm::starts_with(vstr[nStr], "#"))
779 if (vstr[nStr] == "change=1")
781 if (vstr[nStr] == "reserve=1")
783 if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
784 strLabel = DecodeDumpString(vstr[nStr].substr(6));
788 printf("Importing %s...\n", CBitcoinAddress(keyid).ToString().c_str());
789 if (!pwallet->AddKey(key)) {
793 pwallet->mapKeyMetadata[keyid].nCreateTime = nTime;
795 pwallet->SetAddressBookName(keyid, strLabel);
796 nTimeBegin = std::min(nTimeBegin, nTime);
800 // rescan block chain looking for coins from new keys
801 CBlockIndex *pindex = pindexBest;
802 while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200)
803 pindex = pindex->pprev;
805 printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1);
806 pwallet->ScanForWalletTransactions(pindex);
807 pwallet->ReacceptWalletTransactions();
808 pwallet->MarkDirty();
820 // Try to (very carefully!) recover wallet.dat if there is a problem.
822 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
824 // Recovery procedure:
825 // move wallet.dat to wallet.timestamp.bak
826 // Call Salvage with fAggressive=true to
827 // get as much data as possible.
828 // Rewrite salvaged data to wallet.dat
829 // Set -rescan so any missing transactions will be
831 int64_t now = GetTime();
832 std::string newFilename = strprintf("wallet.%" PRId64 ".bak", now);
834 int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
835 newFilename.c_str(), DB_AUTO_COMMIT);
837 printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
840 printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
844 std::vector<CDBEnv::KeyValPair> salvagedData;
845 bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
846 if (salvagedData.empty())
848 printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
851 printf("Salvage(aggressive) found %" PRIszu " records\n", salvagedData.size());
853 bool fSuccess = allOK;
854 Db* pdbCopy = new Db(&dbenv.dbenv, 0);
855 int ret = pdbCopy->open(NULL, // Txn pointer
856 filename.c_str(), // Filename
857 "main", // Logical db name
858 DB_BTREE, // Database type
863 printf("Cannot create database file %s\n", filename.c_str());
867 CWalletScanState wss;
869 DbTxn* ptxn = dbenv.TxnBegin();
870 BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData)
874 CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
875 CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
876 string strType, strErr;
877 bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
878 wss, strType, strErr);
879 if (!IsKeyType(strType))
883 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
887 Dbt datKey(&row.first[0], row.first.size());
888 Dbt datValue(&row.second[0], row.second.size());
889 int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
900 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
902 return CWalletDB::Recover(dbenv, filename, false);