diff options
Diffstat (limited to 'src/wallet/bdb.cpp')
-rw-r--r-- | src/wallet/bdb.cpp | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index cbf6c9b1ea..d82d8d4513 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -65,6 +65,8 @@ RecursiveMutex cs_db; std::map<std::string, std::weak_ptr<BerkeleyEnvironment>> g_dbenvs GUARDED_BY(cs_db); //!< Map from directory name to db environment. } // namespace +static constexpr auto REVERSE_BYTE_ORDER{std::endian::native == std::endian::little ? 4321 : 1234}; + bool WalletDatabaseFileId::operator==(const WalletDatabaseFileId& rhs) const { return memcmp(value, &rhs.value, sizeof(value)) == 0; @@ -300,7 +302,11 @@ static Span<const std::byte> SpanFromDbt(const SafeDbt& dbt) } BerkeleyDatabase::BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, fs::path filename, const DatabaseOptions& options) : - WalletDatabase(), env(std::move(env)), m_filename(std::move(filename)), m_max_log_mb(options.max_log_mb) + WalletDatabase(), + env(std::move(env)), + m_byteswap(options.require_format == DatabaseFormat::BERKELEY_SWAP), + m_filename(std::move(filename)), + m_max_log_mb(options.max_log_mb) { auto inserted = this->env->m_databases.emplace(m_filename, std::ref(*this)); assert(inserted.second); @@ -389,6 +395,10 @@ void BerkeleyDatabase::Open() } } + if (m_byteswap) { + pdb_temp->set_lorder(REVERSE_BYTE_ORDER); + } + ret = pdb_temp->open(nullptr, // Txn pointer fMockDb ? nullptr : strFile.c_str(), // Filename fMockDb ? strFile.c_str() : "main", // Logical db name @@ -521,6 +531,10 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip) BerkeleyBatch db(*this, true); std::unique_ptr<Db> pdbCopy = std::make_unique<Db>(env->dbenv.get(), 0); + if (m_byteswap) { + pdbCopy->set_lorder(REVERSE_BYTE_ORDER); + } + int ret = pdbCopy->open(nullptr, // Txn pointer strFileRes.c_str(), // Filename "main", // Logical db name @@ -887,7 +901,12 @@ bool BerkeleyBatch::HasKey(DataStream&& key) bool BerkeleyBatch::ErasePrefix(Span<const std::byte> prefix) { - if (!TxnBegin()) return false; + // Because this function erases records one by one, ensure that it is executed within a txn context. + // Otherwise, consistency is at risk; it's possible that certain records are removed while others + // remain due to an internal failure during the procedure. + // Additionally, the Dbc::del() cursor delete call below would fail without an active transaction. + if (!Assume(activeTxn)) return false; + auto cursor{std::make_unique<BerkeleyCursor>(m_database, *this)}; // const_cast is safe below even though prefix_key is an in/out parameter, // because we are not using the DB_DBT_USERMEM flag, so BDB will allocate @@ -901,7 +920,7 @@ bool BerkeleyBatch::ErasePrefix(Span<const std::byte> prefix) ret = cursor->dbc()->del(0); } cursor.reset(); - return TxnCommit() && (ret == 0 || ret == DB_NOTFOUND); + return ret == 0 || ret == DB_NOTFOUND; } void BerkeleyDatabase::AddRef() |