Skip to content

Commit

Permalink
Implement changesettings
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Jun 8, 2021
1 parent 2904a49 commit 189d28a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,96 @@ UniValue listsettings(const UniValue& params, bool fHelp)
return gArgs.OutputArgs();
}

UniValue changesettings(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() < 1)
{
throw runtime_error(
"changesettings\n"
);
}

UniValue result(UniValue::VOBJ);
UniValue settings_stored_with_no_state_change(UniValue::VARR);
UniValue settings_immediate(UniValue::VARR);
UniValue settings_applied_requiring_restart(UniValue::VARR);
UniValue invalid_settings_ignored(UniValue::VARR);

bool restart_required = false;

for (unsigned int i = 0; i < params.size(); ++i)
{
std::string param = params[i].get_str();
if (param.size() > 0 && param[0] == '-')
{
throw JSONRPCError(RPC_INVALID_PARAMETER, "Incorrectly formatted setting change: " + param);
}

std::string::size_type pos;
std::string name;
std::string value;

if ((pos = param.find('=')) != std::string::npos)
{
name = param.substr(0, pos);
value = param.substr(pos + 1);
}
else
{
throw JSONRPCError(RPC_INVALID_PARAMETER, "Incorrectly formatted setting change: " + param);
}

std::optional<unsigned int> flags = gArgs.GetArgFlags('-' + name);

// Do not process if not a valid argument/setting. This is different than incorrect format. This means the arg
// is not in the args list from AddArg.
if (!flags)
{
invalid_settings_ignored.push_back(param);
continue;
}

// TODO: Record explicit default state for settings.
// This currently has a problem that I am not sure yet how to solve. Settings that are defaulted to true, unless
// they are set to the contrary, such as -staking, will falsely indicate a change because the defaulted state is
// not explicitly stored for comparison. After there is an explicit entry defined in the settings file, it works
// correctly.
bool value_changed = (gArgs.GetArg("-" + name, "never_used_default") != value);
bool immediate_effect = *flags & ArgsManager::IMMEDIATE_EFFECT;

// Regardless, store in r-w settings file.
if (!updateRwSetting(name, value))
{
throw JSONRPCError(RPC_MISC_ERROR, "Error storing setting in read-write settings file.");
}

if (value_changed)
{
gArgs.ForceSetArg("-" + name, value);

if (immediate_effect)
{
settings_immediate.push_back(param);
}
else
{
settings_applied_requiring_restart.push_back(param);

// Record if restart required.
restart_required |= !immediate_effect;
}
}
else
{
settings_stored_with_no_state_change.push_back(param);
}
}

result.pushKV("settings_change_requires_restart", restart_required);
result.pushKV("settings_stored_with_no_state_change", settings_stored_with_no_state_change);
result.pushKV("settings_changed_taking_immediate_effect", settings_immediate);
result.pushKV("settings_changed_requiring_restart", settings_applied_requiring_restart);

return result;
}

1 change: 1 addition & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ static const CRPCCommand vRPCCommands[] =
{ "auditsnapshotaccrual", &auditsnapshotaccrual, cat_developer },
{ "auditsnapshotaccruals", &auditsnapshotaccruals, cat_developer },
{ "addkey", &addkey, cat_developer },
{ "changesettings", &changesettings, cat_developer },
{ "currentcontractaverage", &currentcontractaverage, cat_developer },
{ "debug", &debug, cat_developer },
{ "dumpcontracts", &dumpcontracts, cat_developer },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ extern UniValue scraperreport(const UniValue& params, bool fHelp);
// Network
extern UniValue addnode(const UniValue& params, bool fHelp);
extern UniValue askforoutstandingblocks(const UniValue& params, bool fHelp);
extern UniValue changesettings(const UniValue& params, bool fHelp);
extern UniValue clearbanned(const UniValue& params, bool fHelp);
extern UniValue currenttime(const UniValue& params, bool fHelp);
extern UniValue getaddednodeinfo(const UniValue& params, bool fHelp);
Expand Down

0 comments on commit 189d28a

Please sign in to comment.