Skip to content

Commit a8cdaf5

Browse files
committed
checkpoints: move the checkpoints enable boolean into main
This pertains to app-state, so it doesn't make sense to handle inside the checkpoint functions.
1 parent 11982d3 commit a8cdaf5

File tree

6 files changed

+26
-27
lines changed

6 files changed

+26
-27
lines changed

src/checkpoints.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,8 @@ namespace Checkpoints {
2323
*/
2424
static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
2525

26-
bool fEnabled = true;
27-
2826
bool CheckBlock(const CCheckpointData& data, int nHeight, const uint256& hash)
2927
{
30-
if (!fEnabled)
31-
return true;
32-
3328
const MapCheckpoints& checkpoints = data.mapCheckpoints;
3429

3530
MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
@@ -69,19 +64,16 @@ namespace Checkpoints {
6964

7065
int GetTotalBlocksEstimate(const CCheckpointData& data)
7166
{
72-
if (!fEnabled)
73-
return 0;
74-
7567
const MapCheckpoints& checkpoints = data.mapCheckpoints;
7668

69+
if (checkpoints.empty())
70+
return 0;
71+
7772
return checkpoints.rbegin()->first;
7873
}
7974

8075
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data)
8176
{
82-
if (!fEnabled)
83-
return NULL;
84-
8577
const MapCheckpoints& checkpoints = data.mapCheckpoints;
8678

8779
BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)

src/checkpoints.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ CBlockIndex* GetLastCheckpoint(const CCheckpointData& data);
3737

3838
double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex* pindex, bool fSigchecks = true);
3939

40-
extern bool fEnabled;
41-
4240
} //namespace Checkpoints
4341

4442
#endif // BITCOIN_CHECKPOINTS_H

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ bool AppInit2(boost::thread_group& threadGroup)
741741
// Checkmempool and checkblockindex default to true in regtest mode
742742
mempool.setSanityCheck(GetBoolArg("-checkmempool", chainparams.DefaultConsistencyChecks()));
743743
fCheckBlockIndex = GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks());
744-
Checkpoints::fEnabled = GetBoolArg("-checkpoints", true);
744+
fCheckpointsEnabled = GetBoolArg("-checkpoints", true);
745745

746746
// -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency
747747
nScriptCheckThreads = GetArg("-par", DEFAULT_SCRIPTCHECK_THREADS);

src/main.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ bool fHavePruned = false;
5656
bool fPruneMode = false;
5757
bool fIsBareMultisigStd = true;
5858
bool fCheckBlockIndex = false;
59+
bool fCheckpointsEnabled = true;
5960
unsigned int nCoinCacheSize = 5000;
6061
uint64_t nPruneTarget = 0;
6162

@@ -1206,7 +1207,9 @@ bool IsInitialBlockDownload()
12061207
{
12071208
const CChainParams& chainParams = Params();
12081209
LOCK(cs_main);
1209-
if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
1210+
if (fImporting || fReindex)
1211+
return true;
1212+
if (fCheckpointsEnabled && chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
12101213
return true;
12111214
static bool lockIBDState = false;
12121215
if (lockIBDState)
@@ -1710,7 +1713,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
17101713
return true;
17111714
}
17121715

1713-
bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate(chainparams.Checkpoints());
1716+
bool fScriptChecks = (!fCheckpointsEnabled || pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate(chainparams.Checkpoints()));
17141717

17151718
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
17161719
// unless those are already completely spent.
@@ -2274,7 +2277,9 @@ bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
22742277
if (!fInitialDownload) {
22752278
uint256 hashNewTip = pindexNewTip->GetBlockHash();
22762279
// Relay inventory, but don't relay old inventory during initial block download.
2277-
int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints());
2280+
int nBlockEstimate = 0;
2281+
if (fCheckpointsEnabled)
2282+
nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints());
22782283
// Don't relay blocks if pruning -- could cause a peer to try to download, resulting
22792284
// in a stalled download if the block file is pruned before the request.
22802285
if (nLocalServices & NODE_NETWORK) {
@@ -2624,15 +2629,18 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
26242629
return state.Invalid(error("%s: block's timestamp is too early", __func__),
26252630
REJECT_INVALID, "time-too-old");
26262631

2627-
// Check that the block chain matches the known block chain up to a checkpoint
2628-
if (!Checkpoints::CheckBlock(chainParams.Checkpoints(), nHeight, hash))
2629-
return state.DoS(100, error("%s: rejected by checkpoint lock-in at %d", __func__, nHeight),
2630-
REJECT_CHECKPOINT, "checkpoint mismatch");
2632+
if(fCheckpointsEnabled)
2633+
{
2634+
// Check that the block chain matches the known block chain up to a checkpoint
2635+
if (!Checkpoints::CheckBlock(chainParams.Checkpoints(), nHeight, hash))
2636+
return state.DoS(100, error("%s: rejected by checkpoint lock-in at %d", __func__, nHeight),
2637+
REJECT_CHECKPOINT, "checkpoint mismatch");
26312638

2632-
// Don't accept any forks from the main chain prior to last checkpoint
2633-
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainParams.Checkpoints());
2634-
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2635-
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
2639+
// Don't accept any forks from the main chain prior to last checkpoint
2640+
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainParams.Checkpoints());
2641+
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2642+
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
2643+
}
26362644

26372645
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
26382646
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated))

src/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ extern int nScriptCheckThreads;
118118
extern bool fTxIndex;
119119
extern bool fIsBareMultisigStd;
120120
extern bool fCheckBlockIndex;
121+
extern bool fCheckpointsEnabled;
121122
extern unsigned int nCoinCacheSize;
122123
extern CFeeRate minRelayTxFee;
123124

src/test/miner_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
5959
uint256 hash;
6060

6161
LOCK(cs_main);
62-
Checkpoints::fEnabled = false;
62+
fCheckpointsEnabled = false;
6363

6464
// Simple block creation, nothing special yet:
6565
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
@@ -262,7 +262,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
262262
BOOST_FOREACH(CTransaction *tx, txFirst)
263263
delete tx;
264264

265-
Checkpoints::fEnabled = true;
265+
fCheckpointsEnabled = true;
266266
}
267267

268268
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)