Skip to content

Commit

Permalink
Add -checkpoints-file flag to defid (#1347)
Browse files Browse the repository at this point in the history
* Add -checkpoints-file flag to defid

* Fix lint: use ParseInt32 and add -checkpoints-file to SET_DOC_OPTIONAL

* Accepts comments and empty line in checkpoints file

* Generic file error message

* Trim lines

* Move trim_ws from masternodes/token to strencodings
  • Loading branch information
Jouzo authored and Bushstar committed Jul 5, 2022
1 parent fb890f4 commit f0f6649
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 16 deletions.
37 changes: 37 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.

#include <core_io.h>

#include <chainparams.h>
#include <chainparamsseeds.h>
#include <consensus/merkle.h>
Expand Down Expand Up @@ -1076,3 +1078,38 @@ void SelectParams(const std::string& network)
void ClearCheckpoints(CChainParams &params) {
params.checkpointData = {};
}

Res UpdateCheckpointsFromFile(CChainParams &params, const std::string &fileName) {
std::ifstream file(fileName);
if (!file.good()) {
return Res::Err("Could not read %s. Ensure it exists and has read permissions", fileName);
}

ClearCheckpoints(params);

std::string line;
while (std::getline(file, line)) {
auto trimmed = trim_ws(line);
if (trimmed.rfind('#', 0) == 0 || trimmed.find_first_not_of(" \n\r\t") == std::string::npos)
continue;

std::istringstream iss(trimmed);
std::string hashStr, heightStr;
if (!(iss >> heightStr >> hashStr)) {
return Res::Err("Error parsing line %s", trimmed);
}

uint256 hash;
if (!ParseHashStr(hashStr, hash)) {
return Res::Err("Invalid hash: %s", hashStr);
}

int32_t height;
if (!ParseInt32(heightStr, &height)) {
return Res::Err("Invalid height: %s", heightStr);
}

params.checkpointData.mapCheckpoints[height] = hash;
}
return Res::Ok();
}
1 change: 1 addition & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class CChainParams
std::set<CKeyID> genesisTeam;

friend void ClearCheckpoints(CChainParams &params);
friend Res UpdateCheckpointsFromFile(CChainParams &params, const std::string &fileName);
};

const auto SMART_CONTRACT_DFIP_2201 = "DFIP2201";
Expand Down
11 changes: 10 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ void SetupServerArgs()
std::vector<std::string> hidden_args = {
"-dbcrashratio", "-forcecompactdb",
"-interrupt-block=<hash|height>", "-stop-block=<hash|height>",
"-mocknet", "-mocknet-blocktime=<secs>", "-mocknet-key=<pubkey>"
"-mocknet", "-mocknet-blocktime=<secs>", "-mocknet-key=<pubkey>",
"-checkpoints-file",
// GUI args. These will be overwritten by SetupUIArgs for the GUI
"-choosedatadir", "-lang=<lang>", "-min", "-resetguisettings", "-splash"};

Expand Down Expand Up @@ -1120,6 +1121,14 @@ bool AppInitParameterInteraction()
mempool.setSanityCheck(1.0 / ratio);
}
fCheckBlockIndex = gArgs.GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks());

auto checkpoints_file = gArgs.GetArg("-checkpoints-file", "");
if (!checkpoints_file.empty()) {
auto res = UpdateCheckpointsFromFile(const_cast<CChainParams&>(chainparams), checkpoints_file);
if (!res)
return InitError(strprintf(_("Error in checkpoints file : %s").translated, res.msg));
}

if (!gArgs.GetBoolArg("-checkpoints", DEFAULT_CHECKPOINTS_ENABLED)) {
LogPrintf("conf: checkpoints disabled.\n");
// Safe to const_cast, as we know it's always allocated, and is always in the global var
Expand Down
13 changes: 1 addition & 12 deletions src/masternodes/tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,14 @@
#include <chainparams.h> // Params()
#include <core_io.h>
#include <primitives/transaction.h>
#include <util/strencodings.h>

#include <univalue.h>

const DCT_ID CTokensView::DCT_ID_START = DCT_ID{128};

extern const std::string CURRENCY_UNIT;

std::string trim_ws(std::string const & str)
{
std::string const ws = " \n\r\t";
size_t first = str.find_first_not_of(ws);
if (std::string::npos == first)
{
return str;
}
size_t last = str.find_last_not_of(ws);
return str.substr(first, (last - first + 1));
}

std::optional<CTokensView::CTokenImpl> CTokensView::GetToken(DCT_ID id) const
{
return ReadBy<ID, CTokenImpl>(id);
Expand Down
2 changes: 0 additions & 2 deletions src/masternodes/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
class CTransaction;
class UniValue;

std::string trim_ws(std::string const & str);

class CToken
{
public:
Expand Down
12 changes: 12 additions & 0 deletions src/util/strencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,15 @@ std::string Capitalize(std::string str)
str[0] = ToUpper(str.front());
return str;
}

std::string trim_ws(std::string const & str)
{
std::string const ws = " \n\r\t";
size_t first = str.find_first_not_of(ws);
if (std::string::npos == first)
{
return str;
}
size_t last = str.find_last_not_of(ws);
return str.substr(first, (last - first + 1));
}
1 change: 1 addition & 0 deletions src/util/strencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT
std::vector<unsigned char> ParseHex(const char* psz);
std::vector<unsigned char> ParseHex(const std::string& str);
signed char HexDigit(char c);
std::string trim_ws(std::string const & str);
/* Returns true if each character in str is a hex character, and has an even
* number of hex digits.*/
bool IsHex(const std::string& str);
Expand Down
2 changes: 1 addition & 1 deletion test/lint/check-doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
CMD_GREP_WALLET_HIDDEN_ARGS = r"git grep --function-context 'void DummyWalletInit::AddWalletOptions' -- {}".format(CMD_ROOT_DIR)
CMD_GREP_DOCS = r"git grep --perl-regexp '{}' {}".format(REGEX_DOC, CMD_ROOT_DIR)
# list unsupported, deprecated and duplicate args as they need no documentation
SET_DOC_OPTIONAL = set(['-h', '-help', '-dbcrashratio', '-forcecompactdb', '-interrupt-block', '-stop-block', '-mocknet', '-mocknet-key', '-mocknet-blocktime'])
SET_DOC_OPTIONAL = set(['-h', '-help', '-dbcrashratio', '-forcecompactdb', '-interrupt-block', '-stop-block', '-mocknet', '-mocknet-key', '-mocknet-blocktime', '-checkpoints-file'])


def lint_missing_argument_documentation():
Expand Down

0 comments on commit f0f6649

Please sign in to comment.