Bitcoin Core  21.99.0
P2P Digital Currency
bdb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <wallet/bdb.h>
7 #include <wallet/db.h>
8 
9 #include <util/strencodings.h>
10 #include <util/translation.h>
11 
12 #include <stdint.h>
13 
14 #ifndef WIN32
15 #include <sys/stat.h>
16 #endif
17 
18 namespace {
19 
29 void CheckUniqueFileid(const BerkeleyEnvironment& env, const std::string& filename, Db& db, WalletDatabaseFileId& fileid)
30 {
31  if (env.IsMock()) return;
32 
33  int ret = db.get_mpf()->get_fileid(fileid.value);
34  if (ret != 0) {
35  throw std::runtime_error(strprintf("BerkeleyDatabase: Can't open database %s (get_fileid failed with %d)", filename, ret));
36  }
37 
38  for (const auto& item : env.m_fileids) {
39  if (fileid == item.second && &fileid != &item.second) {
40  throw std::runtime_error(strprintf("BerkeleyDatabase: Can't open database %s (duplicates fileid %s from %s)", filename,
41  HexStr(item.second.value), item.first));
42  }
43  }
44 }
45 
46 RecursiveMutex cs_db;
47 std::map<std::string, std::weak_ptr<BerkeleyEnvironment>> g_dbenvs GUARDED_BY(cs_db);
48 } // namespace
49 
51 {
52  return memcmp(value, &rhs.value, sizeof(value)) == 0;
53 }
54 
61 std::shared_ptr<BerkeleyEnvironment> GetBerkeleyEnv(const fs::path& env_directory)
62 {
63  LOCK(cs_db);
64  auto inserted = g_dbenvs.emplace(env_directory.string(), std::weak_ptr<BerkeleyEnvironment>());
65  if (inserted.second) {
66  auto env = std::make_shared<BerkeleyEnvironment>(env_directory.string());
67  inserted.first->second = env;
68  return env;
69  }
70  return inserted.first->second.lock();
71 }
72 
73 //
74 // BerkeleyBatch
75 //
76 
78 {
79  if (!fDbEnvInit)
80  return;
81 
82  fDbEnvInit = false;
83 
84  for (auto& db : m_databases) {
85  BerkeleyDatabase& database = db.second.get();
86  assert(database.m_refcount <= 0);
87  if (database.m_db) {
88  database.m_db->close(0);
89  database.m_db.reset();
90  }
91  }
92 
93  FILE* error_file = nullptr;
94  dbenv->get_errfile(&error_file);
95 
96  int ret = dbenv->close(0);
97  if (ret != 0)
98  LogPrintf("BerkeleyEnvironment::Close: Error %d closing database environment: %s\n", ret, DbEnv::strerror(ret));
99  if (!fMockDb)
100  DbEnv((u_int32_t)0).remove(strPath.c_str(), 0);
101 
102  if (error_file) fclose(error_file);
103 
104  UnlockDirectory(strPath, ".walletlock");
105 }
106 
108 {
109  dbenv.reset(new DbEnv(DB_CXX_NO_EXCEPTIONS));
110  fDbEnvInit = false;
111  fMockDb = false;
112 }
113 
114 BerkeleyEnvironment::BerkeleyEnvironment(const fs::path& dir_path) : strPath(dir_path.string())
115 {
116  Reset();
117 }
118 
120 {
121  LOCK(cs_db);
122  g_dbenvs.erase(strPath);
123  Close();
124 }
125 
127 {
128  if (fDbEnvInit) {
129  return true;
130  }
131 
132  fs::path pathIn = strPath;
133  TryCreateDirectories(pathIn);
134  if (!LockDirectory(pathIn, ".walletlock")) {
135  LogPrintf("Cannot obtain a lock on wallet directory %s. Another instance of bitcoin may be using it.\n", strPath);
136  err = strprintf(_("Error initializing wallet database environment %s!"), Directory());
137  return false;
138  }
139 
140  fs::path pathLogDir = pathIn / "database";
141  TryCreateDirectories(pathLogDir);
142  fs::path pathErrorFile = pathIn / "db.log";
143  LogPrintf("BerkeleyEnvironment::Open: LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());
144 
145  unsigned int nEnvFlags = 0;
146  if (gArgs.GetBoolArg("-privdb", DEFAULT_WALLET_PRIVDB))
147  nEnvFlags |= DB_PRIVATE;
148 
149  dbenv->set_lg_dir(pathLogDir.string().c_str());
150  dbenv->set_cachesize(0, 0x100000, 1); // 1 MiB should be enough for just the wallet
151  dbenv->set_lg_bsize(0x10000);
152  dbenv->set_lg_max(1048576);
153  dbenv->set_lk_max_locks(40000);
154  dbenv->set_lk_max_objects(40000);
155  dbenv->set_errfile(fsbridge::fopen(pathErrorFile, "a"));
156  dbenv->set_flags(DB_AUTO_COMMIT, 1);
157  dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1);
158  dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1);
159  int ret = dbenv->open(strPath.c_str(),
160  DB_CREATE |
161  DB_INIT_LOCK |
162  DB_INIT_LOG |
163  DB_INIT_MPOOL |
164  DB_INIT_TXN |
165  DB_THREAD |
166  DB_RECOVER |
167  nEnvFlags,
168  S_IRUSR | S_IWUSR);
169  if (ret != 0) {
170  LogPrintf("BerkeleyEnvironment::Open: Error %d opening database environment: %s\n", ret, DbEnv::strerror(ret));
171  int ret2 = dbenv->close(0);
172  if (ret2 != 0) {
173  LogPrintf("BerkeleyEnvironment::Open: Error %d closing failed database environment: %s\n", ret2, DbEnv::strerror(ret2));
174  }
175  Reset();
176  err = strprintf(_("Error initializing wallet database environment %s!"), Directory());
177  if (ret == DB_RUNRECOVERY) {
178  err += Untranslated(" ") + _("This error could occur if this wallet was not shutdown cleanly and was last loaded using a build with a newer version of Berkeley DB. If so, please use the software that last loaded this wallet");
179  }
180  return false;
181  }
182 
183  fDbEnvInit = true;
184  fMockDb = false;
185  return true;
186 }
187 
190 {
191  Reset();
192 
193  LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::MakeMock\n");
194 
195  dbenv->set_cachesize(1, 0, 1);
196  dbenv->set_lg_bsize(10485760 * 4);
197  dbenv->set_lg_max(10485760);
198  dbenv->set_lk_max_locks(10000);
199  dbenv->set_lk_max_objects(10000);
200  dbenv->set_flags(DB_AUTO_COMMIT, 1);
201  dbenv->log_set_config(DB_LOG_IN_MEMORY, 1);
202  int ret = dbenv->open(nullptr,
203  DB_CREATE |
204  DB_INIT_LOCK |
205  DB_INIT_LOG |
206  DB_INIT_MPOOL |
207  DB_INIT_TXN |
208  DB_THREAD |
209  DB_PRIVATE,
210  S_IRUSR | S_IWUSR);
211  if (ret > 0) {
212  throw std::runtime_error(strprintf("BerkeleyEnvironment::MakeMock: Error %d opening database environment.", ret));
213  }
214 
215  fDbEnvInit = true;
216  fMockDb = true;
217 }
218 
220 {
221  m_dbt.set_flags(DB_DBT_MALLOC);
222 }
223 
224 BerkeleyBatch::SafeDbt::SafeDbt(void* data, size_t size)
225  : m_dbt(data, size)
226 {
227 }
228 
230 {
231  if (m_dbt.get_data() != nullptr) {
232  // Clear memory, e.g. in case it was a private key
233  memory_cleanse(m_dbt.get_data(), m_dbt.get_size());
234  // under DB_DBT_MALLOC, data is malloced by the Dbt, but must be
235  // freed by the caller.
236  // https://docs.oracle.com/cd/E17275_01/html/api_reference/C/dbt.html
237  if (m_dbt.get_flags() & DB_DBT_MALLOC) {
238  free(m_dbt.get_data());
239  }
240  }
241 }
242 
244 {
245  return m_dbt.get_data();
246 }
247 
249 {
250  return m_dbt.get_size();
251 }
252 
253 BerkeleyBatch::SafeDbt::operator Dbt*()
254 {
255  return &m_dbt;
256 }
257 
259 {
260  fs::path walletDir = env->Directory();
261  fs::path file_path = walletDir / strFile;
262 
263  LogPrintf("Using BerkeleyDB version %s\n", BerkeleyDatabaseVersion());
264  LogPrintf("Using wallet %s\n", file_path.string());
265 
266  if (!env->Open(errorStr)) {
267  return false;
268  }
269 
270  if (fs::exists(file_path))
271  {
272  assert(m_refcount == 0);
273 
274  Db db(env->dbenv.get(), 0);
275  int result = db.verify(strFile.c_str(), nullptr, nullptr, 0);
276  if (result != 0) {
277  errorStr = strprintf(_("%s corrupt. Try using the wallet tool bitcoin-wallet to salvage or restoring a backup."), file_path);
278  return false;
279  }
280  }
281  // also return true if files does not exists
282  return true;
283 }
284 
286 {
287  dbenv->txn_checkpoint(0, 0, 0);
288  if (fMockDb)
289  return;
290  dbenv->lsn_reset(strFile.c_str(), 0);
291 }
292 
294 {
295  if (env) {
296  LOCK(cs_db);
297  env->CloseDb(strFile);
298  assert(!m_db);
299  size_t erased = env->m_databases.erase(strFile);
300  assert(erased == 1);
301  env->m_fileids.erase(strFile);
302  }
303 }
304 
305 BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const bool read_only, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database)
306 {
307  database.AddRef();
308  database.Open();
309  fReadOnly = read_only;
310  fFlushOnClose = fFlushOnCloseIn;
311  env = database.env.get();
312  pdb = database.m_db.get();
313  strFile = database.strFile;
314  if (!Exists(std::string("version"))) {
315  bool fTmp = fReadOnly;
316  fReadOnly = false;
317  Write(std::string("version"), CLIENT_VERSION);
318  fReadOnly = fTmp;
319  }
320 }
321 
323 {
324  unsigned int nFlags = DB_THREAD | DB_CREATE;
325 
326  {
327  LOCK(cs_db);
328  bilingual_str open_err;
329  if (!env->Open(open_err))
330  throw std::runtime_error("BerkeleyDatabase: Failed to open database environment.");
331 
332  if (m_db == nullptr) {
333  int ret;
334  std::unique_ptr<Db> pdb_temp = std::make_unique<Db>(env->dbenv.get(), 0);
335 
336  bool fMockDb = env->IsMock();
337  if (fMockDb) {
338  DbMpoolFile* mpf = pdb_temp->get_mpf();
339  ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
340  if (ret != 0) {
341  throw std::runtime_error(strprintf("BerkeleyDatabase: Failed to configure for no temp file backing for database %s", strFile));
342  }
343  }
344 
345  ret = pdb_temp->open(nullptr, // Txn pointer
346  fMockDb ? nullptr : strFile.c_str(), // Filename
347  fMockDb ? strFile.c_str() : "main", // Logical db name
348  DB_BTREE, // Database type
349  nFlags, // Flags
350  0);
351 
352  if (ret != 0) {
353  throw std::runtime_error(strprintf("BerkeleyDatabase: Error %d, can't open database %s", ret, strFile));
354  }
355 
356  // Call CheckUniqueFileid on the containing BDB environment to
357  // avoid BDB data consistency bugs that happen when different data
358  // files in the same environment have the same fileid.
359  CheckUniqueFileid(*env, strFile, *pdb_temp, this->env->m_fileids[strFile]);
360 
361  m_db.reset(pdb_temp.release());
362 
363  }
364  }
365 }
366 
368 {
369  if (activeTxn)
370  return;
371 
372  // Flush database activity from memory pool to disk log
373  unsigned int nMinutes = 0;
374  if (fReadOnly)
375  nMinutes = 1;
376 
377  if (env) { // env is nullptr for dummy databases (i.e. in tests). Don't actually flush if env is nullptr so we don't segfault
378  env->dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, nMinutes, 0);
379  }
380 }
381 
383 {
384  ++nUpdateCounter;
385 }
386 
388 {
389  Close();
391 }
392 
394 {
395  if (!pdb)
396  return;
397  if (activeTxn)
398  activeTxn->abort();
399  activeTxn = nullptr;
400  pdb = nullptr;
401  CloseCursor();
402 
403  if (fFlushOnClose)
404  Flush();
405 }
406 
407 void BerkeleyEnvironment::CloseDb(const std::string& strFile)
408 {
409  {
410  LOCK(cs_db);
411  auto it = m_databases.find(strFile);
412  assert(it != m_databases.end());
413  BerkeleyDatabase& database = it->second.get();
414  if (database.m_db) {
415  // Close the database handle
416  database.m_db->close(0);
417  database.m_db.reset();
418  }
419  }
420 }
421 
423 {
424  // Make sure that no Db's are in use
425  AssertLockNotHeld(cs_db);
426  std::unique_lock<RecursiveMutex> lock(cs_db);
427  m_db_in_use.wait(lock, [this](){
428  for (auto& db : m_databases) {
429  if (db.second.get().m_refcount > 0) return false;
430  }
431  return true;
432  });
433 
434  std::vector<std::string> filenames;
435  for (auto it : m_databases) {
436  filenames.push_back(it.first);
437  }
438  // Close the individual Db's
439  for (const std::string& filename : filenames) {
440  CloseDb(filename);
441  }
442  // Reset the environment
443  Flush(true); // This will flush and close the environment
444  Reset();
445  bilingual_str open_err;
446  Open(open_err);
447 }
448 
449 bool BerkeleyDatabase::Rewrite(const char* pszSkip)
450 {
451  while (true) {
452  {
453  LOCK(cs_db);
454  if (m_refcount <= 0) {
455  // Flush log data to the dat file
456  env->CloseDb(strFile);
457  env->CheckpointLSN(strFile);
458  m_refcount = -1;
459 
460  bool fSuccess = true;
461  LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile);
462  std::string strFileRes = strFile + ".rewrite";
463  { // surround usage of db with extra {}
464  BerkeleyBatch db(*this, true);
465  std::unique_ptr<Db> pdbCopy = std::make_unique<Db>(env->dbenv.get(), 0);
466 
467  int ret = pdbCopy->open(nullptr, // Txn pointer
468  strFileRes.c_str(), // Filename
469  "main", // Logical db name
470  DB_BTREE, // Database type
471  DB_CREATE, // Flags
472  0);
473  if (ret > 0) {
474  LogPrintf("BerkeleyBatch::Rewrite: Can't create database file %s\n", strFileRes);
475  fSuccess = false;
476  }
477 
478  if (db.StartCursor()) {
479  while (fSuccess) {
482  bool complete;
483  bool ret1 = db.ReadAtCursor(ssKey, ssValue, complete);
484  if (complete) {
485  break;
486  } else if (!ret1) {
487  fSuccess = false;
488  break;
489  }
490  if (pszSkip &&
491  strncmp((const char*)ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
492  continue;
493  if (strncmp((const char*)ssKey.data(), "\x07version", 8) == 0) {
494  // Update version:
495  ssValue.clear();
496  ssValue << CLIENT_VERSION;
497  }
498  Dbt datKey(ssKey.data(), ssKey.size());
499  Dbt datValue(ssValue.data(), ssValue.size());
500  int ret2 = pdbCopy->put(nullptr, &datKey, &datValue, DB_NOOVERWRITE);
501  if (ret2 > 0)
502  fSuccess = false;
503  }
504  db.CloseCursor();
505  }
506  if (fSuccess) {
507  db.Close();
508  env->CloseDb(strFile);
509  if (pdbCopy->close(0))
510  fSuccess = false;
511  } else {
512  pdbCopy->close(0);
513  }
514  }
515  if (fSuccess) {
516  Db dbA(env->dbenv.get(), 0);
517  if (dbA.remove(strFile.c_str(), nullptr, 0))
518  fSuccess = false;
519  Db dbB(env->dbenv.get(), 0);
520  if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0))
521  fSuccess = false;
522  }
523  if (!fSuccess)
524  LogPrintf("BerkeleyBatch::Rewrite: Failed to rewrite database file %s\n", strFileRes);
525  return fSuccess;
526  }
527  }
528  UninterruptibleSleep(std::chrono::milliseconds{100});
529  }
530 }
531 
532 
533 void BerkeleyEnvironment::Flush(bool fShutdown)
534 {
535  int64_t nStart = GetTimeMillis();
536  // Flush log data to the actual data file on all files that are not in use
537  LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: [%s] Flush(%s)%s\n", strPath, fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started");
538  if (!fDbEnvInit)
539  return;
540  {
541  LOCK(cs_db);
542  bool no_dbs_accessed = true;
543  for (auto& db_it : m_databases) {
544  std::string strFile = db_it.first;
545  int nRefCount = db_it.second.get().m_refcount;
546  if (nRefCount < 0) continue;
547  LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: Flushing %s (refcount = %d)...\n", strFile, nRefCount);
548  if (nRefCount == 0) {
549  // Move log data to the dat file
550  CloseDb(strFile);
551  LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: %s checkpoint\n", strFile);
552  dbenv->txn_checkpoint(0, 0, 0);
553  LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: %s detach\n", strFile);
554  if (!fMockDb)
555  dbenv->lsn_reset(strFile.c_str(), 0);
556  LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: %s closed\n", strFile);
557  nRefCount = -1;
558  } else {
559  no_dbs_accessed = false;
560  }
561  }
562  LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started", GetTimeMillis() - nStart);
563  if (fShutdown) {
564  char** listp;
565  if (no_dbs_accessed) {
566  dbenv->log_archive(&listp, DB_ARCH_REMOVE);
567  Close();
568  if (!fMockDb) {
569  fs::remove_all(fs::path(strPath) / "database");
570  }
571  }
572  }
573  }
574 }
575 
577 {
578  // Don't flush if we can't acquire the lock.
579  TRY_LOCK(cs_db, lockDb);
580  if (!lockDb) return false;
581 
582  // Don't flush if any databases are in use
583  for (auto& it : env->m_databases) {
584  if (it.second.get().m_refcount > 0) return false;
585  }
586 
587  // Don't flush if there haven't been any batch writes for this database.
588  if (m_refcount < 0) return false;
589 
590  LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile);
591  int64_t nStart = GetTimeMillis();
592 
593  // Flush wallet file so it's self contained
594  env->CloseDb(strFile);
595  env->CheckpointLSN(strFile);
596  m_refcount = -1;
597 
598  LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
599 
600  return true;
601 }
602 
603 bool BerkeleyDatabase::Backup(const std::string& strDest) const
604 {
605  while (true)
606  {
607  {
608  LOCK(cs_db);
609  if (m_refcount <= 0)
610  {
611  // Flush log data to the dat file
612  env->CloseDb(strFile);
613  env->CheckpointLSN(strFile);
614 
615  // Copy wallet file
616  fs::path pathSrc = env->Directory() / strFile;
617  fs::path pathDest(strDest);
618  if (fs::is_directory(pathDest))
619  pathDest /= strFile;
620 
621  try {
622  if (fs::equivalent(pathSrc, pathDest)) {
623  LogPrintf("cannot backup to wallet source file %s\n", pathDest.string());
624  return false;
625  }
626 
627  fs::copy_file(pathSrc, pathDest, fs::copy_option::overwrite_if_exists);
628  LogPrintf("copied %s to %s\n", strFile, pathDest.string());
629  return true;
630  } catch (const fs::filesystem_error& e) {
631  LogPrintf("error copying %s to %s - %s\n", strFile, pathDest.string(), fsbridge::get_filesystem_error_message(e));
632  return false;
633  }
634  }
635  }
636  UninterruptibleSleep(std::chrono::milliseconds{100});
637  }
638 }
639 
641 {
642  env->Flush(false);
643 }
644 
646 {
647  env->Flush(true);
648 }
649 
651 {
652  env->ReloadDbEnv();
653 }
654 
656 {
657  assert(!m_cursor);
658  if (!pdb)
659  return false;
660  int ret = pdb->cursor(nullptr, &m_cursor, 0);
661  return ret == 0;
662 }
663 
664 bool BerkeleyBatch::ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete)
665 {
666  complete = false;
667  if (m_cursor == nullptr) return false;
668  // Read at cursor
669  SafeDbt datKey;
670  SafeDbt datValue;
671  int ret = m_cursor->get(datKey, datValue, DB_NEXT);
672  if (ret == DB_NOTFOUND) {
673  complete = true;
674  }
675  if (ret != 0)
676  return false;
677  else if (datKey.get_data() == nullptr || datValue.get_data() == nullptr)
678  return false;
679 
680  // Convert to streams
681  ssKey.SetType(SER_DISK);
682  ssKey.clear();
683  ssKey.write((char*)datKey.get_data(), datKey.get_size());
684  ssValue.SetType(SER_DISK);
685  ssValue.clear();
686  ssValue.write((char*)datValue.get_data(), datValue.get_size());
687  return true;
688 }
689 
691 {
692  if (!m_cursor) return;
693  m_cursor->close();
694  m_cursor = nullptr;
695 }
696 
698 {
699  if (!pdb || activeTxn)
700  return false;
701  DbTxn* ptxn = env->TxnBegin();
702  if (!ptxn)
703  return false;
704  activeTxn = ptxn;
705  return true;
706 }
707 
709 {
710  if (!pdb || !activeTxn)
711  return false;
712  int ret = activeTxn->commit(0);
713  activeTxn = nullptr;
714  return (ret == 0);
715 }
716 
718 {
719  if (!pdb || !activeTxn)
720  return false;
721  int ret = activeTxn->abort();
722  activeTxn = nullptr;
723  return (ret == 0);
724 }
725 
727 {
728  int major, minor;
729  DbEnv::version(&major, &minor, nullptr);
730 
731  /* If the major version differs, or the minor version of library is *older*
732  * than the header that was compiled against, flag an error.
733  */
734  if (major != DB_VERSION_MAJOR || minor < DB_VERSION_MINOR) {
735  LogPrintf("BerkeleyDB database version conflict: header version is %d.%d, library version is %d.%d\n",
736  DB_VERSION_MAJOR, DB_VERSION_MINOR, major, minor);
737  return false;
738  }
739 
740  return true;
741 }
742 
744 {
745  return DbEnv::version(nullptr, nullptr, nullptr);
746 }
747 
749 {
750  if (!pdb)
751  return false;
752 
753  SafeDbt datKey(key.data(), key.size());
754 
755  SafeDbt datValue;
756  int ret = pdb->get(activeTxn, datKey, datValue, 0);
757  if (ret == 0 && datValue.get_data() != nullptr) {
758  value.write((char*)datValue.get_data(), datValue.get_size());
759  return true;
760  }
761  return false;
762 }
763 
764 bool BerkeleyBatch::WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite)
765 {
766  if (!pdb)
767  return false;
768  if (fReadOnly)
769  assert(!"Write called on database in read-only mode");
770 
771  SafeDbt datKey(key.data(), key.size());
772 
773  SafeDbt datValue(value.data(), value.size());
774 
775  int ret = pdb->put(activeTxn, datKey, datValue, (overwrite ? 0 : DB_NOOVERWRITE));
776  return (ret == 0);
777 }
778 
780 {
781  if (!pdb)
782  return false;
783  if (fReadOnly)
784  assert(!"Erase called on database in read-only mode");
785 
786  SafeDbt datKey(key.data(), key.size());
787 
788  int ret = pdb->del(activeTxn, datKey, 0);
789  return (ret == 0 || ret == DB_NOTFOUND);
790 }
791 
793 {
794  if (!pdb)
795  return false;
796 
797  SafeDbt datKey(key.data(), key.size());
798 
799  int ret = pdb->exists(activeTxn, datKey, 0);
800  return ret == 0;
801 }
802 
804 {
805  LOCK(cs_db);
806  if (m_refcount < 0) {
807  m_refcount = 1;
808  } else {
809  m_refcount++;
810  }
811 }
812 
814 {
815  LOCK(cs_db);
816  m_refcount--;
817  if (env) env->m_db_in_use.notify_all();
818 }
819 
820 std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(bool flush_on_close)
821 {
822  return std::make_unique<BerkeleyBatch>(*this, false, flush_on_close);
823 }
824 
825 std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
826 {
827  fs::path data_file = BDBDataFile(path);
828  std::unique_ptr<BerkeleyDatabase> db;
829  {
830  LOCK(cs_db); // Lock env.m_databases until insert in BerkeleyDatabase constructor
831  std::string data_filename = data_file.filename().string();
832  std::shared_ptr<BerkeleyEnvironment> env = GetBerkeleyEnv(data_file.parent_path());
833  if (env->m_databases.count(data_filename)) {
834  error = Untranslated(strprintf("Refusing to load database. Data file '%s' is already loaded.", (env->Directory() / data_filename).string()));
836  return nullptr;
837  }
838  db = std::make_unique<BerkeleyDatabase>(std::move(env), std::move(data_filename));
839  }
840 
841  if (options.verify && !db->Verify(error)) {
843  return nullptr;
844  }
845 
846  status = DatabaseStatus::SUCCESS;
847  return db;
848 }
BerkeleyEnvironment::BerkeleyEnvironment
BerkeleyEnvironment()
Construct an in-memory mock Berkeley environment for testing.
Definition: bdb.cpp:189
DatabaseStatus::FAILED_ALREADY_LOADED
@ FAILED_ALREADY_LOADED
DatabaseOptions
Definition: db.h:203
ArgsManager::GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:595
BerkeleyBatch::SafeDbt::SafeDbt
SafeDbt()
Definition: bdb.cpp:219
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:57
BerkeleyEnvironment::dbenv
std::unique_ptr< DbEnv > dbenv
Definition: bdb.h:54
assert
assert(!tx.IsCoinBase())
DatabaseStatus
DatabaseStatus
Definition: db.h:212
SER_DISK
@ SER_DISK
Definition: serialize.h:139
DatabaseOptions::verify
bool verify
Definition: db.h:209
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:24
BerkeleyDatabase::Flush
void Flush() override
Make sure all changes are flushed to database file.
Definition: bdb.cpp:640
CDataStream::data
value_type * data()
Definition: streams.h:264
fsbridge::get_filesystem_error_message
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:137
bilingual_str
Bilingual messages:
Definition: translation.h:16
BerkeleyDatabase::Open
void Open() override
Open the database if it is not already opened.
Definition: bdb.cpp:322
BerkeleyBatch::SafeDbt
RAII class that automatically cleanses its data on destruction.
Definition: bdb.h:171
UnlockDirectory
void UnlockDirectory(const fs::path &directory, const std::string &lockfile_name)
Definition: system.cpp:114
BerkeleyDatabaseVersion
std::string BerkeleyDatabaseVersion()
Definition: bdb.cpp:743
BerkeleyBatch::fFlushOnClose
bool fFlushOnClose
Definition: bdb.h:202
BerkeleyBatch::activeTxn
DbTxn * activeTxn
Definition: bdb.h:199
AnnotatedMixin< std::recursive_mutex >
BerkeleyDatabase::AddRef
void AddRef() override
Indicate the a new database user has began using the database.
Definition: bdb.cpp:803
TRY_LOCK
#define TRY_LOCK(cs, name)
Definition: sync.h:236
BerkeleyEnvironment::~BerkeleyEnvironment
~BerkeleyEnvironment()
Definition: bdb.cpp:119
BerkeleyDatabase::Rewrite
bool Rewrite(const char *pszSkip=nullptr) override
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
Definition: bdb.cpp:449
GUARDED_BY
#define GUARDED_BY(x)
Definition: threadsafety.h:38
bdb.h
BerkeleyEnvironment::fMockDb
bool fMockDb
Definition: bdb.h:48
memory_cleanse
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
BerkeleyBatch::ReadKey
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: bdb.cpp:748
BerkeleyEnvironment::m_db_in_use
std::condition_variable_any m_db_in_use
Definition: bdb.h:57
db.h
DatabaseBatch::Write
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:60
CDataStream::write
void write(const char *pch, size_t nSize)
Definition: streams.h:402
BerkeleyBatch::TxnAbort
bool TxnAbort() override
Definition: bdb.cpp:717
BerkeleyDatabase::m_db
std::unique_ptr< Db > m_db
Database pointer.
Definition: bdb.h:159
BerkeleyBatch::SafeDbt::m_dbt
Dbt m_dbt
Definition: bdb.h:173
BerkeleyEnvironment::ReloadDbEnv
void ReloadDbEnv()
Definition: bdb.cpp:422
strencodings.h
WalletDatabase::nUpdateCounter
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:148
WalletDatabaseFileId::operator==
bool operator==(const WalletDatabaseFileId &rhs) const
Definition: bdb.cpp:50
BerkeleyBatch::Flush
void Flush() override
Definition: bdb.cpp:367
BCLog::WALLETDB
@ WALLETDB
Definition: logging.h:44
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:40
AssertLockNotHeld
#define AssertLockNotHeld(cs)
Definition: sync.h:82
BerkeleyEnvironment::fDbEnvInit
bool fDbEnvInit
Definition: bdb.h:47
BerkeleyDatabase::strFile
std::string strFile
Definition: bdb.h:161
BerkeleyBatch::BerkeleyBatch
BerkeleyBatch(BerkeleyDatabase &database, const bool fReadOnly, bool fFlushOnCloseIn=true)
Definition: bdb.cpp:305
BerkeleyDatabase::env
std::shared_ptr< BerkeleyEnvironment > env
Pointer to shared database environment.
Definition: bdb.h:156
BerkeleyBatch::~BerkeleyBatch
~BerkeleyBatch() override
Definition: bdb.cpp:387
BerkeleyEnvironment::m_fileids
std::unordered_map< std::string, WalletDatabaseFileId > m_fileids
Definition: bdb.h:56
DatabaseBatch::Exists
bool Exists(const K &key)
Definition: db.h:84
BerkeleyDatabase
An instance of this class represents one database.
Definition: bdb.h:94
BerkeleyEnvironment::Flush
void Flush(bool fShutdown)
Definition: bdb.cpp:533
filenames
static const char * filenames[]
Definition: unitester.cpp:80
BerkeleyBatch::SafeDbt::get_data
const void * get_data() const
Definition: bdb.cpp:243
BerkeleyDatabase::Verify
bool Verify(bilingual_str &error)
Verifies the environment and database file.
Definition: bdb.cpp:258
BerkeleyBatch::WriteKey
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: bdb.cpp:764
BerkeleyBatch::fReadOnly
bool fReadOnly
Definition: bdb.h:201
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:583
BerkeleyDatabase::~BerkeleyDatabase
~BerkeleyDatabase() override
Definition: bdb.cpp:293
LogPrintf
#define LogPrintf(...)
Definition: logging.h:184
BerkeleyBatch::strFile
std::string strFile
Definition: bdb.h:198
BerkeleyBatch::Close
void Close() override
Definition: bdb.cpp:393
BerkeleyDatabase::ReloadDbEnv
void ReloadDbEnv() override
Definition: bdb.cpp:650
BDBDataFile
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:61
DatabaseStatus::FAILED_VERIFY
@ FAILED_VERIFY
BerkeleyEnvironment::Directory
fs::path Directory() const
Definition: bdb.h:66
BerkeleyEnvironment::Reset
void Reset()
Definition: bdb.cpp:107
WalletDatabaseFileId
Definition: bdb.h:37
BerkeleyBatch::HasKey
bool HasKey(CDataStream &&key) override
Definition: bdb.cpp:792
BerkeleyBatch::EraseKey
bool EraseKey(CDataStream &&key) override
Definition: bdb.cpp:779
LogPrint
#define LogPrint(category,...)
Definition: logging.h:188
DEFAULT_WALLET_PRIVDB
static const bool DEFAULT_WALLET_PRIVDB
Definition: bdb.h:35
BerkeleyBatch::SafeDbt::~SafeDbt
~SafeDbt()
Definition: bdb.cpp:229
CDataStream::size
size_type size() const
Definition: streams.h:255
GetBerkeleyEnv
std::shared_ptr< BerkeleyEnvironment > GetBerkeleyEnv(const fs::path &env_directory)
Get BerkeleyEnvironment given a directory path.
Definition: bdb.cpp:61
BerkeleyBatch::m_cursor
Dbc * m_cursor
Definition: bdb.h:200
UninterruptibleSleep
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:22
WalletDatabase::m_refcount
std::atomic< int > m_refcount
Counts the number of active database users to be sure that the database is not closed while someone i...
Definition: db.h:114
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
BerkeleyBatch::m_database
BerkeleyDatabase & m_database
Definition: bdb.h:204
WalletDatabaseFileId::value
u_int8_t value[DB_FILE_ID_LEN]
Definition: bdb.h:38
BerkeleyDatabase::PeriodicFlush
bool PeriodicFlush() override
Definition: bdb.cpp:576
BerkeleyBatch
RAII class that provides access to a Berkeley database.
Definition: bdb.h:168
BerkeleyBatch::StartCursor
bool StartCursor() override
Definition: bdb.cpp:655
translation.h
CDataStream::clear
void clear()
Definition: streams.h:261
BerkeleyEnvironment::m_databases
std::map< std::string, std::reference_wrapper< BerkeleyDatabase > > m_databases
Definition: bdb.h:55
LOCK
#define LOCK(cs)
Definition: sync.h:232
gArgs
ArgsManager gArgs
Definition: system.cpp:79
BerkeleyEnvironment::CheckpointLSN
void CheckpointLSN(const std::string &strFile)
Definition: bdb.cpp:285
BerkeleyEnvironment::Close
void Close()
Definition: bdb.cpp:77
BerkeleyBatch::SafeDbt::get_size
u_int32_t get_size() const
Definition: bdb.cpp:248
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
BerkeleyEnvironment::IsMock
bool IsMock() const
Definition: bdb.h:64
MakeBerkeleyDatabase
std::unique_ptr< BerkeleyDatabase > MakeBerkeleyDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Return object giving access to Berkeley database at specified path.
Definition: bdb.cpp:825
BerkeleyEnvironment::strPath
std::string strPath
Definition: bdb.h:51
BerkeleyDatabase::Backup
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: bdb.cpp:603
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
BerkeleyBatch::env
BerkeleyEnvironment * env
Definition: bdb.h:203
BerkeleyBatch::TxnCommit
bool TxnCommit() override
Definition: bdb.cpp:708
BerkeleyDatabaseSanityCheck
bool BerkeleyDatabaseSanityCheck()
Perform sanity check of runtime BDB version versus linked BDB version.
Definition: bdb.cpp:726
TryCreateDirectories
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost's create_directories if the requested directory exists.
Definition: system.cpp:1070
BerkeleyBatch::pdb
Db * pdb
Definition: bdb.h:197
BerkeleyDatabase::IncrementUpdateCounter
void IncrementUpdateCounter() override
Definition: bdb.cpp:382
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
BerkeleyEnvironment::Open
bool Open(bilingual_str &error)
Definition: bdb.cpp:126
BerkeleyBatch::CloseCursor
void CloseCursor() override
Definition: bdb.cpp:690
BerkeleyDatabase::Close
void Close() override
Flush to the database file and close the database.
Definition: bdb.cpp:645
BerkeleyBatch::TxnBegin
bool TxnBegin() override
Definition: bdb.cpp:697
DatabaseStatus::SUCCESS
@ SUCCESS
BerkeleyDatabase::RemoveRef
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: bdb.cpp:813
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:594
CDataStream::SetType
void SetType(int n)
Definition: streams.h:360
LockDirectory
bool LockDirectory(const fs::path &directory, const std::string lockfile_name, bool probe_only)
Definition: system.cpp:90
DEFAULT_WALLET_DBLOGSIZE
static const unsigned int DEFAULT_WALLET_DBLOGSIZE
Definition: bdb.h:34
BerkeleyEnvironment::CloseDb
void CloseDb(const std::string &strFile)
Definition: bdb.cpp:407
BerkeleyBatch::ReadAtCursor
bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) override
Definition: bdb.cpp:664
BerkeleyDatabase::MakeBatch
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a BerkeleyBatch connected to this database.
Definition: bdb.cpp:820
BerkeleyEnvironment
Definition: bdb.h:44
GetTimeMillis
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:117
it
auto it
Definition: validation.cpp:355
BerkeleyEnvironment::TxnBegin
DbTxn * TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
Definition: bdb.h:76