Skip to content

Commit 49ee2a0

Browse files
committed
Avoid wallet code writing node settings file
Change wallet loading code to access settings through the Chain interface instead of writing settings.json directly.
1 parent 1704bbf commit 49ee2a0

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
@@ -262,11 +262,18 @@ class Chain
262262
//! Current RPC serialization flags.
263263
virtual int rpcSerializationFlags() = 0;
264264

265+
//! Get settings value.
266+
virtual util::SettingsValue getSetting(const std::string& arg) = 0;
267+
268+
//! Get list of settings values.
269+
virtual std::vector<util::SettingsValue> getSettingsList(const std::string& arg) = 0;
270+
265271
//! Return <datadir>/settings.json setting value.
266272
virtual util::SettingsValue getRwSetting(const std::string& name) = 0;
267273

268-
//! Write a setting to <datadir>/settings.json.
269-
virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value) = 0;
274+
//! Write a setting to <datadir>/settings.json. Optionally just update the
275+
//! setting in memory and do not write the file.
276+
virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value, bool write=true) = 0;
270277

271278
//! Synchronously send transactionAddedToMempool notifications about all
272279
//! 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
@@ -690,6 +690,14 @@ class ChainImpl : public Chain
690690
RPCRunLater(name, std::move(fn), seconds);
691691
}
692692
int rpcSerializationFlags() override { return RPCSerializationFlags(); }
693+
util::SettingsValue getSetting(const std::string& name) override
694+
{
695+
return gArgs.GetSetting(name);
696+
}
697+
std::vector<util::SettingsValue> getSettingsList(const std::string& name) override
698+
{
699+
return gArgs.GetSettingsList(name);
700+
}
693701
util::SettingsValue getRwSetting(const std::string& name) override
694702
{
695703
util::SettingsValue result;
@@ -700,7 +708,7 @@ class ChainImpl : public Chain
700708
});
701709
return result;
702710
}
703-
bool updateRwSetting(const std::string& name, const util::SettingsValue& value) override
711+
bool updateRwSetting(const std::string& name, const util::SettingsValue& value, bool write) override
704712
{
705713
gArgs.LockSettings([&](util::Settings& settings) {
706714
if (value.isNull()) {
@@ -709,7 +717,7 @@ class ChainImpl : public Chain
709717
settings.rw_settings[name] = value;
710718
}
711719
});
712-
return gArgs.WriteSettingsFile();
720+
return !write || gArgs.WriteSettingsFile();
713721
}
714722
void requestMempoolTransactions(Notifications& notifications) override
715723
{

src/util/system.h

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

210+
public:
210211
/**
211212
* Get setting value.
212213
*
@@ -221,7 +222,6 @@ class ArgsManager
221222
*/
222223
std::vector<util::SettingsValue> GetSettingsList(const std::string& arg) const;
223224

224-
public:
225225
ArgsManager();
226226
~ArgsManager();
227227

src/wallet/load.cpp

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

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

64-
for (const auto& wallet_file : gArgs.GetArgs("-wallet")) {
65+
for (const auto& wallet : chain.getSettingsList("wallet")) {
66+
const auto& wallet_file = wallet.get_str();
6567
const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), wallet_file);
6668

6769
if (!wallet_paths.insert(path).second) {
@@ -91,7 +93,8 @@ bool LoadWallets(interfaces::Chain& chain)
9193
{
9294
try {
9395
std::set<fs::path> wallet_paths;
94-
for (const std::string& name : gArgs.GetArgs("-wallet")) {
96+
for (const auto& wallet : chain.getSettingsList("wallet")) {
97+
const auto& name = wallet.get_str();
9598
if (!wallet_paths.insert(name).second) {
9699
continue;
97100
}

0 commit comments

Comments
 (0)