Skip to content

Commit e9d6eb1

Browse files
committed
Merge bitcoin/bitcoin#22217: refactor: Avoid wallet code writing node settings file
49ee2a0 Avoid wallet code writing node settings file (Russell Yanofsky) Pull request description: Change wallet loading code to access settings through the Chain interface instead of writing settings.json directly. This is for running wallet and node in separate processes, since multiprocess code wouldn't easily work with different processes updating the same file. --- This PR is part of the [process separation project](https://github.com/bitcoin/bitcoin/projects/10). The commit was first part of larger PR #10102. ACKs for top commit: jamesob: ACK 49ee2a0 ([`jamesob/ackr/22217.1.ryanofsky.refactor_avoid_wallet_co`](https://github.com/jamesob/bitcoin/tree/ackr/22217.1.ryanofsky.refactor_avoid_wallet_co)) ryanofsky: > ACK [49ee2a0](bitcoin/bitcoin@49ee2a0) ([`jamesob/ackr/22217.1.ryanofsky.refactor_avoid_wallet_co`](https://github.com/jamesob/bitcoin/tree/ackr/22217.1.ryanofsky.refactor_avoid_wallet_co)) Zero-1729: crACK 49ee2a0 meshcollider: Code review ACK 49ee2a0 Tree-SHA512: a81c63b87816f739e02e3992808f314294d6c7213babaafdaaf3c4650ebc97ee4f98f9a4684ce4ff87372df59989b8ad5929159c5686293a7cce04e97e2fabba
2 parents f5a406f + 49ee2a0 commit e9d6eb1

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

src/interfaces/chain.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,18 @@ class Chain
265265
//! Current RPC serialization flags.
266266
virtual int rpcSerializationFlags() = 0;
267267

268+
//! Get settings value.
269+
virtual util::SettingsValue getSetting(const std::string& arg) = 0;
270+
271+
//! Get list of settings values.
272+
virtual std::vector<util::SettingsValue> getSettingsList(const std::string& arg) = 0;
273+
268274
//! Return <datadir>/settings.json setting value.
269275
virtual util::SettingsValue getRwSetting(const std::string& name) = 0;
270276

271-
//! Write a setting to <datadir>/settings.json.
272-
virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value) = 0;
277+
//! Write a setting to <datadir>/settings.json. Optionally just update the
278+
//! setting in memory and do not write the file.
279+
virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value, bool write=true) = 0;
273280

274281
//! Synchronously send transactionAddedToMempool notifications about all
275282
//! current mempool transactions to the specified handler and return after

src/node/interfaces.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,14 @@ class ChainImpl : public Chain
661661
RPCRunLater(name, std::move(fn), seconds);
662662
}
663663
int rpcSerializationFlags() override { return RPCSerializationFlags(); }
664+
util::SettingsValue getSetting(const std::string& name) override
665+
{
666+
return gArgs.GetSetting(name);
667+
}
668+
std::vector<util::SettingsValue> getSettingsList(const std::string& name) override
669+
{
670+
return gArgs.GetSettingsList(name);
671+
}
664672
util::SettingsValue getRwSetting(const std::string& name) override
665673
{
666674
util::SettingsValue result;
@@ -671,7 +679,7 @@ class ChainImpl : public Chain
671679
});
672680
return result;
673681
}
674-
bool updateRwSetting(const std::string& name, const util::SettingsValue& value) override
682+
bool updateRwSetting(const std::string& name, const util::SettingsValue& value, bool write) override
675683
{
676684
gArgs.LockSettings([&](util::Settings& settings) {
677685
if (value.isNull()) {
@@ -680,7 +688,7 @@ class ChainImpl : public Chain
680688
settings.rw_settings[name] = value;
681689
}
682690
});
683-
return gArgs.WriteSettingsFile();
691+
return !write || gArgs.WriteSettingsFile();
684692
}
685693
void requestMempoolTransactions(Notifications& notifications) override
686694
{

src/util/system.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class ArgsManager
205205
*/
206206
bool UseDefaultSection(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
207207

208+
public:
208209
/**
209210
* Get setting value.
210211
*
@@ -219,7 +220,6 @@ class ArgsManager
219220
*/
220221
std::vector<util::SettingsValue> GetSettingsList(const std::string& arg) const;
221222

222-
public:
223223
ArgsManager();
224224
~ArgsManager();
225225

src/wallet/load.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,20 @@ bool VerifyWallets(WalletContext& context)
5252
options.require_existing = true;
5353
options.verify = false;
5454
if (MakeWalletDatabase("", options, status, error_string)) {
55-
gArgs.LockSettings([&](util::Settings& settings) {
56-
util::SettingsValue wallets(util::SettingsValue::VARR);
57-
wallets.push_back(""); // Default wallet name is ""
58-
settings.rw_settings["wallet"] = wallets;
59-
});
55+
util::SettingsValue wallets(util::SettingsValue::VARR);
56+
wallets.push_back(""); // Default wallet name is ""
57+
// Pass write=false because no need to write file and probably
58+
// better not to. If unnamed wallet needs to be added next startup
59+
// and the setting is empty, this code will just run again.
60+
chain.updateRwSetting("wallet", wallets, /* write= */ false);
6061
}
6162
}
6263

6364
// Keep track of each wallet absolute path to detect duplicates.
6465
std::set<fs::path> wallet_paths;
6566

66-
for (const auto& wallet_file : gArgs.GetArgs("-wallet")) {
67+
for (const auto& wallet : chain.getSettingsList("wallet")) {
68+
const auto& wallet_file = wallet.get_str();
6769
const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), wallet_file);
6870

6971
if (!wallet_paths.insert(path).second) {
@@ -94,7 +96,8 @@ bool LoadWallets(WalletContext& context)
9496
interfaces::Chain& chain = *context.chain;
9597
try {
9698
std::set<fs::path> wallet_paths;
97-
for (const std::string& name : gArgs.GetArgs("-wallet")) {
99+
for (const auto& wallet : chain.getSettingsList("wallet")) {
100+
const auto& name = wallet.get_str();
98101
if (!wallet_paths.insert(name).second) {
99102
continue;
100103
}

0 commit comments

Comments
 (0)