Skip to content

Commit

Permalink
Added option 'checkpoint-days'
Browse files Browse the repository at this point in the history
Configure how old blocks must be before we skip script validation
  • Loading branch information
dagurval committed Apr 13, 2016
1 parent c854a39 commit 23e0b01
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageGroup(_("Debugging/Testing options:"));
if (showDebug)
{
strUsage += HelpMessageOpt("-checkpoints", strprintf("Only accept block chain matching built-in checkpoints (default: %u)", 1));
strUsage += HelpMessageOpt("-checkpoints", strprintf("Skip validating scripts for old blocks with valid PoW (default: %u)", 1));
strUsage += HelpMessageOpt("-checkpoint-days", strprintf("Minimum age of blocks (in days) to skip validation for (default: %u)", DEFAULT_CHECKPOINT_DAYS));
strUsage += HelpMessageOpt("-dblogsize=<n>", strprintf("Flush database activity from memory pool to disk log every <n> megabytes (default: %u)", 100));
strUsage += HelpMessageOpt("-disablesafemode", strprintf("Disable safemode, override a real safe mode event (default: %u)", 0));
strUsage += HelpMessageOpt("-testsafemode", strprintf("Force safe mode (default: %u)", 0));
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2152,7 +2152,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
return true;
}

const int64_t timeBarrier = GetTime() - 24 * 3600 * std::max(1, nScriptCheckThreads);
const int64_t timeBarrier = GetTime() - 24 * 3600 * Opt().CheckpointDays();
// Blocks that have varius days of POW behind them makes them secure in
// that actually online nodes checked the scripts, so during initial sync we
// don't need to check the scripts.
Expand Down
5 changes: 5 additions & 0 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ int Opt::ScriptCheckThreads() {
return nScriptCheckThreads;
}

int64_t Opt::CheckpointDays() {
int64_t def = DEFAULT_CHECKPOINT_DAYS * std::max(1, ScriptCheckThreads());
return std::max(int64_t(1), Args->GetArg("-checkpoint-days", def));
}

std::auto_ptr<ArgReset> SetDummyArgGetter(std::auto_ptr<ArgGetter> getter) {
Args.reset(getter.release());
return std::auto_ptr<ArgReset>(new ArgReset);
Expand Down
4 changes: 3 additions & 1 deletion src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ struct Opt {
bool HidePlatform();
std::vector<std::string> UAComment(bool validate = false);
int ScriptCheckThreads();
int64_t CheckpointDays();
};

/** Maximum number of script-checking threads allowed */
static const int MAX_SCRIPTCHECK_THREADS = 16;
/** -par default (number of script-checking threads, 0 = auto) */
static const int DEFAULT_SCRIPTCHECK_THREADS = 0;

// Blocks newer than n days will have their script validated during sync.
static const int DEFAULT_CHECKPOINT_DAYS = 30;

//
// For unit testing
Expand Down
42 changes: 40 additions & 2 deletions src/test/options_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@
#include <boost/test/unit_test.hpp>

#include "options.h"
#include <limits.h>

const int NOT_SET = std::numeric_limits<int>::min();

struct DummyArgGetter : public ArgGetter {

DummyArgGetter() : ArgGetter(),
par(NOT_SET), checkpdays(NOT_SET)
{
}

virtual int64_t GetArg(const std::string& strArg, int64_t nDefault) {
assert(strArg == "-par");
return par;
if (strArg == "-par")
return par == NOT_SET ? nDefault : par;

if (strArg == "-checkpoint-days")
return checkpdays == NOT_SET ? nDefault : checkpdays;

assert(false);
}
virtual std::vector<std::string> GetMultiArgs(const std::string& arg) {
assert(false);
Expand All @@ -18,6 +32,7 @@ struct DummyArgGetter : public ArgGetter {
assert(false);
}
int par;
int checkpdays;
};

BOOST_AUTO_TEST_SUITE(options_tests);
Expand All @@ -38,4 +53,27 @@ BOOST_AUTO_TEST_CASE(scripthreads) {
BOOST_CHECK_EQUAL(3, Opt().ScriptCheckThreads());
}

BOOST_AUTO_TEST_CASE(checkpointdays) {
std::auto_ptr<DummyArgGetter> arg(new DummyArgGetter);
DummyArgGetter* argPtr = arg.get();
std::auto_ptr<ArgReset> argraii
= SetDummyArgGetter(std::auto_ptr<ArgGetter>(arg.release()));

// No multiplier
argPtr->par = 1;
BOOST_CHECK_EQUAL(DEFAULT_CHECKPOINT_DAYS, Opt().CheckpointDays());

// x3 multiplier (3 script threads)
argPtr->par = 3;
BOOST_CHECK_EQUAL(3 * DEFAULT_CHECKPOINT_DAYS, Opt().CheckpointDays());

// Explicitly set days overrides default and multipliers
argPtr->checkpdays = 1;
BOOST_CHECK_EQUAL(1, Opt().CheckpointDays());

// Can't have less than 1 day
argPtr->checkpdays = 0;
BOOST_CHECK_EQUAL(1, Opt().CheckpointDays());
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 23e0b01

Please sign in to comment.