diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index ac07b34174168..ab67505243905 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2359,7 +2359,7 @@ void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve } } -DBErrors CWallet::LoadWallet() +DBErrors CWallet::LoadWallet(const do_init_used_flag do_init_used_flag_val) { LOCK(cs_wallet); @@ -2383,7 +2383,7 @@ DBErrors CWallet::LoadWallet() return nLoadWalletRet; } - InitialiseAddressBookUsed(); + if (do_init_used_flag_val == do_init_used_flag::Init) InitialiseAddressBookUsed(); return DBErrors::LOAD_OK; } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index b67c225ea2dcd..7e86a6292d39f 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -791,7 +791,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati CAmount GetDebit(const CTransaction& tx, const isminefilter& filter) const; void chainStateFlushed(ChainstateRole role, const CBlockLocator& loc) override; - DBErrors LoadWallet(); + enum class do_init_used_flag { Init, Skip }; + DBErrors LoadWallet(const do_init_used_flag do_init_used_flag_val = do_init_used_flag::Init); DBErrors ZapSelectTx(std::vector& vHashIn, std::vector& vHashOut) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::optional& purpose); diff --git a/src/wallet/wallettool.cpp b/src/wallet/wallettool.cpp index 2f3f8ef77de2f..39a0113042ac3 100644 --- a/src/wallet/wallettool.cpp +++ b/src/wallet/wallettool.cpp @@ -47,7 +47,7 @@ static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flag wallet_instance->TopUpKeyPool(); } -static std::shared_ptr MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options) +static std::shared_ptr MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options, CWallet::do_init_used_flag do_init_used_flag_val = CWallet::do_init_used_flag::Init) { DatabaseStatus status; bilingual_str error; @@ -61,7 +61,7 @@ static std::shared_ptr MakeWallet(const std::string& name, const fs::pa std::shared_ptr wallet_instance{new CWallet(/*chain=*/nullptr, name, std::move(database)), WalletToolReleaseWallet}; DBErrors load_wallet_ret; try { - load_wallet_ret = wallet_instance->LoadWallet(); + load_wallet_ret = wallet_instance->LoadWallet(do_init_used_flag_val); } catch (const std::runtime_error&) { tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name); return nullptr; @@ -168,7 +168,8 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command) DatabaseOptions options; ReadDatabaseArgs(args, options); options.require_existing = true; - const std::shared_ptr wallet_instance = MakeWallet(name, path, options); + // NOTE: We need to skip initialisation of the m_used flag, or else the address book count might be wrong + const std::shared_ptr wallet_instance = MakeWallet(name, path, options, CWallet::do_init_used_flag::Skip); if (!wallet_instance) return false; WalletShowInfo(wallet_instance.get()); wallet_instance->Close();