diff --git a/src/interfaces/node.h b/src/interfaces/node.h index 1585174a6f8..2c31e12ada6 100644 --- a/src/interfaces/node.h +++ b/src/interfaces/node.h @@ -112,6 +112,10 @@ class Node //! source, but not being persisted. virtual void forceSetting(const std::string& name, const util::SettingsValue& value) = 0; + //! Clear all settings in /settings.json and store a backup of + //! previous settings in /settings.json.bak. + virtual void resetSettings() = 0; + //! Map port. virtual void mapPort(bool use_upnp, bool use_natpmp) = 0; diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 09404a5f852..01db843976c 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -144,6 +144,14 @@ class NodeImpl : public Node } }); } + void resetSettings() override + { + gArgs.WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true); + gArgs.LockSettings([&](util::Settings& settings) { + settings.rw_settings.clear(); + }); + gArgs.WriteSettingsFile(); + } void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); } bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); } size_t getNodeCount(ConnectionDirection flags) override diff --git a/src/util/system.cpp b/src/util/system.cpp index 87d3be9d231..fa2e564791b 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -526,12 +526,15 @@ bool ArgsManager::InitSettings(std::string& error) return true; } -bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const +bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const { fs::path settings = GetPathArg("-settings", fs::path{BITCOIN_SETTINGS_FILENAME}); if (settings.empty()) { return false; } + if (backup) { + settings += ".bak"; + } if (filepath) { *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings); } @@ -572,10 +575,10 @@ bool ArgsManager::ReadSettingsFile(std::vector* errors) return true; } -bool ArgsManager::WriteSettingsFile(std::vector* errors) const +bool ArgsManager::WriteSettingsFile(std::vector* errors, bool backup) const { fs::path path, path_tmp; - if (!GetSettingsPath(&path, /* temp= */ false) || !GetSettingsPath(&path_tmp, /* temp= */ true)) { + if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) { throw std::logic_error("Attempt to write settings file when dynamic settings are disabled."); } diff --git a/src/util/system.h b/src/util/system.h index bfc96982215..6af4f1dc18e 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -440,7 +440,7 @@ class ArgsManager * Get settings file path, or return false if read-write settings were * disabled with -nosettings. */ - bool GetSettingsPath(fs::path* filepath = nullptr, bool temp = false) const; + bool GetSettingsPath(fs::path* filepath = nullptr, bool temp = false, bool backup = false) const; /** * Read settings file. Push errors to vector, or log them if null. @@ -448,9 +448,10 @@ class ArgsManager bool ReadSettingsFile(std::vector* errors = nullptr); /** - * Write settings file. Push errors to vector, or log them if null. + * Write settings file or backup settings file. Push errors to vector, or + * log them if null. */ - bool WriteSettingsFile(std::vector* errors = nullptr) const; + bool WriteSettingsFile(std::vector* errors = nullptr, bool backup = false) const; /** * Get current setting from config file or read/write settings file,