refactor: move first run detection to client code

This commit is contained in:
Ivan Metlushko 2020-12-18 17:45:11 +01:00
parent 2fa3f30050
commit e2a47ce085
10 changed files with 16 additions and 24 deletions

View File

@ -22,8 +22,7 @@ static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const b
CWallet wallet{test_setup->m_node.chain.get(), "", CreateMockWalletDatabase()}; CWallet wallet{test_setup->m_node.chain.get(), "", CreateMockWalletDatabase()};
{ {
wallet.SetupLegacyScriptPubKeyMan(); wallet.SetupLegacyScriptPubKeyMan();
bool first_run; if (wallet.LoadWallet() != DBErrors::LOAD_OK) assert(false);
if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false);
} }
auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}}); auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});

View File

@ -63,8 +63,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
node.setContext(&test.m_node); node.setContext(&test.m_node);
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase()); std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
wallet->SetupLegacyScriptPubKeyMan(); wallet->SetupLegacyScriptPubKeyMan();
bool firstRun; wallet->LoadWallet();
wallet->LoadWallet(firstRun);
auto build_address = [&wallet]() { auto build_address = [&wallet]() {
CKey key; CKey key;

View File

@ -140,8 +140,7 @@ void TestGUI(interfaces::Node& node)
} }
node.setContext(&test.m_node); node.setContext(&test.m_node);
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase()); std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
bool firstRun; wallet->LoadWallet();
wallet->LoadWallet(firstRun);
{ {
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan(); auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore); LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);

View File

@ -194,8 +194,7 @@ bool CreateFromDump(const std::string& name, const fs::path& wallet_path, biling
std::shared_ptr<CWallet> wallet(new CWallet(nullptr /* chain */, name, std::move(database)), WalletToolReleaseWallet); std::shared_ptr<CWallet> wallet(new CWallet(nullptr /* chain */, name, std::move(database)), WalletToolReleaseWallet);
{ {
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);
bool first_run = true; DBErrors load_wallet_ret = wallet->LoadWallet();
DBErrors load_wallet_ret = wallet->LoadWallet(first_run);
if (load_wallet_ret != DBErrors::LOAD_OK) { if (load_wallet_ret != DBErrors::LOAD_OK) {
error = strprintf(_("Error creating %s"), name); error = strprintf(_("Error creating %s"), name);
return false; return false;

View File

@ -297,8 +297,7 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
empty_wallet(); empty_wallet();
{ {
std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockWalletDatabase()); std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockWalletDatabase());
bool firstRun; wallet->LoadWallet();
wallet->LoadWallet(firstRun);
wallet->SetupLegacyScriptPubKeyMan(); wallet->SetupLegacyScriptPubKeyMan();
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);
add_coin(*wallet, 5 * CENT, 6 * 24, false, 0, true); add_coin(*wallet, 5 * CENT, 6 * 24, false, 0, true);

View File

@ -8,8 +8,7 @@ WalletTestingSetup::WalletTestingSetup(const std::string& chainName)
: TestingSetup(chainName), : TestingSetup(chainName),
m_wallet(m_node.chain.get(), "", CreateMockWalletDatabase()) m_wallet(m_node.chain.get(), "", CreateMockWalletDatabase())
{ {
bool fFirstRun; m_wallet.LoadWallet();
m_wallet.LoadWallet(fFirstRun);
m_chain_notifications_handler = m_node.chain->handleNotifications({ &m_wallet, [](CWallet*) {} }); m_chain_notifications_handler = m_node.chain->handleNotifications({ &m_wallet, [](CWallet*) {} });
m_wallet_client->registerRpcs(); m_wallet_client->registerRpcs();
} }

View File

@ -483,8 +483,7 @@ public:
LOCK2(wallet->cs_wallet, ::cs_main); LOCK2(wallet->cs_wallet, ::cs_main);
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
} }
bool firstRun; wallet->LoadWallet();
wallet->LoadWallet(firstRun);
AddKey(*wallet, coinbaseKey); AddKey(*wallet, coinbaseKey);
WalletRescanReserver reserver(*wallet); WalletRescanReserver reserver(*wallet);
reserver.reserve(); reserver.reserve();

View File

@ -3247,11 +3247,10 @@ void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
} }
} }
DBErrors CWallet::LoadWallet(bool& fFirstRunRet) DBErrors CWallet::LoadWallet()
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
fFirstRunRet = false;
DBErrors nLoadWalletRet = WalletBatch(GetDatabase()).LoadWallet(this); DBErrors nLoadWalletRet = WalletBatch(GetDatabase()).LoadWallet(this);
if (nLoadWalletRet == DBErrors::NEED_REWRITE) if (nLoadWalletRet == DBErrors::NEED_REWRITE)
{ {
@ -3263,9 +3262,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
} }
} }
// This wallet is in its first run if there are no ScriptPubKeyMans and it isn't blank or no privkeys if (m_spk_managers.empty()) {
fFirstRunRet = m_spk_managers.empty() && !IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET);
if (fFirstRunRet) {
assert(m_external_spk_managers.empty()); assert(m_external_spk_managers.empty());
assert(m_internal_spk_managers.empty()); assert(m_internal_spk_managers.empty());
} }
@ -3893,11 +3890,10 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
chain.initMessage(_("Loading wallet…").translated); chain.initMessage(_("Loading wallet…").translated);
int64_t nStart = GetTimeMillis(); int64_t nStart = GetTimeMillis();
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, std::move(database)), ReleaseWallet); std::shared_ptr<CWallet> walletInstance(new CWallet(&chain, name, std::move(database)), ReleaseWallet);
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun); DBErrors nLoadWalletRet = walletInstance->LoadWallet();
if (nLoadWalletRet != DBErrors::LOAD_OK) { if (nLoadWalletRet != DBErrors::LOAD_OK) {
if (nLoadWalletRet == DBErrors::CORRUPT) { if (nLoadWalletRet == DBErrors::CORRUPT) {
error = strprintf(_("Error loading %s: Wallet corrupted"), walletFile); error = strprintf(_("Error loading %s: Wallet corrupted"), walletFile);
@ -3924,6 +3920,10 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
} }
} }
// This wallet is in its first run if there are no ScriptPubKeyMans and it isn't blank or no privkeys
const bool fFirstRun = walletInstance->m_spk_managers.empty() &&
!walletInstance->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) &&
!walletInstance->IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET);
if (fFirstRun) if (fFirstRun)
{ {
// ensure this wallet.dat can only be opened by clients supporting HD with chain split and expects no default key // ensure this wallet.dat can only be opened by clients supporting HD with chain split and expects no default key

View File

@ -1126,7 +1126,7 @@ public:
CAmount GetChange(const CTransaction& tx) const; CAmount GetChange(const CTransaction& tx) const;
void chainStateFlushed(const CBlockLocator& loc) override; void chainStateFlushed(const CBlockLocator& loc) override;
DBErrors LoadWallet(bool& fFirstRunRet); DBErrors LoadWallet();
DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& purpose); bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& purpose);

View File

@ -54,8 +54,7 @@ static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::pa
std::shared_ptr<CWallet> wallet_instance{new CWallet(nullptr /* chain */, name, std::move(database)), WalletToolReleaseWallet}; std::shared_ptr<CWallet> wallet_instance{new CWallet(nullptr /* chain */, name, std::move(database)), WalletToolReleaseWallet};
DBErrors load_wallet_ret; DBErrors load_wallet_ret;
try { try {
bool first_run; load_wallet_ret = wallet_instance->LoadWallet();
load_wallet_ret = wallet_instance->LoadWallet(first_run);
} catch (const std::runtime_error&) { } catch (const std::runtime_error&) {
tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name); tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name);
return nullptr; return nullptr;