From dd57713f6ede3d46e97ee7df87c10001b0bf4c3d Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Tue, 2 Jan 2024 16:36:06 -0500 Subject: [PATCH] Add MakeBerkeleyRODatabase Implements MakeBerkeleyRODatabase and adds DatabaseFormat::BERKELEY_RO so that MakeDatabase can use BerkeleyRO as the backend database. --- src/wallet/db.h | 1 + src/wallet/migrate.cpp | 15 +++++++++++++++ src/wallet/migrate.h | 3 +++ src/wallet/walletdb.cpp | 10 ++++++++++ 4 files changed, 29 insertions(+) diff --git a/src/wallet/db.h b/src/wallet/db.h index 648adff5fe..5751bba2e9 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -183,6 +183,7 @@ public: enum class DatabaseFormat { BERKELEY, SQLITE, + BERKELEY_RO, }; struct DatabaseOptions { diff --git a/src/wallet/migrate.cpp b/src/wallet/migrate.cpp index 44e8b26be5..895cee9e14 100644 --- a/src/wallet/migrate.cpp +++ b/src/wallet/migrate.cpp @@ -5,6 +5,7 @@ #include // For ReadBE32 #include #include +#include #include #include @@ -748,4 +749,18 @@ std::unique_ptr BerkeleyROBatch::GetNewPrefixCursor(Span(m_database, prefix); } + +std::unique_ptr MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error) +{ + fs::path data_file = BDBDataFile(path); + try { + std::unique_ptr db = std::make_unique(data_file); + status = DatabaseStatus::SUCCESS; + return db; + } catch (const std::runtime_error& e) { + error.original = e.what(); + status = DatabaseStatus::FAILED_LOAD; + return nullptr; + } +} } // namespace wallet diff --git a/src/wallet/migrate.h b/src/wallet/migrate.h index a3b0d78d02..e4826450af 100644 --- a/src/wallet/migrate.h +++ b/src/wallet/migrate.h @@ -116,6 +116,9 @@ public: bool TxnCommit() override { return false; } bool TxnAbort() override { return false; } }; + +//! Return object giving access to Berkeley Read Only database at specified path. +std::unique_ptr MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error); } // namespace wallet #endif // BITCOIN_WALLET_MIGRATE_H diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index b1ce7ee4e7..ddd90939bf 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -23,6 +23,7 @@ #ifdef USE_BDB #include #endif +#include #ifdef USE_SQLITE #include #endif @@ -1389,6 +1390,11 @@ std::unique_ptr MakeDatabase(const fs::path& path, const Databas return nullptr; } + // If BERKELEY was the format, then change the format from BERKELEY to BERKELEY_RO + if (format && options.require_format && format == DatabaseFormat::BERKELEY && options.require_format == DatabaseFormat::BERKELEY_RO) { + format = DatabaseFormat::BERKELEY_RO; + } + // A db already exists so format is set, but options also specifies the format, so make sure they agree if (format && options.require_format && format != options.require_format) { error = Untranslated(strprintf("Failed to load database path '%s'. Data is not in required format.", fs::PathToString(path))); @@ -1422,6 +1428,10 @@ std::unique_ptr MakeDatabase(const fs::path& path, const Databas } } + if (format == DatabaseFormat::BERKELEY_RO) { + return MakeBerkeleyRODatabase(path, options, status, error); + } + #ifdef USE_BDB if constexpr (true) { return MakeBerkeleyDatabase(path, options, status, error);