Skip to content

Commit e901404

Browse files
furszyryanofsky
andcommitted
settings: add auto-generated warning msg for editing the file manually
Hopefully, refraining users from modifying the file unless they are certain about the potential consequences. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
1 parent 966f5de commit e901404

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/common/settings.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
#include <common/settings.h>
66

7+
#if defined(HAVE_CONFIG_H)
8+
#include <config/bitcoin-config.h>
9+
#endif
10+
711
#include <tinyformat.h>
812
#include <univalue.h>
913
#include <util/fs.h>
@@ -116,6 +120,13 @@ bool WriteSettings(const fs::path& path,
116120
std::vector<std::string>& errors)
117121
{
118122
SettingsValue out(SettingsValue::VOBJ);
123+
// Add auto-generated warning comment only if it does not exist
124+
if (!values.contains("_warning_")) {
125+
out.pushKV("_warning_", strprintf("This file is automatically generated and updated by %s. Please do not edit this file while the node "
126+
"is running, as any changes might be ignored or overwritten.",
127+
PACKAGE_NAME));
128+
}
129+
// Push settings values
119130
for (const auto& value : values) {
120131
out.pushKVEnd(value.first, value.second);
121132
}

src/qt/test/optiontests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ void OptionTests::migrateSettings()
6161
QVERIFY(!settings.contains("addrSeparateProxyTor"));
6262

6363
std::ifstream file(gArgs.GetDataDirNet() / "settings.json");
64+
std::string default_warning = strprintf("This file is automatically generated and updated by %s. Please do not edit this file while the node "
65+
"is running, as any changes might be ignored or overwritten.",
66+
PACKAGE_NAME);
6467
QCOMPARE(std::string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()).c_str(), "{\n"
68+
" \"_warning_\": \""+ default_warning+"\",\n"
6569
" \"dbcache\": \"600\",\n"
6670
" \"listen\": false,\n"
6771
" \"onion\": \"onion:234\",\n"

test/functional/feature_settings.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ def run_test(self):
2323
settings = node.chain_path / "settings.json"
2424
conf = node.datadir_path / "bitcoin.conf"
2525

26-
# Assert empty settings file was created
26+
# Assert default settings file was created
2727
self.stop_node(0)
28+
default_settings = {"_warning_": "This file is automatically generated and updated by Bitcoin Core. Please do not edit this file while the node is running, as any changes might be ignored or overwritten."}
2829
with settings.open() as fp:
29-
assert_equal(json.load(fp), {})
30+
assert_equal(json.load(fp), default_settings)
3031

3132
# Assert settings are parsed and logged
3233
with settings.open("w") as fp:
@@ -48,7 +49,7 @@ def run_test(self):
4849

4950
# Assert settings are unchanged after shutdown
5051
with settings.open() as fp:
51-
assert_equal(json.load(fp), {"string": "string", "num": 5, "bool": True, "null": None, "list": [6, 7]})
52+
assert_equal(json.load(fp), {**default_settings, **{"string": "string", "num": 5, "bool": True, "null": None, "list": [6, 7]}})
5253

5354
# Test invalid json
5455
with settings.open("w") as fp:

0 commit comments

Comments
 (0)