2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 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-03-19 23:46:56 +01:00
|
|
|
#include <clientversion.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <serialize.h>
|
|
|
|
#include <streams.h>
|
|
|
|
#include <sync.h>
|
2018-03-07 17:05:08 +01:00
|
|
|
#include <util.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <version.h>
|
2011-05-15 09:11:04 +02:00
|
|
|
|
2017-06-13 00:02:22 +02:00
|
|
|
#include <atomic>
|
2011-05-15 09:11:04 +02:00
|
|
|
#include <map>
|
2018-04-02 20:31:40 +02:00
|
|
|
#include <memory>
|
2011-05-15 09:11:04 +02:00
|
|
|
#include <string>
|
2018-10-24 17:22:25 +02:00
|
|
|
#include <unordered_map>
|
2011-05-15 09:11:04 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <db_cxx.h>
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2015-06-28 01:15:11 +02:00
|
|
|
static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100;
|
2015-06-27 21:21:41 +02:00
|
|
|
static const bool DEFAULT_WALLET_PRIVDB = true;
|
2015-06-28 01:15:11 +02:00
|
|
|
|
2018-10-24 17:22:25 +02:00
|
|
|
struct WalletDatabaseFileId {
|
|
|
|
u_int8_t value[DB_FILE_ID_LEN];
|
|
|
|
bool operator==(const WalletDatabaseFileId& rhs) const;
|
|
|
|
};
|
|
|
|
|
2020-04-15 00:13:51 +02:00
|
|
|
class BerkeleyEnvironment
|
2012-05-14 03:37:39 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
bool fDbEnvInit;
|
2012-05-22 21:51:13 +02:00
|
|
|
bool fMockDb;
|
2017-04-06 20:19:21 +02:00
|
|
|
// Don't change into fs::path, as that can result in
|
2015-06-15 07:46:51 +02:00
|
|
|
// shutdown problems/crashes caused by a static initialized internal pointer.
|
|
|
|
std::string strPath;
|
2012-05-14 03:37:39 +02:00
|
|
|
|
|
|
|
public:
|
2017-11-09 21:22:08 +01:00
|
|
|
std::unique_ptr<DbEnv> dbenv;
|
2012-05-18 08:49:50 +02:00
|
|
|
std::map<std::string, int> mapFileUseCount;
|
|
|
|
std::map<std::string, Db*> mapDb;
|
2018-10-24 17:22:25 +02:00
|
|
|
std::unordered_map<std::string, WalletDatabaseFileId> m_fileids;
|
2018-09-14 10:28:27 +02:00
|
|
|
std::condition_variable_any m_db_in_use;
|
2012-05-14 03:37:39 +02:00
|
|
|
|
2020-04-15 00:13:51 +02:00
|
|
|
BerkeleyEnvironment(const fs::path& env_directory);
|
|
|
|
~BerkeleyEnvironment();
|
2015-03-03 16:49:12 +01:00
|
|
|
void Reset();
|
|
|
|
|
2012-05-22 21:51:13 +02:00
|
|
|
void MakeMock();
|
2019-09-25 12:28:11 +02:00
|
|
|
bool IsMock() const { return fMockDb; }
|
2018-03-07 17:05:08 +01:00
|
|
|
bool IsInitialized() const { return fDbEnvInit; }
|
|
|
|
fs::path Directory() const { return strPath; }
|
2012-09-18 20:30:47 +02:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
2012-09-18 20:30:47 +02:00
|
|
|
* Verify that database file strFile is OK. If it is not,
|
|
|
|
* call the callback to try to recover.
|
|
|
|
* This must be called BEFORE strFile is opened.
|
|
|
|
* Returns true if strFile is OK.
|
|
|
|
*/
|
2020-06-09 05:44:04 +02:00
|
|
|
enum class VerifyResult { VERIFY_OK,
|
2014-09-19 19:21:46 +02:00
|
|
|
RECOVER_OK,
|
|
|
|
RECOVER_FAIL };
|
2018-03-07 17:05:08 +01:00
|
|
|
typedef bool (*recoverFunc_type)(const fs::path& file_path, std::string& out_backup_filename);
|
2019-07-11 09:50:52 +02:00
|
|
|
VerifyResult Verify(const std::string& strFile, recoverFunc_type recoverFunc, std::string& out_backup_filename);
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
2012-09-18 20:30:47 +02:00
|
|
|
* Salvage data from a file that Verify says is bad.
|
|
|
|
* fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
|
|
|
|
* Appends binary key/value pairs to vResult, returns true if successful.
|
|
|
|
* NOTE: reads the entire database into memory, so cannot be used
|
|
|
|
* for huge databases.
|
|
|
|
*/
|
|
|
|
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
|
2015-03-21 18:40:51 +01:00
|
|
|
bool Salvage(const std::string& strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
|
2012-09-18 20:30:47 +02:00
|
|
|
|
2018-03-07 17:05:08 +01:00
|
|
|
bool Open(bool retry);
|
2012-05-14 03:37:39 +02:00
|
|
|
void Close();
|
|
|
|
void Flush(bool fShutdown);
|
2014-09-16 15:16:29 +02:00
|
|
|
void CheckpointLSN(const std::string& strFile);
|
2012-05-14 18:33:34 +02:00
|
|
|
|
2012-05-18 08:49:50 +02:00
|
|
|
void CloseDb(const std::string& strFile);
|
2018-09-14 10:28:27 +02:00
|
|
|
void ReloadDbEnv();
|
2012-05-18 08:49:50 +02:00
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
|
2012-05-14 18:33:34 +02:00
|
|
|
{
|
2019-08-06 05:08:33 +02:00
|
|
|
DbTxn* ptxn = nullptr;
|
|
|
|
int ret = dbenv->txn_begin(nullptr, &ptxn, flags);
|
2012-05-14 18:33:34 +02:00
|
|
|
if (!ptxn || ret != 0)
|
2019-08-06 05:08:33 +02:00
|
|
|
return nullptr;
|
2012-05-14 18:33:34 +02:00
|
|
|
return ptxn;
|
|
|
|
}
|
2012-05-14 03:37:39 +02:00
|
|
|
};
|
|
|
|
|
2020-04-15 00:13:51 +02:00
|
|
|
/** Get BerkeleyEnvironment and database filename given a wallet path. */
|
|
|
|
BerkeleyEnvironment* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename);
|
2012-05-14 03:37:39 +02:00
|
|
|
|
2017-04-24 14:43:38 +02:00
|
|
|
/** An instance of this class represents one database.
|
|
|
|
* For BerkeleyDB this is just a (env, strFile) tuple.
|
|
|
|
**/
|
2020-04-15 00:13:51 +02:00
|
|
|
class BerkeleyDatabase
|
2017-04-24 14:43:38 +02:00
|
|
|
{
|
2020-04-15 00:13:51 +02:00
|
|
|
friend class BerkeleyBatch;
|
2017-04-24 14:43:38 +02:00
|
|
|
public:
|
|
|
|
/** Create dummy DB handle */
|
2020-04-15 00:13:51 +02:00
|
|
|
BerkeleyDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr)
|
2017-04-24 14:43:38 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create DB handle to real database */
|
2020-04-15 00:13:51 +02:00
|
|
|
BerkeleyDatabase(const fs::path& wallet_path, bool mock = false) :
|
2018-03-07 17:05:08 +01:00
|
|
|
nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0)
|
2017-04-24 14:43:38 +02:00
|
|
|
{
|
2018-03-07 17:05:08 +01:00
|
|
|
env = GetWalletEnv(wallet_path, strFile);
|
|
|
|
if (mock) {
|
|
|
|
env->Close();
|
|
|
|
env->Reset();
|
|
|
|
env->MakeMock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return object for accessing database at specified path. */
|
2020-04-15 00:13:51 +02:00
|
|
|
static std::unique_ptr<BerkeleyDatabase> Create(const fs::path& path)
|
2018-03-07 17:05:08 +01:00
|
|
|
{
|
2020-04-15 00:13:51 +02:00
|
|
|
return MakeUnique<BerkeleyDatabase>(path);
|
2018-03-07 17:05:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return object for accessing dummy database with no read/write capabilities. */
|
2020-04-15 00:13:51 +02:00
|
|
|
static std::unique_ptr<BerkeleyDatabase> CreateDummy()
|
2018-03-07 17:05:08 +01:00
|
|
|
{
|
2020-04-15 00:13:51 +02:00
|
|
|
return MakeUnique<BerkeleyDatabase>();
|
2018-03-07 17:05:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return object for accessing temporary in-memory database. */
|
2020-04-15 00:13:51 +02:00
|
|
|
static std::unique_ptr<BerkeleyDatabase> CreateMock()
|
2018-03-07 17:05:08 +01:00
|
|
|
{
|
2020-04-15 00:13:51 +02:00
|
|
|
return MakeUnique<BerkeleyDatabase>("", true /* mock */);
|
2017-04-24 14:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
|
|
|
|
*/
|
|
|
|
bool Rewrite(const char* pszSkip=nullptr);
|
|
|
|
|
|
|
|
/** Back up the entire database to a file.
|
|
|
|
*/
|
|
|
|
bool Backup(const std::string& strDest);
|
|
|
|
|
|
|
|
/** Make sure all changes are flushed to disk.
|
|
|
|
*/
|
|
|
|
void Flush(bool shutdown);
|
|
|
|
|
2019-07-11 09:50:52 +02:00
|
|
|
void IncrementUpdateCounter();
|
|
|
|
|
2018-09-14 10:28:27 +02:00
|
|
|
void ReloadDbEnv();
|
|
|
|
|
2019-07-11 09:50:52 +02:00
|
|
|
std::atomic<unsigned int> nUpdateCounter;
|
|
|
|
unsigned int nLastSeen;
|
|
|
|
unsigned int nLastFlushed;
|
|
|
|
int64_t nLastWalletUpdate;
|
|
|
|
|
2017-04-24 14:43:38 +02:00
|
|
|
private:
|
|
|
|
/** BerkeleyDB specific */
|
2020-04-15 00:13:51 +02:00
|
|
|
BerkeleyEnvironment *env;
|
2017-04-24 14:43:38 +02:00
|
|
|
std::string strFile;
|
|
|
|
|
|
|
|
/** Return whether this database handle is a dummy for testing.
|
|
|
|
* Only to be used at a low level, application should ideally not care
|
|
|
|
* about this.
|
|
|
|
*/
|
|
|
|
bool IsDummy() { return env == nullptr; }
|
|
|
|
};
|
|
|
|
|
2012-05-14 03:37:39 +02:00
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** RAII class that provides access to a Berkeley database */
|
2020-04-15 00:13:51 +02:00
|
|
|
class BerkeleyBatch
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
Db* pdb;
|
2011-05-15 09:11:04 +02:00
|
|
|
std::string strFile;
|
2014-09-19 19:21:46 +02:00
|
|
|
DbTxn* activeTxn;
|
2010-08-29 18:58:15 +02:00
|
|
|
bool fReadOnly;
|
2014-08-31 05:55:27 +02:00
|
|
|
bool fFlushOnClose;
|
2020-04-15 00:13:51 +02:00
|
|
|
BerkeleyEnvironment *env;
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2017-04-24 14:43:38 +02:00
|
|
|
public:
|
2020-04-15 00:13:51 +02:00
|
|
|
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
|
|
|
|
~BerkeleyBatch() { Close(); }
|
2014-09-16 15:16:29 +02:00
|
|
|
|
2020-04-15 00:13:51 +02:00
|
|
|
BerkeleyBatch(const BerkeleyBatch&) = delete;
|
|
|
|
BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;
|
2017-09-22 07:33:06 +02:00
|
|
|
|
2012-07-06 16:33:34 +02:00
|
|
|
void Flush();
|
2010-08-29 18:58:15 +02:00
|
|
|
void Close();
|
2018-03-07 17:05:08 +01:00
|
|
|
static bool Recover(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
|
2017-03-06 12:49:01 +01:00
|
|
|
|
|
|
|
/* flush the wallet passively (TRY_LOCK)
|
|
|
|
ideal to be called periodically */
|
2020-04-15 00:13:51 +02:00
|
|
|
static bool PeriodicFlush(BerkeleyDatabase& database);
|
2017-03-06 12:49:01 +01:00
|
|
|
/* verifies the database environment */
|
2018-03-07 17:05:08 +01:00
|
|
|
static bool VerifyEnvironment(const fs::path& file_path, std::string& errorStr);
|
2017-03-06 12:49:01 +01:00
|
|
|
/* verifies the database file */
|
2020-04-15 00:13:51 +02:00
|
|
|
static bool VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc);
|
2014-09-16 15:16:29 +02:00
|
|
|
|
2017-04-24 14:43:38 +02:00
|
|
|
public:
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename K, typename T>
|
2010-08-29 18:58:15 +02:00
|
|
|
bool Read(const K& key, T& value)
|
|
|
|
{
|
|
|
|
if (!pdb)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Key
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
2017-01-09 17:39:08 +01:00
|
|
|
Dbt datKey(ssKey.data(), ssKey.size());
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Read
|
|
|
|
Dbt datValue;
|
|
|
|
datValue.set_flags(DB_DBT_MALLOC);
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
|
2017-05-11 19:28:04 +02:00
|
|
|
memory_cleanse(datKey.get_data(), datKey.get_size());
|
|
|
|
bool success = false;
|
2019-08-06 05:08:33 +02:00
|
|
|
if (datValue.get_data() != nullptr) {
|
2017-05-11 19:28:04 +02:00
|
|
|
// Unserialize value
|
|
|
|
try {
|
|
|
|
CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
|
|
|
|
ssValue >> value;
|
|
|
|
success = true;
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
// In this case success remains 'false'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear and free memory
|
|
|
|
memory_cleanse(datValue.get_data(), datValue.get_size());
|
|
|
|
free(datValue.get_data());
|
2012-05-22 21:12:52 +02:00
|
|
|
}
|
2017-05-11 19:28:04 +02:00
|
|
|
return ret == 0 && success;
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename K, typename T>
|
|
|
|
bool Write(const K& key, const T& value, bool fOverwrite = true)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
if (!pdb)
|
2017-04-24 14:43:38 +02:00
|
|
|
return true;
|
2010-08-29 18:58:15 +02:00
|
|
|
if (fReadOnly)
|
2011-06-24 19:56:23 +02:00
|
|
|
assert(!"Write called on database in read-only mode");
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Key
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
2017-01-09 17:39:08 +01:00
|
|
|
Dbt datKey(ssKey.data(), ssKey.size());
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Value
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssValue.reserve(10000);
|
|
|
|
ssValue << value;
|
2017-01-09 17:39:08 +01:00
|
|
|
Dbt datValue(ssValue.data(), ssValue.size());
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Write
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Clear memory in case it was a private key
|
2017-05-11 19:28:04 +02:00
|
|
|
memory_cleanse(datKey.get_data(), datKey.get_size());
|
|
|
|
memory_cleanse(datValue.get_data(), datValue.get_size());
|
2010-08-29 18:58:15 +02:00
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename K>
|
2010-08-29 18:58:15 +02:00
|
|
|
bool Erase(const K& key)
|
|
|
|
{
|
|
|
|
if (!pdb)
|
|
|
|
return false;
|
|
|
|
if (fReadOnly)
|
2011-06-24 19:56:23 +02:00
|
|
|
assert(!"Erase called on database in read-only mode");
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Key
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
2017-01-09 17:39:08 +01:00
|
|
|
Dbt datKey(ssKey.data(), ssKey.size());
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Erase
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = pdb->del(activeTxn, &datKey, 0);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Clear memory
|
2017-05-11 19:28:04 +02:00
|
|
|
memory_cleanse(datKey.get_data(), datKey.get_size());
|
2010-08-29 18:58:15 +02:00
|
|
|
return (ret == 0 || ret == DB_NOTFOUND);
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename K>
|
2010-08-29 18:58:15 +02:00
|
|
|
bool Exists(const K& key)
|
|
|
|
{
|
|
|
|
if (!pdb)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Key
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
2017-01-09 17:39:08 +01:00
|
|
|
Dbt datKey(ssKey.data(), ssKey.size());
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Exists
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = pdb->exists(activeTxn, &datKey, 0);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Clear memory
|
2017-05-11 19:28:04 +02:00
|
|
|
memory_cleanse(datKey.get_data(), datKey.get_size());
|
2010-08-29 18:58:15 +02:00
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dbc* GetCursor()
|
|
|
|
{
|
|
|
|
if (!pdb)
|
2019-08-06 05:08:33 +02:00
|
|
|
return nullptr;
|
|
|
|
Dbc* pcursor = nullptr;
|
|
|
|
int ret = pdb->cursor(nullptr, &pcursor, 0);
|
2010-08-29 18:58:15 +02:00
|
|
|
if (ret != 0)
|
2019-08-06 05:08:33 +02:00
|
|
|
return nullptr;
|
2010-08-29 18:58:15 +02:00
|
|
|
return pcursor;
|
|
|
|
}
|
|
|
|
|
2016-08-29 16:15:42 +02:00
|
|
|
int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, bool setRange = false)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
// Read at cursor
|
|
|
|
Dbt datKey;
|
2016-08-29 16:15:42 +02:00
|
|
|
unsigned int fFlags = DB_NEXT;
|
|
|
|
if (setRange) {
|
2017-01-09 17:39:08 +01:00
|
|
|
datKey.set_data(ssKey.data());
|
2010-08-29 18:58:15 +02:00
|
|
|
datKey.set_size(ssKey.size());
|
2016-08-29 16:15:42 +02:00
|
|
|
fFlags = DB_SET_RANGE;
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
|
|
|
Dbt datValue;
|
|
|
|
datKey.set_flags(DB_DBT_MALLOC);
|
|
|
|
datValue.set_flags(DB_DBT_MALLOC);
|
|
|
|
int ret = pcursor->get(&datKey, &datValue, fFlags);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2019-08-06 05:08:33 +02:00
|
|
|
else if (datKey.get_data() == nullptr || datValue.get_data() == nullptr)
|
2010-08-29 18:58:15 +02:00
|
|
|
return 99999;
|
|
|
|
|
|
|
|
// Convert to streams
|
|
|
|
ssKey.SetType(SER_DISK);
|
|
|
|
ssKey.clear();
|
|
|
|
ssKey.write((char*)datKey.get_data(), datKey.get_size());
|
|
|
|
ssValue.SetType(SER_DISK);
|
|
|
|
ssValue.clear();
|
|
|
|
ssValue.write((char*)datValue.get_data(), datValue.get_size());
|
|
|
|
|
|
|
|
// Clear and free memory
|
2017-05-11 19:28:04 +02:00
|
|
|
memory_cleanse(datKey.get_data(), datKey.get_size());
|
|
|
|
memory_cleanse(datValue.get_data(), datValue.get_size());
|
2010-08-29 18:58:15 +02:00
|
|
|
free(datKey.get_data());
|
|
|
|
free(datValue.get_data());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool TxnBegin()
|
|
|
|
{
|
2012-05-14 18:39:29 +02:00
|
|
|
if (!pdb || activeTxn)
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
2018-03-07 17:05:08 +01:00
|
|
|
DbTxn* ptxn = env->TxnBegin();
|
2012-05-14 18:33:34 +02:00
|
|
|
if (!ptxn)
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
2012-05-14 18:39:29 +02:00
|
|
|
activeTxn = ptxn;
|
2010-08-29 18:58:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TxnCommit()
|
|
|
|
{
|
2012-05-14 18:39:29 +02:00
|
|
|
if (!pdb || !activeTxn)
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = activeTxn->commit(0);
|
2019-08-06 05:08:33 +02:00
|
|
|
activeTxn = nullptr;
|
2010-08-29 18:58:15 +02:00
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TxnAbort()
|
|
|
|
{
|
2012-05-14 18:39:29 +02:00
|
|
|
if (!pdb || !activeTxn)
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = activeTxn->abort();
|
2019-08-06 05:08:33 +02:00
|
|
|
activeTxn = nullptr;
|
2010-08-29 18:58:15 +02:00
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadVersion(int& nVersion)
|
|
|
|
{
|
|
|
|
nVersion = 0;
|
2011-05-15 09:11:04 +02:00
|
|
|
return Read(std::string("version"), nVersion);
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteVersion(int nVersion)
|
|
|
|
{
|
2011-05-15 09:11:04 +02:00
|
|
|
return Write(std::string("version"), nVersion);
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
2011-11-10 21:29:23 +01:00
|
|
|
|
2020-04-15 00:13:51 +02:00
|
|
|
bool static Rewrite(BerkeleyDatabase& database, const char* pszSkip = nullptr);
|
2010-08-29 18:58:15 +02:00
|
|
|
};
|
|
|
|
|
2015-03-21 18:15:31 +01:00
|
|
|
#endif // BITCOIN_WALLET_DB_H
|