Skip to content

Commit

Permalink
Baseline ArgsManager port
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed May 30, 2021
1 parent 564accf commit 4976c9b
Show file tree
Hide file tree
Showing 63 changed files with 3,336 additions and 1,335 deletions.
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
6 changes: 6 additions & 0 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 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
23 changes: 10 additions & 13 deletions src/gridcoin/researcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ std::optional<std::string> ReadClientStateXml()

LogPrintf("WARNING: Unable to obtain BOINC CPIDs.");

if (!GetArgument("boincdatadir", "").empty()) {
if (!gArgs.GetArg("-boincdatadir", "").empty()) {
LogPrintf("Could not access configured BOINC data directory %s", path.string());
} else {
LogPrintf(
Expand Down Expand Up @@ -854,7 +854,7 @@ MiningProject MiningProject::Parse(const std::string& xml)
std::strtold(ExtractXML(xml, "<user_expavg_credit>",
"</user_expavg_credit>").c_str(), nullptr));

if (IsPoolCpid(project.m_cpid) && !GetBoolArg("-pooloperator", false)) {
if (IsPoolCpid(project.m_cpid) && !gArgs.GetBoolArg("-pooloperator", false)) {
project.m_error = MiningProject::Error::POOL;
return project;
}
Expand Down Expand Up @@ -885,7 +885,7 @@ MiningProject MiningProject::Parse(const std::string& xml)
// not reply with an external CPID:
//
if (IsPoolUsername(ExtractXML(xml, "<user_name>", "</user_name>"))
&& !GetBoolArg("-pooloperator", false))
&& !gArgs.GetBoolArg("-pooloperator", false))
{
project.m_error = MiningProject::Error::POOL;
return project;
Expand Down Expand Up @@ -1141,15 +1141,15 @@ void Researcher::RunRenewBeaconJob()

std::string Researcher::Email()
{
std::string email = GetArgument("email", "");
std::string email = gArgs.GetArg("-email", "");
boost::to_lower(email);

return email;
}

bool Researcher::ConfiguredForInvestorMode(bool log)
{
if (GetBoolArg("-investor", false) || Researcher::Email() == "investor") {
if (gArgs.GetBoolArg("-investor", false) || Researcher::Email() == "investor") {
if (log) LogPrintf("Investor mode configured. Skipping CPID import.");
return true;
}
Expand Down Expand Up @@ -1184,7 +1184,7 @@ void Researcher::Reload()

LogPrintf("Loading BOINC CPIDs...");

if (!GetArgument("boinckey", "").empty()) {
if (!gArgs.GetArg("-boinckey", "").empty()) {
// TODO: implement a safer way to export researcher context that does
// not risk accidental exposure of an internal CPID and email address.
LogPrintf("WARNING: boinckey is no longer supported.");
Expand Down Expand Up @@ -1214,8 +1214,8 @@ void Researcher::Reload(MiningProjectMap projects, GRC::BeaconError beacon_error
// split CPID situation or for people that run a wallet on computers that
// do not have BOINC installed:
//
if (mapArgs.count("-forcecpid")) {
mining_id = MiningId::Parse(GetArg("-forcecpid", ""));
if (gArgs.IsArgSet("-forcecpid")) {
mining_id = MiningId::Parse(gArgs.GetArg("-forcecpid", ""));

if (mining_id.Which() == MiningId::Kind::CPID) {
LogPrintf("Configuration forces CPID: %s", mining_id.ToString());
Expand Down Expand Up @@ -1401,11 +1401,8 @@ bool Researcher::ChangeMode(const ResearcherMode mode, std::string email)
return false;
}

{
LOCK(cs_main);
ForceSetArg("-email", email);
ForceSetArg("-investor", mode == ResearcherMode::INVESTOR ? "1" : "0");
}
gArgs.ForceSetArg("-email", email);
gArgs.ForceSetArg("-investor", mode == ResearcherMode::INVESTOR ? "1" : "0");

Reload();

Expand Down
6 changes: 3 additions & 3 deletions src/gridcoin/scraper/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ std::string Http::GetLatestVersionResponse()
{
std::string buffer;
std::string header;
std::string url = GetArg("-updatecheckurl", "https://api.github.com/repos/gridcoin-community/Gridcoin-Research/releases/latest");
std::string url = gArgs.GetArg("-updatecheckurl", "https://api.github.com/repos/gridcoin-community/Gridcoin-Research/releases/latest");

struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Accept: */*");
Expand Down Expand Up @@ -301,7 +301,7 @@ std::string Http::GetLatestVersionResponse()

void Http::DownloadSnapshot()
{
std::string url = GetArg("-snapshoturl", "https://snapshot.gridcoin.us/snapshot.zip");
std::string url = gArgs.GetArg("-snapshoturl", "https://snapshot.gridcoin.us/snapshot.zip");

fs::path destination = GetDataDir() / "snapshot.zip";

Expand Down Expand Up @@ -383,7 +383,7 @@ std::string Http::GetSnapshotSHA256()
{
std::string buffer;
std::string header;
std::string url = GetArg("-snapshotsha256url", "https://snapshot.gridcoin.us/snapshot.zip.sha256");
std::string url = gArgs.GetArg("-snapshotsha256url", "https://snapshot.gridcoin.us/snapshot.zip.sha256");

struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Accept: */*");
Expand Down
8 changes: 4 additions & 4 deletions src/gridcoin/scraper/scraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class ScraperLogger

bool archive(bool fImmediate, fs::path pfile_out)
{
bool fArchiveDaily = GetBoolArg("-logarchivedaily", true);
bool fArchiveDaily = gArgs.GetBoolArg("-logarchivedaily", true);

int64_t nTime = GetAdjustedTime();
boost::gregorian::date ArchiveCheckDate = boost::posix_time::from_time_t(nTime).date();
Expand Down Expand Up @@ -547,11 +547,11 @@ class ScraperLogger

fs::remove(pfile_temp);

bool fDeleteOldLogArchives = GetBoolArg("-deleteoldlogarchives", true);
bool fDeleteOldLogArchives = gArgs.GetBoolArg("-deleteoldlogarchives", true);

if (fDeleteOldLogArchives)
{
unsigned int nRetention = (unsigned int)GetArg("-logarchiveretainnumfiles", 30);
unsigned int nRetention = (unsigned int)gArgs.GetArg("-logarchiveretainnumfiles", 30);
LogPrintf ("INFO: ScraperLogger: nRetention %i.", nRetention);

std::set<fs::directory_entry, std::greater <fs::directory_entry>> SortedDirEntries;
Expand Down Expand Up @@ -3699,7 +3699,7 @@ bool IsScraperAuthorizedToBroadcastManifests(CBitcoinAddress& AddressOut, CKey&
_log(logattribute::INFO, "ENDLOCK", "cs_main");
}

std::string sScraperAddressFromConfig = GetArg("-scraperkey", "false");
std::string sScraperAddressFromConfig = gArgs.GetArg("-scraperkey", "false");

// Check against the -scraperkey config entry first and return quickly to avoid extra work.
// If the config entry exists and is in the map (i.e. in the appcache)...
Expand Down
4 changes: 2 additions & 2 deletions src/gridcoin/scraper/scraper_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ bool CScraperManifest::IsManifestAuthorized(int64_t& nTime, CPubKey& PubKey, uns
if (IsScraperMaximumManifestPublishingRateExceeded(nTime, PubKey))
{
// Immediate ban
banscore_out = GetArg("-banscore", 100);
banscore_out = gArgs.GetArg("-banscore", 100);
return false;
}

Expand Down Expand Up @@ -502,7 +502,7 @@ void CScraperManifest::UnserializeCheck(CDataStream& ss, unsigned int& banscore_
if (!OutOfSyncByAge() && projects.size() > nMaxProjects)
{
// Immediately ban the node from which the manifest was received.
banscore_out = GetArg("-banscore", 100);
banscore_out = gArgs.GetArg("-banscore", 100);

throw error("CScraperManifest::UnserializeCheck: Too many projects in the manifest.");
}
Expand Down
Loading

0 comments on commit 4976c9b

Please sign in to comment.