Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::optional<interfaces::SettingsAction>(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.
Expand Down Expand Up @@ -349,9 +360,16 @@ class Chain
//! Return <datadir>/settings.json setting value.
virtual common::SettingsValue getRwSetting(const std::string& name) = 0;

//! Write a setting to <datadir>/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 <datadir>/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 <datadir>/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 <datadir>/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
Expand Down
30 changes: 24 additions & 6 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<interfaces::SettingsAction> 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);
Comment on lines +1734 to +1737

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocking: Do not insert a null setting for no-op updates

When name is absent, settings.rw_settings[name] creates a null SettingsValue before the callback runs. RemoveWalletSetting receives that null value, returns SKIP_WRITE because it is not an array, and leaves the null entry in the in-memory settings map. Although that call does not immediately write the file, any later settings write serializes the entry as "wallet": null; on the next startup, getSettingsList("wallet") returns the null value and VerifyWallets calls get_str() on it. The update path must operate on a temporary value for absent keys and erase an existing value if the callback leaves it null, inserting a new key only when the callback produces a non-null value.

Suggested change
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 (auto* value = common::FindKey(settings.rw_settings, name)) {
action = update_settings_func(*value);
if (value->isNull()) settings.rw_settings.erase(name);
} else {
common::SettingsValue new_value;
action = update_settings_func(new_value);
if (!new_value.isNull()) settings.rw_settings[name] = std::move(new_value);
}

source: gpt-6.1-sol (phase2-reviewer: general)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hunk matches upstream bitcoin#30697 exactly. The suggested change is upstream commit df60199 from bitcoin#30828, which also changes src/interfaces/chain.h, so it belongs in a backport of bitcoin#30828 and not in this commit.


🤖 backportsys, on behalf of the Dash backport pipeline.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching upstream bitcoin#30697 does not make this implementation safe; the regression is introduced by this backport's new absent-key handling. In the existing RemoveWalletSetting caller, an absent wallet setting remains a null map entry because the callback returns SKIP_WRITE, and a later unrelated settings write persists that null value. The mention of bitcoin#30828 confirms this was fixed upstream later, but it does not resolve the defect in this commit.

});
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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpc/spend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/sqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::thread> 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.
//
Expand Down
35 changes: 20 additions & 15 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions test/functional/wallet_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))

Expand Down
27 changes: 27 additions & 0 deletions test/functional/wallet_sendall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()

Expand Down
Loading