fix: follow-up bitcoin#20773 - coinjoin loader can be nullptr too

This commit is contained in:
Konstantin Akimov 2024-07-29 12:29:45 +07:00 committed by pasta
parent 4125485401
commit 9ad26f1664
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
4 changed files with 16 additions and 13 deletions

View File

@ -111,7 +111,7 @@ bool LoadWallets(interfaces::Chain& chain, interfaces::CoinJoin::Loader& coinjoi
continue; continue;
} }
chain.initMessage(_("Loading wallet...").translated); chain.initMessage(_("Loading wallet...").translated);
std::shared_ptr<CWallet> pwallet = database ? CWallet::Create(&chain, coinjoin_loader, name, std::move(database), options.create_flags, error_string, warnings) : nullptr; std::shared_ptr<CWallet> 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 (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
if (!pwallet) { if (!pwallet) {
chain.initError(error_string); chain.initError(error_string);

View File

@ -51,7 +51,7 @@ namespace {
constexpr CAmount fallbackFee = 1000; constexpr CAmount fallbackFee = 1000;
} // anonymous namespace } // anonymous namespace
static std::shared_ptr<CWallet> TestLoadWallet(interfaces::Chain* chain, interfaces::CoinJoin::Loader& coinjoin_loader) static std::shared_ptr<CWallet> TestLoadWallet(interfaces::Chain* chain, interfaces::CoinJoin::Loader* coinjoin_loader)
{ {
DatabaseOptions options; DatabaseOptions options;
DatabaseStatus status; DatabaseStatus status;
@ -1232,7 +1232,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
{ {
gArgs.ForceSetArg("-unsafesqlitesync", "1"); gArgs.ForceSetArg("-unsafesqlitesync", "1");
// Create new wallet with known key and unload it. // 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; CKey key;
key.MakeNewKey(true); key.MakeNewKey(true);
AddKey(*wallet, key); AddKey(*wallet, key);
@ -1272,7 +1272,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
// Reload wallet and make sure new transactions are detected despite events // Reload wallet and make sure new transactions are detected despite events
// being blocked // 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(rescan_completed);
BOOST_CHECK_EQUAL(addtx_count, 2); 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(wallet->wallet()->cs_wallet);
ENTER_CRITICAL_SECTION(cs_wallets); 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); BOOST_CHECK_EQUAL(addtx_count, 4);
{ {
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);
@ -1392,7 +1392,8 @@ BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup)
BOOST_FIXTURE_TEST_CASE(CreateWalletWithoutChain, 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); BOOST_CHECK(wallet);
UnloadWallet(std::move(wallet)); UnloadWallet(std::move(wallet));
} }
@ -1401,7 +1402,7 @@ BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
{ {
gArgs.ForceSetArg("-unsafesqlitesync", "1"); gArgs.ForceSetArg("-unsafesqlitesync", "1");
auto chain = interfaces::MakeChain(m_node); 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; CKey key;
key.MakeNewKey(true); key.MakeNewKey(true);
AddKey(*wallet, key); AddKey(*wallet, key);

View File

@ -239,7 +239,7 @@ std::shared_ptr<CWallet> LoadWalletInternal(interfaces::Chain& chain, interfaces
} }
chain.initMessage(_("Loading wallet...").translated); chain.initMessage(_("Loading wallet...").translated);
std::shared_ptr<CWallet> wallet = CWallet::Create(&chain, coinjoin_loader, name, std::move(database), options.create_flags, error, warnings); std::shared_ptr<CWallet> wallet = CWallet::Create(&chain, &coinjoin_loader, name, std::move(database), options.create_flags, error, warnings);
if (!wallet) { if (!wallet) {
error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error; error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error;
status = DatabaseStatus::FAILED_LOAD; status = DatabaseStatus::FAILED_LOAD;
@ -305,7 +305,7 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, interfaces::Coin
// Make the wallet // Make the wallet
chain.initMessage(_("Loading wallet...").translated); chain.initMessage(_("Loading wallet...").translated);
std::shared_ptr<CWallet> wallet = CWallet::Create(&chain, coinjoin_loader, name, std::move(database), wallet_creation_flags, error, warnings); std::shared_ptr<CWallet> wallet = CWallet::Create(&chain, &coinjoin_loader, name, std::move(database), wallet_creation_flags, error, warnings);
if (!wallet) { if (!wallet) {
error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error; error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
status = DatabaseStatus::FAILED_CREATE; status = DatabaseStatus::FAILED_CREATE;
@ -4689,14 +4689,14 @@ std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, cons
return MakeDatabase(wallet_path, options, status, error_string); return MakeDatabase(wallet_path, options, status, error_string);
} }
std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain* chain, interfaces::CoinJoin::Loader& coinjoin_loader, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings) std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain* chain, interfaces::CoinJoin::Loader* coinjoin_loader, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings)
{ {
const std::string& walletFile = database->Filename(); const std::string& walletFile = database->Filename();
int64_t nStart = GetTimeMillis(); int64_t nStart = GetTimeMillis();
// 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, &coinjoin_loader, name, std::move(database)), ReleaseWallet); std::shared_ptr<CWallet> walletInstance(new CWallet(chain, coinjoin_loader, name, std::move(database)), ReleaseWallet);
// TODO: refactor this condition: validation of error looks like workaround // TODO: refactor this condition: validation of error looks like workaround
if (!walletInstance->AutoBackupWallet(fs::PathFromString(walletFile), error, warnings) && !error.original.empty()) { if (!walletInstance->AutoBackupWallet(fs::PathFromString(walletFile), error, warnings) && !error.original.empty()) {
return nullptr; return nullptr;
@ -4950,7 +4950,9 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain* chain, interfaces::C
return nullptr; return nullptr;
} }
coinjoin_loader.AddWallet(*walletInstance); if (coinjoin_loader) {
coinjoin_loader->AddWallet(*walletInstance);
}
{ {
LOCK(cs_wallets); LOCK(cs_wallets);

View File

@ -1345,7 +1345,7 @@ public:
bool ResendTransaction(const uint256& hashTx); bool ResendTransaction(const uint256& hashTx);
/* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */ /* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
static std::shared_ptr<CWallet> Create(interfaces::Chain* chain, interfaces::CoinJoin::Loader& coinjoin_loader, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings); static std::shared_ptr<CWallet> Create(interfaces::Chain* chain, interfaces::CoinJoin::Loader* coinjoin_loader, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings);
/** /**
* Wallet post-init setup * Wallet post-init setup