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.
7 void ThreadFlushWalletDB(void* parg);
10 unsigned int nWalletDBUpdated;
19 static CCriticalSection cs_db;
20 static bool fDbEnvInit = false;
22 static map<string, int> mapFileUseCount;
23 static map<string, Db*> mapDb;
43 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
49 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
50 bool fCreate = strchr(pszMode, 'c');
51 unsigned int nFlags = DB_THREAD;
61 string strDataDir = GetDataDir();
62 string strLogDir = strDataDir + "/database";
63 filesystem::create_directory(strLogDir.c_str());
64 string strErrorFile = strDataDir + "/db.log";
65 printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
67 dbenv.set_lg_dir(strLogDir.c_str());
68 dbenv.set_lg_max(10000000);
69 dbenv.set_lk_max_locks(10000);
70 dbenv.set_lk_max_objects(10000);
71 dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
72 dbenv.set_flags(DB_AUTO_COMMIT, 1);
73 ret = dbenv.open(strDataDir.c_str(),
83 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
88 ++mapFileUseCount[strFile];
92 pdb = new Db(&dbenv, 0);
94 ret = pdb->open(NULL, // Txn pointer
96 "main", // Logical db name
97 DB_BTREE, // Database type
105 CRITICAL_BLOCK(cs_db)
106 --mapFileUseCount[strFile];
108 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
111 if (fCreate && !Exists(string("version")))
113 bool fTmp = fReadOnly;
115 WriteVersion(VERSION);
119 mapDb[strFile] = pdb;
129 vTxn.front()->abort();
133 // Flush database activity from memory pool to disk log
134 unsigned int nMinutes = 0;
135 if (strFile == "addr.dat")
137 if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
139 dbenv.txn_checkpoint(0, nMinutes, 0);
141 CRITICAL_BLOCK(cs_db)
142 --mapFileUseCount[strFile];
145 void CloseDb(const string& strFile)
147 CRITICAL_BLOCK(cs_db)
149 if (mapDb[strFile] != NULL)
151 // Close the database handle
152 Db* pdb = mapDb[strFile];
155 mapDb[strFile] = NULL;
160 void DBFlush(bool fShutdown)
162 // Flush log data to the actual data file
163 // on all files that are not in use
164 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
167 CRITICAL_BLOCK(cs_db)
169 map<string, int>::iterator mi = mapFileUseCount.begin();
170 while (mi != mapFileUseCount.end())
172 string strFile = (*mi).first;
173 int nRefCount = (*mi).second;
174 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
177 // Move log data to the dat file
179 dbenv.txn_checkpoint(0, 0, 0);
180 printf("%s flush\n", strFile.c_str());
181 dbenv.lsn_reset(strFile.c_str(), 0);
182 mapFileUseCount.erase(mi++);
190 if (mapFileUseCount.empty())
191 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
207 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
211 return Read(make_pair(string("tx"), hash), txindex);
214 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
217 return Write(make_pair(string("tx"), hash), txindex);
220 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
225 uint256 hash = tx.GetHash();
226 CTxIndex txindex(pos, tx.vout.size());
227 return Write(make_pair(string("tx"), hash), txindex);
230 bool CTxDB::EraseTxIndex(const CTransaction& tx)
233 uint256 hash = tx.GetHash();
235 return Erase(make_pair(string("tx"), hash));
238 bool CTxDB::ContainsTx(uint256 hash)
241 return Exists(make_pair(string("tx"), hash));
244 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
250 Dbc* pcursor = GetCursor();
254 unsigned int fFlags = DB_SET_RANGE;
259 if (fFlags == DB_SET_RANGE)
260 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
262 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
264 if (ret == DB_NOTFOUND)
276 ssKey >> strType >> hashItem >> pos;
278 ssValue >> nItemHeight;
281 if (strType != "owner" || hashItem != hash160)
283 if (nItemHeight >= nMinHeight)
285 vtx.resize(vtx.size()+1);
286 if (!vtx.back().ReadFromDisk(pos))
298 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
302 if (!ReadTxIndex(hash, txindex))
304 return (tx.ReadFromDisk(txindex.pos));
307 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
310 return ReadDiskTx(hash, tx, txindex);
313 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
315 return ReadDiskTx(outpoint.hash, tx, txindex);
318 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
321 return ReadDiskTx(outpoint.hash, tx, txindex);
324 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
326 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
329 bool CTxDB::EraseBlockIndex(uint256 hash)
331 return Erase(make_pair(string("blockindex"), hash));
334 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
336 return Read(string("hashBestChain"), hashBestChain);
339 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
341 return Write(string("hashBestChain"), hashBestChain);
344 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
346 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
349 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
351 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
354 CBlockIndex* InsertBlockIndex(uint256 hash)
360 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
361 if (mi != mapBlockIndex.end())
365 CBlockIndex* pindexNew = new CBlockIndex();
367 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
368 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
369 pindexNew->phashBlock = &((*mi).first);
374 bool CTxDB::LoadBlockIndex()
376 // Get database cursor
377 Dbc* pcursor = GetCursor();
381 // Load mapBlockIndex
382 unsigned int fFlags = DB_SET_RANGE;
387 if (fFlags == DB_SET_RANGE)
388 ssKey << make_pair(string("blockindex"), uint256(0));
390 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
392 if (ret == DB_NOTFOUND)
400 if (strType == "blockindex")
402 CDiskBlockIndex diskindex;
403 ssValue >> diskindex;
405 // Construct block index object
406 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
407 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
408 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
409 pindexNew->nFile = diskindex.nFile;
410 pindexNew->nBlockPos = diskindex.nBlockPos;
411 pindexNew->nHeight = diskindex.nHeight;
412 pindexNew->nVersion = diskindex.nVersion;
413 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
414 pindexNew->nTime = diskindex.nTime;
415 pindexNew->nBits = diskindex.nBits;
416 pindexNew->nNonce = diskindex.nNonce;
418 // Watch for genesis block
419 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
420 pindexGenesisBlock = pindexNew;
422 if (!pindexNew->CheckIndex())
423 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
432 // Calculate bnChainWork
433 vector<pair<int, CBlockIndex*> > vSortedByHeight;
434 vSortedByHeight.reserve(mapBlockIndex.size());
435 foreach(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
437 CBlockIndex* pindex = item.second;
438 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
440 sort(vSortedByHeight.begin(), vSortedByHeight.end());
441 foreach(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
443 CBlockIndex* pindex = item.second;
444 pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
447 // Load hashBestChain pointer to end of best chain
448 if (!ReadHashBestChain(hashBestChain))
450 if (pindexGenesisBlock == NULL)
452 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
454 if (!mapBlockIndex.count(hashBestChain))
455 return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
456 pindexBest = mapBlockIndex[hashBestChain];
457 nBestHeight = pindexBest->nHeight;
458 bnBestChainWork = pindexBest->bnChainWork;
459 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
461 // Load bnBestInvalidWork, OK if it doesn't exist
462 ReadBestInvalidWork(bnBestInvalidWork);
464 // Verify blocks in the best chain
465 CBlockIndex* pindexFork = NULL;
466 for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
468 if (pindex->nHeight < nBestHeight-2500 && !mapArgs.count("-checkblocks"))
471 if (!block.ReadFromDisk(pindex))
472 return error("LoadBlockIndex() : block.ReadFromDisk failed");
473 if (!block.CheckBlock())
475 printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
476 pindexFork = pindex->pprev;
481 // Reorg back to the fork
482 printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
484 if (!block.ReadFromDisk(pindexFork))
485 return error("LoadBlockIndex() : block.ReadFromDisk failed");
487 block.SetBestChain(txdb, pindexFork);
501 bool CAddrDB::WriteAddress(const CAddress& addr)
503 return Write(make_pair(string("addr"), addr.GetKey()), addr);
506 bool CAddrDB::EraseAddress(const CAddress& addr)
508 return Erase(make_pair(string("addr"), addr.GetKey()));
511 bool CAddrDB::LoadAddresses()
513 CRITICAL_BLOCK(cs_mapAddresses)
515 // Load user provided addresses
516 CAutoFile filein = fopen((GetDataDir() + "/addr.txt").c_str(), "rt");
522 while (fgets(psz, sizeof(psz), filein))
524 CAddress addr(psz, NODE_NETWORK);
525 addr.nTime = 0; // so it won't relay unless successfully connected
534 Dbc* pcursor = GetCursor();
543 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
544 if (ret == DB_NOTFOUND)
552 if (strType == "addr")
556 mapAddresses.insert(make_pair(addr.GetKey(), addr));
561 printf("Loaded %d addresses\n", mapAddresses.size());
569 return CAddrDB("cr+").LoadAddresses();
579 static set<int64> setKeyPool;
580 static CCriticalSection cs_setKeyPool;
582 bool CWalletDB::LoadWallet()
584 vchDefaultKey.clear();
585 int nFileVersion = 0;
589 // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
590 fMinimizeToTray = false;
591 fMinimizeOnClose = false;
594 //// todo: shouldn't we catch exceptions and try to recover and continue?
595 CRITICAL_BLOCK(cs_mapKeys)
596 CRITICAL_BLOCK(cs_mapWallet)
599 Dbc* pcursor = GetCursor();
608 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
609 if (ret == DB_NOTFOUND)
615 // Taking advantage of the fact that pair serialization
616 // is just the two items serialized one after the other
619 if (strType == "name")
623 ssValue >> mapAddressBook[strAddress];
625 else if (strType == "tx")
629 CWalletTx& wtx = mapWallet[hash];
632 if (wtx.GetHash() != hash)
633 printf("Error in wallet.dat, hash mismatch\n");
636 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
637 //printf(" %12I64d %s %s %s\n",
638 // wtx.vout[0].nValue,
639 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
640 // wtx.hashBlock.ToString().substr(0,20).c_str(),
641 // wtx.mapValue["message"].c_str());
643 else if (strType == "key" || strType == "wkey")
645 vector<unsigned char> vchPubKey;
648 if (strType == "key")
649 ssValue >> wkey.vchPrivKey;
653 mapKeys[vchPubKey] = wkey.vchPrivKey;
654 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
656 else if (strType == "defaultkey")
658 ssValue >> vchDefaultKey;
660 else if (strType == "pool")
664 setKeyPool.insert(nIndex);
666 else if (strType == "version")
668 ssValue >> nFileVersion;
669 if (nFileVersion == 10300)
672 else if (strType == "setting")
678 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
681 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
682 if (strKey == "addrIncoming") ssValue >> addrIncoming;
683 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
684 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
685 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
686 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
687 if (strKey == "fUseProxy") ssValue >> fUseProxy;
688 if (strKey == "addrProxy") ssValue >> addrProxy;
695 printf("nFileVersion = %d\n", nFileVersion);
696 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
697 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
698 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
699 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
700 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
701 printf("fUseProxy = %d\n", fUseProxy);
702 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
705 // The transaction fee setting won't be needed for many years to come.
706 // Setting it to zero here in case they set it to something in an earlier version.
707 if (nTransactionFee != 0)
710 WriteSetting("nTransactionFee", nTransactionFee);
714 if (nFileVersion < VERSION)
716 // Get rid of old debug.log file in current directory
717 if (nFileVersion <= 105 && !pszSetDataDir[0])
720 WriteVersion(VERSION);
726 bool LoadWallet(bool& fFirstRunRet)
728 fFirstRunRet = false;
729 if (!CWalletDB("cr+").LoadWallet())
731 fFirstRunRet = vchDefaultKey.empty();
733 if (mapKeys.count(vchDefaultKey))
736 keyUser.SetPubKey(vchDefaultKey);
737 keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
741 // Create new keyUser and set as default key
742 RandAddSeedPerfmon();
743 keyUser.MakeNewKey();
744 if (!AddKey(keyUser))
746 if (!SetAddressBookName(PubKeyToAddress(keyUser.GetPubKey()), "Your Address"))
748 CWalletDB().WriteDefaultKey(keyUser.GetPubKey());
751 CreateThread(ThreadFlushWalletDB, NULL);
755 void ThreadFlushWalletDB(void* parg)
757 static bool fOneThread;
761 if (mapArgs.count("-noflushwallet"))
764 unsigned int nLastSeen = nWalletDBUpdated;
765 unsigned int nLastFlushed = nWalletDBUpdated;
766 int64 nLastWalletUpdate = GetTime();
771 if (nLastSeen != nWalletDBUpdated)
773 nLastSeen = nWalletDBUpdated;
774 nLastWalletUpdate = GetTime();
777 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
779 TRY_CRITICAL_BLOCK(cs_db)
781 // Don't do this if any databases are in use
783 map<string, int>::iterator mi = mapFileUseCount.begin();
784 while (mi != mapFileUseCount.end())
786 nRefCount += (*mi).second;
790 if (nRefCount == 0 && !fShutdown)
792 string strFile = "wallet.dat";
793 map<string, int>::iterator mi = mapFileUseCount.find(strFile);
794 if (mi != mapFileUseCount.end())
796 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
797 printf("Flushing wallet.dat\n");
798 nLastFlushed = nWalletDBUpdated;
799 int64 nStart = GetTimeMillis();
801 // Flush wallet.dat so it's self contained
803 dbenv.txn_checkpoint(0, 0, 0);
804 dbenv.lsn_reset(strFile.c_str(), 0);
806 mapFileUseCount.erase(mi++);
807 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
815 void BackupWallet(const string& strDest)
819 CRITICAL_BLOCK(cs_db)
821 const string strFile = "wallet.dat";
822 if (!mapFileUseCount.count(strFile) || mapFileUseCount[strFile] == 0)
824 // Flush log data to the dat file
826 dbenv.txn_checkpoint(0, 0, 0);
827 dbenv.lsn_reset(strFile.c_str(), 0);
828 mapFileUseCount.erase(strFile);
831 filesystem::path pathSrc(GetDataDir() + "/" + strFile);
832 filesystem::path pathDest(strDest);
833 if (filesystem::is_directory(pathDest))
834 pathDest = pathDest / strFile;
835 #if BOOST_VERSION >= 104000
836 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
838 filesystem::copy_file(pathSrc, pathDest);
840 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
850 void CWalletDB::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
853 keypool.vchPubKey.clear();
854 CRITICAL_BLOCK(cs_main)
855 CRITICAL_BLOCK(cs_mapWallet)
856 CRITICAL_BLOCK(cs_setKeyPool)
859 int64 nTargetSize = max(GetArg("-keypool", 100), (int64)0);
860 while (setKeyPool.size() < nTargetSize+1)
863 if (!setKeyPool.empty())
864 nEnd = *(--setKeyPool.end()) + 1;
865 if (!Write(make_pair(string("pool"), nEnd), CKeyPool(GenerateNewKey())))
866 throw runtime_error("ReserveKeyFromKeyPool() : writing generated key failed");
867 setKeyPool.insert(nEnd);
868 printf("keypool added key %"PRI64d", size=%d\n", nEnd, setKeyPool.size());
871 // Get the oldest key
872 assert(!setKeyPool.empty());
873 nIndex = *(setKeyPool.begin());
874 setKeyPool.erase(setKeyPool.begin());
875 if (!Read(make_pair(string("pool"), nIndex), keypool))
876 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
877 if (!mapKeys.count(keypool.vchPubKey))
878 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
879 assert(!keypool.vchPubKey.empty());
880 printf("keypool reserve %"PRI64d"\n", nIndex);
884 void CWalletDB::KeepKey(int64 nIndex)
886 // Remove from key pool
887 CRITICAL_BLOCK(cs_main)
888 CRITICAL_BLOCK(cs_mapWallet)
890 Erase(make_pair(string("pool"), nIndex));
892 printf("keypool keep %"PRI64d"\n", nIndex);
895 void CWalletDB::ReturnKey(int64 nIndex)
897 // Return to key pool
898 CRITICAL_BLOCK(cs_setKeyPool)
899 setKeyPool.insert(nIndex);
900 printf("keypool return %"PRI64d"\n", nIndex);
903 vector<unsigned char> CWalletDB::GetKeyFromKeyPool()
907 ReserveKeyFromKeyPool(nIndex, keypool);
909 return keypool.vchPubKey;
912 int64 CWalletDB::GetOldestKeyPoolTime()
916 ReserveKeyFromKeyPool(nIndex, keypool);
918 return keypool.nTime;