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