Skip to content

Commit 00820f9

Browse files
committed
Merge pull request #6055
a8cdaf5 checkpoints: move the checkpoints enable boolean into main (Cory Fields) 11982d3 checkpoints: Decouple checkpoints from Params (Cory Fields) 6996823 checkpoints: make checkpoints a member of CChainParams (Cory Fields) 9f13a10 checkpoints: store mapCheckpoints in CCheckpointData rather than a pointer (Cory Fields)
2 parents 40f5e8d + a8cdaf5 commit 00820f9

File tree

12 files changed

+103
-117
lines changed

12 files changed

+103
-117
lines changed

src/chainparams.cpp

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,6 @@ using namespace std;
2626
* timestamp before)
2727
* + Contains no strange transactions
2828
*/
29-
static Checkpoints::MapCheckpoints mapCheckpoints =
30-
boost::assign::map_list_of
31-
( 11111, uint256S("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"))
32-
( 33333, uint256S("0x000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6"))
33-
( 74000, uint256S("0x0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20"))
34-
(105000, uint256S("0x00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97"))
35-
(134444, uint256S("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe"))
36-
(168000, uint256S("0x000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763"))
37-
(193000, uint256S("0x000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317"))
38-
(210000, uint256S("0x000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e"))
39-
(216116, uint256S("0x00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e"))
40-
(225430, uint256S("0x00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932"))
41-
(250000, uint256S("0x000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214"))
42-
(279000, uint256S("0x0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40"))
43-
(295000, uint256S("0x00000000000000004d9b4ef50f0f9d686fd69db2e03af35a100370c64632a983"))
44-
;
45-
static const Checkpoints::CCheckpointData data = {
46-
&mapCheckpoints,
47-
1397080064, // * UNIX timestamp of last checkpoint block
48-
36544669, // * total number of transactions between genesis and last checkpoint
49-
// (the tx=... number in the SetBestChain debug.log lines)
50-
60000.0 // * estimated number of transactions per day after checkpoint
51-
};
52-
53-
static Checkpoints::MapCheckpoints mapCheckpointsTestnet =
54-
boost::assign::map_list_of
55-
( 546, uint256S("000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70"))
56-
;
57-
static const Checkpoints::CCheckpointData dataTestnet = {
58-
&mapCheckpointsTestnet,
59-
1337966069,
60-
1488,
61-
300
62-
};
63-
64-
static Checkpoints::MapCheckpoints mapCheckpointsRegtest =
65-
boost::assign::map_list_of
66-
( 0, uint256S("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"))
67-
;
68-
static const Checkpoints::CCheckpointData dataRegtest = {
69-
&mapCheckpointsRegtest,
70-
0,
71-
0,
72-
0
73-
};
7429

7530
class CMainParams : public CChainParams {
7631
public:
@@ -149,11 +104,27 @@ class CMainParams : public CChainParams {
149104
fRequireStandard = true;
150105
fMineBlocksOnDemand = false;
151106
fTestnetToBeDeprecatedFieldRPC = false;
152-
}
153107

154-
const Checkpoints::CCheckpointData& Checkpoints() const
155-
{
156-
return data;
108+
checkpointData = (Checkpoints::CCheckpointData) {
109+
boost::assign::map_list_of
110+
( 11111, uint256S("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"))
111+
( 33333, uint256S("0x000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6"))
112+
( 74000, uint256S("0x0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20"))
113+
(105000, uint256S("0x00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97"))
114+
(134444, uint256S("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe"))
115+
(168000, uint256S("0x000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763"))
116+
(193000, uint256S("0x000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317"))
117+
(210000, uint256S("0x000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e"))
118+
(216116, uint256S("0x00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e"))
119+
(225430, uint256S("0x00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932"))
120+
(250000, uint256S("0x000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214"))
121+
(279000, uint256S("0x0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40"))
122+
(295000, uint256S("0x00000000000000004d9b4ef50f0f9d686fd69db2e03af35a100370c64632a983")),
123+
1397080064, // * UNIX timestamp of last checkpoint block
124+
36544669, // * total number of transactions between genesis and last checkpoint
125+
// (the tx=... number in the SetBestChain debug.log lines)
126+
60000.0 // * estimated number of transactions per day after checkpoint
127+
};
157128
}
158129
};
159130
static CMainParams mainParams;
@@ -205,10 +176,15 @@ class CTestNetParams : public CMainParams {
205176
fRequireStandard = false;
206177
fMineBlocksOnDemand = false;
207178
fTestnetToBeDeprecatedFieldRPC = true;
208-
}
209-
const Checkpoints::CCheckpointData& Checkpoints() const
210-
{
211-
return dataTestnet;
179+
180+
checkpointData = (Checkpoints::CCheckpointData) {
181+
boost::assign::map_list_of
182+
( 546, uint256S("000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70")),
183+
1337966069,
184+
1488,
185+
300
186+
};
187+
212188
}
213189
};
214190
static CTestNetParams testNetParams;
@@ -247,10 +223,14 @@ class CRegTestParams : public CTestNetParams {
247223
fRequireStandard = false;
248224
fMineBlocksOnDemand = true;
249225
fTestnetToBeDeprecatedFieldRPC = false;
250-
}
251-
const Checkpoints::CCheckpointData& Checkpoints() const
252-
{
253-
return dataRegtest;
226+
227+
checkpointData = (Checkpoints::CCheckpointData){
228+
boost::assign::map_list_of
229+
( 0, uint256S("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206")),
230+
0,
231+
0,
232+
0
233+
};
254234
}
255235
};
256236
static CRegTestParams regTestParams;

src/chainparams.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class CChainParams
7474
const std::vector<CDNSSeedData>& DNSSeeds() const { return vSeeds; }
7575
const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
7676
const std::vector<SeedSpec6>& FixedSeeds() const { return vFixedSeeds; }
77-
virtual const Checkpoints::CCheckpointData& Checkpoints() const = 0;
77+
const Checkpoints::CCheckpointData& Checkpoints() const { return checkpointData; }
7878
protected:
7979
CChainParams() {}
8080

@@ -96,6 +96,7 @@ class CChainParams
9696
bool fRequireStandard;
9797
bool fMineBlocksOnDemand;
9898
bool fTestnetToBeDeprecatedFieldRPC;
99+
Checkpoints::CCheckpointData checkpointData;
99100
};
100101

101102
/**

src/checkpoints.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,17 @@ namespace Checkpoints {
2424
*/
2525
static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
2626

27-
bool fEnabled = true;
28-
29-
bool CheckBlock(int nHeight, const uint256& hash)
27+
bool CheckBlock(const CCheckpointData& data, int nHeight, const uint256& hash)
3028
{
31-
if (!fEnabled)
32-
return true;
33-
34-
const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
29+
const MapCheckpoints& checkpoints = data.mapCheckpoints;
3530

3631
MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
3732
if (i == checkpoints.end()) return true;
3833
return hash == i->second;
3934
}
4035

4136
//! Guess how far we are in the verification process at the given block index
42-
double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks) {
37+
double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex *pindex, bool fSigchecks) {
4338
if (pindex==NULL)
4439
return 0.0;
4540

@@ -51,8 +46,6 @@ namespace Checkpoints {
5146
// Work is defined as: 1.0 per transaction before the last checkpoint, and
5247
// fSigcheckVerificationFactor per transaction after.
5348

54-
const CCheckpointData &data = Params().Checkpoints();
55-
5649
if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
5750
double nCheapBefore = pindex->nChainTx;
5851
double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
@@ -70,22 +63,19 @@ namespace Checkpoints {
7063
return fWorkBefore / (fWorkBefore + fWorkAfter);
7164
}
7265

73-
int GetTotalBlocksEstimate()
66+
int GetTotalBlocksEstimate(const CCheckpointData& data)
7467
{
75-
if (!fEnabled)
76-
return 0;
68+
const MapCheckpoints& checkpoints = data.mapCheckpoints;
7769

78-
const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
70+
if (checkpoints.empty())
71+
return 0;
7972

8073
return checkpoints.rbegin()->first;
8174
}
8275

83-
CBlockIndex* GetLastCheckpoint()
76+
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data)
8477
{
85-
if (!fEnabled)
86-
return NULL;
87-
88-
const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
78+
const MapCheckpoints& checkpoints = data.mapCheckpoints;
8979

9080
BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
9181
{

src/checkpoints.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,22 @@ namespace Checkpoints
2020
typedef std::map<int, uint256> MapCheckpoints;
2121

2222
struct CCheckpointData {
23-
const MapCheckpoints *mapCheckpoints;
23+
MapCheckpoints mapCheckpoints;
2424
int64_t nTimeLastCheckpoint;
2525
int64_t nTransactionsLastCheckpoint;
2626
double fTransactionsPerDay;
2727
};
2828

2929
//! Returns true if block passes checkpoint checks
30-
bool CheckBlock(int nHeight, const uint256& hash);
30+
bool CheckBlock(const CCheckpointData& data, int nHeight, const uint256& hash);
3131

3232
//! Return conservative estimate of total number of blocks, 0 if unknown
33-
int GetTotalBlocksEstimate();
33+
int GetTotalBlocksEstimate(const CCheckpointData& data);
3434

3535
//! Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
36-
CBlockIndex* GetLastCheckpoint();
36+
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data);
3737

38-
double GuessVerificationProgress(CBlockIndex* pindex, bool fSigchecks = true);
39-
40-
extern bool fEnabled;
38+
double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex* pindex, bool fSigchecks = true);
4139

4240
} //namespace Checkpoints
4341

src/init.cpp

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

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

src/main.cpp

Lines changed: 30 additions & 17 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

@@ -1205,8 +1206,11 @@ CAmount GetBlockValue(int nHeight, const CAmount& nFees)
12051206

12061207
bool IsInitialBlockDownload()
12071208
{
1209+
const CChainParams& chainParams = Params();
12081210
LOCK(cs_main);
1209-
if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate())
1211+
if (fImporting || fReindex)
1212+
return true;
1213+
if (fCheckpointsEnabled && chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
12101214
return true;
12111215
static bool lockIBDState = false;
12121216
if (lockIBDState)
@@ -1710,7 +1714,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
17101714
return true;
17111715
}
17121716

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

17151719
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
17161720
// unless those are already completely spent.
@@ -1955,6 +1959,7 @@ void PruneAndFlush() {
19551959

19561960
/** Update chainActive and related internal data structures. */
19571961
void static UpdateTip(CBlockIndex *pindexNew) {
1962+
const CChainParams& chainParams = Params();
19581963
chainActive.SetTip(pindexNew);
19591964

19601965
// New best block
@@ -1964,7 +1969,7 @@ void static UpdateTip(CBlockIndex *pindexNew) {
19641969
LogPrintf("%s: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%u\n", __func__,
19651970
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
19661971
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
1967-
Checkpoints::GuessVerificationProgress(chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize());
1972+
Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize());
19681973

19691974
cvBlockChange.notify_all();
19701975

@@ -2248,6 +2253,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
22482253
bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
22492254
CBlockIndex *pindexNewTip = NULL;
22502255
CBlockIndex *pindexMostWork = NULL;
2256+
const CChainParams& chainParams = Params();
22512257
do {
22522258
boost::this_thread::interruption_point();
22532259

@@ -2272,7 +2278,9 @@ bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
22722278
if (!fInitialDownload) {
22732279
uint256 hashNewTip = pindexNewTip->GetBlockHash();
22742280
// Relay inventory, but don't relay old inventory during initial block download.
2275-
int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate();
2281+
int nBlockEstimate = 0;
2282+
if (fCheckpointsEnabled)
2283+
nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints());
22762284
// Don't relay blocks if pruning -- could cause a peer to try to download, resulting
22772285
// in a stalled download if the block file is pruned before the request.
22782286
if (nLocalServices & NODE_NETWORK) {
@@ -2602,7 +2610,8 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
26022610

26032611
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
26042612
{
2605-
const Consensus::Params& consensusParams = Params().GetConsensus();
2613+
const CChainParams& chainParams = Params();
2614+
const Consensus::Params& consensusParams = chainParams.GetConsensus();
26062615
uint256 hash = block.GetHash();
26072616
if (hash == consensusParams.hashGenesisBlock)
26082617
return true;
@@ -2612,7 +2621,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
26122621
int nHeight = pindexPrev->nHeight+1;
26132622

26142623
// Check proof of work
2615-
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, Params().GetConsensus()))
2624+
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
26162625
return state.DoS(100, error("%s: incorrect proof of work", __func__),
26172626
REJECT_INVALID, "bad-diffbits");
26182627

@@ -2621,25 +2630,28 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
26212630
return state.Invalid(error("%s: block's timestamp is too early", __func__),
26222631
REJECT_INVALID, "time-too-old");
26232632

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

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

26342646
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
2635-
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, Params().RejectBlockOutdatedMajority()))
2647+
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated))
26362648
{
26372649
return state.Invalid(error("%s: rejected nVersion=1 block", __func__),
26382650
REJECT_OBSOLETE, "bad-version");
26392651
}
26402652

26412653
// Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded:
2642-
if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, Params().RejectBlockOutdatedMajority()))
2654+
if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated))
26432655
{
26442656
return state.Invalid(error("%s : rejected nVersion=2 block", __func__),
26452657
REJECT_OBSOLETE, "bad-version");
@@ -3026,6 +3038,7 @@ CBlockIndex * InsertBlockIndex(uint256 hash)
30263038

30273039
bool static LoadBlockIndexDB()
30283040
{
3041+
const CChainParams& chainparams = Params();
30293042
if (!pblocktree->LoadBlockIndexGuts())
30303043
return false;
30313044

@@ -3128,7 +3141,7 @@ bool static LoadBlockIndexDB()
31283141
LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
31293142
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
31303143
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
3131-
Checkpoints::GuessVerificationProgress(chainActive.Tip()));
3144+
Checkpoints::GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
31323145

31333146
return true;
31343147
}

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

0 commit comments

Comments
 (0)