Skip to content

Commit d7a20b3

Browse files
meshcollidervijaydasmp
authored andcommitted
Merge 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 bitcoin#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@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
1 parent 0c243ab commit d7a20b3

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

src/interfaces/chain.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,18 @@ class Chain
285285
//! Run function after given number of seconds. Cancel any previous calls with same name.
286286
virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0;
287287

288+
//! Get settings value.
289+
virtual util::SettingsValue getSetting(const std::string& arg) = 0;
290+
291+
//! Get list of settings values.
292+
virtual std::vector<util::SettingsValue> getSettingsList(const std::string& arg) = 0;
293+
288294
//! Return <datadir>/settings.json setting value.
289295
virtual util::SettingsValue getRwSetting(const std::string& name) = 0;
290296

291-
//! Write a setting to <datadir>/settings.json.
292-
virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value) = 0;
297+
//! Write a setting to <datadir>/settings.json. Optionally just update the
298+
//! setting in memory and do not write the file.
299+
virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value, bool write=true) = 0;
293300

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

src/node/interfaces.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,14 @@ class ChainImpl : public Chain
971971
{
972972
RPCRunLater(name, std::move(fn), seconds);
973973
}
974+
util::SettingsValue getSetting(const std::string& name) override
975+
{
976+
return gArgs.GetSetting(name);
977+
}
978+
std::vector<util::SettingsValue> getSettingsList(const std::string& name) override
979+
{
980+
return gArgs.GetSettingsList(name);
981+
}
974982
util::SettingsValue getRwSetting(const std::string& name) override
975983
{
976984
util::SettingsValue result;
@@ -981,7 +989,7 @@ class ChainImpl : public Chain
981989
});
982990
return result;
983991
}
984-
bool updateRwSetting(const std::string& name, const util::SettingsValue& value) override
992+
bool updateRwSetting(const std::string& name, const util::SettingsValue& value, bool write) override
985993
{
986994
gArgs.LockSettings([&](util::Settings& settings) {
987995
if (value.isNull()) {
@@ -990,7 +998,7 @@ class ChainImpl : public Chain
990998
settings.rw_settings[name] = value;
991999
}
9921000
});
993-
return gArgs.WriteSettingsFile();
1001+
return !write || gArgs.WriteSettingsFile();
9941002
}
9951003
void requestMempoolTransactions(Notifications& notifications) override
9961004
{

src/util/system.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class ArgsManager
225225
*/
226226
bool UseDefaultSection(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
227227

228+
public:
228229
/**
229230
* Get setting value.
230231
*
@@ -239,7 +240,6 @@ class ArgsManager
239240
*/
240241
std::vector<util::SettingsValue> GetSettingsList(const std::string& arg) const;
241242

242-
public:
243243
ArgsManager();
244244
~ArgsManager();
245245

src/wallet/load.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,20 @@ bool VerifyWallets(interfaces::Chain& chain)
5757
options.require_existing = true;
5858
options.verify = false;
5959
if (MakeWalletDatabase("", options, status, error_string)) {
60-
gArgs.LockSettings([&](util::Settings& settings) {
61-
util::SettingsValue wallets(util::SettingsValue::VARR);
62-
wallets.push_back(""); // Default wallet name is ""
63-
settings.rw_settings["wallet"] = wallets;
64-
});
60+
util::SettingsValue wallets(util::SettingsValue::VARR);
61+
wallets.push_back(""); // Default wallet name is ""
62+
// Pass write=false because no need to write file and probably
63+
// better not to. If unnamed wallet needs to be added next startup
64+
// and the setting is empty, this code will just run again.
65+
chain.updateRwSetting("wallet", wallets, /* write= */ false);
6566
}
6667
}
6768

6869
// Keep track of each wallet absolute path to detect duplicates.
6970
std::set<fs::path> wallet_paths;
7071

71-
for (const auto& wallet_file : gArgs.GetArgs("-wallet")) {
72+
for (const auto& wallet : chain.getSettingsList("wallet")) {
73+
const auto& wallet_file = wallet.get_str();
7274
const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), fs::PathFromString(wallet_file));
7375

7476
if (!wallet_paths.insert(path).second) {
@@ -98,7 +100,8 @@ bool LoadWallets(interfaces::Chain& chain, interfaces::CoinJoin::Loader& coinjoi
98100
{
99101
try {
100102
std::set<fs::path> wallet_paths;
101-
for (const std::string& name : gArgs.GetArgs("-wallet")) {
103+
for (const auto& wallet : chain.getSettingsList("wallet")) {
104+
const auto& name = wallet.get_str();
102105
if (!wallet_paths.insert(fs::PathFromString(name)).second) {
103106
continue;
104107
}

0 commit comments

Comments
 (0)