1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
8 #include <boost/filesystem/fstream.hpp>
11 using namespace boost;
14 unsigned int nWalletDBUpdated;
15 uint64 nAccountingEntryNumber = 0;
23 static CCriticalSection cs_db;
24 static bool fDbEnvInit = false;
26 static map<string, int> mapFileUseCount;
27 static map<string, Db*> mapDb;
47 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
53 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
54 bool fCreate = strchr(pszMode, 'c');
55 unsigned int nFlags = DB_THREAD;
65 string strDataDir = GetDataDir();
66 string strLogDir = strDataDir + "/database";
67 filesystem::create_directory(strLogDir.c_str());
68 string strErrorFile = strDataDir + "/db.log";
69 printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
71 dbenv.set_lg_dir(strLogDir.c_str());
72 dbenv.set_lg_max(10000000);
73 dbenv.set_lk_max_locks(10000);
74 dbenv.set_lk_max_objects(10000);
75 dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
76 dbenv.set_flags(DB_AUTO_COMMIT, 1);
77 ret = dbenv.open(strDataDir.c_str(),
87 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
92 ++mapFileUseCount[strFile];
96 pdb = new Db(&dbenv, 0);
98 ret = pdb->open(NULL, // Txn pointer
100 "main", // Logical db name
101 DB_BTREE, // Database type
109 CRITICAL_BLOCK(cs_db)
110 --mapFileUseCount[strFile];
112 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
115 if (fCreate && !Exists(string("version")))
117 bool fTmp = fReadOnly;
119 WriteVersion(VERSION);
123 mapDb[strFile] = pdb;
133 vTxn.front()->abort();
137 // Flush database activity from memory pool to disk log
138 unsigned int nMinutes = 0;
141 if (strFile == "addr.dat")
143 if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
145 dbenv.txn_checkpoint(0, nMinutes, 0);
147 CRITICAL_BLOCK(cs_db)
148 --mapFileUseCount[strFile];
151 void static CloseDb(const string& strFile)
153 CRITICAL_BLOCK(cs_db)
155 if (mapDb[strFile] != NULL)
157 // Close the database handle
158 Db* pdb = mapDb[strFile];
161 mapDb[strFile] = NULL;
166 void DBFlush(bool fShutdown)
168 // Flush log data to the actual data file
169 // on all files that are not in use
170 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
173 CRITICAL_BLOCK(cs_db)
175 map<string, int>::iterator mi = mapFileUseCount.begin();
176 while (mi != mapFileUseCount.end())
178 string strFile = (*mi).first;
179 int nRefCount = (*mi).second;
180 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
183 // Move log data to the dat file
185 dbenv.txn_checkpoint(0, 0, 0);
186 printf("%s flush\n", strFile.c_str());
187 dbenv.lsn_reset(strFile.c_str(), 0);
188 mapFileUseCount.erase(mi++);
196 if (mapFileUseCount.empty())
197 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
213 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
217 return Read(make_pair(string("tx"), hash), txindex);
220 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
223 return Write(make_pair(string("tx"), hash), txindex);
226 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
231 uint256 hash = tx.GetHash();
232 CTxIndex txindex(pos, tx.vout.size());
233 return Write(make_pair(string("tx"), hash), txindex);
236 bool CTxDB::EraseTxIndex(const CTransaction& tx)
239 uint256 hash = tx.GetHash();
241 return Erase(make_pair(string("tx"), hash));
244 bool CTxDB::ContainsTx(uint256 hash)
247 return Exists(make_pair(string("tx"), hash));
250 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
256 Dbc* pcursor = GetCursor();
260 unsigned int fFlags = DB_SET_RANGE;
265 if (fFlags == DB_SET_RANGE)
266 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
268 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
270 if (ret == DB_NOTFOUND)
282 ssKey >> strType >> hashItem >> pos;
284 ssValue >> nItemHeight;
287 if (strType != "owner" || hashItem != hash160)
289 if (nItemHeight >= nMinHeight)
291 vtx.resize(vtx.size()+1);
292 if (!vtx.back().ReadFromDisk(pos))
304 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
308 if (!ReadTxIndex(hash, txindex))
310 return (tx.ReadFromDisk(txindex.pos));
313 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
316 return ReadDiskTx(hash, tx, txindex);
319 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
321 return ReadDiskTx(outpoint.hash, tx, txindex);
324 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
327 return ReadDiskTx(outpoint.hash, tx, txindex);
330 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
332 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
335 bool CTxDB::EraseBlockIndex(uint256 hash)
337 return Erase(make_pair(string("blockindex"), hash));
340 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
342 return Read(string("hashBestChain"), hashBestChain);
345 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
347 return Write(string("hashBestChain"), hashBestChain);
350 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
352 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
355 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
357 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
360 CBlockIndex static * InsertBlockIndex(uint256 hash)
366 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
367 if (mi != mapBlockIndex.end())
371 CBlockIndex* pindexNew = new CBlockIndex();
373 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
374 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
375 pindexNew->phashBlock = &((*mi).first);
380 bool CTxDB::LoadBlockIndex()
382 // Get database cursor
383 Dbc* pcursor = GetCursor();
387 // Load mapBlockIndex
388 unsigned int fFlags = DB_SET_RANGE;
393 if (fFlags == DB_SET_RANGE)
394 ssKey << make_pair(string("blockindex"), uint256(0));
396 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
398 if (ret == DB_NOTFOUND)
406 if (strType == "blockindex")
408 CDiskBlockIndex diskindex;
409 ssValue >> diskindex;
411 // Construct block index object
412 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
413 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
414 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
415 pindexNew->nFile = diskindex.nFile;
416 pindexNew->nBlockPos = diskindex.nBlockPos;
417 pindexNew->nHeight = diskindex.nHeight;
418 pindexNew->nVersion = diskindex.nVersion;
419 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
420 pindexNew->nTime = diskindex.nTime;
421 pindexNew->nBits = diskindex.nBits;
422 pindexNew->nNonce = diskindex.nNonce;
424 // Watch for genesis block
425 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
426 pindexGenesisBlock = pindexNew;
428 if (!pindexNew->CheckIndex())
429 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
438 // Calculate bnChainWork
439 vector<pair<int, CBlockIndex*> > vSortedByHeight;
440 vSortedByHeight.reserve(mapBlockIndex.size());
441 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
443 CBlockIndex* pindex = item.second;
444 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
446 sort(vSortedByHeight.begin(), vSortedByHeight.end());
447 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
449 CBlockIndex* pindex = item.second;
450 pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
453 // Load hashBestChain pointer to end of best chain
454 if (!ReadHashBestChain(hashBestChain))
456 if (pindexGenesisBlock == NULL)
458 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
460 if (!mapBlockIndex.count(hashBestChain))
461 return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
462 pindexBest = mapBlockIndex[hashBestChain];
463 nBestHeight = pindexBest->nHeight;
464 bnBestChainWork = pindexBest->bnChainWork;
465 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
467 // Load bnBestInvalidWork, OK if it doesn't exist
468 ReadBestInvalidWork(bnBestInvalidWork);
470 // Verify blocks in the best chain
471 CBlockIndex* pindexFork = NULL;
472 for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
474 if (pindex->nHeight < nBestHeight-2500 && !mapArgs.count("-checkblocks"))
477 if (!block.ReadFromDisk(pindex))
478 return error("LoadBlockIndex() : block.ReadFromDisk failed");
479 if (!block.CheckBlock())
481 printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
482 pindexFork = pindex->pprev;
487 // Reorg back to the fork
488 printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
490 if (!block.ReadFromDisk(pindexFork))
491 return error("LoadBlockIndex() : block.ReadFromDisk failed");
493 block.SetBestChain(txdb, pindexFork);
507 bool CAddrDB::WriteAddress(const CAddress& addr)
509 return Write(make_pair(string("addr"), addr.GetKey()), addr);
512 bool CAddrDB::EraseAddress(const CAddress& addr)
514 return Erase(make_pair(string("addr"), addr.GetKey()));
517 bool CAddrDB::LoadAddresses()
519 CRITICAL_BLOCK(cs_mapAddresses)
521 // Load user provided addresses
522 CAutoFile filein = fopen((GetDataDir() + "/addr.txt").c_str(), "rt");
528 while (fgets(psz, sizeof(psz), filein))
530 CAddress addr(psz, NODE_NETWORK);
531 addr.nTime = 0; // so it won't relay unless successfully connected
540 Dbc* pcursor = GetCursor();
549 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
550 if (ret == DB_NOTFOUND)
558 if (strType == "addr")
562 mapAddresses.insert(make_pair(addr.GetKey(), addr));
567 printf("Loaded %d addresses\n", mapAddresses.size());
575 return CAddrDB("cr+").LoadAddresses();
585 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
588 return Write(make_pair(string("name"), strAddress), strName);
591 bool CWalletDB::EraseName(const string& strAddress)
593 // This should only be used for sending addresses, never for receiving addresses,
594 // receiving addresses must always have an address book entry if they're not change return.
596 return Erase(make_pair(string("name"), strAddress));
599 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
602 return Read(make_pair(string("acc"), strAccount), account);
605 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
607 return Write(make_pair(string("acc"), strAccount), account);
610 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
612 return Write(make_tuple(string("acentry"), acentry.strAccount, ++nAccountingEntryNumber), acentry);
615 int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
617 list<CAccountingEntry> entries;
618 ListAccountCreditDebit(strAccount, entries);
620 int64 nCreditDebit = 0;
621 BOOST_FOREACH (const CAccountingEntry& entry, entries)
622 nCreditDebit += entry.nCreditDebit;
627 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
629 int64 nCreditDebit = 0;
631 bool fAllAccounts = (strAccount == "*");
633 Dbc* pcursor = GetCursor();
635 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
636 unsigned int fFlags = DB_SET_RANGE;
641 if (fFlags == DB_SET_RANGE)
642 ssKey << make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
644 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
646 if (ret == DB_NOTFOUND)
651 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
657 if (strType != "acentry")
659 CAccountingEntry acentry;
660 ssKey >> acentry.strAccount;
661 if (!fAllAccounts && acentry.strAccount != strAccount)
665 entries.push_back(acentry);
672 bool CWalletDB::LoadWallet(CWallet* pwallet)
674 pwallet->vchDefaultKey.clear();
675 int nFileVersion = 0;
676 vector<uint256> vWalletUpgrade;
680 // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
681 fMinimizeToTray = false;
682 fMinimizeOnClose = false;
685 //// todo: shouldn't we catch exceptions and try to recover and continue?
686 CRITICAL_BLOCK(pwallet->cs_mapWallet)
687 CRITICAL_BLOCK(pwallet->cs_mapKeys)
690 Dbc* pcursor = GetCursor();
699 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
700 if (ret == DB_NOTFOUND)
706 // Taking advantage of the fact that pair serialization
707 // is just the two items serialized one after the other
710 if (strType == "name")
714 ssValue >> pwallet->mapAddressBook[strAddress];
716 else if (strType == "tx")
720 CWalletTx& wtx = pwallet->mapWallet[hash];
722 wtx.pwallet = pwallet;
724 if (wtx.GetHash() != hash)
725 printf("Error in wallet.dat, hash mismatch\n");
727 // Undo serialize changes in 31600
728 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
730 if (!ssValue.empty())
734 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
735 printf("LoadWallet() upgrading tx ver=%d %d '%s' %s\n", wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
736 wtx.fTimeReceivedIsTxTime = fTmp;
740 printf("LoadWallet() repairing tx ver=%d %s\n", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
741 wtx.fTimeReceivedIsTxTime = 0;
743 vWalletUpgrade.push_back(hash);
747 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
748 //printf(" %12I64d %s %s %s\n",
749 // wtx.vout[0].nValue,
750 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
751 // wtx.hashBlock.ToString().substr(0,20).c_str(),
752 // wtx.mapValue["message"].c_str());
754 else if (strType == "acentry")
760 if (nNumber > nAccountingEntryNumber)
761 nAccountingEntryNumber = nNumber;
763 else if (strType == "key" || strType == "wkey")
765 vector<unsigned char> vchPubKey;
768 if (strType == "key")
769 ssValue >> wkey.vchPrivKey;
773 pwallet->mapKeys[vchPubKey] = wkey.vchPrivKey;
774 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
776 else if (strType == "defaultkey")
778 ssValue >> pwallet->vchDefaultKey;
780 else if (strType == "pool")
784 pwallet->setKeyPool.insert(nIndex);
786 else if (strType == "version")
788 ssValue >> nFileVersion;
789 if (nFileVersion == 10300)
792 else if (strType == "setting")
799 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
801 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
802 if (strKey == "addrIncoming") ssValue >> addrIncoming;
803 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
804 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
805 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
806 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
807 if (strKey == "fUseProxy") ssValue >> fUseProxy;
808 if (strKey == "addrProxy") ssValue >> addrProxy;
809 if (fHaveUPnP && strKey == "fUseUPnP") ssValue >> fUseUPnP;
815 BOOST_FOREACH(uint256 hash, vWalletUpgrade)
816 WriteTx(hash, pwallet->mapWallet[hash]);
818 printf("nFileVersion = %d\n", nFileVersion);
819 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
820 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
821 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
822 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
823 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
824 printf("fUseProxy = %d\n", fUseProxy);
825 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
827 printf("fUseUPnP = %d\n", fUseUPnP);
831 if (nFileVersion < VERSION)
833 // Get rid of old debug.log file in current directory
834 if (nFileVersion <= 105 && !pszSetDataDir[0])
837 WriteVersion(VERSION);
844 void ThreadFlushWalletDB(void* parg)
846 const string& strFile = ((const string*)parg)[0];
847 static bool fOneThread;
851 if (mapArgs.count("-noflushwallet"))
854 unsigned int nLastSeen = nWalletDBUpdated;
855 unsigned int nLastFlushed = nWalletDBUpdated;
856 int64 nLastWalletUpdate = GetTime();
861 if (nLastSeen != nWalletDBUpdated)
863 nLastSeen = nWalletDBUpdated;
864 nLastWalletUpdate = GetTime();
867 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
869 TRY_CRITICAL_BLOCK(cs_db)
871 // Don't do this if any databases are in use
873 map<string, int>::iterator mi = mapFileUseCount.begin();
874 while (mi != mapFileUseCount.end())
876 nRefCount += (*mi).second;
880 if (nRefCount == 0 && !fShutdown)
882 map<string, int>::iterator mi = mapFileUseCount.find(strFile);
883 if (mi != mapFileUseCount.end())
885 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
886 printf("Flushing wallet.dat\n");
887 nLastFlushed = nWalletDBUpdated;
888 int64 nStart = GetTimeMillis();
890 // Flush wallet.dat so it's self contained
892 dbenv.txn_checkpoint(0, 0, 0);
893 dbenv.lsn_reset(strFile.c_str(), 0);
895 mapFileUseCount.erase(mi++);
896 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
904 bool BackupWallet(const CWallet& wallet, const string& strDest)
906 if (!wallet.fFileBacked)
910 CRITICAL_BLOCK(cs_db)
912 if (!mapFileUseCount.count(wallet.strWalletFile) || mapFileUseCount[wallet.strWalletFile] == 0)
914 // Flush log data to the dat file
915 CloseDb(wallet.strWalletFile);
916 dbenv.txn_checkpoint(0, 0, 0);
917 dbenv.lsn_reset(wallet.strWalletFile.c_str(), 0);
918 mapFileUseCount.erase(wallet.strWalletFile);
921 filesystem::path pathSrc(GetDataDir() + "/" + wallet.strWalletFile);
922 filesystem::path pathDest(strDest);
923 if (filesystem::is_directory(pathDest))
924 pathDest = pathDest / wallet.strWalletFile;
925 #if BOOST_VERSION >= 104000
926 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
928 filesystem::copy_file(pathSrc, pathDest);
930 printf("copied wallet.dat to %s\n", pathDest.string().c_str());