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