Skip to content

Commit

Permalink
scripted-diff: Rename wallet database classes (begin bitcoin#11851)
Browse files Browse the repository at this point in the history
-BEGIN VERIFY SCRIPT-

sed -i 's/\<CWalletDBWrapper\>/BerkeleyDatabase/g' src/wallet/db.h src/wallet/db.cpp
sed -i '/statuses/i/** Backend-agnostic database type. */\nusing WalletDatabase = BerkeleyDatabase\;\n' src/wallet/walletdb.h
ren() { git grep -l "\<$1\>" 'src/*.cpp' 'src/*.h' ':(exclude)*dbwrapper*' test | xargs sed -i "s:\<$1\>:$2:g"; }
ren CDBEnv           BerkeleyEnvironment
ren CDB              BerkeleyBatch
ren CWalletDBWrapper WalletDatabase
ren CWalletDB        WalletBatch
ren dbw              database
ren m_dbw            m_database
ren walletdb         batch
ren pwalletdb        batch
ren pwalletdbIn      batch_in
ren wallet/batch.h   wallet/walletdb.h
ren pwalletdbEncryption encrypted_batch

-END VERIFY SCRIPT-

Signed-off-by: Pasta <pasta@dashboost.org>
  • Loading branch information
PastaPastaPasta committed Apr 26, 2020
1 parent 9017b26 commit 7770a7d
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 329 deletions.
2 changes: 1 addition & 1 deletion src/bench/coin_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<CO
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
static void CoinSelection(benchmark::State& state)
{
const CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
const CWallet wallet("dummy", WalletDatabase::CreateDummy());
std::vector<COutput> vCoins;
LOCK(wallet.cs_wallet);

Expand Down
2 changes: 1 addition & 1 deletion src/qt/test/wallettests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void TestGUI()
for (int i = 0; i < 5; ++i) {
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
}
CWallet wallet("mock", CWalletDBWrapper::CreateMock());
CWallet wallet("mock", WalletDatabase::CreateMock());
bool firstRun;
wallet.LoadWallet(firstRun);
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(util_ParseHex)
result = ParseHex("12 34 56 78");
BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);

// Leading space must be supported (used in CDBEnv::Salvage)
// Leading space must be supported (used in BerkeleyEnvironment::Salvage)
result = ParseHex(" 89 34 56 78");
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);

Expand Down
144 changes: 72 additions & 72 deletions src/wallet/db.cpp

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions src/wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100;
static const bool DEFAULT_WALLET_PRIVDB = true;

class CDBEnv
class BerkeleyEnvironment
{
private:
bool fDbEnvInit;
Expand All @@ -39,8 +39,8 @@ class CDBEnv
std::map<std::string, int> mapFileUseCount;
std::map<std::string, Db*> mapDb;

CDBEnv(const fs::path& env_directory);
~CDBEnv();
BerkeleyEnvironment(const fs::path& env_directory);
~BerkeleyEnvironment();
void Reset();

void MakeMock();
Expand Down Expand Up @@ -86,23 +86,23 @@ class CDBEnv
}
};

/** Get CDBEnv and database filename given a wallet path. */
CDBEnv* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename);
/** Get BerkeleyEnvironment and database filename given a wallet path. */
BerkeleyEnvironment* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename);

/** An instance of this class represents one database.
* For BerkeleyDB this is just a (env, strFile) tuple.
**/
class CWalletDBWrapper
class BerkeleyDatabase
{
friend class CDB;
friend class BerkeleyBatch;
public:
/** Create dummy DB handle */
CWalletDBWrapper() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr)
BerkeleyDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr)
{
}

/** Create DB handle to real database */
CWalletDBWrapper(const fs::path& wallet_path, bool mock = false) :
BerkeleyDatabase(const fs::path& wallet_path, bool mock = false) :
nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0)
{
env = GetWalletEnv(wallet_path, strFile);
Expand All @@ -114,21 +114,21 @@ class CWalletDBWrapper
}

/** Return object for accessing database at specified path. */
static std::unique_ptr<CWalletDBWrapper> Create(const fs::path& path)
static std::unique_ptr<BerkeleyDatabase> Create(const fs::path& path)
{
return MakeUnique<CWalletDBWrapper>(path);
return MakeUnique<BerkeleyDatabase>(path);
}

/** Return object for accessing dummy database with no read/write capabilities. */
static std::unique_ptr<CWalletDBWrapper> CreateDummy()
static std::unique_ptr<BerkeleyDatabase> CreateDummy()
{
return MakeUnique<CWalletDBWrapper>();
return MakeUnique<BerkeleyDatabase>();
}

/** Return object for accessing temporary in-memory database. */
static std::unique_ptr<CWalletDBWrapper> CreateMock()
static std::unique_ptr<BerkeleyDatabase> CreateMock()
{
return MakeUnique<CWalletDBWrapper>("", true /* mock */);
return MakeUnique<BerkeleyDatabase>("", true /* mock */);
}

/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
Expand All @@ -152,7 +152,7 @@ class CWalletDBWrapper

private:
/** BerkeleyDB specific */
CDBEnv *env;
BerkeleyEnvironment *env;
std::string strFile;

/** Return whether this database handle is a dummy for testing.
Expand All @@ -164,34 +164,34 @@ class CWalletDBWrapper


/** RAII class that provides access to a Berkeley database */
class CDB
class BerkeleyBatch
{
protected:
Db* pdb;
std::string strFile;
DbTxn* activeTxn;
bool fReadOnly;
bool fFlushOnClose;
CDBEnv *env;
BerkeleyEnvironment *env;

public:
explicit CDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
~CDB() { Close(); }
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
~BerkeleyBatch() { Close(); }

CDB(const CDB&) = delete;
CDB& operator=(const CDB&) = delete;
BerkeleyBatch(const BerkeleyBatch&) = delete;
BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;

void Flush();
void Close();
static bool Recover(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);

/* flush the wallet passively (TRY_LOCK)
ideal to be called periodically */
static bool PeriodicFlush(CWalletDBWrapper& dbw);
static bool PeriodicFlush(BerkeleyDatabase& database);
/* verifies the database environment */
static bool VerifyEnvironment(const fs::path& file_path, std::string& errorStr);
/* verifies the database file */
static bool VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc);
static bool VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc);

public:
template <typename K, typename T>
Expand Down Expand Up @@ -387,7 +387,7 @@ class CDB
return Write(std::string("version"), nVersion);
}

bool static Rewrite(CWalletDBWrapper& dbw, const char* pszSkip = nullptr);
bool static Rewrite(BerkeleyDatabase& database, const char* pszSkip = nullptr);
};

#endif // BITCOIN_WALLET_DB_H
2 changes: 1 addition & 1 deletion src/wallet/test/wallet_test_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <wallet/wallet.h>

WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
TestingSetup(chainName), m_wallet("mock", CWalletDBWrapper::CreateMock())
TestingSetup(chainName), m_wallet("mock", WalletDatabase::CreateMock())
{
bool fFirstRun;
m_wallet.LoadWallet(fFirstRun);
Expand Down
16 changes: 8 additions & 8 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ typedef std::set<CInputCoin> CoinSet;

BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)

static const CWallet testWallet("dummy", CWalletDBWrapper::CreateDummy());
static const CWallet testWallet("dummy", WalletDatabase::CreateDummy());
static std::vector<COutput> vCoins;

static void add_coin(const CAmount& nValue, int nAge = 6*24, bool fIsFromMe = false, int nInput=0)
Expand Down Expand Up @@ -383,7 +383,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// Verify ScanForWalletTransactions picks up transactions in both the old
// and new block files.
{
CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
CWallet wallet("dummy", WalletDatabase::CreateDummy());
AddKey(wallet, coinbaseKey);
WalletRescanReserver reserver(&wallet);
reserver.reserve();
Expand All @@ -398,7 +398,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// Verify ScanForWalletTransactions only picks transactions in the new block
// file.
{
CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
CWallet wallet("dummy", WalletDatabase::CreateDummy());
AddKey(wallet, coinbaseKey);
WalletRescanReserver reserver(&wallet);
reserver.reserve();
Expand All @@ -410,7 +410,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// before the missing block, and success for a key whose creation time is
// after.
{
CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
CWallet wallet("dummy", WalletDatabase::CreateDummy());
AddWallet(&wallet);
UniValue keys;
keys.setArray();
Expand Down Expand Up @@ -469,7 +469,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)

// Import key into wallet and call dumpwallet to create backup file.
{
CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
CWallet wallet("dummy", WalletDatabase::CreateDummy());
LOCK(wallet.cs_wallet);
wallet.mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
Expand All @@ -484,7 +484,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
// Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME
// were scanned, and no prior blocks were scanned.
{
CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
CWallet wallet("dummy", WalletDatabase::CreateDummy());

JSONRPCRequest request;
request.params.setArray();
Expand Down Expand Up @@ -514,7 +514,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
// debit functions.
BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
{
CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
CWallet wallet("dummy", WalletDatabase::CreateDummy());
CWalletTx wtx(&wallet, MakeTransactionRef(coinbaseTxns.back()));
LOCK2(cs_main, wallet.cs_wallet);
wtx.hashBlock = chainActive.Tip()->GetBlockHash();
Expand Down Expand Up @@ -604,7 +604,7 @@ class ListCoinsTestingSetup : public TestChain100Setup
ListCoinsTestingSetup()
{
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
wallet = MakeUnique<CWallet>("mock", CWalletDBWrapper::CreateMock());
wallet = MakeUnique<CWallet>("mock", WalletDatabase::CreateMock());
bool firstRun;
wallet->LoadWallet(firstRun);
AddKey(*wallet, coinbaseKey);
Expand Down
Loading

0 comments on commit 7770a7d

Please sign in to comment.