From aeee419c6aae085cacd75343c1ce23486b2b8916 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Tue, 29 Mar 2022 22:08:26 +0200 Subject: [PATCH 1/2] wallet, refactor: Add wallet::NotifyWalletLoaded() function This change is a prerequisite for the following bugfix. --- src/wallet/wallet.cpp | 15 +++++++++------ src/wallet/wallet.h | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index be64b4cdbb..99113a1a71 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -167,6 +167,14 @@ std::unique_ptr HandleLoadWallet(WalletContext& context, Lo return interfaces::MakeHandler([&context, it] { LOCK(context.wallets_mutex); context.wallet_load_fns.erase(it); }); } +void NotifyWalletLoaded(WalletContext& context, const std::shared_ptr& wallet) +{ + LOCK(context.wallets_mutex); + for (auto& load_wallet : context.wallet_load_fns) { + load_wallet(interfaces::MakeWallet(context, wallet)); + } +} + static Mutex g_loading_wallet_mutex; static Mutex g_wallet_release_mutex; static std::condition_variable g_wallet_release_cv; @@ -2904,12 +2912,7 @@ std::shared_ptr CWallet::Create(WalletContext& context, const std::stri return nullptr; } - { - LOCK(context.wallets_mutex); - for (auto& load_wallet : context.wallet_load_fns) { - load_wallet(interfaces::MakeWallet(context, walletInstance)); - } - } + NotifyWalletLoaded(context, walletInstance); { LOCK(walletInstance->cs_wallet); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 0490d321ab..26b7f97b5f 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -67,6 +67,7 @@ std::shared_ptr LoadWallet(WalletContext& context, const std::string& n std::shared_ptr CreateWallet(WalletContext& context, const std::string& name, std::optional load_on_start, DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector& warnings); std::shared_ptr RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector& warnings); std::unique_ptr HandleLoadWallet(WalletContext& context, LoadWalletFn load_wallet); +void NotifyWalletLoaded(WalletContext& context, const std::shared_ptr& wallet); std::unique_ptr MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error); //! -paytxfee default From 0c12f0116ca802f55f5ab43e6c4842ac403b9889 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Tue, 29 Mar 2022 22:28:42 +0200 Subject: [PATCH 2/2] wallet: Postpone NotifyWalletLoaded() for encrypted wallets Too early NotifyWalletLoaded() call in CWallet::Create() results the notification goes before DescriptorScriptPubKeyMans were created and added to an encrypted wallet. Co-authored-by: Andrew Chow --- src/wallet/load.cpp | 2 ++ src/wallet/test/wallet_tests.cpp | 1 + src/wallet/wallet.cpp | 6 ++++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/wallet/load.cpp b/src/wallet/load.cpp index 98ce95dcd1..c06513588b 100644 --- a/src/wallet/load.cpp +++ b/src/wallet/load.cpp @@ -130,6 +130,8 @@ bool LoadWallets(WalletContext& context) chain.initError(error); return false; } + + NotifyWalletLoaded(context, pwallet); AddWallet(context, pwallet); } return true; diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 5518f4fdae..683f0eb327 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -54,6 +54,7 @@ static const std::shared_ptr TestLoadWallet(WalletContext& context) std::vector warnings; auto database = MakeWalletDatabase("", options, status, error); auto wallet = CWallet::Create(context, "", std::move(database), options.create_flags, error, warnings); + NotifyWalletLoaded(context, wallet); if (context.chain) { wallet->postInitProcess(); } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 99113a1a71..2a0653c719 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -240,6 +240,8 @@ std::shared_ptr LoadWalletInternal(WalletContext& context, const std::s status = DatabaseStatus::FAILED_LOAD; return nullptr; } + + NotifyWalletLoaded(context, wallet); AddWallet(context, wallet); wallet->postInitProcess(); @@ -356,6 +358,8 @@ std::shared_ptr CreateWallet(WalletContext& context, const std::string& wallet->Lock(); } } + + NotifyWalletLoaded(context, wallet); AddWallet(context, wallet); wallet->postInitProcess(); @@ -2912,8 +2916,6 @@ std::shared_ptr CWallet::Create(WalletContext& context, const std::stri return nullptr; } - NotifyWalletLoaded(context, walletInstance); - { LOCK(walletInstance->cs_wallet); walletInstance->SetBroadcastTransactions(args.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));