1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
10 #include "ui_interface.h"
11 #include <boost/filesystem.hpp>
12 #include <boost/filesystem/fstream.hpp>
19 using namespace boost;
22 unsigned int nWalletDBUpdated;
23 extern bool fUseMemoryLog;
32 void CDBEnv::EnvShutdown()
38 int ret = dbenv.close(0);
40 printf("EnvShutdown exception: %s (%d)\n", DbEnv::strerror(ret), ret);
42 DbEnv(0).remove(strPath.c_str(), 0);
45 CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS)
61 bool CDBEnv::Open(boost::filesystem::path pathEnv_)
70 filesystem::path pathDataDir = pathEnv;
71 strPath = pathDataDir.string();
72 filesystem::path pathLogDir = pathDataDir / "database";
73 filesystem::create_directory(pathLogDir);
74 filesystem::path pathErrorFile = pathDataDir / "db.log";
75 printf("dbenv.open LogDir=%s ErrorFile=%s\n", pathLogDir.string().c_str(), pathErrorFile.string().c_str());
77 unsigned int nEnvFlags = 0;
78 if (GetBoolArg("-privdb", true))
79 nEnvFlags |= DB_PRIVATE;
81 int nDbCache = GetArgInt("-dbcache", 25);
82 dbenv.set_lg_dir(pathLogDir.string().c_str());
83 dbenv.set_cachesize(nDbCache / 1024, (nDbCache % 1024)*1048576, 1);
84 dbenv.set_lg_bsize(1048576);
85 dbenv.set_lg_max(10485760);
87 // Bugfix: Bump lk_max_locks default to 537000, to safely handle reorgs with up to 5 blocks reversed
88 // dbenv.set_lk_max_locks(10000);
89 dbenv.set_lk_max_locks(537000);
91 dbenv.set_lk_max_objects(10000);
92 dbenv.set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
93 dbenv.set_flags(DB_AUTO_COMMIT, 1);
94 dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1);
95 #ifdef DB_LOG_AUTO_REMOVE
96 dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1);
98 int ret = dbenv.open(strPath.c_str(),
109 return error("CDB() : error %s (%d) opening database environment", DbEnv::strerror(ret), ret);
115 // Check that the number of locks is sufficient (to prevent chain fork possibility, read http://bitcoin.org/may15 for more info)
117 if (!dbenv.get_lk_max_locks(&nMaxLocks))
119 int nBlocks, nDeepReorg;
120 std::string strMessage;
122 nBlocks = nMaxLocks / 48768;
123 nDeepReorg = (nBlocks - 1) / 2;
125 printf("Final lk_max_locks is %lu, sufficient for (worst case) %d block%s in a single transaction (up to a %d-deep reorganization)\n", (unsigned long)nMaxLocks, nBlocks, (nBlocks == 1) ? "" : "s", nDeepReorg);
129 strMessage = strprintf(_("Warning: DB_CONFIG has set_lk_max_locks %lu, which may be too low for a single block. If this limit is reached, NovaCoin may stop working."), (unsigned long)nMaxLocks);
131 strMessage = strprintf(_("Warning: DB_CONFIG has set_lk_max_locks %lu, which may be too low for a common blockchain reorganization. If this limit is reached, NovaCoin may stop working."), (unsigned long)nMaxLocks);
133 strMiscWarning = strMessage;
134 printf("*** %s\n", strMessage.c_str());
142 void CDBEnv::MakeMock()
145 throw runtime_error("CDBEnv::MakeMock(): already initialized");
148 throw runtime_error("CDBEnv::MakeMock(): during shutdown");
150 printf("CDBEnv::MakeMock()\n");
152 dbenv.set_cachesize(1, 0, 1);
153 dbenv.set_lg_bsize(10485760*4);
154 dbenv.set_lg_max(10485760);
155 dbenv.set_lk_max_locks(10000);
156 dbenv.set_lk_max_objects(10000);
157 dbenv.set_flags(DB_AUTO_COMMIT, 1);
158 #ifdef DB_LOG_IN_MEMORY
159 dbenv.log_set_config(DB_LOG_IN_MEMORY, fUseMemoryLog ? 1 : 0);
161 int ret = dbenv.open(NULL,
171 throw runtime_error(strprintf("CDBEnv::MakeMock(): error %d opening database environment", ret));
177 CDBEnv::VerifyResult CDBEnv::Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile))
180 assert(mapFileUseCount.count(strFile) == 0);
183 int result = db.verify(strFile.c_str(), NULL, NULL, 0);
186 else if (recoverFunc == NULL)
190 bool fRecovered = (*recoverFunc)(*this, strFile);
191 return (fRecovered ? RECOVER_OK : RECOVER_FAIL);
194 bool CDBEnv::Salvage(std::string strFile, bool fAggressive,
195 std::vector<CDBEnv::KeyValPair >& vResult)
198 assert(mapFileUseCount.count(strFile) == 0);
200 u_int32_t flags = DB_SALVAGE;
201 if (fAggressive) flags |= DB_AGGRESSIVE;
203 stringstream strDump;
206 int result = db.verify(strFile.c_str(), NULL, &strDump, flags);
209 printf("ERROR: db salvage failed\n");
213 // Format of bdb dump is ascii lines:
222 while (!strDump.eof() && strLine != "HEADER=END")
223 getline(strDump, strLine); // Skip past header
225 std::string keyHex, valueHex;
226 while (!strDump.eof() && keyHex != "DATA=END")
228 getline(strDump, keyHex);
229 if (keyHex != "DATA_END")
231 getline(strDump, valueHex);
232 vResult.push_back(make_pair(ParseHex(keyHex),ParseHex(valueHex)));
236 return (result == 0);
240 void CDBEnv::CheckpointLSN(std::string strFile)
242 dbenv.txn_checkpoint(0, 0, 0);
245 dbenv.lsn_reset(strFile.c_str(), 0);
249 CDB::CDB(const char *pszFile, const char* pszMode) :
250 pdb(NULL), activeTxn(NULL)
256 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
257 bool fCreate = strchr(pszMode, 'c') != NULL;
258 unsigned int nFlags = DB_THREAD;
264 if (!bitdb.Open(GetDataDir()))
265 throw runtime_error("env open failed");
268 ++bitdb.mapFileUseCount[strFile];
269 pdb = bitdb.mapDb[strFile];
272 pdb = new Db(&bitdb.dbenv, 0);
274 bool fMockDb = bitdb.IsMock();
277 DbMpoolFile*mpf = pdb->get_mpf();
278 ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
280 throw runtime_error(strprintf("CDB() : failed to configure for no temp file backing for database %s", pszFile));
283 ret = pdb->open(NULL, // Txn pointer
284 fMockDb ? NULL : pszFile, // Filename
285 "main", // Logical db name
286 DB_BTREE, // Database type
294 --bitdb.mapFileUseCount[strFile];
296 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
299 if (fCreate && !Exists(string("version")))
301 bool fTmp = fReadOnly;
303 WriteVersion(CLIENT_VERSION);
307 bitdb.mapDb[strFile] = pdb;
312 static bool IsChainFile(std::string strFile)
314 if (strFile == "blkindex.dat")
329 // Flush database activity from memory pool to disk log
330 unsigned int nMinutes = 0;
333 if (IsChainFile(strFile))
335 if (IsChainFile(strFile) && IsInitialBlockDownload())
338 bitdb.dbenv.txn_checkpoint(nMinutes ? GetArgUInt("-dblogsize", 100)*1024 : 0, nMinutes, 0);
342 --bitdb.mapFileUseCount[strFile];
346 void CDBEnv::CloseDb(const string& strFile)
350 if (mapDb[strFile] != NULL)
352 // Close the database handle
353 Db* pdb = mapDb[strFile];
356 mapDb[strFile] = NULL;
361 bool CDBEnv::RemoveDb(const string& strFile)
363 this->CloseDb(strFile);
366 int rc = dbenv.dbremove(NULL, strFile.c_str(), NULL, DB_AUTO_COMMIT);
370 bool CDB::Rewrite(const string& strFile, const char* pszSkip)
376 if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0)
378 // Flush log data to the dat file
379 bitdb.CloseDb(strFile);
380 bitdb.CheckpointLSN(strFile);
381 bitdb.mapFileUseCount.erase(strFile);
383 bool fSuccess = true;
384 printf("Rewriting %s...\n", strFile.c_str());
385 string strFileRes = strFile + ".rewrite";
386 { // surround usage of db with extra {}
387 CDB db(strFile.c_str(), "r");
388 Db* pdbCopy = new Db(&bitdb.dbenv, 0);
390 int ret = pdbCopy->open(NULL, // Txn pointer
391 strFileRes.c_str(), // Filename
392 "main", // Logical db name
393 DB_BTREE, // Database type
398 printf("Cannot create database file %s\n", strFileRes.c_str());
402 Dbc* pcursor = db.GetCursor();
406 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
407 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
408 int ret = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT);
409 if (ret == DB_NOTFOUND)
421 strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
423 if (strncmp(&ssKey[0], "\x07version", 8) == 0)
427 ssValue << CLIENT_VERSION;
429 Dbt datKey(&ssKey[0], ssKey.size());
430 Dbt datValue(&ssValue[0], ssValue.size());
431 int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE);
438 bitdb.CloseDb(strFile);
439 if (pdbCopy->close(0))
446 Db dbA(&bitdb.dbenv, 0);
447 if (dbA.remove(strFile.c_str(), NULL, 0))
449 Db dbB(&bitdb.dbenv, 0);
450 if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
454 printf("Rewriting of %s FAILED!\n", strFileRes.c_str());
464 void CDBEnv::Flush(bool fShutdown)
466 int64_t nStart = GetTimeMillis();
467 // Flush log data to the actual data file
468 // on all files that are not in use
469 printf("Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
474 map<string, int>::iterator mi = mapFileUseCount.begin();
475 while (mi != mapFileUseCount.end())
477 string strFile = (*mi).first;
478 int nRefCount = (*mi).second;
479 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
482 // Move log data to the dat file
484 printf("%s checkpoint\n", strFile.c_str());
485 dbenv.txn_checkpoint(0, 0, 0);
486 if (!IsChainFile(strFile) || fDetachDB) {
487 printf("%s detach\n", strFile.c_str());
489 dbenv.lsn_reset(strFile.c_str(), 0);
491 printf("%s closed\n", strFile.c_str());
492 mapFileUseCount.erase(mi++);
497 printf("DBFlush(%s)%s ended %15" PRId64 "ms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started", GetTimeMillis() - nStart);
501 if (mapFileUseCount.empty())
503 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
518 pathAddr = GetDataDir() / "peers.dat";
521 bool CAddrDB::Write(const CAddrMan& addr)
523 // Generate random temporary filename
524 unsigned short randv = 0;
525 RAND_bytes((unsigned char *)&randv, sizeof(randv));
526 std::string tmpfn = strprintf("peers.dat.%04x", randv);
528 // serialize addresses, checksum data up to that point, then append csum
529 CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
530 ssPeers << FLATDATA(pchMessageStart);
532 uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
535 // open temp output file, and associate with CAutoFile
536 boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
537 FILE *file = fopen(pathTmp.string().c_str(), "wb");
538 CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
540 return error("CAddrman::Write() : open failed");
542 // Write and commit header, data
546 catch (const std::exception&) {
547 return error("CAddrman::Write() : I/O error");
552 // replace existing peers.dat, if any, with new peers.dat.XXXX
553 if (!RenameOver(pathTmp, pathAddr))
554 return error("CAddrman::Write() : Rename-into-place failed");
559 bool CAddrDB::Read(CAddrMan& addr)
561 // open input file, and associate with CAutoFile
562 FILE *file = fopen(pathAddr.string().c_str(), "rb");
563 CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
565 return error("CAddrman::Read() : open failed");
567 // use file size to size memory buffer
568 int fileSize = GetFilesize(filein);
569 int dataSize = fileSize - sizeof(uint256);
570 //Don't try to resize to a negative number if file is small
571 if ( dataSize < 0 ) dataSize = 0;
572 vector<unsigned char> vchData;
573 vchData.resize(dataSize);
576 // read data and checksum from file
578 filein.read((char *)&vchData[0], dataSize);
581 catch (const std::exception&) {
582 return error("CAddrman::Read() 2 : I/O error or stream data corrupted");
586 CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
588 // verify stored checksum matches input data
589 uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
590 if (hashIn != hashTmp)
591 return error("CAddrman::Read() : checksum mismatch; data corrupted");
593 unsigned char pchMsgTmp[4];
595 // de-serialize file header (pchMessageStart magic number) and
596 ssPeers >> FLATDATA(pchMsgTmp);
598 // verify the network matches ours
599 if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
600 return error("CAddrman::Read() : invalid network magic number");
602 // de-serialize address data into one CAddrMan object
605 catch (const std::exception&) {
606 return error("CAddrman::Read() : I/O error or stream data corrupted");