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)\n", fShutdown ? "true" : "false");
\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
508 //// todo: shouldn't we catch exceptions and try to recover and continue?
\r
509 CRITICAL_BLOCK(cs_mapKeys)
\r
510 CRITICAL_BLOCK(cs_mapWallet)
\r
513 Dbc* pcursor = GetCursor();
\r
519 // Read next record
\r
521 CDataStream ssValue;
\r
522 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
\r
523 if (ret == DB_NOTFOUND)
\r
529 // Taking advantage of the fact that pair serialization
\r
530 // is just the two items serialized one after the other
\r
533 if (strType == "name")
\r
536 ssKey >> strAddress;
\r
537 ssValue >> mapAddressBook[strAddress];
\r
539 else if (strType == "tx")
\r
543 CWalletTx& wtx = mapWallet[hash];
\r
546 if (wtx.GetHash() != hash)
\r
547 printf("Error in wallet.dat, hash mismatch\n");
\r
550 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
\r
551 //printf(" %12I64d %s %s %s\n",
\r
552 // wtx.vout[0].nValue,
\r
553 // DateTimeStrFormat("%x %H:%M:%S", wtx.nTime).c_str(),
\r
554 // wtx.hashBlock.ToString().substr(0,14).c_str(),
\r
555 // wtx.mapValue["message"].c_str());
\r
557 else if (strType == "key")
\r
559 vector<unsigned char> vchPubKey;
\r
560 ssKey >> vchPubKey;
\r
561 CPrivKey vchPrivKey;
\r
562 ssValue >> vchPrivKey;
\r
564 mapKeys[vchPubKey] = vchPrivKey;
\r
565 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
\r
567 else if (strType == "defaultkey")
\r
569 ssValue >> vchDefaultKeyRet;
\r
571 else if (strType == "setting")
\r
577 if (strKey == "fShowGenerated") ssValue >> fShowGenerated;
\r
578 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
\r
581 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
\r
582 if (strKey == "addrIncoming") ssValue >> addrIncoming;
\r
583 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
\r
584 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
\r
585 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
\r
586 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
\r
587 if (strKey == "fUseProxy") ssValue >> fUseProxy;
\r
588 if (strKey == "addrProxy") ssValue >> addrProxy;
\r
594 printf("fShowGenerated = %d\n", fShowGenerated);
\r
595 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
\r
596 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
\r
597 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
\r
598 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
\r
599 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
\r
600 printf("fUseProxy = %d\n", fUseProxy);
\r
601 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
\r
604 // The transaction fee setting won't be needed for many years to come.
\r
605 // Setting it to zero here in case they set it to something in an earlier version.
\r
606 if (nTransactionFee != 0)
\r
608 nTransactionFee = 0;
\r
609 WriteSetting("nTransactionFee", nTransactionFee);
\r
615 bool LoadWallet(bool& fFirstRunRet)
\r
617 fFirstRunRet = false;
\r
618 vector<unsigned char> vchDefaultKey;
\r
619 if (!CWalletDB("cr").LoadWallet(vchDefaultKey))
\r
621 fFirstRunRet = vchDefaultKey.empty();
\r
623 if (mapKeys.count(vchDefaultKey))
\r
626 keyUser.SetPubKey(vchDefaultKey);
\r
627 keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
\r
631 // Create new keyUser and set as default key
\r
632 RandAddSeedPerfmon();
\r
633 keyUser.MakeNewKey();
\r
634 if (!AddKey(keyUser))
\r
636 if (!SetAddressBookName(PubKeyToAddress(keyUser.GetPubKey()), "Your Address"))
\r
638 CWalletDB().WriteDefaultKey(keyUser.GetPubKey());
\r
641 _beginthread(ThreadFlushWalletDB, 0, NULL);
\r
645 void ThreadFlushWalletDB(void* parg)
\r
647 static bool fOneThread;
\r
652 unsigned int nLastSeen = nWalletDBUpdated;
\r
653 unsigned int nLastFlushed = nWalletDBUpdated;
\r
654 int64 nLastWalletUpdate = GetTime();
\r
659 if (nLastSeen != nWalletDBUpdated)
\r
661 nLastSeen = nWalletDBUpdated;
\r
662 nLastWalletUpdate = GetTime();
\r
665 if (nLastFlushed != nWalletDBUpdated && nLastWalletUpdate < GetTime() - 1)
\r
667 TRY_CRITICAL_BLOCK(cs_db)
\r
669 string strFile = "wallet.dat";
\r
670 map<string, int>::iterator mi = mapFileUseCount.find(strFile);
\r
671 if (mi != mapFileUseCount.end())
\r
673 int nRefCount = (*mi).second;
\r
674 if (nRefCount == 0 && !fShutdown)
\r
676 // Flush wallet.dat so it's self contained
\r
677 nLastFlushed = nWalletDBUpdated;
\r
678 int64 nStart = GetTimeMillis();
\r
679 dbenv.txn_checkpoint(0, 0, 0);
\r
680 dbenv.lsn_reset(strFile.c_str(), 0);
\r
681 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
\r
682 mapFileUseCount.erase(mi++);
\r