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"
12 #include <boost/filesystem.hpp>
20 uint32_t nWalletDBUpdated;
21 extern bool fUseMemoryLog;
29 void CDBEnv::EnvShutdown()
35 int ret = dbenv.close(0);
37 printf("EnvShutdown exception: %s (%d)\n", DbEnv::strerror(ret), ret);
39 DbEnv(0).remove(strPath.c_str(), 0);
42 CDBEnv::CDBEnv() : fDetachDB(false), fDbEnvInit(false), fMockDb(false), dbenv(DB_CXX_NO_EXCEPTIONS) { }
54 bool CDBEnv::Open(boost::filesystem::path pathEnv_)
63 boost::filesystem::path pathDataDir = pathEnv;
64 strPath = pathDataDir.string();
65 boost::filesystem::path pathLogDir = pathDataDir / "database";
66 boost::filesystem::create_directory(pathLogDir);
67 boost::filesystem::path pathErrorFile = pathDataDir / "db.log";
68 printf("dbenv.open LogDir=%s ErrorFile=%s\n", pathLogDir.string().c_str(), pathErrorFile.string().c_str());
70 uint32_t nEnvFlags = 0;
71 if (GetBoolArg("-privdb", true))
72 nEnvFlags |= DB_PRIVATE;
74 int nDbCache = GetArgInt("-dbcache", 25);
75 dbenv.set_lg_dir(pathLogDir.string().c_str());
76 dbenv.set_cachesize(nDbCache / 1024, (nDbCache % 1024)*1048576, 1);
77 dbenv.set_lg_bsize(1048576);
78 dbenv.set_lg_max(10485760);
80 // Bugfix: Bump lk_max_locks default to 537000, to safely handle reorgs with up to 5 blocks reversed
81 // dbenv.set_lk_max_locks(10000);
82 dbenv.set_lk_max_locks(537000);
84 dbenv.set_lk_max_objects(10000);
85 dbenv.set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
86 dbenv.set_flags(DB_AUTO_COMMIT, 1);
87 dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1);
88 #ifdef DB_LOG_AUTO_REMOVE
89 dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1);
91 int ret = dbenv.open(strPath.c_str(),
102 return error("CDB() : error %s (%d) opening database environment", DbEnv::strerror(ret), ret);
108 // Check that the number of locks is sufficient (to prevent chain fork possibility, read http://bitcoin.org/may15 for more info)
110 if (!dbenv.get_lk_max_locks(&nMaxLocks))
112 int nBlocks, nDeepReorg;
115 nBlocks = nMaxLocks / 48768;
116 nDeepReorg = (nBlocks - 1) / 2;
118 printf("Final lk_max_locks is %u, sufficient for (worst case) %d block%s in a single transaction (up to a %d-deep reorganization)\n", nMaxLocks, nBlocks, (nBlocks == 1) ? "" : "s", nDeepReorg);
122 strMessage = strprintf(_("Warning: DB_CONFIG has set_lk_max_locks %u, which may be too low for a single block. If this limit is reached, NovaCoin may stop working."), nMaxLocks);
124 strMessage = strprintf(_("Warning: DB_CONFIG has set_lk_max_locks %u, which may be too low for a common blockchain reorganization. If this limit is reached, NovaCoin may stop working."), nMaxLocks);
126 strMiscWarning = strMessage;
127 printf("*** %s\n", strMessage.c_str());
135 CDBEnv::VerifyResult CDBEnv::Verify(string strFile, bool (*recoverFunc)(CDBEnv& dbenv, string strFile))
138 assert(mapFileUseCount.count(strFile) == 0);
141 int result = db.verify(strFile.c_str(), NULL, NULL, 0);
144 else if (recoverFunc == NULL)
148 bool fRecovered = (*recoverFunc)(*this, strFile);
149 return (fRecovered ? RECOVER_OK : RECOVER_FAIL);
152 bool CDBEnv::Salvage(string strFile, bool fAggressive,
153 vector<CDBEnv::KeyValPair >& vResult)
156 assert(mapFileUseCount.count(strFile) == 0);
158 u_int32_t flags = DB_SALVAGE;
159 if (fAggressive) flags |= DB_AGGRESSIVE;
161 stringstream strDump;
164 int result = db.verify(strFile.c_str(), NULL, &strDump, flags);
167 printf("ERROR: db salvage failed\n");
171 // Format of bdb dump is ascii lines:
180 while (!strDump.eof() && strLine != "HEADER=END")
181 getline(strDump, strLine); // Skip past header
183 string keyHex, valueHex;
184 while (!strDump.eof() && keyHex != "DATA=END")
186 getline(strDump, keyHex);
187 if (keyHex != "DATA=END")
189 getline(strDump, valueHex);
190 vResult.push_back(make_pair(ParseHex(keyHex),ParseHex(valueHex)));
194 return (result == 0);
198 void CDBEnv::CheckpointLSN(string strFile)
200 dbenv.txn_checkpoint(0, 0, 0);
203 dbenv.lsn_reset(strFile.c_str(), 0);
207 CDB::CDB(const char *pszFile, const char* pszMode) :
208 pdb(NULL), activeTxn(NULL), fReadOnly(true)
214 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
215 bool fCreate = strchr(pszMode, 'c') != NULL;
216 uint32_t nFlags = DB_THREAD;
222 if (!bitdb.Open(GetDataDir()))
223 throw runtime_error("env open failed");
226 ++bitdb.mapFileUseCount[strFile];
227 pdb = bitdb.mapDb[strFile];
230 pdb = new Db(&bitdb.dbenv, 0);
232 ret = pdb->open(NULL, // Txn pointer
234 "main", // Logical db name
235 DB_BTREE, // Database type
243 --bitdb.mapFileUseCount[strFile];
245 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
248 if (fCreate && !Exists(string("version")))
250 bool fTmp = fReadOnly;
252 WriteVersion(CLIENT_VERSION);
256 bitdb.mapDb[strFile] = pdb;
261 static bool IsChainFile(string strFile)
263 if (strFile == "blkindex.dat")
278 // Flush database activity from memory pool to disk log
279 uint32_t nMinutes = 0;
282 if (IsChainFile(strFile))
284 if (IsChainFile(strFile) && IsInitialBlockDownload())
287 bitdb.dbenv.txn_checkpoint(nMinutes ? GetArgUInt("-dblogsize", 100)*1024 : 0, nMinutes, 0);
291 --bitdb.mapFileUseCount[strFile];
295 void CDBEnv::CloseDb(const string& strFile)
299 if (mapDb[strFile] != NULL)
301 // Close the database handle
302 Db* pdb = mapDb[strFile];
305 mapDb[strFile] = NULL;
310 bool CDBEnv::RemoveDb(const string& strFile)
312 this->CloseDb(strFile);
315 int rc = dbenv.dbremove(NULL, strFile.c_str(), NULL, DB_AUTO_COMMIT);
319 DbTxn *CDBEnv::TxnBegin(int flags)
322 int ret = dbenv.txn_begin(NULL, &ptxn, flags);
323 if (!ptxn || ret != 0)
328 Dbc* CDB::GetCursor()
333 int ret = pdb->cursor(NULL, &pcursor, 0);
339 int CDB::ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags)
343 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
345 datKey.set_data(&ssKey[0]);
346 datKey.set_size((uint32_t)ssKey.size());
349 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
351 datValue.set_data(&ssValue[0]);
352 datValue.set_size((uint32_t)ssValue.size());
354 datKey.set_flags(DB_DBT_MALLOC);
355 datValue.set_flags(DB_DBT_MALLOC);
356 int ret = pcursor->get(&datKey, &datValue, fFlags);
359 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
362 // Convert to streams
363 ssKey.SetType(SER_DISK);
365 ssKey.write((char*)datKey.get_data(), datKey.get_size());
366 ssValue.SetType(SER_DISK);
368 ssValue.write((char*)datValue.get_data(), datValue.get_size());
370 // Clear and free memory
371 memset(datKey.get_data(), 0, datKey.get_size());
372 memset(datValue.get_data(), 0, datValue.get_size());
373 free(datKey.get_data());
374 free(datValue.get_data());
380 if (!pdb || activeTxn)
382 DbTxn* ptxn = bitdb.TxnBegin();
389 bool CDB::TxnCommit()
391 if (!pdb || !activeTxn)
393 int ret = activeTxn->commit(0);
400 if (!pdb || !activeTxn)
402 int ret = activeTxn->abort();
407 bool CDB::ReadVersion(int& nVersion)
410 return Read(std::string("version"), nVersion);
413 bool CDB::WriteVersion(int nVersion)
415 return Write(std::string("version"), nVersion);
418 bool CDB::Rewrite(const string& strFile, const char* pszSkip)
424 if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0)
426 // Flush log data to the dat file
427 bitdb.CloseDb(strFile);
428 bitdb.CheckpointLSN(strFile);
429 bitdb.mapFileUseCount.erase(strFile);
431 bool fSuccess = true;
432 printf("Rewriting %s...\n", strFile.c_str());
433 string strFileRes = strFile + ".rewrite";
434 { // surround usage of db with extra {}
435 CDB db(strFile.c_str(), "r");
436 Db* pdbCopy = new Db(&bitdb.dbenv, 0);
438 int ret = pdbCopy->open(NULL, // Txn pointer
439 strFileRes.c_str(), // Filename
440 "main", // Logical db name
441 DB_BTREE, // Database type
446 printf("Cannot create database file %s\n", strFileRes.c_str());
450 Dbc* pcursor = db.GetCursor();
454 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
455 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
456 int ret = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT);
457 if (ret == DB_NOTFOUND)
471 size_t pszSkipLen = strlen(pszSkip);
472 if (strncmp(&ssKey[0], pszSkip, min(ssKey.size(), pszSkipLen)) == 0)
476 if (strncmp(&ssKey[0], "\x07version", 8) == 0)
480 ssValue << CLIENT_VERSION;
482 Dbt datKey(&ssKey[0], ssKey.size());
483 Dbt datValue(&ssValue[0], ssValue.size());
484 int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE);
492 bitdb.CloseDb(strFile);
493 if (pdbCopy->close(0))
500 Db dbA(&bitdb.dbenv, 0);
501 if (dbA.remove(strFile.c_str(), NULL, 0))
503 Db dbB(&bitdb.dbenv, 0);
504 if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
508 printf("Rewriting of %s FAILED!\n", strFileRes.c_str());
518 void CDBEnv::Flush(bool fShutdown)
520 int64_t nStart = GetTimeMillis();
521 // Flush log data to the actual data file
522 // on all files that are not in use
523 printf("Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
528 auto mi = mapFileUseCount.begin();
529 while (mi != mapFileUseCount.end())
531 string strFile = (*mi).first;
532 int nRefCount = (*mi).second;
533 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
536 // Move log data to the dat file
538 printf("%s checkpoint\n", strFile.c_str());
539 dbenv.txn_checkpoint(0, 0, 0);
540 if (!IsChainFile(strFile) || fDetachDB) {
541 printf("%s detach\n", strFile.c_str());
543 dbenv.lsn_reset(strFile.c_str(), 0);
545 printf("%s closed\n", strFile.c_str());
546 mapFileUseCount.erase(mi++);
551 printf("DBFlush(%s)%s ended %15" PRId64 "ms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started", GetTimeMillis() - nStart);
555 if (mapFileUseCount.empty())
557 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
572 pathAddr = GetDataDir() / "peers.dat";
575 bool CAddrDB::Write(const CAddrMan& addr)
577 // Generate random temporary filename
578 unsigned short randv = 0;
579 RAND_bytes((unsigned char *)&randv, sizeof(randv));
580 string tmpfn = strprintf("peers.dat.%04x", randv);
582 // serialize addresses, checksum data up to that point, then append csum
583 CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
584 ssPeers << nNetworkID;
586 uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
589 // open temp output file, and associate with CAutoFile
590 boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
591 FILE *file = fopen(pathTmp.string().c_str(), "wb");
592 CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
594 return error("CAddrman::Write() : open failed");
596 // Write and commit header, data
600 catch (const exception&) {
601 return error("CAddrman::Write() : I/O error");
606 // replace existing peers.dat, if any, with new peers.dat.XXXX
607 if (!RenameOver(pathTmp, pathAddr))
608 return error("CAddrman::Write() : Rename-into-place failed");
613 bool CAddrDB::Read(CAddrMan& addr)
615 // open input file, and associate with CAutoFile
616 FILE *file = fopen(pathAddr.string().c_str(), "rb");
617 CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
619 return error("CAddrman::Read() : open failed");
621 // use file size to size memory buffer
622 int fileSize = GetFilesize(filein);
623 int dataSize = fileSize - sizeof(uint256);
624 //Don't try to resize to a negative number if file is small
625 if ( dataSize < 0 ) dataSize = 0;
626 vector<unsigned char> vchData;
627 vchData.resize(dataSize);
630 // read data and checksum from file
632 filein.read((char *)&vchData[0], dataSize);
635 catch (const exception&) {
636 return error("CAddrman::Read() 2 : I/O error or stream data corrupted");
640 CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
642 // verify stored checksum matches input data
643 uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
644 if (hashIn != hashTmp)
645 return error("CAddrman::Read() : checksum mismatch; data corrupted");
649 // de-serialize file header (pchMessageStart magic number) and
650 ssPeers >> nMsgNetID;
651 // verify the network matches ours
652 if (nMsgNetID != nNetworkID)
653 return error("CAddrman::Read() : invalid network magic number");
654 // de-serialize address data into one CAddrMan object
657 catch (const exception&) {
658 return error("CAddrman::Read() : I/O error or stream data corrupted");