2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2023-08-16 19:27:31 +02:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-10-31 01:43:19 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2015-03-21 18:15:31 +01:00
|
|
|
#ifndef BITCOIN_WALLET_DB_H
|
|
|
|
#define BITCOIN_WALLET_DB_H
|
2011-05-15 09:11:04 +02:00
|
|
|
|
2020-06-18 22:15:33 +02:00
|
|
|
#include <clientversion.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <fs.h>
|
2020-06-18 22:15:33 +02:00
|
|
|
#include <streams.h>
|
2022-10-17 04:33:10 +02:00
|
|
|
#include <support/allocators/secure.h>
|
2011-05-15 09:11:04 +02:00
|
|
|
|
2022-03-04 09:26:11 +01:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
2022-10-15 22:11:49 +02:00
|
|
|
#include <optional>
|
2011-05-15 09:11:04 +02:00
|
|
|
#include <string>
|
2018-11-20 15:15:51 +01:00
|
|
|
|
2022-04-07 06:43:16 +02:00
|
|
|
struct bilingual_str;
|
|
|
|
|
2022-03-03 19:37:45 +01:00
|
|
|
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
|
2020-03-14 07:51:27 +01:00
|
|
|
|
2020-06-18 22:15:33 +02:00
|
|
|
/** RAII class that provides access to a WalletDatabase */
|
|
|
|
class DatabaseBatch
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
virtual bool ReadKey(CDataStream&& key, CDataStream& value) = 0;
|
|
|
|
virtual bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) = 0;
|
|
|
|
virtual bool EraseKey(CDataStream&& key) = 0;
|
|
|
|
virtual bool HasKey(CDataStream&& key) = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit DatabaseBatch() {}
|
|
|
|
virtual ~DatabaseBatch() {}
|
|
|
|
|
|
|
|
DatabaseBatch(const DatabaseBatch&) = delete;
|
|
|
|
DatabaseBatch& operator=(const DatabaseBatch&) = delete;
|
|
|
|
|
|
|
|
virtual void Flush() = 0;
|
|
|
|
virtual void Close() = 0;
|
|
|
|
|
|
|
|
template <typename K, typename T>
|
|
|
|
bool Read(const K& key, T& value)
|
|
|
|
{
|
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
|
|
|
|
|
|
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
|
|
|
if (!ReadKey(std::move(ssKey), ssValue)) return false;
|
|
|
|
try {
|
|
|
|
ssValue >> value;
|
|
|
|
return true;
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename K, typename T>
|
|
|
|
bool Write(const K& key, const T& value, bool fOverwrite = true)
|
|
|
|
{
|
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
|
|
|
|
|
|
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssValue.reserve(10000);
|
|
|
|
ssValue << value;
|
|
|
|
|
|
|
|
return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename K>
|
|
|
|
bool Erase(const K& key)
|
|
|
|
{
|
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
|
|
|
|
|
|
|
return EraseKey(std::move(ssKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename K>
|
|
|
|
bool Exists(const K& key)
|
|
|
|
{
|
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
|
|
|
|
|
|
|
return HasKey(std::move(ssKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool StartCursor() = 0;
|
|
|
|
virtual bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) = 0;
|
|
|
|
virtual void CloseCursor() = 0;
|
|
|
|
virtual bool TxnBegin() = 0;
|
|
|
|
virtual bool TxnCommit() = 0;
|
|
|
|
virtual bool TxnAbort() = 0;
|
|
|
|
};
|
|
|
|
|
2022-03-04 09:26:11 +01:00
|
|
|
/** An instance of this class represents one database.
|
|
|
|
**/
|
|
|
|
class WalletDatabase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Create dummy DB handle */
|
|
|
|
WalletDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0) {}
|
|
|
|
virtual ~WalletDatabase() {};
|
|
|
|
|
|
|
|
/** Open the database if it is not already opened. */
|
2022-11-03 16:52:28 +01:00
|
|
|
virtual void Open() = 0;
|
2022-03-04 09:26:11 +01:00
|
|
|
|
|
|
|
//! Counts the number of active database users to be sure that the database is not closed while someone is using it
|
|
|
|
std::atomic<int> m_refcount{0};
|
|
|
|
/** Indicate the a new database user has began using the database. Increments m_refcount */
|
|
|
|
virtual void AddRef() = 0;
|
|
|
|
/** Indicate that database user has stopped using the database and that it could be flushed or closed. Decrement m_refcount */
|
|
|
|
virtual void RemoveRef() = 0;
|
|
|
|
|
|
|
|
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
|
|
|
|
*/
|
|
|
|
virtual bool Rewrite(const char* pszSkip=nullptr) = 0;
|
|
|
|
|
|
|
|
/** Back up the entire database to a file.
|
|
|
|
*/
|
|
|
|
virtual bool Backup(const std::string& strDest) const = 0;
|
|
|
|
|
|
|
|
/** Make sure all changes are flushed to database file.
|
|
|
|
*/
|
|
|
|
virtual void Flush() = 0;
|
|
|
|
/** Flush to the database file and close the database.
|
|
|
|
* Also close the environment if no other databases are open in it.
|
|
|
|
*/
|
|
|
|
virtual void Close() = 0;
|
|
|
|
/* flush the wallet passively (TRY_LOCK)
|
|
|
|
ideal to be called periodically */
|
|
|
|
virtual bool PeriodicFlush() = 0;
|
|
|
|
|
|
|
|
virtual void IncrementUpdateCounter() = 0;
|
|
|
|
|
|
|
|
virtual void ReloadDbEnv() = 0;
|
|
|
|
|
2022-10-17 04:33:10 +02:00
|
|
|
/** Return path to main database file for logs and error messages. */
|
|
|
|
virtual std::string Filename() = 0;
|
|
|
|
|
2020-10-20 01:33:13 +02:00
|
|
|
virtual std::string Format() = 0;
|
|
|
|
|
2022-03-04 09:26:11 +01:00
|
|
|
std::atomic<unsigned int> nUpdateCounter;
|
|
|
|
unsigned int nLastSeen;
|
|
|
|
unsigned int nLastFlushed;
|
|
|
|
int64_t nLastWalletUpdate;
|
|
|
|
|
|
|
|
/** Make a DatabaseBatch connected to this database */
|
2022-11-03 16:52:28 +01:00
|
|
|
virtual std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) = 0;
|
2024-02-16 16:48:35 +01:00
|
|
|
|
|
|
|
virtual bool SupportsAutoBackup() { return false; }
|
2022-03-04 09:26:11 +01:00
|
|
|
};
|
|
|
|
|
2022-03-04 08:49:30 +01:00
|
|
|
/** RAII class that provides access to a DummyDatabase. Never fails. */
|
|
|
|
class DummyBatch : public DatabaseBatch
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
bool ReadKey(CDataStream&& key, CDataStream& value) override { return true; }
|
|
|
|
bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) override { return true; }
|
|
|
|
bool EraseKey(CDataStream&& key) override { return true; }
|
|
|
|
bool HasKey(CDataStream&& key) override { return true; }
|
|
|
|
|
|
|
|
public:
|
|
|
|
void Flush() override {}
|
|
|
|
void Close() override {}
|
|
|
|
|
|
|
|
bool StartCursor() override { return true; }
|
|
|
|
bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override { return true; }
|
|
|
|
void CloseCursor() override {}
|
|
|
|
bool TxnBegin() override { return true; }
|
|
|
|
bool TxnCommit() override { return true; }
|
|
|
|
bool TxnAbort() override { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/** A dummy WalletDatabase that does nothing and never fails. Only used by unit tests.
|
|
|
|
**/
|
|
|
|
class DummyDatabase : public WalletDatabase
|
|
|
|
{
|
|
|
|
public:
|
2022-11-03 16:52:28 +01:00
|
|
|
void Open() override {};
|
2022-03-04 08:49:30 +01:00
|
|
|
void AddRef() override {}
|
|
|
|
void RemoveRef() override {}
|
|
|
|
bool Rewrite(const char* pszSkip=nullptr) override { return true; }
|
|
|
|
bool Backup(const std::string& strDest) const override { return true; }
|
|
|
|
void Close() override {}
|
|
|
|
void Flush() override {}
|
|
|
|
bool PeriodicFlush() override { return true; }
|
|
|
|
void IncrementUpdateCounter() override { ++nUpdateCounter; }
|
|
|
|
void ReloadDbEnv() override {}
|
2022-10-17 04:33:10 +02:00
|
|
|
std::string Filename() override { return "dummy"; }
|
2020-10-20 01:33:13 +02:00
|
|
|
std::string Format() override { return "dummy"; }
|
2022-11-03 16:52:28 +01:00
|
|
|
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<DummyBatch>(); }
|
2022-03-04 08:49:30 +01:00
|
|
|
};
|
|
|
|
|
2022-10-17 04:33:10 +02:00
|
|
|
enum class DatabaseFormat {
|
|
|
|
BERKELEY,
|
2023-02-04 19:26:20 +01:00
|
|
|
SQLITE,
|
2022-10-17 04:33:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DatabaseOptions {
|
|
|
|
bool require_existing = false;
|
|
|
|
bool require_create = false;
|
2023-02-04 19:26:20 +01:00
|
|
|
std::optional<DatabaseFormat> require_format;
|
2022-10-17 04:33:10 +02:00
|
|
|
uint64_t create_flags = 0;
|
|
|
|
SecureString create_passphrase;
|
|
|
|
bool verify = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class DatabaseStatus {
|
|
|
|
SUCCESS,
|
|
|
|
FAILED_BAD_PATH,
|
|
|
|
FAILED_BAD_FORMAT,
|
|
|
|
FAILED_ALREADY_LOADED,
|
|
|
|
FAILED_ALREADY_EXISTS,
|
|
|
|
FAILED_NOT_FOUND,
|
|
|
|
FAILED_CREATE,
|
2020-09-13 01:50:18 +02:00
|
|
|
FAILED_LOAD,
|
2022-10-17 04:33:10 +02:00
|
|
|
FAILED_VERIFY,
|
|
|
|
FAILED_ENCRYPT,
|
2024-07-20 20:06:17 +02:00
|
|
|
FAILED_INVALID_BACKUP_FILE,
|
2022-10-17 04:33:10 +02:00
|
|
|
};
|
|
|
|
|
2023-02-04 19:29:32 +01:00
|
|
|
/** Recursively list database paths in directory. */
|
|
|
|
std::vector<fs::path> ListDatabases(const fs::path& path);
|
|
|
|
|
2022-10-17 04:33:10 +02:00
|
|
|
std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
|
|
|
|
|
2023-02-04 19:29:32 +01:00
|
|
|
fs::path BDBDataFile(const fs::path& path);
|
|
|
|
fs::path SQLiteDataFile(const fs::path& path);
|
|
|
|
bool IsBDBFile(const fs::path& path);
|
|
|
|
bool IsSQLiteFile(const fs::path& path);
|
|
|
|
|
2015-03-21 18:15:31 +01:00
|
|
|
#endif // BITCOIN_WALLET_DB_H
|