1 // Copyright (c) 2009-2010 Satoshi Nakamoto
\r
2 // Distributed under the MIT/X11 software license, see the accompanying
\r
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
\r
7 void ThreadFlushWalletDB(void* parg);
\r
10 unsigned int nWalletDBUpdated;
\r
19 static CCriticalSection cs_db;
\r
20 static bool fDbEnvInit = false;
\r
22 static map<string, int> mapFileUseCount;
\r
23 static map<string, Db*> mapDb;
\r
40 instance_of_cdbinit;
\r
43 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
\r
46 if (pszFile == NULL)
\r
49 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
\r
50 bool fCreate = strchr(pszMode, 'c');
\r
51 unsigned int nFlags = DB_THREAD;
\r
53 nFlags |= DB_CREATE;
\r
55 CRITICAL_BLOCK(cs_db)
\r
61 string strDataDir = GetDataDir();
\r
62 string strLogDir = strDataDir + "/database";
\r
63 filesystem::create_directory(strLogDir.c_str());
\r
64 string strErrorFile = strDataDir + "/db.log";
\r
65 printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
\r
67 dbenv.set_lg_dir(strLogDir.c_str());
\r
68 dbenv.set_lg_max(10000000);
\r
69 dbenv.set_lk_max_locks(10000);
\r
70 dbenv.set_lk_max_objects(10000);
\r
71 dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
\r
72 dbenv.set_flags(DB_AUTO_COMMIT, 1);
\r
73 ret = dbenv.open(strDataDir.c_str(),
\r
84 throw runtime_error(strprintf("CDB() : error %d opening database environment\n", ret));
\r
89 ++mapFileUseCount[strFile];
\r
90 pdb = mapDb[strFile];
\r
93 pdb = new Db(&dbenv, 0);
\r
95 ret = pdb->open(NULL, // Txn pointer
\r
96 pszFile, // Filename
\r
97 "main", // Logical db name
\r
98 DB_BTREE, // Database type
\r
106 CRITICAL_BLOCK(cs_db)
\r
107 --mapFileUseCount[strFile];
\r
109 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d\n", pszFile, ret));
\r
112 if (fCreate && !Exists(string("version")))
\r
114 bool fTmp = fReadOnly;
\r
116 WriteVersion(VERSION);
\r
120 mapDb[strFile] = pdb;
\r
130 vTxn.front()->abort();
\r
134 // Flush database activity from memory pool to disk log
\r
135 unsigned int nMinutes = 0;
\r
136 if (strFile == "addr.dat")
\r
138 if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
\r
140 dbenv.txn_checkpoint(0, nMinutes, 0);
\r
142 CRITICAL_BLOCK(cs_db)
\r
143 --mapFileUseCount[strFile];
\r
146 void CloseDb(const string& strFile)
\r
148 CRITICAL_BLOCK(cs_db)
\r
150 if (mapDb[strFile] != NULL)
\r
152 // Close the database handle
\r
153 Db* pdb = mapDb[strFile];
\r
156 mapDb[strFile] = NULL;
\r
161 void DBFlush(bool fShutdown)
\r
163 // Flush log data to the actual data file
\r
164 // on all files that are not in use
\r
165 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
\r
168 CRITICAL_BLOCK(cs_db)
\r
170 map<string, int>::iterator mi = mapFileUseCount.begin();
\r
171 while (mi != mapFileUseCount.end())
\r
173 string strFile = (*mi).first;
\r
174 int nRefCount = (*mi).second;
\r
175 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
\r
176 if (nRefCount == 0)
\r
178 // Move log data to the dat file
\r
180 dbenv.txn_checkpoint(0, 0, 0);
\r
181 printf("%s flush\n", strFile.c_str());
\r
182 dbenv.lsn_reset(strFile.c_str(), 0);
\r
183 mapFileUseCount.erase(mi++);
\r
191 if (mapFileUseCount.empty())
\r
192 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
\r
194 fDbEnvInit = false;
\r
208 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
\r
212 return Read(make_pair(string("tx"), hash), txindex);
\r
215 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
\r
218 return Write(make_pair(string("tx"), hash), txindex);
\r
221 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
\r
226 uint256 hash = tx.GetHash();
\r
227 CTxIndex txindex(pos, tx.vout.size());
\r
228 return Write(make_pair(string("tx"), hash), txindex);
\r
231 bool CTxDB::EraseTxIndex(const CTransaction& tx)
\r
234 uint256 hash = tx.GetHash();
\r
236 return Erase(make_pair(string("tx"), hash));
\r
239 bool CTxDB::ContainsTx(uint256 hash)
\r
242 return Exists(make_pair(string("tx"), hash));
\r
245 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
\r
251 Dbc* pcursor = GetCursor();
\r
255 unsigned int fFlags = DB_SET_RANGE;
\r
258 // Read next record
\r
260 if (fFlags == DB_SET_RANGE)
\r
261 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
\r
262 CDataStream ssValue;
\r
263 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
\r
265 if (ret == DB_NOTFOUND)
\r
277 ssKey >> strType >> hashItem >> pos;
\r
279 ssValue >> nItemHeight;
\r
281 // Read transaction
\r
282 if (strType != "owner" || hashItem != hash160)
\r
284 if (nItemHeight >= nMinHeight)
\r
286 vtx.resize(vtx.size()+1);
\r
287 if (!vtx.back().ReadFromDisk(pos))
\r
299 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
\r
303 if (!ReadTxIndex(hash, txindex))
\r
305 return (tx.ReadFromDisk(txindex.pos));
\r
308 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
\r
311 return ReadDiskTx(hash, tx, txindex);
\r
314 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
\r
316 return ReadDiskTx(outpoint.hash, tx, txindex);
\r
319 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
\r
322 return ReadDiskTx(outpoint.hash, tx, txindex);
\r
325 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
\r
327 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
\r
330 bool CTxDB::EraseBlockIndex(uint256 hash)
\r
332 return Erase(make_pair(string("blockindex"), hash));
\r
335 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
\r
337 return Read(string("hashBestChain"), hashBestChain);
\r
340 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
\r
342 return Write(string("hashBestChain"), hashBestChain);
\r
345 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
\r
347 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
\r
350 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
\r
352 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
\r
355 CBlockIndex* InsertBlockIndex(uint256 hash)
\r
361 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
\r
362 if (mi != mapBlockIndex.end())
\r
363 return (*mi).second;
\r
366 CBlockIndex* pindexNew = new CBlockIndex();
\r
368 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
\r
369 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
\r
370 pindexNew->phashBlock = &((*mi).first);
\r
375 bool CTxDB::LoadBlockIndex()
\r
377 // Get database cursor
\r
378 Dbc* pcursor = GetCursor();
\r
382 // Load mapBlockIndex
\r
383 unsigned int fFlags = DB_SET_RANGE;
\r
386 // Read next record
\r
388 if (fFlags == DB_SET_RANGE)
\r
389 ssKey << make_pair(string("blockindex"), uint256(0));
\r
390 CDataStream ssValue;
\r
391 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
\r
393 if (ret == DB_NOTFOUND)
\r
401 if (strType == "blockindex")
\r
403 CDiskBlockIndex diskindex;
\r
404 ssValue >> diskindex;
\r
406 // Construct block index object
\r
407 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
\r
408 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
\r
409 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
\r
410 pindexNew->nFile = diskindex.nFile;
\r
411 pindexNew->nBlockPos = diskindex.nBlockPos;
\r
412 pindexNew->nHeight = diskindex.nHeight;
\r
413 pindexNew->nVersion = diskindex.nVersion;
\r
414 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
\r
415 pindexNew->nTime = diskindex.nTime;
\r
416 pindexNew->nBits = diskindex.nBits;
\r
417 pindexNew->nNonce = diskindex.nNonce;
\r
419 // Watch for genesis block
\r
420 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
\r
421 pindexGenesisBlock = pindexNew;
\r
430 // Calculate bnChainWork
\r
431 vector<pair<int, CBlockIndex*> > vSortedByHeight;
\r
432 vSortedByHeight.reserve(mapBlockIndex.size());
\r
433 foreach(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
\r
435 CBlockIndex* pindex = item.second;
\r
436 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
\r
438 sort(vSortedByHeight.begin(), vSortedByHeight.end());
\r
439 foreach(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
\r
441 CBlockIndex* pindex = item.second;
\r
442 pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
\r
445 // Load hashBestChain pointer to end of best chain
\r
446 if (!ReadHashBestChain(hashBestChain))
\r
448 if (pindexGenesisBlock == NULL)
\r
450 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
\r
452 if (!mapBlockIndex.count(hashBestChain))
\r
453 return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
\r
454 pindexBest = mapBlockIndex[hashBestChain];
\r
455 nBestHeight = pindexBest->nHeight;
\r
456 bnBestChainWork = pindexBest->bnChainWork;
\r
457 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,16).c_str(), nBestHeight);
\r
459 // Load bnBestInvalidWork, OK if it doesn't exist
\r
460 ReadBestInvalidWork(bnBestInvalidWork);
\r
462 // Verify blocks in the best chain
\r
463 CBlockIndex* pindexFork = NULL;
\r
464 for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
\r
467 if (!block.ReadFromDisk(pindex))
\r
468 return error("LoadBlockIndex() : block.ReadFromDisk failed");
\r
469 if (!block.CheckBlock())
\r
471 printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
\r
472 pindexFork = pindex->pprev;
\r
477 // Reorg back to the fork
\r
478 printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
\r
480 if (!block.ReadFromDisk(pindexFork))
\r
481 return error("LoadBlockIndex() : block.ReadFromDisk failed");
\r
483 block.SetBestChain(txdb, pindexFork);
\r
497 bool CAddrDB::WriteAddress(const CAddress& addr)
\r
499 return Write(make_pair(string("addr"), addr.GetKey()), addr);
\r
502 bool CAddrDB::LoadAddresses()
\r
504 CRITICAL_BLOCK(cs_mapAddresses)
\r
506 // Load user provided addresses
\r
507 CAutoFile filein = fopen((GetDataDir() + "/addr.txt").c_str(), "rt");
\r
513 while (fgets(psz, sizeof(psz), filein))
\r
515 CAddress addr(psz, NODE_NETWORK);
\r
516 addr.nTime = 0; // so it won't relay unless successfully connected
\r
517 if (addr.IsValid())
\r
525 Dbc* pcursor = GetCursor();
\r
531 // Read next record
\r
533 CDataStream ssValue;
\r
534 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
\r
535 if (ret == DB_NOTFOUND)
\r
543 if (strType == "addr")
\r
547 mapAddresses.insert(make_pair(addr.GetKey(), addr));
\r
552 printf("Loaded %d addresses\n", mapAddresses.size());
\r
554 // Fix for possible bug that manifests in mapAddresses.count in irc.cpp,
\r
555 // just need to call count here and it doesn't happen there. The bug was the
\r
556 // pack pragma in irc.cpp and has been fixed, but I'm not in a hurry to delete this.
\r
557 mapAddresses.count(vector<unsigned char>(18));
\r
563 bool LoadAddresses()
\r
565 return CAddrDB("cr+").LoadAddresses();
\r
575 bool CWalletDB::LoadWallet()
\r
577 vchDefaultKey.clear();
\r
578 int nFileVersion = 0;
\r
582 // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
\r
583 fMinimizeToTray = false;
\r
584 fMinimizeOnClose = false;
\r
587 //// todo: shouldn't we catch exceptions and try to recover and continue?
\r
588 CRITICAL_BLOCK(cs_mapKeys)
\r
589 CRITICAL_BLOCK(cs_mapWallet)
\r
592 Dbc* pcursor = GetCursor();
\r
598 // Read next record
\r
600 CDataStream ssValue;
\r
601 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
\r
602 if (ret == DB_NOTFOUND)
\r
608 // Taking advantage of the fact that pair serialization
\r
609 // is just the two items serialized one after the other
\r
612 if (strType == "name")
\r
615 ssKey >> strAddress;
\r
616 ssValue >> mapAddressBook[strAddress];
\r
618 else if (strType == "tx")
\r
622 CWalletTx& wtx = mapWallet[hash];
\r
625 if (wtx.GetHash() != hash)
\r
626 printf("Error in wallet.dat, hash mismatch\n");
\r
629 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
\r
630 //printf(" %12I64d %s %s %s\n",
\r
631 // wtx.vout[0].nValue,
\r
632 // DateTimeStrFormat("%x %H:%M:%S", wtx.nTime).c_str(),
\r
633 // wtx.hashBlock.ToString().substr(0,16).c_str(),
\r
634 // wtx.mapValue["message"].c_str());
\r
636 else if (strType == "key" || strType == "wkey")
\r
638 vector<unsigned char> vchPubKey;
\r
639 ssKey >> vchPubKey;
\r
641 if (strType == "key")
\r
642 ssValue >> wkey.vchPrivKey;
\r
646 mapKeys[vchPubKey] = wkey.vchPrivKey;
\r
647 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
\r
649 else if (strType == "defaultkey")
\r
651 ssValue >> vchDefaultKey;
\r
653 else if (strType == "version")
\r
655 ssValue >> nFileVersion;
\r
656 if (nFileVersion == 10300)
\r
657 nFileVersion = 300;
\r
659 else if (strType == "setting")
\r
665 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
\r
668 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
\r
669 if (strKey == "addrIncoming") ssValue >> addrIncoming;
\r
670 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
\r
671 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
\r
672 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
\r
673 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
\r
674 if (strKey == "fUseProxy") ssValue >> fUseProxy;
\r
675 if (strKey == "addrProxy") ssValue >> addrProxy;
\r
682 printf("nFileVersion = %d\n", nFileVersion);
\r
683 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
\r
684 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
\r
685 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
\r
686 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
\r
687 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
\r
688 printf("fUseProxy = %d\n", fUseProxy);
\r
689 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
\r
692 // The transaction fee setting won't be needed for many years to come.
\r
693 // Setting it to zero here in case they set it to something in an earlier version.
\r
694 if (nTransactionFee != 0)
\r
696 nTransactionFee = 0;
\r
697 WriteSetting("nTransactionFee", nTransactionFee);
\r
701 if (nFileVersion < VERSION)
\r
703 // Get rid of old debug.log file in current directory
\r
704 if (nFileVersion <= 105 && !pszSetDataDir[0])
\r
705 unlink("debug.log");
\r
707 WriteVersion(VERSION);
\r
713 bool LoadWallet(bool& fFirstRunRet)
\r
715 fFirstRunRet = false;
\r
716 if (!CWalletDB("cr+").LoadWallet())
\r
718 fFirstRunRet = vchDefaultKey.empty();
\r
720 if (mapKeys.count(vchDefaultKey))
\r
723 keyUser.SetPubKey(vchDefaultKey);
\r
724 keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
\r
728 // Create new keyUser and set as default key
\r
729 RandAddSeedPerfmon();
\r
730 keyUser.MakeNewKey();
\r
731 if (!AddKey(keyUser))
\r
733 if (!SetAddressBookName(PubKeyToAddress(keyUser.GetPubKey()), "Your Address"))
\r
735 CWalletDB().WriteDefaultKey(keyUser.GetPubKey());
\r
738 CreateThread(ThreadFlushWalletDB, NULL);
\r
742 void ThreadFlushWalletDB(void* parg)
\r
744 static bool fOneThread;
\r
748 if (mapArgs.count("-noflushwallet"))
\r
751 unsigned int nLastSeen = nWalletDBUpdated;
\r
752 unsigned int nLastFlushed = nWalletDBUpdated;
\r
753 int64 nLastWalletUpdate = GetTime();
\r
758 if (nLastSeen != nWalletDBUpdated)
\r
760 nLastSeen = nWalletDBUpdated;
\r
761 nLastWalletUpdate = GetTime();
\r
764 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
\r
766 TRY_CRITICAL_BLOCK(cs_db)
\r
768 // Don't do this if any databases are in use
\r
770 map<string, int>::iterator mi = mapFileUseCount.begin();
\r
771 while (mi != mapFileUseCount.end())
\r
773 nRefCount += (*mi).second;
\r
777 if (nRefCount == 0 && !fShutdown)
\r
779 string strFile = "wallet.dat";
\r
780 map<string, int>::iterator mi = mapFileUseCount.find(strFile);
\r
781 if (mi != mapFileUseCount.end())
\r
783 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
\r
784 printf("Flushing wallet.dat\n");
\r
785 nLastFlushed = nWalletDBUpdated;
\r
786 int64 nStart = GetTimeMillis();
\r
788 // Flush wallet.dat so it's self contained
\r
790 dbenv.txn_checkpoint(0, 0, 0);
\r
791 dbenv.lsn_reset(strFile.c_str(), 0);
\r
793 mapFileUseCount.erase(mi++);
\r
794 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
\r