diff --git a/src/interfaces/node.h b/src/interfaces/node.h --- a/src/interfaces/node.h +++ b/src/interfaces/node.h @@ -100,6 +100,10 @@ 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/interfaces/node.cpp b/src/interfaces/node.cpp --- a/src/interfaces/node.cpp +++ b/src/interfaces/node.cpp @@ -121,6 +121,13 @@ } }); } + 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); } diff --git a/src/util/system.h b/src/util/system.h --- a/src/util/system.h +++ b/src/util/system.h @@ -399,7 +399,8 @@ * 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. @@ -407,9 +408,11 @@ 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, diff --git a/src/util/system.cpp b/src/util/system.cpp --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -504,10 +504,14 @@ return true; } -bool ArgsManager::GetSettingsPath(fs::path *filepath, bool temp) const { +bool ArgsManager::GetSettingsPath(fs::path *filepath, bool temp, + bool backup) const { if (IsArgNegated("-settings")) { return false; } + if (backup) { + settings += ".bak"; + } if (filepath) { std::string settings = GetArg("-settings", BITCOIN_SETTINGS_FILENAME); *filepath = fsbridge::AbsPathJoin( @@ -553,10 +557,11 @@ 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."); }