diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index c0a2895a587..830c04c4cf5 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -300,6 +299,20 @@ std::vector CDBWrapper::CreateObfuscateKey() const return ret; } +std::optional CDBWrapper::ReadImpl(Span ssKey) const +{ + leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); + std::string strValue; + leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); + if (!status.ok()) { + if (status.IsNotFound()) + return std::nullopt; + LogPrintf("LevelDB read failure: %s\n", status.ToString()); + dbwrapper_private::HandleError(status); + } + return strValue; +} + bool CDBWrapper::IsEmpty() { std::unique_ptr it(NewIterator()); diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 9bcb79c169e..7e5ffe10620 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -235,6 +235,8 @@ private: //! whether or not the database resides in memory bool m_is_memory; + std::optional ReadImpl(Span ssKey) const; + public: CDBWrapper(const DBParams& params); ~CDBWrapper(); @@ -248,18 +250,12 @@ public: DataStream ssKey{}; ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey << key; - leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); - - std::string strValue; - leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); - if (!status.ok()) { - if (status.IsNotFound()) - return false; - LogPrintf("LevelDB read failure: %s\n", status.ToString()); - dbwrapper_private::HandleError(status); + std::optional strValue{ReadImpl(ssKey)}; + if (!strValue) { + return false; } try { - CDataStream ssValue{MakeByteSpan(strValue), SER_DISK, CLIENT_VERSION}; + CDataStream ssValue{MakeByteSpan(*strValue), SER_DISK, CLIENT_VERSION}; ssValue.Xor(obfuscate_key); ssValue >> value; } catch (const std::exception&) {