From 9ad26f1664821804aeed9b82d055093cc078c143 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Mon, 29 Jul 2024 12:29:45 +0700 Subject: [PATCH] fix: follow-up bitcoin#20773 - coinjoin loader can be nullptr too --- src/wallet/load.cpp | 2 +- src/wallet/test/wallet_tests.cpp | 13 +++++++------ src/wallet/wallet.cpp | 12 +++++++----- src/wallet/wallet.h | 2 +- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/wallet/load.cpp b/src/wallet/load.cpp index 66647d6804..d09fd1f555 100644 --- a/src/wallet/load.cpp +++ b/src/wallet/load.cpp @@ -111,7 +111,7 @@ bool LoadWallets(interfaces::Chain& chain, interfaces::CoinJoin::Loader& coinjoi continue; } chain.initMessage(_("Loading wallet...").translated); - std::shared_ptr pwallet = database ? CWallet::Create(&chain, coinjoin_loader, name, std::move(database), options.create_flags, error_string, warnings) : nullptr; + std::shared_ptr pwallet = database ? CWallet::Create(&chain, &coinjoin_loader, name, std::move(database), options.create_flags, error_string, warnings) : nullptr; if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n"))); if (!pwallet) { chain.initError(error_string); diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 8595b56b57..9e6089e617 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -51,7 +51,7 @@ namespace { constexpr CAmount fallbackFee = 1000; } // anonymous namespace -static std::shared_ptr TestLoadWallet(interfaces::Chain* chain, interfaces::CoinJoin::Loader& coinjoin_loader) +static std::shared_ptr TestLoadWallet(interfaces::Chain* chain, interfaces::CoinJoin::Loader* coinjoin_loader) { DatabaseOptions options; DatabaseStatus status; @@ -1232,7 +1232,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup) { gArgs.ForceSetArg("-unsafesqlitesync", "1"); // Create new wallet with known key and unload it. - auto wallet = TestLoadWallet(m_node.chain.get(), *m_node.coinjoin_loader); + auto wallet = TestLoadWallet(m_node.chain.get(), m_node.coinjoin_loader.get()); CKey key; key.MakeNewKey(true); AddKey(*wallet, key); @@ -1272,7 +1272,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup) // Reload wallet and make sure new transactions are detected despite events // being blocked - wallet = TestLoadWallet(m_node.chain.get(), *m_node.coinjoin_loader); + wallet = TestLoadWallet(m_node.chain.get(), m_node.coinjoin_loader.get()); BOOST_CHECK(rescan_completed); BOOST_CHECK_EQUAL(addtx_count, 2); { @@ -1311,7 +1311,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup) ENTER_CRITICAL_SECTION(wallet->wallet()->cs_wallet); ENTER_CRITICAL_SECTION(cs_wallets); }); - wallet = TestLoadWallet(m_node.chain.get(), *m_node.coinjoin_loader); + wallet = TestLoadWallet(m_node.chain.get(), m_node.coinjoin_loader.get()); BOOST_CHECK_EQUAL(addtx_count, 4); { LOCK(wallet->cs_wallet); @@ -1392,7 +1392,8 @@ BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup) BOOST_FIXTURE_TEST_CASE(CreateWalletWithoutChain, BasicTestingSetup) { - auto wallet = TestLoadWallet(nullptr, *m_node.coinjoin_loader); + // TODO: FIX FIX FIX - coinjoin_loader is null heere! + auto wallet = TestLoadWallet(nullptr, nullptr); BOOST_CHECK(wallet); UnloadWallet(std::move(wallet)); } @@ -1401,7 +1402,7 @@ BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup) { gArgs.ForceSetArg("-unsafesqlitesync", "1"); auto chain = interfaces::MakeChain(m_node); - auto wallet = TestLoadWallet(m_node.chain.get(), *m_node.coinjoin_loader); + auto wallet = TestLoadWallet(m_node.chain.get(), m_node.coinjoin_loader.get()); CKey key; key.MakeNewKey(true); AddKey(*wallet, key); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index fb3f5e8ba9..bc6bc8f4b1 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -239,7 +239,7 @@ std::shared_ptr LoadWalletInternal(interfaces::Chain& chain, interfaces } chain.initMessage(_("Loading wallet...").translated); - std::shared_ptr wallet = CWallet::Create(&chain, coinjoin_loader, name, std::move(database), options.create_flags, error, warnings); + std::shared_ptr wallet = CWallet::Create(&chain, &coinjoin_loader, name, std::move(database), options.create_flags, error, warnings); if (!wallet) { error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error; status = DatabaseStatus::FAILED_LOAD; @@ -305,7 +305,7 @@ std::shared_ptr CreateWallet(interfaces::Chain& chain, interfaces::Coin // Make the wallet chain.initMessage(_("Loading wallet...").translated); - std::shared_ptr wallet = CWallet::Create(&chain, coinjoin_loader, name, std::move(database), wallet_creation_flags, error, warnings); + std::shared_ptr wallet = CWallet::Create(&chain, &coinjoin_loader, name, std::move(database), wallet_creation_flags, error, warnings); if (!wallet) { error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error; status = DatabaseStatus::FAILED_CREATE; @@ -4689,14 +4689,14 @@ std::unique_ptr MakeWalletDatabase(const std::string& name, cons return MakeDatabase(wallet_path, options, status, error_string); } -std::shared_ptr CWallet::Create(interfaces::Chain* chain, interfaces::CoinJoin::Loader& coinjoin_loader, const std::string& name, std::unique_ptr database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector& warnings) +std::shared_ptr CWallet::Create(interfaces::Chain* chain, interfaces::CoinJoin::Loader* coinjoin_loader, const std::string& name, std::unique_ptr database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector& warnings) { const std::string& walletFile = database->Filename(); int64_t nStart = GetTimeMillis(); // TODO: Can't use std::make_shared because we need a custom deleter but // should be possible to use std::allocate_shared. - std::shared_ptr walletInstance(new CWallet(chain, &coinjoin_loader, name, std::move(database)), ReleaseWallet); + std::shared_ptr walletInstance(new CWallet(chain, coinjoin_loader, name, std::move(database)), ReleaseWallet); // TODO: refactor this condition: validation of error looks like workaround if (!walletInstance->AutoBackupWallet(fs::PathFromString(walletFile), error, warnings) && !error.original.empty()) { return nullptr; @@ -4950,7 +4950,9 @@ std::shared_ptr CWallet::Create(interfaces::Chain* chain, interfaces::C return nullptr; } - coinjoin_loader.AddWallet(*walletInstance); + if (coinjoin_loader) { + coinjoin_loader->AddWallet(*walletInstance); + } { LOCK(cs_wallets); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 5e514d56cd..4487aa17b9 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1345,7 +1345,7 @@ public: bool ResendTransaction(const uint256& hashTx); /* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */ - static std::shared_ptr Create(interfaces::Chain* chain, interfaces::CoinJoin::Loader& coinjoin_loader, const std::string& name, std::unique_ptr database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector& warnings); + static std::shared_ptr Create(interfaces::Chain* chain, interfaces::CoinJoin::Loader* coinjoin_loader, const std::string& name, std::unique_ptr database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector& warnings); /** * Wallet post-init setup