Skip to content

Commit

Permalink
fix: follow-up bitcoin#20773 - coinjoin loader can be nullptr too
Browse files Browse the repository at this point in the history
  • Loading branch information
knst committed Aug 5, 2024
1 parent 9c3b6e2 commit bd5bd72
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/wallet/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ bool LoadWallets(interfaces::Chain& chain, interfaces::CoinJoin::Loader& coinjoi
continue;
}
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 (!pwallet) {
chain.initError(error_string);
Expand Down
13 changes: 7 additions & 6 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace {
constexpr CAmount fallbackFee = 1000;
} // 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;
DatabaseStatus status;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
Expand All @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ std::shared_ptr<CWallet> LoadWalletInternal(interfaces::Chain& chain, interfaces
}

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) {
error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error;
status = DatabaseStatus::FAILED_LOAD;
Expand Down Expand Up @@ -305,7 +305,7 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, interfaces::Coin

// Make the wallet
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) {
error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
status = DatabaseStatus::FAILED_CREATE;
Expand Down Expand Up @@ -4689,14 +4689,14 @@ std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, cons
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();

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<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
if (!walletInstance->AutoBackupWallet(walletFile, error, warnings) && !error.original.empty()) {
return nullptr;
Expand Down Expand Up @@ -4950,7 +4950,9 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain* chain, interfaces::C
return nullptr;
}

coinjoin_loader.AddWallet(*walletInstance);
if (coinjoin_loader) {
coinjoin_loader->AddWallet(*walletInstance);
}

{
LOCK(cs_wallets);
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
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<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
Expand Down

0 comments on commit bd5bd72

Please sign in to comment.