mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-05-17 23:56:39 +00:00
refactor: Pass wallet database into CWallet::Create
No changes in behavior
This commit is contained in:
parent
3c815cfe54
commit
8b5e7297c0
@ -359,7 +359,6 @@ void BerkeleyDatabase::Open(const char* pszMode)
|
|||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
throw std::runtime_error(strprintf("BerkeleyDatabase: Error %d, can't open database %s", ret, strFile));
|
throw std::runtime_error(strprintf("BerkeleyDatabase: Error %d, can't open database %s", ret, strFile));
|
||||||
}
|
}
|
||||||
m_file_path = (env->Directory() / strFile).string();
|
|
||||||
|
|
||||||
// Call CheckUniqueFileid on the containing BDB environment to
|
// Call CheckUniqueFileid on the containing BDB environment to
|
||||||
// avoid BDB data consistency bugs that happen when different data
|
// avoid BDB data consistency bugs that happen when different data
|
||||||
|
@ -144,6 +144,9 @@ public:
|
|||||||
/** Verifies the environment and database file */
|
/** Verifies the environment and database file */
|
||||||
bool Verify(bilingual_str& error);
|
bool Verify(bilingual_str& error);
|
||||||
|
|
||||||
|
/** Return path to main database filename */
|
||||||
|
std::string Filename() override { return (env->Directory() / strFile).string(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pointer to shared database environment.
|
* Pointer to shared database environment.
|
||||||
*
|
*
|
||||||
|
@ -23,11 +23,3 @@ void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::
|
|||||||
database_filename = "wallet.dat";
|
database_filename = "wallet.dat";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path WalletDataFilePath(const fs::path& wallet_path)
|
|
||||||
{
|
|
||||||
fs::path env_directory;
|
|
||||||
std::string database_filename;
|
|
||||||
SplitWalletPath(wallet_path, env_directory, database_filename);
|
|
||||||
return env_directory / database_filename;
|
|
||||||
}
|
|
||||||
|
@ -18,8 +18,6 @@
|
|||||||
|
|
||||||
struct bilingual_str;
|
struct bilingual_str;
|
||||||
|
|
||||||
/** Given a wallet directory path or legacy file path, return path to main data file in the wallet database. */
|
|
||||||
fs::path WalletDataFilePath(const fs::path& wallet_path);
|
|
||||||
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
|
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
|
||||||
|
|
||||||
/** RAII class that provides access to a WalletDatabase */
|
/** RAII class that provides access to a WalletDatabase */
|
||||||
@ -142,13 +140,14 @@ public:
|
|||||||
|
|
||||||
virtual void ReloadDbEnv() = 0;
|
virtual void ReloadDbEnv() = 0;
|
||||||
|
|
||||||
|
/** Return path to main database file for logs and error messages. */
|
||||||
|
virtual std::string Filename() = 0;
|
||||||
|
|
||||||
std::atomic<unsigned int> nUpdateCounter;
|
std::atomic<unsigned int> nUpdateCounter;
|
||||||
unsigned int nLastSeen;
|
unsigned int nLastSeen;
|
||||||
unsigned int nLastFlushed;
|
unsigned int nLastFlushed;
|
||||||
int64_t nLastWalletUpdate;
|
int64_t nLastWalletUpdate;
|
||||||
|
|
||||||
std::string m_file_path;
|
|
||||||
|
|
||||||
/** Make a DatabaseBatch connected to this database */
|
/** Make a DatabaseBatch connected to this database */
|
||||||
virtual std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0;
|
virtual std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0;
|
||||||
};
|
};
|
||||||
@ -189,6 +188,7 @@ public:
|
|||||||
bool PeriodicFlush() override { return true; }
|
bool PeriodicFlush() override { return true; }
|
||||||
void IncrementUpdateCounter() override { ++nUpdateCounter; }
|
void IncrementUpdateCounter() override { ++nUpdateCounter; }
|
||||||
void ReloadDbEnv() override {}
|
void ReloadDbEnv() override {}
|
||||||
|
std::string Filename() override { return "dummy"; }
|
||||||
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override { return MakeUnique<DummyBatch>(); }
|
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override { return MakeUnique<DummyBatch>(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -68,10 +68,14 @@ bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wal
|
|||||||
bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
|
bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
for (const std::string& walletFile : wallet_files) {
|
for (const std::string& name : wallet_files) {
|
||||||
|
DatabaseOptions options;
|
||||||
|
DatabaseStatus status;
|
||||||
|
options.verify = false; // No need to verify, assuming verified earlier in VerifyWallets()
|
||||||
bilingual_str error;
|
bilingual_str error;
|
||||||
std::vector<bilingual_str> warnings;
|
std::vector<bilingual_str> warnings;
|
||||||
std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(chain, walletFile, error, warnings);
|
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
|
||||||
|
std::shared_ptr<CWallet> pwallet = database ? CWallet::Create(chain, name, std::move(database), options.create_flags, error, warnings) : nullptr;
|
||||||
if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
|
if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
|
||||||
if (!pwallet) {
|
if (!pwallet) {
|
||||||
chain.initError(error);
|
chain.initError(error);
|
||||||
|
@ -37,9 +37,12 @@ BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
|
|||||||
|
|
||||||
static std::shared_ptr<CWallet> TestLoadWallet(interfaces::Chain& chain)
|
static std::shared_ptr<CWallet> TestLoadWallet(interfaces::Chain& chain)
|
||||||
{
|
{
|
||||||
|
DatabaseOptions options;
|
||||||
|
DatabaseStatus status;
|
||||||
bilingual_str error;
|
bilingual_str error;
|
||||||
std::vector<bilingual_str> warnings;
|
std::vector<bilingual_str> warnings;
|
||||||
auto wallet = CWallet::CreateWalletFromFile(chain, "", error, warnings);
|
auto database = MakeWalletDatabase("", options, status, error);
|
||||||
|
auto wallet = CWallet::Create(chain, "", std::move(database), options.create_flags, error, warnings);
|
||||||
wallet->postInitProcess();
|
wallet->postInitProcess();
|
||||||
return wallet;
|
return wallet;
|
||||||
}
|
}
|
||||||
|
@ -203,12 +203,13 @@ namespace {
|
|||||||
std::shared_ptr<CWallet> LoadWalletInternal(interfaces::Chain& chain, const std::string& name, Optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings)
|
std::shared_ptr<CWallet> LoadWalletInternal(interfaces::Chain& chain, const std::string& name, Optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (!MakeWalletDatabase(name, options, status, error)) {
|
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
|
||||||
|
if (!database) {
|
||||||
error = Untranslated("Wallet file verification failed.") + Untranslated(" ") + error;
|
error = Untranslated("Wallet file verification failed.") + Untranslated(" ") + error;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, name, error, warnings);
|
std::shared_ptr<CWallet> wallet = CWallet::Create(chain, name, std::move(database), options.create_flags, error, warnings);
|
||||||
if (!wallet) {
|
if (!wallet) {
|
||||||
error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error;
|
error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -260,7 +261,8 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wallet::Verify will check if we're trying to create a wallet with a duplicate name.
|
// Wallet::Verify will check if we're trying to create a wallet with a duplicate name.
|
||||||
if (!MakeWalletDatabase(name, options, status, error)) {
|
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
|
||||||
|
if (!database) {
|
||||||
error = Untranslated("Wallet file verification failed.") + Untranslated(" ") + error;
|
error = Untranslated("Wallet file verification failed.") + Untranslated(" ") + error;
|
||||||
status = DatabaseStatus::FAILED_VERIFY;
|
status = DatabaseStatus::FAILED_VERIFY;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -274,7 +276,7 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Make the wallet
|
// Make the wallet
|
||||||
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, name, error, warnings, wallet_creation_flags);
|
std::shared_ptr<CWallet> wallet = CWallet::Create(chain, name, std::move(database), wallet_creation_flags, error, warnings);
|
||||||
if (!wallet) {
|
if (!wallet) {
|
||||||
error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
|
error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
|
||||||
status = DatabaseStatus::FAILED_CREATE;
|
status = DatabaseStatus::FAILED_CREATE;
|
||||||
@ -3803,10 +3805,9 @@ std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, cons
|
|||||||
return MakeDatabase(wallet_path, options, status, error_string);
|
return MakeDatabase(wallet_path, options, status, error_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, uint64_t wallet_creation_flags)
|
std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings)
|
||||||
{
|
{
|
||||||
fs::path path = fs::absolute(name, GetWalletDir());
|
const std::string& walletFile = database->Filename();
|
||||||
const std::string walletFile = WalletDataFilePath(path).string();
|
|
||||||
|
|
||||||
chain.initMessage(_("Loading wallet...").translated);
|
chain.initMessage(_("Loading wallet...").translated);
|
||||||
|
|
||||||
@ -3814,7 +3815,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
|
|||||||
bool fFirstRun = true;
|
bool fFirstRun = true;
|
||||||
// TODO: Can't use std::make_shared because we need a custom deleter but
|
// TODO: Can't use std::make_shared because we need a custom deleter but
|
||||||
// should be possible to use std::allocate_shared.
|
// should be possible to use std::allocate_shared.
|
||||||
std::shared_ptr<CWallet> walletInstance(new CWallet(&chain, name, CreateWalletDatabase(path)), ReleaseWallet);
|
std::shared_ptr<CWallet> walletInstance(new CWallet(&chain, name, std::move(database)), ReleaseWallet);
|
||||||
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
|
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
|
||||||
if (nLoadWalletRet != DBErrors::LOAD_OK) {
|
if (nLoadWalletRet != DBErrors::LOAD_OK) {
|
||||||
if (nLoadWalletRet == DBErrors::CORRUPT) {
|
if (nLoadWalletRet == DBErrors::CORRUPT) {
|
||||||
|
@ -1146,7 +1146,7 @@ public:
|
|||||||
bool MarkReplaced(const uint256& originalHash, const uint256& newHash);
|
bool MarkReplaced(const uint256& originalHash, const uint256& newHash);
|
||||||
|
|
||||||
/* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
|
/* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
|
||||||
static std::shared_ptr<CWallet> CreateWalletFromFile(interfaces::Chain& chain, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, uint64_t wallet_creation_flags = 0);
|
static std::shared_ptr<CWallet> Create(interfaces::Chain& chain, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wallet post-init setup
|
* Wallet post-init setup
|
||||||
|
Loading…
x
Reference in New Issue
Block a user