1 // Copyright (c) 2009 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
39 instance_of_cdbinit;
\r
42 CDB::CDB(const char* pszFile, const char* pszMode, bool fTxn) : pdb(NULL)
\r
45 if (pszFile == NULL)
\r
48 bool fCreate = strchr(pszMode, 'c');
\r
49 bool fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
\r
50 unsigned int nFlags = DB_THREAD;
\r
52 nFlags |= DB_CREATE;
\r
54 nFlags |= DB_RDONLY;
\r
55 if (!fReadOnly || fTxn)
\r
56 nFlags |= DB_AUTO_COMMIT;
\r
58 CRITICAL_BLOCK(cs_db)
\r
64 string strDataDir = GetDataDir();
\r
65 string strLogDir = strDataDir + "/database";
\r
66 _mkdir(strLogDir.c_str());
\r
67 string strErrorFile = strDataDir + "/db.log";
\r
68 printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
\r
70 dbenv.set_lg_dir(strLogDir.c_str());
\r
71 dbenv.set_lg_max(10000000);
\r
72 dbenv.set_lk_max_locks(10000);
\r
73 dbenv.set_lk_max_objects(10000);
\r
74 dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
\r
75 ///dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1); /// causes corruption
\r
76 ret = dbenv.open(strDataDir.c_str(),
\r
87 throw runtime_error(strprintf("CDB() : error %d opening database environment\n", ret));
\r
92 ++mapFileUseCount[strFile];
\r
95 pdb = new Db(&dbenv, 0);
\r
97 ret = pdb->open(NULL, // Txn pointer
\r
98 pszFile, // Filename
\r
99 "main", // Logical db name
\r
100 DB_BTREE, // Database type
\r
108 CRITICAL_BLOCK(cs_db)
\r
109 --mapFileUseCount[strFile];
\r
111 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d\n", pszFile, ret));
\r
114 if (fCreate && !Exists(string("version")))
\r
115 WriteVersion(VERSION);
\r
125 vTxn.front()->abort();
\r
130 dbenv.txn_checkpoint(0, 0, 0);
\r
132 CRITICAL_BLOCK(cs_db)
\r
133 --mapFileUseCount[strFile];
\r
138 void DBFlush(bool fShutdown)
\r
140 // Flush log data to the actual data file
\r
141 // on all files that are not in use
\r
142 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
\r
145 CRITICAL_BLOCK(cs_db)
\r
147 dbenv.txn_checkpoint(0, 0, 0);
\r
148 map<string, int>::iterator mi = mapFileUseCount.begin();
\r
149 while (mi != mapFileUseCount.end())
\r
151 string strFile = (*mi).first;
\r
152 int nRefCount = (*mi).second;
\r
153 if (nRefCount == 0)
\r
155 dbenv.lsn_reset(strFile.c_str(), 0);
\r
156 mapFileUseCount.erase(mi++);
\r
164 if (mapFileUseCount.empty())
\r
165 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
\r
167 fDbEnvInit = false;
\r
181 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
\r
185 return Read(make_pair(string("tx"), hash), txindex);
\r
188 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
\r
191 return Write(make_pair(string("tx"), hash), txindex);
\r
194 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
\r
199 uint256 hash = tx.GetHash();
\r
200 CTxIndex txindex(pos, tx.vout.size());
\r
201 return Write(make_pair(string("tx"), hash), txindex);
\r
204 bool CTxDB::EraseTxIndex(const CTransaction& tx)
\r
207 uint256 hash = tx.GetHash();
\r
209 return Erase(make_pair(string("tx"), hash));
\r
212 bool CTxDB::ContainsTx(uint256 hash)
\r
215 return Exists(make_pair(string("tx"), hash));
\r
218 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
\r
224 Dbc* pcursor = GetCursor();
\r
228 unsigned int fFlags = DB_SET_RANGE;
\r
231 // Read next record
\r
233 if (fFlags == DB_SET_RANGE)
\r
234 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
\r
235 CDataStream ssValue;
\r
236 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
\r
238 if (ret == DB_NOTFOUND)
\r
247 ssKey >> strType >> hashItem >> pos;
\r
249 ssValue >> nItemHeight;
\r
251 // Read transaction
\r
252 if (strType != "owner" || hashItem != hash160)
\r
254 if (nItemHeight >= nMinHeight)
\r
256 vtx.resize(vtx.size()+1);
\r
257 if (!vtx.back().ReadFromDisk(pos))
\r
264 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
\r
268 if (!ReadTxIndex(hash, txindex))
\r
270 return (tx.ReadFromDisk(txindex.pos));
\r
273 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
\r
276 return ReadDiskTx(hash, tx, txindex);
\r
279 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
\r
281 return ReadDiskTx(outpoint.hash, tx, txindex);
\r
284 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
\r
287 return ReadDiskTx(outpoint.hash, tx, txindex);
\r
290 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
\r
292 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
\r
295 bool CTxDB::EraseBlockIndex(uint256 hash)
\r
297 return Erase(make_pair(string("blockindex"), hash));
\r
300 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
\r
302 return Read(string("hashBestChain"), hashBestChain);
\r
305 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
\r
307 return Write(string("hashBestChain"), hashBestChain);
\r
310 CBlockIndex* InsertBlockIndex(uint256 hash)
\r
316 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
\r
317 if (mi != mapBlockIndex.end())
\r
318 return (*mi).second;
\r
321 CBlockIndex* pindexNew = new CBlockIndex();
\r
323 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
\r
324 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
\r
325 pindexNew->phashBlock = &((*mi).first);
\r
330 bool CTxDB::LoadBlockIndex()
\r
333 Dbc* pcursor = GetCursor();
\r
337 unsigned int fFlags = DB_SET_RANGE;
\r
340 // Read next record
\r
342 if (fFlags == DB_SET_RANGE)
\r
343 ssKey << make_pair(string("blockindex"), uint256(0));
\r
344 CDataStream ssValue;
\r
345 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
\r
347 if (ret == DB_NOTFOUND)
\r
355 if (strType == "blockindex")
\r
357 CDiskBlockIndex diskindex;
\r
358 ssValue >> diskindex;
\r
360 // Construct block index object
\r
361 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
\r
362 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
\r
363 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
\r
364 pindexNew->nFile = diskindex.nFile;
\r
365 pindexNew->nBlockPos = diskindex.nBlockPos;
\r
366 pindexNew->nHeight = diskindex.nHeight;
\r
367 pindexNew->nVersion = diskindex.nVersion;
\r
368 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
\r
369 pindexNew->nTime = diskindex.nTime;
\r
370 pindexNew->nBits = diskindex.nBits;
\r
371 pindexNew->nNonce = diskindex.nNonce;
\r
373 // Watch for genesis block and best block
\r
374 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
\r
375 pindexGenesisBlock = pindexNew;
\r
383 if (!ReadHashBestChain(hashBestChain))
\r
385 if (pindexGenesisBlock == NULL)
\r
387 return error("CTxDB::LoadBlockIndex() : hashBestChain not found");
\r
390 if (!mapBlockIndex.count(hashBestChain))
\r
391 return error("CTxDB::LoadBlockIndex() : blockindex for hashBestChain not found");
\r
392 pindexBest = mapBlockIndex[hashBestChain];
\r
393 nBestHeight = pindexBest->nHeight;
\r
394 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,14).c_str(), nBestHeight);
\r
407 bool CAddrDB::WriteAddress(const CAddress& addr)
\r
409 return Write(make_pair(string("addr"), addr.GetKey()), addr);
\r
412 bool CAddrDB::LoadAddresses()
\r
414 CRITICAL_BLOCK(cs_mapAddresses)
\r
416 // Load user provided addresses
\r
417 CAutoFile filein = fopen("addr.txt", "rt");
\r
423 while (fgets(psz, sizeof(psz), filein))
\r
425 CAddress addr(psz, NODE_NETWORK);
\r
426 if (addr.IsValid())
\r
427 AddAddress(*this, addr);
\r
434 Dbc* pcursor = GetCursor();
\r
440 // Read next record
\r
442 CDataStream ssValue;
\r
443 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
\r
444 if (ret == DB_NOTFOUND)
\r
452 if (strType == "addr")
\r
456 mapAddresses.insert(make_pair(addr.GetKey(), addr));
\r
460 printf("Loaded %d addresses\n", mapAddresses.size());
\r
462 // Fix for possible bug that manifests in mapAddresses.count in irc.cpp,
\r
463 // just need to call count here and it doesn't happen there. The bug was the
\r
464 // pack pragma in irc.cpp and has been fixed, but I'm not in a hurry to delete this.
\r
465 mapAddresses.count(vector<unsigned char>(18));
\r
471 bool LoadAddresses()
\r
473 return CAddrDB("cr+").LoadAddresses();
\r
483 bool CReviewDB::ReadReviews(uint256 hash, vector<CReview>& vReviews)
\r
485 vReviews.size(); // msvc workaround, just need to do anything with vReviews
\r
486 return Read(make_pair(string("reviews"), hash), vReviews);
\r
489 bool CReviewDB::WriteReviews(uint256 hash, const vector<CReview>& vReviews)
\r
491 return Write(make_pair(string("reviews"), hash), vReviews);
\r
504 bool CWalletDB::LoadWallet(vector<unsigned char>& vchDefaultKeyRet)
\r
506 vchDefaultKeyRet.clear();
\r
510 // Reports that tray icon can disappear on gnome, leaving no way to access the program
\r
511 fMinimizeToTray = false;
\r
512 fMinimizeOnClose = false;
\r
515 //// todo: shouldn't we catch exceptions and try to recover and continue?
\r
516 CRITICAL_BLOCK(cs_mapKeys)
\r
517 CRITICAL_BLOCK(cs_mapWallet)
\r
520 Dbc* pcursor = GetCursor();
\r
526 // Read next record
\r
528 CDataStream ssValue;
\r
529 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
\r
530 if (ret == DB_NOTFOUND)
\r
536 // Taking advantage of the fact that pair serialization
\r
537 // is just the two items serialized one after the other
\r
540 if (strType == "name")
\r
543 ssKey >> strAddress;
\r
544 ssValue >> mapAddressBook[strAddress];
\r
546 else if (strType == "tx")
\r
550 CWalletTx& wtx = mapWallet[hash];
\r
553 if (wtx.GetHash() != hash)
\r
554 printf("Error in wallet.dat, hash mismatch\n");
\r
557 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
\r
558 //printf(" %12I64d %s %s %s\n",
\r
559 // wtx.vout[0].nValue,
\r
560 // DateTimeStrFormat("%x %H:%M:%S", wtx.nTime).c_str(),
\r
561 // wtx.hashBlock.ToString().substr(0,14).c_str(),
\r
562 // wtx.mapValue["message"].c_str());
\r
564 else if (strType == "key")
\r
566 vector<unsigned char> vchPubKey;
\r
567 ssKey >> vchPubKey;
\r
568 CPrivKey vchPrivKey;
\r
569 ssValue >> vchPrivKey;
\r
571 mapKeys[vchPubKey] = vchPrivKey;
\r
572 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
\r
574 else if (strType == "defaultkey")
\r
576 ssValue >> vchDefaultKeyRet;
\r
578 else if (strType == "setting")
\r
584 if (strKey == "fShowGenerated") ssValue >> fShowGenerated;
\r
585 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
\r
588 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
\r
589 if (strKey == "addrIncoming") ssValue >> addrIncoming;
\r
590 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
\r
591 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
\r
592 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
\r
593 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
\r
594 if (strKey == "fUseProxy") ssValue >> fUseProxy;
\r
595 if (strKey == "addrProxy") ssValue >> addrProxy;
\r
601 printf("fShowGenerated = %d\n", fShowGenerated);
\r
602 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
\r
603 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
\r
604 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
\r
605 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
\r
606 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
\r
607 printf("fUseProxy = %d\n", fUseProxy);
\r
608 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
\r
611 // The transaction fee setting won't be needed for many years to come.
\r
612 // Setting it to zero here in case they set it to something in an earlier version.
\r
613 if (nTransactionFee != 0)
\r
615 nTransactionFee = 0;
\r
616 WriteSetting("nTransactionFee", nTransactionFee);
\r
622 bool LoadWallet(bool& fFirstRunRet)
\r
624 fFirstRunRet = false;
\r
625 vector<unsigned char> vchDefaultKey;
\r
626 if (!CWalletDB("cr").LoadWallet(vchDefaultKey))
\r
628 fFirstRunRet = vchDefaultKey.empty();
\r
630 if (mapKeys.count(vchDefaultKey))
\r
633 keyUser.SetPubKey(vchDefaultKey);
\r
634 keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
\r
638 // Create new keyUser and set as default key
\r
639 RandAddSeedPerfmon();
\r
640 keyUser.MakeNewKey();
\r
641 if (!AddKey(keyUser))
\r
643 if (!SetAddressBookName(PubKeyToAddress(keyUser.GetPubKey()), "Your Address"))
\r
645 CWalletDB().WriteDefaultKey(keyUser.GetPubKey());
\r
648 CreateThread(ThreadFlushWalletDB, NULL);
\r
652 void ThreadFlushWalletDB(void* parg)
\r
654 static bool fOneThread;
\r
659 unsigned int nLastSeen = nWalletDBUpdated;
\r
660 unsigned int nLastFlushed = nWalletDBUpdated;
\r
661 int64 nLastWalletUpdate = GetTime();
\r
666 if (nLastSeen != nWalletDBUpdated)
\r
668 nLastSeen = nWalletDBUpdated;
\r
669 nLastWalletUpdate = GetTime();
\r
672 if (nLastFlushed != nWalletDBUpdated && nLastWalletUpdate < GetTime() - 1)
\r
674 TRY_CRITICAL_BLOCK(cs_db)
\r
676 string strFile = "wallet.dat";
\r
677 map<string, int>::iterator mi = mapFileUseCount.find(strFile);
\r
678 if (mi != mapFileUseCount.end())
\r
680 int nRefCount = (*mi).second;
\r
681 if (nRefCount == 0 && !fShutdown)
\r
683 // Flush wallet.dat so it's self contained
\r
684 nLastFlushed = nWalletDBUpdated;
\r
685 int64 nStart = GetTimeMillis();
\r
686 dbenv.txn_checkpoint(0, 0, 0);
\r
687 dbenv.lsn_reset(strFile.c_str(), 0);
\r
688 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
\r
689 mapFileUseCount.erase(mi++);
\r