Skip to content

Commit e2a47ce

Browse files
committed
refactor: move first run detection to client code
1 parent 2fa3f30 commit e2a47ce

File tree

10 files changed

+16
-24
lines changed

10 files changed

+16
-24
lines changed

src/bench/wallet_balance.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const b
2222
CWallet wallet{test_setup->m_node.chain.get(), "", CreateMockWalletDatabase()};
2323
{
2424
wallet.SetupLegacyScriptPubKeyMan();
25-
bool first_run;
26-
if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false);
25+
if (wallet.LoadWallet() != DBErrors::LOAD_OK) assert(false);
2726
}
2827
auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
2928

src/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
6363
node.setContext(&test.m_node);
6464
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
6565
wallet->SetupLegacyScriptPubKeyMan();
66-
bool firstRun;
67-
wallet->LoadWallet(firstRun);
66+
wallet->LoadWallet();
6867

6968
auto build_address = [&wallet]() {
7069
CKey key;

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ void TestGUI(interfaces::Node& node)
140140
}
141141
node.setContext(&test.m_node);
142142
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
143-
bool firstRun;
144-
wallet->LoadWallet(firstRun);
143+
wallet->LoadWallet();
145144
{
146145
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
147146
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);

src/wallet/dump.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ bool CreateFromDump(const std::string& name, const fs::path& wallet_path, biling
194194
std::shared_ptr<CWallet> wallet(new CWallet(nullptr /* chain */, name, std::move(database)), WalletToolReleaseWallet);
195195
{
196196
LOCK(wallet->cs_wallet);
197-
bool first_run = true;
198-
DBErrors load_wallet_ret = wallet->LoadWallet(first_run);
197+
DBErrors load_wallet_ret = wallet->LoadWallet();
199198
if (load_wallet_ret != DBErrors::LOAD_OK) {
200199
error = strprintf(_("Error creating %s"), name);
201200
return false;

src/wallet/test/coinselector_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
297297
empty_wallet();
298298
{
299299
std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockWalletDatabase());
300-
bool firstRun;
301-
wallet->LoadWallet(firstRun);
300+
wallet->LoadWallet();
302301
wallet->SetupLegacyScriptPubKeyMan();
303302
LOCK(wallet->cs_wallet);
304303
add_coin(*wallet, 5 * CENT, 6 * 24, false, 0, true);

src/wallet/test/wallet_test_fixture.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ WalletTestingSetup::WalletTestingSetup(const std::string& chainName)
88
: TestingSetup(chainName),
99
m_wallet(m_node.chain.get(), "", CreateMockWalletDatabase())
1010
{
11-
bool fFirstRun;
12-
m_wallet.LoadWallet(fFirstRun);
11+
m_wallet.LoadWallet();
1312
m_chain_notifications_handler = m_node.chain->handleNotifications({ &m_wallet, [](CWallet*) {} });
1413
m_wallet_client->registerRpcs();
1514
}

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,7 @@ class ListCoinsTestingSetup : public TestChain100Setup
483483
LOCK2(wallet->cs_wallet, ::cs_main);
484484
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
485485
}
486-
bool firstRun;
487-
wallet->LoadWallet(firstRun);
486+
wallet->LoadWallet();
488487
AddKey(*wallet, coinbaseKey);
489488
WalletRescanReserver reserver(*wallet);
490489
reserver.reserve();

src/wallet/wallet.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3247,11 +3247,10 @@ void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
32473247
}
32483248
}
32493249

3250-
DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
3250+
DBErrors CWallet::LoadWallet()
32513251
{
32523252
LOCK(cs_wallet);
32533253

3254-
fFirstRunRet = false;
32553254
DBErrors nLoadWalletRet = WalletBatch(GetDatabase()).LoadWallet(this);
32563255
if (nLoadWalletRet == DBErrors::NEED_REWRITE)
32573256
{
@@ -3263,9 +3262,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
32633262
}
32643263
}
32653264

3266-
// This wallet is in its first run if there are no ScriptPubKeyMans and it isn't blank or no privkeys
3267-
fFirstRunRet = m_spk_managers.empty() && !IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET);
3268-
if (fFirstRunRet) {
3265+
if (m_spk_managers.empty()) {
32693266
assert(m_external_spk_managers.empty());
32703267
assert(m_internal_spk_managers.empty());
32713268
}
@@ -3893,11 +3890,10 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
38933890
chain.initMessage(_("Loading wallet…").translated);
38943891

38953892
int64_t nStart = GetTimeMillis();
3896-
bool fFirstRun = true;
38973893
// TODO: Can't use std::make_shared because we need a custom deleter but
38983894
// should be possible to use std::allocate_shared.
38993895
std::shared_ptr<CWallet> walletInstance(new CWallet(&chain, name, std::move(database)), ReleaseWallet);
3900-
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
3896+
DBErrors nLoadWalletRet = walletInstance->LoadWallet();
39013897
if (nLoadWalletRet != DBErrors::LOAD_OK) {
39023898
if (nLoadWalletRet == DBErrors::CORRUPT) {
39033899
error = strprintf(_("Error loading %s: Wallet corrupted"), walletFile);
@@ -3924,6 +3920,10 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
39243920
}
39253921
}
39263922

3923+
// This wallet is in its first run if there are no ScriptPubKeyMans and it isn't blank or no privkeys
3924+
const bool fFirstRun = walletInstance->m_spk_managers.empty() &&
3925+
!walletInstance->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) &&
3926+
!walletInstance->IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET);
39273927
if (fFirstRun)
39283928
{
39293929
// ensure this wallet.dat can only be opened by clients supporting HD with chain split and expects no default key

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
11261126
CAmount GetChange(const CTransaction& tx) const;
11271127
void chainStateFlushed(const CBlockLocator& loc) override;
11281128

1129-
DBErrors LoadWallet(bool& fFirstRunRet);
1129+
DBErrors LoadWallet();
11301130
DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
11311131

11321132
bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& purpose);

src/wallet/wallettool.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::pa
5454
std::shared_ptr<CWallet> wallet_instance{new CWallet(nullptr /* chain */, name, std::move(database)), WalletToolReleaseWallet};
5555
DBErrors load_wallet_ret;
5656
try {
57-
bool first_run;
58-
load_wallet_ret = wallet_instance->LoadWallet(first_run);
57+
load_wallet_ret = wallet_instance->LoadWallet();
5958
} catch (const std::runtime_error&) {
6059
tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name);
6160
return nullptr;

0 commit comments

Comments
 (0)