diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index 46d2d38013cb..16aa05365946 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -108,6 +108,17 @@ struct BlockInfo { BlockInfo(const uint256& hash LIFETIMEBOUND) : hash(hash) {} }; +//! The action to be taken after updating a settings value. +//! WRITE indicates that the updated value must be written to disk, +//! while SKIP_WRITE indicates that the change will be kept in memory-only +//! without persisting it. +enum class SettingsAction { + WRITE, + SKIP_WRITE +}; + +using SettingsUpdate = std::function(common::SettingsValue&)>; + //! Interface giving clients (wallet processes, maybe other analysis tools in //! the future) ability to access to the chain state, receive notifications, //! estimate fees, and submit transactions. @@ -349,9 +360,16 @@ class Chain //! Return /settings.json setting value. virtual common::SettingsValue getRwSetting(const std::string& name) = 0; - //! Write a setting to /settings.json. Optionally just update the - //! setting in memory and do not write the file. - virtual bool updateRwSetting(const std::string& name, const common::SettingsValue& value, bool write=true) = 0; + //! Updates a setting in /settings.json. + //! Depending on the action returned by the update function, this will either + //! update the setting in memory or write the updated settings to disk. + virtual bool updateRwSetting(const std::string& name, const SettingsUpdate& update_function) = 0; + + //! Replace a setting in /settings.json with a new value. + virtual bool overwriteRwSetting(const std::string& name, common::SettingsValue& value, bool write = true) = 0; + + //! Delete a given setting in /settings.json. + virtual bool deleteRwSettings(const std::string& name, bool write = true) = 0; //! Synchronously send transactionAddedToMempool notifications about all //! current mempool transactions to the specified handler and return after diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 30c23041d463..480596803ae0 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -1726,14 +1726,32 @@ class ChainImpl : public Chain }); return result; } - bool updateRwSetting(const std::string& name, const common::SettingsValue& value, bool write) override + bool updateRwSetting(const std::string& name, + const interfaces::SettingsUpdate& update_settings_func) override { + std::optional action; args().LockSettings([&](common::Settings& settings) { - if (value.isNull()) { - settings.rw_settings.erase(name); - } else { - settings.rw_settings[name] = value; - } + auto* ptr_value = common::FindKey(settings.rw_settings, name); + // Create value if it doesn't exist + auto& value = ptr_value ? *ptr_value : settings.rw_settings[name]; + action = update_settings_func(value); + }); + if (!action) return false; + // Now dump value to disk if requested + return *action == interfaces::SettingsAction::SKIP_WRITE || args().WriteSettingsFile(); + } + bool overwriteRwSetting(const std::string& name, common::SettingsValue& value, bool write) override + { + if (value.isNull()) return deleteRwSettings(name, write); + return updateRwSetting(name, [&](common::SettingsValue& settings) { + settings = std::move(value); + return write ? interfaces::SettingsAction::WRITE : interfaces::SettingsAction::SKIP_WRITE; + }); + } + bool deleteRwSettings(const std::string& name, bool write) override + { + args().LockSettings([&](common::Settings& settings) { + settings.rw_settings.erase(name); }); return !write || args().WriteSettingsFile(); } diff --git a/src/wallet/load.cpp b/src/wallet/load.cpp index 74b407410bc7..0dafc1415ac2 100644 --- a/src/wallet/load.cpp +++ b/src/wallet/load.cpp @@ -71,7 +71,7 @@ bool VerifyWallets(WalletContext& context) // Pass write=false because no need to write file and probably // better not to. If unnamed wallet needs to be added next startup // and the setting is empty, this code will just run again. - chain.updateRwSetting("wallet", wallets, /* write= */ false); + chain.overwriteRwSetting("wallet", wallets, /*write=*/false); } } diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp index fa21a43c9660..53e653e5b4e7 100644 --- a/src/wallet/rpc/spend.cpp +++ b/src/wallet/rpc/spend.cpp @@ -1078,7 +1078,7 @@ RPCHelpMan sendall() { return RPCHelpMan{"sendall", "EXPERIMENTAL warning: this call may be changed in future releases.\n" - "\nSpend the value of all (or specific) confirmed UTXOs in the wallet to one or more recipients.\n" + "\nSpend the value of all (or specific) confirmed UTXOs and unconfirmed change in the wallet to one or more recipients.\n" "Unconfirmed inbound UTXOs and locked UTXOs will not be spent. Sendall will respect the avoid_reuse wallet flag.\n" "If your wallet contains many small inputs, either because it received tiny payments or as a result of accumulating change, consider using `send_max` to exclude inputs that are worth less than the fees needed to spend them.\n", { diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index 8d8510511f45..6043197f09a9 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -54,7 +54,7 @@ static int TraceSqlCallback(unsigned code, void* context, void* param1, void* pa // in the log file, only expand statements that query the database, not // statements that update the database. char* expanded{sqlite3_stmt_readonly(stmt) ? sqlite3_expanded_sql(stmt) : nullptr}; - LogPrintf("[%s] SQLite Statement: %s\n", db->Filename(), expanded ? expanded : sqlite3_sql(stmt)); + LogTrace(BCLog::WALLETDB, "[%s] SQLite Statement: %s\n", db->Filename(), expanded ? expanded : sqlite3_sql(stmt)); if (expanded) sqlite3_free(expanded); } return SQLITE_OK; diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 462ee572c58b..05aafa19fdfd 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -478,6 +478,40 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) } } +// This test verifies that wallet settings can be added and removed +// concurrently, ensuring no race conditions occur during either process. +BOOST_FIXTURE_TEST_CASE(write_wallet_settings_concurrently, TestingSetup) +{ + WalletContext context; + context.chain = m_node.chain.get(); + const auto NUM_WALLETS{5}; + + // Since we're counting the number of wallets, ensure we start without any. + BOOST_REQUIRE(context.chain->getRwSetting("wallet").isNull()); + + const auto& check_concurrent_wallet = [&](const auto& settings_function, int num_expected_wallets) { + std::vector threads; + threads.reserve(NUM_WALLETS); + for (auto i{0}; i < NUM_WALLETS; ++i) threads.emplace_back(settings_function, i); + for (auto& t : threads) t.join(); + + auto wallets = context.chain->getRwSetting("wallet"); + BOOST_CHECK_EQUAL(wallets.getValues().size(), num_expected_wallets); + }; + + // Add NUM_WALLETS wallets concurrently, ensure we end up with NUM_WALLETS stored. + check_concurrent_wallet([&context](int i) { + Assert(AddWalletSetting(*context.chain, strprintf("wallet_%d", i))); + }, + /*num_expected_wallets=*/NUM_WALLETS); + + // Remove NUM_WALLETS wallets concurrently, ensure we end up with 0 wallets. + check_concurrent_wallet([&context](int i) { + Assert(RemoveWalletSetting(*context.chain, strprintf("wallet_%d", i))); + }, + /*num_expected_wallets=*/0); +} + // Check that GetImmatureCredit() returns a newly calculated value instead of // the cached value after a MarkDirty() call. // diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 18719bce0f55..736a9071912a 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -115,25 +115,30 @@ static isminetype InputIsMine(const CWallet& wallet, const CTxIn& txin) EXCLUSIV bool AddWalletSetting(interfaces::Chain& chain, const std::string& wallet_name) { - common::SettingsValue setting_value = chain.getRwSetting("wallet"); - if (!setting_value.isArray()) setting_value.setArray(); - for (const common::SettingsValue& value : setting_value.getValues()) { - if (value.isStr() && value.get_str() == wallet_name) return true; - } - setting_value.push_back(wallet_name); - return chain.updateRwSetting("wallet", setting_value); + const auto update_function = [&wallet_name](common::SettingsValue& setting_value) { + if (!setting_value.isArray()) setting_value.setArray(); + for (const auto& value : setting_value.getValues()) { + if (value.isStr() && value.get_str() == wallet_name) return interfaces::SettingsAction::SKIP_WRITE; + } + setting_value.push_back(wallet_name); + return interfaces::SettingsAction::WRITE; + }; + return chain.updateRwSetting("wallet", update_function); } bool RemoveWalletSetting(interfaces::Chain& chain, const std::string& wallet_name) { - common::SettingsValue setting_value = chain.getRwSetting("wallet"); - if (!setting_value.isArray()) return true; - common::SettingsValue new_value(common::SettingsValue::VARR); - for (const common::SettingsValue& value : setting_value.getValues()) { - if (!value.isStr() || value.get_str() != wallet_name) new_value.push_back(value); - } - if (new_value.size() == setting_value.size()) return true; - return chain.updateRwSetting("wallet", new_value); + const auto update_function = [&wallet_name](common::SettingsValue& setting_value) { + if (!setting_value.isArray()) return interfaces::SettingsAction::SKIP_WRITE; + common::SettingsValue new_value(common::SettingsValue::VARR); + for (const auto& value : setting_value.getValues()) { + if (!value.isStr() || value.get_str() != wallet_name) new_value.push_back(value); + } + if (new_value.size() == setting_value.size()) return interfaces::SettingsAction::SKIP_WRITE; + setting_value = std::move(new_value); + return interfaces::SettingsAction::WRITE; + }; + return chain.updateRwSetting("wallet", update_function); } static void UpdateWalletSetting(interfaces::Chain& chain, diff --git a/test/functional/wallet_migration.py b/test/functional/wallet_migration.py index defd3ebe5dbf..4ab374d2529f 100755 --- a/test/functional/wallet_migration.py +++ b/test/functional/wallet_migration.py @@ -590,11 +590,20 @@ def test_default_wallet(self): self.log.info("Test migration of the wallet named as the empty string") wallet = self.create_legacy_wallet("") + # Set time to verify backup existence later + curr_time = self.mocktime + wallet.setmocktime(curr_time) + res = self.migrate_wallet(wallet) info = wallet.getwalletinfo() assert_equal(info["descriptors"], True) assert_equal(info["format"], "sqlite") + # Check backup existence and its non-empty wallet filename + backup_path = self.nodes[0].wallets_path / f'default_wallet_{curr_time}.legacy.bak' + assert backup_path.exists() + assert_equal(str(backup_path), res['backup_path']) + wallet.unloadwallet() self.clear_default_wallet(backup_file=Path(res["backup_path"])) diff --git a/test/functional/wallet_sendall.py b/test/functional/wallet_sendall.py index bf11e6620d11..6cc2688fb9e7 100755 --- a/test/functional/wallet_sendall.py +++ b/test/functional/wallet_sendall.py @@ -381,6 +381,27 @@ def sendall_with_maxconf(self): assert_equal(len(self.wallet.listunspent()), 1) assert_equal(self.wallet.listunspent()[0]['confirmations'], 6) + @cleanup + def sendall_spends_unconfirmed_change(self): + self.log.info("Test that sendall spends unconfirmed change") + self.add_utxos([17]) + self.wallet.sendtoaddress(self.remainder_target, 10) + assert_greater_than(self.wallet.getbalances()["mine"]["trusted"], 6) + self.test_sendall_success(sendall_args = [self.remainder_target]) + + assert_equal(self.wallet.getbalance(), 0) + + @cleanup + def sendall_spends_unconfirmed_inputs_if_specified(self): + self.log.info("Test that sendall spends specified unconfirmed inputs") + self.def_wallet.sendtoaddress(self.wallet.getnewaddress(), 17) + self.wallet.syncwithvalidationinterfacequeue() + assert_equal(self.wallet.getbalances()["mine"]["untrusted_pending"], 17) + unspent = self.wallet.listunspent(minconf=0)[0] + + self.wallet.sendall(recipients=[self.remainder_target], inputs=[unspent]) + assert_equal(self.wallet.getbalance(), 0) + # This tests needs to be the last one otherwise @cleanup will fail with "Transaction too large" error def sendall_fails_with_transaction_too_large(self): self.log.info("Test that sendall fails if resulting transaction is too large") @@ -462,6 +483,12 @@ def run_test(self): # Sendall only uses outputs with less than a given number of confirmation when using minconf self.sendall_with_maxconf() + # Sendall spends unconfirmed change + self.sendall_spends_unconfirmed_change() + + # Sendall spends unconfirmed inputs if they are specified + self.sendall_spends_unconfirmed_inputs_if_specified() + # Sendall fails when many inputs result to too large transaction self.sendall_fails_with_transaction_too_large()