Bitcoin ABC 0.32.11
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 <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 (util::LockDirectory(pathIn, ".walletlock") !=
163 LogPrintf("Cannot obtain a lock on wallet directory %s. Another "
164 "instance of bitcoin may be using it.\n",
165 strPath);
166 err = strprintf(_("Error initializing wallet database environment %s!"),
168 return false;
169 }
170
171 fs::path pathLogDir = pathIn / "database";
172 TryCreateDirectories(pathLogDir);
173 fs::path pathErrorFile = pathIn / "db.log";
174 LogPrintf("BerkeleyEnvironment::Open: LogDir=%s ErrorFile=%s\n",
175 fs::PathToString(pathLogDir), fs::PathToString(pathErrorFile));
176
177 unsigned int nEnvFlags = 0;
178 if (gArgs.GetBoolArg("-privdb", DEFAULT_WALLET_PRIVDB)) {
179 nEnvFlags |= DB_PRIVATE;
180 }
181
182 dbenv->set_lg_dir(fs::PathToString(pathLogDir).c_str());
183 // 1 MiB should be enough for just the wallet
184 dbenv->set_cachesize(0, 0x100000, 1);
185 dbenv->set_lg_bsize(0x10000);
186 dbenv->set_lg_max(1048576);
187 dbenv->set_lk_max_locks(40000);
188 dbenv->set_lk_max_objects(40000);
190 dbenv->set_errfile(fsbridge::fopen(pathErrorFile, "a"));
191 dbenv->set_flags(DB_AUTO_COMMIT, 1);
192 dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1);
193 dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1);
194 int ret =
195 dbenv->open(strPath.c_str(),
196 DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
197 DB_INIT_TXN | DB_THREAD | DB_RECOVER | nEnvFlags,
198 S_IRUSR | S_IWUSR);
199 if (ret != 0) {
200 LogPrintf("BerkeleyEnvironment::Open: Error %d opening database "
201 "environment: %s\n",
202 ret, DbEnv::strerror(ret));
203 int ret2 = dbenv->close(0);
204 if (ret2 != 0) {
205 LogPrintf("BerkeleyEnvironment::Open: Error %d closing failed "
206 "database environment: %s\n",
207 ret2, DbEnv::strerror(ret2));
208 }
209 Reset();
210 err = strprintf(_("Error initializing wallet database environment %s!"),
212 if (ret == DB_RUNRECOVERY) {
213 err += Untranslated(" ") +
214 _("This error could occur if this wallet was not shutdown "
215 "cleanly and was last loaded using a build with a newer "
216 "version of Berkeley DB. If so, please use the software "
217 "that last loaded this wallet");
218 }
219 return false;
220 }
221
222 fDbEnvInit = true;
223 fMockDb = false;
224 return true;
225}
226
229 Reset();
230
231 LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::MakeMock\n");
232
233 dbenv->set_cachesize(1, 0, 1);
234 dbenv->set_lg_bsize(10485760 * 4);
235 dbenv->set_lg_max(10485760);
236 dbenv->set_lk_max_locks(10000);
237 dbenv->set_lk_max_objects(10000);
238 dbenv->set_flags(DB_AUTO_COMMIT, 1);
239 dbenv->log_set_config(DB_LOG_IN_MEMORY, 1);
240 int ret =
241 dbenv->open(nullptr,
242 DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
243 DB_INIT_TXN | DB_THREAD | DB_PRIVATE,
244 S_IRUSR | S_IWUSR);
245 if (ret > 0) {
246 throw std::runtime_error(
247 strprintf("BerkeleyEnvironment::MakeMock: Error %d opening "
248 "database environment.",
249 ret));
250 }
251
252 fDbEnvInit = true;
253 fMockDb = true;
254}
255
257 m_dbt.set_flags(DB_DBT_MALLOC);
258}
259
260BerkeleyBatch::SafeDbt::SafeDbt(void *data, size_t size) : m_dbt(data, size) {}
261
263 if (m_dbt.get_data() != nullptr) {
264 // Clear memory, e.g. in case it was a private key
265 memory_cleanse(m_dbt.get_data(), m_dbt.get_size());
266 // under DB_DBT_MALLOC, data is malloced by the Dbt, but must be
267 // freed by the caller.
268 // https://docs.oracle.com/cd/E17275_01/html/api_reference/C/dbt.html
269 if (m_dbt.get_flags() & DB_DBT_MALLOC) {
270 free(m_dbt.get_data());
271 }
272 }
273}
274
276 return m_dbt.get_data();
277}
278
280 return m_dbt.get_size();
281}
282
283BerkeleyBatch::SafeDbt::operator Dbt *() {
284 return &m_dbt;
285}
286
288 fs::path walletDir = env->Directory();
289 fs::path file_path = walletDir / strFile;
290
291 LogPrintf("Using BerkeleyDB version %s\n", BerkeleyDatabaseVersion());
292 LogPrintf("Using wallet %s\n", fs::PathToString(file_path));
293
294 if (!env->Open(errorStr)) {
295 return false;
296 }
297
298 if (fs::exists(file_path)) {
299 assert(m_refcount == 0);
300
301 Db db(env->dbenv.get(), 0);
302 int result = db.verify(strFile.c_str(), nullptr, nullptr, 0);
303 if (result != 0) {
304 errorStr =
305 strprintf(_("%s corrupt. Try using the wallet tool "
306 "bitcoin-wallet to salvage or restoring a backup."),
307 fs::quoted(fs::PathToString(file_path)));
308 return false;
309 }
310 }
311 // also return true if files does not exists
312 return true;
313}
314
316 dbenv->txn_checkpoint(0, 0, 0);
317 if (fMockDb) {
318 return;
319 }
320 dbenv->lsn_reset(strFile.c_str(), 0);
321}
322
324 if (env) {
325 LOCK(cs_db);
327 assert(!m_db);
328 size_t erased = env->m_databases.erase(strFile);
329 assert(erased == 1);
330 env->m_fileids.erase(strFile);
331 }
332}
333
334BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase &database, const bool read_only,
335 bool fFlushOnCloseIn)
336 : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr),
337 m_database(database) {
338 database.AddRef();
339 database.Open();
340 fReadOnly = read_only;
341 fFlushOnClose = fFlushOnCloseIn;
342 env = database.env.get();
343 pdb = database.m_db.get();
344 strFile = database.strFile;
345}
346
348 unsigned int nFlags = DB_THREAD | DB_CREATE;
349
350 {
351 LOCK(cs_db);
352 bilingual_str open_err;
353 if (!env->Open(open_err)) {
354 throw std::runtime_error(
355 "BerkeleyDatabase: Failed to open database environment.");
356 }
357
358 if (m_db == nullptr) {
359 int ret;
360 std::unique_ptr<Db> pdb_temp =
361 std::make_unique<Db>(env->dbenv.get(), 0);
362
363 bool fMockDb = env->IsMock();
364 if (fMockDb) {
365 DbMpoolFile *mpf = pdb_temp->get_mpf();
366 ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
367 if (ret != 0) {
368 throw std::runtime_error(strprintf(
369 "BerkeleyDatabase: Failed to configure for no "
370 "temp file backing for database %s",
371 strFile));
372 }
373 }
374
375 ret = pdb_temp->open(
376 nullptr, // Txn pointer
377 fMockDb ? nullptr : strFile.c_str(), // Filename
378 fMockDb ? strFile.c_str() : "main", // Logical db name
379 DB_BTREE, // Database type
380 nFlags, // Flags
381 0);
382
383 if (ret != 0) {
384 throw std::runtime_error(strprintf(
385 "BerkeleyDatabase: Error %d, can't open database %s", ret,
386 strFile));
387 }
388
389 // Call CheckUniqueFileid on the containing BDB environment to
390 // avoid BDB data consistency bugs that happen when different data
391 // files in the same environment have the same fileid.
392 CheckUniqueFileid(*env, strFile, *pdb_temp,
393 this->env->m_fileids[strFile]);
394
395 m_db.reset(pdb_temp.release());
396 }
397 }
398}
399
401 if (activeTxn) {
402 return;
403 }
404
405 // Flush database activity from memory pool to disk log
406 unsigned int nMinutes = 0;
407 if (fReadOnly) {
408 nMinutes = 1;
409 }
410
411 // env is nullptr for dummy databases (i.e. in tests). Don't actually flush
412 // if env is nullptr so we don't segfault
413 if (env) {
414 env->dbenv->txn_checkpoint(
415 nMinutes
416 ? gArgs.GetIntArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024
417 : 0,
418 nMinutes, 0);
419 }
420}
421
424}
425
427 Close();
429}
430
432 if (!pdb) {
433 return;
434 }
435 if (activeTxn) {
436 activeTxn->abort();
437 }
438 activeTxn = nullptr;
439 pdb = nullptr;
440 CloseCursor();
441
442 if (fFlushOnClose) {
443 Flush();
444 }
445}
446
447void BerkeleyEnvironment::CloseDb(const std::string &strFile) {
448 LOCK(cs_db);
449 auto it = m_databases.find(strFile);
450 assert(it != m_databases.end());
451 BerkeleyDatabase &database = it->second.get();
452 if (database.m_db) {
453 // Close the database handle
454 database.m_db->close(0);
455 database.m_db.reset();
456 }
457}
458
460 // Make sure that no Db's are in use
461 AssertLockNotHeld(cs_db);
462 std::unique_lock<RecursiveMutex> lock(cs_db);
463 m_db_in_use.wait(lock, [this]() {
464 for (auto &db : m_databases) {
465 if (db.second.get().m_refcount > 0) {
466 return false;
467 }
468 }
469 return true;
470 });
471
472 std::vector<std::string> filenames;
473 filenames.reserve(m_databases.size());
474 for (const auto &it : m_databases) {
475 filenames.push_back(it.first);
476 }
477 // Close the individual Db's
478 for (const std::string &filename : filenames) {
479 CloseDb(filename);
480 }
481 // Reset the environment
482 // This will flush and close the environment
483 Flush(true);
484 Reset();
485 bilingual_str open_err;
486 Open(open_err);
487}
488
489bool BerkeleyDatabase::Rewrite(const char *pszSkip) {
490 while (true) {
491 {
492 LOCK(cs_db);
493 if (m_refcount <= 0) {
494 // Flush log data to the dat file
495 env->CloseDb(strFile);
496 env->CheckpointLSN(strFile);
497 m_refcount = -1;
498
499 bool fSuccess = true;
500 LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile);
501 std::string strFileRes = strFile + ".rewrite";
502 { // surround usage of db with extra {}
503 BerkeleyBatch db(*this, true);
504 std::unique_ptr<Db> pdbCopy =
505 std::make_unique<Db>(env->dbenv.get(), 0);
506
507 int ret = pdbCopy->open(nullptr, // Txn pointer
508 strFileRes.c_str(), // Filename
509 "main", // Logical db name
510 DB_BTREE, // Database type
511 DB_CREATE, // Flags
512 0);
513 if (ret > 0) {
514 LogPrintf("BerkeleyBatch::Rewrite: Can't create "
515 "database file %s\n",
516 strFileRes);
517 fSuccess = false;
518 }
519
520 if (db.StartCursor()) {
521 while (fSuccess) {
522 DataStream ssKey{};
523 DataStream ssValue{};
524 bool complete;
525 bool ret1 =
526 db.ReadAtCursor(ssKey, ssValue, complete);
527 if (complete) {
528 break;
529 }
530 if (!ret1) {
531 fSuccess = false;
532 break;
533 }
534 if (pszSkip &&
535 strncmp((const char *)ssKey.data(), pszSkip,
536 std::min(ssKey.size(),
537 strlen(pszSkip))) == 0) {
538 continue;
539 }
540 if (strncmp((const char *)ssKey.data(),
541 "\x07version", 8) == 0) {
542 // Update version:
543 ssValue.clear();
544 ssValue << CLIENT_VERSION;
545 }
546 Dbt datKey(ssKey.data(), ssKey.size());
547 Dbt datValue(ssValue.data(), ssValue.size());
548 int ret2 = pdbCopy->put(nullptr, &datKey, &datValue,
549 DB_NOOVERWRITE);
550 if (ret2 > 0) {
551 fSuccess = false;
552 }
553 }
554 db.CloseCursor();
555 }
556 if (fSuccess) {
557 db.Close();
558 env->CloseDb(strFile);
559 if (pdbCopy->close(0)) {
560 fSuccess = false;
561 }
562 } else {
563 pdbCopy->close(0);
564 }
565 }
566 if (fSuccess) {
567 Db dbA(env->dbenv.get(), 0);
568 if (dbA.remove(strFile.c_str(), nullptr, 0)) {
569 fSuccess = false;
570 }
571 Db dbB(env->dbenv.get(), 0);
572 if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(),
573 0)) {
574 fSuccess = false;
575 }
576 }
577 if (!fSuccess) {
578 LogPrintf("BerkeleyBatch::Rewrite: Failed to rewrite "
579 "database file %s\n",
580 strFileRes);
581 }
582 return fSuccess;
583 }
584 }
585 UninterruptibleSleep(std::chrono::milliseconds{100});
586 }
587}
588
589void BerkeleyEnvironment::Flush(bool fShutdown) {
590 int64_t nStart = GetTimeMillis();
591 // Flush log data to the actual data file on all files that are not in use
592 LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: [%s] Flush(%s)%s\n",
593 strPath, fShutdown ? "true" : "false",
594 fDbEnvInit ? "" : " database not started");
595 if (!fDbEnvInit) {
596 return;
597 }
598 {
599 LOCK(cs_db);
600 bool no_dbs_accessed = true;
601 for (auto &db_it : m_databases) {
602 std::string strFile = db_it.first;
603 int nRefCount = db_it.second.get().m_refcount;
604 if (nRefCount < 0) {
605 continue;
606 }
607 LogPrint(
609 "BerkeleyEnvironment::Flush: Flushing %s (refcount = %d)...\n",
610 strFile, nRefCount);
611 if (nRefCount == 0) {
612 // Move log data to the dat file
613 CloseDb(strFile);
615 "BerkeleyEnvironment::Flush: %s checkpoint\n",
616 strFile);
617 dbenv->txn_checkpoint(0, 0, 0);
619 "BerkeleyEnvironment::Flush: %s detach\n", strFile);
620 if (!fMockDb) {
621 dbenv->lsn_reset(strFile.c_str(), 0);
622 }
624 "BerkeleyEnvironment::Flush: %s closed\n", strFile);
625 nRefCount = -1;
626 } else {
627 no_dbs_accessed = false;
628 }
629 }
631 "BerkeleyEnvironment::Flush: Flush(%s)%s took %15dms\n",
632 fShutdown ? "true" : "false",
633 fDbEnvInit ? "" : " database not started",
634 GetTimeMillis() - nStart);
635 if (fShutdown) {
636 char **listp;
637 if (no_dbs_accessed) {
638 dbenv->log_archive(&listp, DB_ARCH_REMOVE);
639 Close();
640 if (!fMockDb) {
641 fs::remove_all(fs::PathFromString(strPath) / "database");
642 }
643 }
644 }
645 }
646}
647
649 // Don't flush if we can't acquire the lock.
650 TRY_LOCK(cs_db, lockDb);
651 if (!lockDb) {
652 return false;
653 }
654
655 // Don't flush if any databases are in use
656 for (auto &it : env->m_databases) {
657 if (it.second.get().m_refcount > 0) {
658 return false;
659 }
660 }
661
662 // Don't flush if there haven't been any batch writes for this database.
663 if (m_refcount < 0) {
664 return false;
665 }
666
667 LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile);
668 int64_t nStart = GetTimeMillis();
669
670 // Flush wallet file so it's self contained
671 env->CloseDb(strFile);
672 env->CheckpointLSN(strFile);
673 m_refcount = -1;
674
675 LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile,
676 GetTimeMillis() - nStart);
677
678 return true;
679}
680
681bool BerkeleyDatabase::Backup(const std::string &strDest) const {
682 while (true) {
683 {
684 LOCK(cs_db);
685 if (m_refcount <= 0) {
686 // Flush log data to the dat file
687 env->CloseDb(strFile);
688 env->CheckpointLSN(strFile);
689
690 // Copy wallet file.
691 fs::path pathSrc = env->Directory() / strFile;
692 fs::path pathDest(fs::PathFromString(strDest));
693 if (fs::is_directory(pathDest)) {
694 pathDest /= fs::PathFromString(strFile);
695 }
696
697 try {
698 if (fs::exists(pathDest) &&
699 fs::equivalent(pathSrc, pathDest)) {
700 LogPrintf("cannot backup to wallet source file %s\n",
701 fs::PathToString(pathDest));
702 return false;
703 }
704
705 fs::copy_file(pathSrc, pathDest,
706 fs::copy_options::overwrite_existing);
707 LogPrintf("copied %s to %s\n", strFile,
708 fs::PathToString(pathDest));
709 return true;
710 } catch (const fs::filesystem_error &e) {
711 LogPrintf("error copying %s to %s - %s\n", strFile,
712 fs::PathToString(pathDest),
714 return false;
715 }
716 }
717 }
718 UninterruptibleSleep(std::chrono::milliseconds{100});
719 }
720}
721
723 env->Flush(false);
724}
725
727 env->Flush(true);
728}
729
731 env->ReloadDbEnv();
732}
733
736 if (!pdb) {
737 return false;
738 }
739 int ret = pdb->cursor(nullptr, &m_cursor, 0);
740 return ret == 0;
741}
742
744 bool &complete) {
745 complete = false;
746 if (m_cursor == nullptr) {
747 return false;
748 }
749 // Read at cursor
750 SafeDbt datKey;
751 SafeDbt datValue;
752 int ret = m_cursor->get(datKey, datValue, DB_NEXT);
753 if (ret == DB_NOTFOUND) {
754 complete = true;
755 }
756 if (ret != 0) {
757 return false;
758 } else if (datKey.get_data() == nullptr || datValue.get_data() == nullptr) {
759 return false;
760 }
761
762 // Convert to streams
763 ssKey.clear();
764 ssKey.write({BytePtr(datKey.get_data()), datKey.get_size()});
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>
903 DatabaseStatus &status, bilingual_str &error) {
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)) {
912 error = Untranslated(strprintf(
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:39
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: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:279
const void * get_data() const
Definition: bdb.cpp:275
RAII class that provides access to a Berkeley database.
Definition: bdb.h:187
bool HasKey(DataStream &&key) override
Definition: bdb.cpp:861
void Close() override
Definition: bdb.cpp:431
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: bdb.cpp:812
std::string strFile
Definition: bdb.h:216
bool TxnCommit() override
Definition: bdb.cpp:790
void Flush() override
Definition: bdb.cpp:400
bool ReadAtCursor(DataStream &ssKey, DataStream &ssValue, bool &complete) override
Definition: bdb.cpp:743
bool StartCursor() override
Definition: bdb.cpp:734
void CloseCursor() override
Definition: bdb.cpp:770
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: bdb.cpp:828
BerkeleyBatch(BerkeleyDatabase &database, const bool fReadOnly, bool fFlushOnCloseIn=true)
Definition: bdb.cpp:334
bool EraseKey(DataStream &&key) override
Definition: bdb.cpp:847
BerkeleyEnvironment * env
Definition: bdb.h:221
bool TxnAbort() override
Definition: bdb.cpp:799
Db * pdb
Definition: bdb.h:215
~BerkeleyBatch() override
Definition: bdb.cpp:426
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: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:422
void ReloadDbEnv() override
Definition: bdb.cpp:730
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:323
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:489
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:722
void Open() override
Open the database if it is not already opened.
Definition: bdb.cpp:347
bool PeriodicFlush() override
flush the wallet passively (TRY_LOCK) ideal to be called periodically
Definition: bdb.cpp:648
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:726
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:287
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: bdb.cpp:681
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:459
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:315
void Flush(bool fShutdown)
Definition: bdb.cpp:589
bool fMockDb
Definition: bdb.h:41
BerkeleyEnvironment()
Construct an in-memory mock Berkeley environment for testing.
Definition: bdb.cpp:228
void CloseDb(const std::string &strFile)
Definition: bdb.cpp:447
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:118
void write(Span< const value_type > src)
Definition: streams.h:290
void clear()
Definition: streams.h:161
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
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
#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:224
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:58
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: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:227