Skip to content

util: Port of ArgsManager and a significant subset of src/util #2146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,12 @@ GRIDCOIN_CORE_H = \
txdb-leveldb.h \
ui_interface.h \
uint256.h \
util/check.h \
util/reverse_iterator.h \
util/settings.h \
util/strencodings.h \
util/string.h \
util/system.h \
util/threadnames.h \
util/time.h \
util.h \
Expand Down Expand Up @@ -259,7 +263,10 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
sync.cpp \
txdb-leveldb.cpp \
uint256.cpp \
util/settings.cpp \
util/strencodings.cpp \
util/string.cpp \
util/system.cpp \
util/threadnames.cpp \
util/time.cpp \
util.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ bool CAlert::ProcessAlert(bool fThread)
if(AppliesToMe())
{
uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
std::string strCmd = GetArg("-alertnotify", "");
std::string strCmd = gArgs.GetArg("-alertnotify", "");
if (!strCmd.empty())
{
// Alert text should be plain ascii coming from a trusted source, but to
Expand Down
10 changes: 8 additions & 2 deletions src/chainparamsbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
#include "chainparamsbase.h"

#include "tinyformat.h"
#include <util/system.h>
#include <assert.h>

const std::string CBaseChainParams::MAIN = "main";
const std::string CBaseChainParams::TESTNET = "test";

void SetupChainParamsBaseOptions(ArgsManager& argsman)
{
argsman.AddArg("-chain=<chain>", "Use the chain <chain> (default: main). Allowed values: main, test", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-testnet", "Use the test chain. Equivalent to -chain=test.", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
}

static std::unique_ptr<CBaseChainParams> globalChainBaseParams;

Expand All @@ -23,9 +29,9 @@ const CBaseChainParams& BaseParams()
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain)
{
if (chain == CBaseChainParams::MAIN)
return std::make_unique<CBaseChainParams>("", 32749);
return std::make_unique<CBaseChainParams>("", 15715);
else if (chain == CBaseChainParams::TESTNET)
return std::make_unique<CBaseChainParams>("testnet", 32748);
return std::make_unique<CBaseChainParams>("testnet", 25715);
else
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
}
Expand Down
5 changes: 5 additions & 0 deletions src/chainparamsbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class CBaseChainParams
*/
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain);

/**
*Set the arguments for chainparams
*/
void SetupChainParamsBaseOptions(ArgsManager& argsman);

/**
* Return the currently selected parameters. This won't change after app
* startup, except for unit tests.
Expand Down
6 changes: 6 additions & 0 deletions src/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ FILE *fopen(const fs::path& p, const char *mode)
#endif
}

fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
{
assert(base.is_absolute());
return fs::absolute(path, base);
}

#ifndef WIN32

static std::string GetErrorReason() {
Expand Down
11 changes: 11 additions & 0 deletions src/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ namespace fs = boost::filesystem;
namespace fsbridge {
FILE *fopen(const fs::path& p, const char *mode);

/**
* Helper function for joining two paths
*
* @param[in] base Base path
* @param[in] path Path to combine with base
* @returns path unchanged if it is an absolute path, otherwise returns base joined with path. Returns base unchanged if path is empty.
* @pre Base path must be absolute
* @post Returned path will always be absolute
*/
fs::path AbsPathJoin(const fs::path& base, const fs::path& path);

class FileLock
{
public:
Expand Down
24 changes: 12 additions & 12 deletions src/gridcoin/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using namespace GRC;
fs::path GRC::GetBackupPath()
{
fs::path defaultDir = GetDataDir() / "walletbackups";
return GetArg("-backupdir", defaultDir.string());
return gArgs.GetArg("-backupdir", defaultDir.string());
}

std::string GRC::GetBackupFilename(const std::string& basename, const std::string& suffix)
Expand All @@ -40,20 +40,20 @@ bool GRC::BackupsEnabled()
{
// If either of these configuration options is explicitly set to zero,
// disable backups completely:
return GetArg("-walletbackupinterval", 1) > 0
&& GetArg("-walletbackupintervalsecs", 1) > 0;
return gArgs.GetArg("-walletbackupinterval", 1) > 0
&& gArgs.GetArg("-walletbackupintervalsecs", 1) > 0;
}

int64_t GRC::GetBackupInterval()
{
int64_t backup_interval_secs = GetArg("-walletbackupintervalsecs", 86400);
int64_t backup_interval_secs = gArgs.GetArg("-walletbackupintervalsecs", 86400);

// The deprecated -walletbackupinterval option specifies the backup interval
// as the number of blocks that pass. If someone still uses this in a config
// file, we'll honor it for now:
//
if (mapArgs.count("-walletbackupinterval")) {
backup_interval_secs = GetArg("-walletbackupinterval", 900) * 90;
if (gArgs.IsArgSet("-walletbackupinterval")) {
backup_interval_secs = gArgs.GetArg("-walletbackupinterval", 900) * 90;
}

return backup_interval_secs;
Expand Down Expand Up @@ -190,7 +190,7 @@ bool GRC::MaintainBackups(fs::path wallet_backup_path, std::vector<std::string>
// TODO: Probably a good idea to encapsulate it into its own function that can be
//used by backups and both loggers.

bool maintain_backup_retention = GetBoolArg("-maintainbackupretention", false);
bool maintain_backup_retention = gArgs.GetBoolArg("-maintainbackupretention", false);

// Nothing to do if maintain_backup_retention is not set, which is the default to be
// safe (i.e. retain backups indefinitely is the default behavior).
Expand All @@ -200,19 +200,19 @@ bool GRC::MaintainBackups(fs::path wallet_backup_path, std::vector<std::string>
if (!retention_by_num && !retention_by_days)
{
// If either argument is set, then assign the one that is set.
if (IsArgSet("-walletbackupretainnumfiles") || IsArgSet("-walletbackupretainnumdays"))
if (gArgs.IsArgSet("-walletbackupretainnumfiles") || gArgs.IsArgSet("-walletbackupretainnumdays"))
{
// Default to zero for the unset argument, which means unset here. Also, clamp
// to zero for nonsensical negative values. That kind of stupidity will be
// caught and dealt with below.
retention_by_num = (unsigned int) std::max((int64_t) 0, GetArg("-walletbackupretainnumfiles", 0));
retention_by_days = (unsigned int) std::max((int64_t) 0, GetArg("-walletbackupretainnumdays", 0));
retention_by_num = (unsigned int) std::max((int64_t) 0, gArgs.GetArg("-walletbackupretainnumfiles", 0));
retention_by_days = (unsigned int) std::max((int64_t) 0, gArgs.GetArg("-walletbackupretainnumdays", 0));
}
else
{
// Default to 365 for each. (A very conservative setting.)
retention_by_num = (unsigned int) GetArg("-walletbackupretainnumfiles", 365);
retention_by_days = (unsigned int) GetArg("-walletbackupretainnumdays", 365);
retention_by_num = (unsigned int) gArgs.GetArg("-walletbackupretainnumfiles", 365);
retention_by_days = (unsigned int) gArgs.GetArg("-walletbackupretainnumdays", 365);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gridcoin/boinc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

fs::path GRC::GetBoincDataDir()
{
std::string path = GetArgument("boincdatadir", "");
std::string path = gArgs.GetArg("-boincdatadir", "");

if (!path.empty()) {
return fs::path(path);
Expand Down
16 changes: 8 additions & 8 deletions src/gridcoin/gridcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void InitializeContracts(CBlockIndex* pindexBest)

// If the clearbeaconhistory argument is provided, then clear everything from the beacon registry,
// including the beacon_db and beacon key type elements from leveldb.
if (GetBoolArg("-clearbeaconhistory", false))
if (gArgs.GetBoolArg("-clearbeaconhistory", false))
{
beacons.Reset();
}
Expand Down Expand Up @@ -270,9 +270,9 @@ void ThreadScraperSubscriber(void* parg)
void InitializeScraper(ThreadHandlerPtr threads)
{
// Default to 300 sec (5 min), clamp to 60 minimum, 600 maximum - converted to milliseconds.
nScraperSleep = std::clamp<int64_t>(GetArg("-scrapersleep", 300), 60, 600) * 1000;
nScraperSleep = std::clamp<int64_t>(gArgs.GetArg("-scrapersleep", 300), 60, 600) * 1000;
// Default to 14400 sec (4 hrs), clamp to 300 minimum, 86400 maximum (meaning active all of the time).
nActiveBeforeSB = std::clamp<int64_t>(GetArg("-activebeforesb", 14400), 300, 86400);
nActiveBeforeSB = std::clamp<int64_t>(gArgs.GetArg("-activebeforesb", 14400), 300, 86400);

// Run the scraper or subscriber housekeeping thread, but not both. The
// subscriber housekeeping thread checks if the flag for the scraper thread
Expand All @@ -283,7 +283,7 @@ void InitializeScraper(ThreadHandlerPtr threads)
// For example. gridcoinresearch(d) with no args will run the subscriber
// but not the scraper.
// gridcoinresearch(d) -scraper will run the scraper but not the subscriber.
if (GetBoolArg("-scraper", false)) {
if (gArgs.GetBoolArg("-scraper", false)) {
LogPrintf("Gridcoin: scraper enabled");

if (!threads->createThread(ThreadScraper, nullptr, "ThreadScraper")) {
Expand All @@ -304,7 +304,7 @@ void InitializeScraper(ThreadHandlerPtr threads)
//!
void InitializeExplorerFeatures()
{
fExplorer = GetBoolArg("-scraper", false) && GetBoolArg("-explorer", false);
fExplorer = gArgs.GetBoolArg("-scraper", false) && gArgs.GetBoolArg("-explorer", false);
}

//!
Expand Down Expand Up @@ -390,16 +390,16 @@ void ScheduleUpdateChecks(CScheduler& scheduler)
return;
}

if (GetBoolArg("-disableupdatecheck", false)) {
if (gArgs.GetBoolArg("-disableupdatecheck", false)) {
LogPrintf("Gridcoin: update checks disabled by configuration");
return;
}

int64_t hours = GetArg("-updatecheckinterval", 5 * 24);
int64_t hours = gArgs.GetArg("-updatecheckinterval", 5 * 24);

if (hours < 1) {
LogPrintf("ERROR: invalid -updatecheckinterval: %s. Using default...",
GetArg("-updatecheckinterval", ""));
gArgs.GetArg("-updatecheckinterval", ""));
hours = 24;
}

Expand Down
Loading