From faee4484ff3677561868c2516360039f568bb7d8 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Mon, 16 Nov 2026 09:27:33 -0600 Subject: [PATCH 1/4] Merge bitcoin/bitcoin#30355: wallet: use LogTrace for walletdb log messages at trace level 46819f5df6de2a860c3ec87d55527b617a3b128f wallet: use LogTrace for walletdb log messages at trace level (Anthony Towns) Pull request description: Wallet sqlite logging is enabled by `-debug=walletdb -loglevel=walletdb:trace` however the actual log messages are sent at `BCLog::Level::Info`. Switch to the trace level to make this consistent. This adds `[walletdb:trace]` to the log output, eg: ``` [httpworker.3] [wallet/sqlite.cpp:55] [TraceSqlCallback] [/tmp/bitcoin_func_test_4fsnatpg/node0/regtest/wallets/boring/wallet.dat] SQLite Statement: BEGIN EXCLUSIVE TRANSACTION ``` becomes ``` [httpworker.0] [wallet/sqlite.cpp:55] [TraceSqlCallback] [walletdb:trace] [/tmp/bitcoin_func_test_9lcwth4z/node0/regtest/wallets/boring/wallet.dat] SQLite Statement: BEGIN EXCLUSIVE TRANSACTION ``` ACKs for top commit: maflcko: ACK 46819f5df6de2a860c3ec87d55527b617a3b128f ryanofsky: Code review ACK 46819f5df6de2a860c3ec87d55527b617a3b128f. Nice catch! furszy: ACK 46819f5df6de2a860c3ec87d55527b617a3b128f luke-jr: utACK 46819f5df6de2a860c3ec87d55527b617a3b128f Tree-SHA512: 6fc1bc63c2ee686d4ca8f4f558f06c0cd9e7813b5fce1588351f55ef8bedfc23c97ea443e54a6a447008fa79ea022b6d631cb010929932f1db23fa8e255e6482 --- src/wallet/sqlite.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From 1d85de3fcd4b032b42d3b32610a304d96661cb8a Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Mon, 16 Nov 2026 09:27:45 -0600 Subject: [PATCH 2/4] Merge bitcoin/bitcoin#30697: Bugfix: Ensure Atomicity in Wallet Settings Updates from Chain Interface 1b41d45d462d856a9d0b44ae0039bbb2cd78407c wallet: bugfix: ensure atomicity in settings updates (ismaelsadeeq) Pull request description: This PR fixes #30620. As outlined in the issue, creating two wallets with `load_on_startup=true` simultaneously results in only one wallet being added to the startup file. The current issue arises because the wallet settings update process involves: 1. Obtaining the settings value while acquiring the settings lock. 2. Modifying the settings value. 3. Overwriting the settings value while acquiring the settings lock again. This sequence is not thread-safe. Different threads could modify the same base value simultaneously, overwriting data from other workers without realizing it. The PR attempts to fix this by modifying the chain interface's `updateRwSetting` method to accept a function that will be called with the settings reference. This function will either update or delete the setting and return an enum indicating whether the settings need to be overwritten in this or not. Additionally, this PR introduces two new methods to the chain interface: - `overwriteRwSetting`: This method replaces the setting with a new value. Used in `VerifyWallets` - `deleteRwSettings`: This method completely erases a specified setting. This method is currently used only in `overwriteRwSetting`. These changes ensure that updates are race-free across all clients. ACKs for top commit: achow101: ACK 1b41d45d462d856a9d0b44ae0039bbb2cd78407c furszy: self-code-ACK https://github.com/bitcoin/bitcoin/commit/1b41d45d462d856a9d0b44ae0039bbb2cd78407c Tree-SHA512: 50cda612b782aeb5e03e2cf63cc44779a013de1c535b883b57af4de22f24b0de80b4edecbcda235413baec0a12bdf0e5750fb6731c9e67d32e742d8c63f08c13 --- src/interfaces/chain.h | 24 +++++++++++++++++++--- src/node/interfaces.cpp | 30 +++++++++++++++++++++------ src/wallet/load.cpp | 2 +- src/wallet/test/wallet_tests.cpp | 34 +++++++++++++++++++++++++++++++ src/wallet/wallet.cpp | 35 ++++++++++++++++++-------------- 5 files changed, 100 insertions(+), 25 deletions(-) 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/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, From 1973a5752928f6ed907518ff9d1d6679c1eb409e Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Mon, 16 Nov 2026 09:28:27 -0600 Subject: [PATCH 3/4] Merge bitcoin/bitcoin#28979: wallet, rpc: document and update `sendall` behavior around unconfirmed inputs 71aae72e1fc998b2629d68a7301d85dc1b65641e test: test sendall does ancestor aware funding (ishaanam) 36757941a05b65c2b61a83820afdf5effd8fc9a2 wallet, rpc: implement ancestor aware funding for sendall (ishaanam) 544131f3fba9ea07fee29f9d3ee0116cd5d8a5b2 rpc, test: test sendall spends unconfirmed change and unconfirmed inputs when specified (ishaanam) Pull request description: This PR: - Adds a functional test that `sendall` spends unconfirmed change - Adds a functional test that `sendall` spends regular unconfirmed inputs when specified by user - Adds ancestor aware funding to `sendall` by using `calculateCombinedBumpFee` and adjusting the effective value accordingly - Adds a functional test for ancestor aware funding in `sendall` ACKs for top commit: S3RK: ACK 71aae72e1fc998b2629d68a7301d85dc1b65641e achow101: ACK 71aae72e1fc998b2629d68a7301d85dc1b65641e furszy: ACK 71aae72e1f Tree-SHA512: acaeb7c65166ce53123a1d6cb5012197202246acc02ef9f37a28154cc93afdbd77c25e840ab79bdc7e0b88904014a43ab1ddea79d5337dc310ea210634ab61f0 Dash adaptations: - src/wallet/rpc/spend.cpp: takes only the sendall help-text update; the bump-fee change to effective_value is left out because Dash declined bitcoin#26152 - test/functional/wallet_sendall.py: leaves out sendall_does_ancestor_aware_funding and its call in run_test, since it only tests the omitted bump-fee change Not applicable to Dash (intentionally omitted): - src/wallet/rpc/spend.cpp: Builds outpoints_spent and subtracts chain().calculateCombinedBumpFee(), a MiniMiner interface from bitcoin/bitcoin#26152, which Dash declined - test/functional/wallet_sendall.py: sendall_does_ancestor_aware_funding checks ancestor bump fees, which Dash does not have without bitcoin/bitcoin#26152 --- src/wallet/rpc/spend.cpp | 2 +- test/functional/wallet_sendall.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) 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/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() From d58c507e889c410dfc4ee263ba9a22662071f344 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Mon, 16 Nov 2026 09:30:28 -0600 Subject: [PATCH 4/4] Merge bitcoin/bitcoin#29586: wallet: default wallet migration, modify inconvenient backup filename a951dba3a9d7663f009701140d663338e6c526a4 wallet: default wallet migration, modify inconvenient backup filename (furszy) Pull request description: Fixes #29584 On default legacy wallets, the backup filename starts with an "-" due to the wallet name being empty. This is inconvenient for systems who treat what follows the initial "-" character as flags. Note: As the user can freely set the wallet name to anything, we could also guard the backup filename against other inconvenient characters in the future (we need to be careful here, because the wallet name could also be a path). ACKs for top commit: achow101: ACK a951dba3a9d7663f009701140d663338e6c526a4 brunoerg: utACK a951dba3a9d7663f009701140d663338e6c526a4 vasild: ACK a951dba3a9d7663f009701140d663338e6c526a4 Tree-SHA512: 6347bb12cfb50526a4baad96e4f1df9d82b493f79f0a0f7e0a1c8335a86a1e8e147c7b7f95cec6ede6f4507506a7eaf7972bd35131a2d5ed4cbbf38d94f0a9ca Dash adaptations: - src/wallet/wallet.cpp: no change; Dash already names the unnamed wallet's backup default_wallet_