Skip to content

Commit 11982d3

Browse files
committed
checkpoints: Decouple checkpoints from Params
Pass checkpoint data in as necessary
1 parent 6996823 commit 11982d3

File tree

7 files changed

+43
-37
lines changed

7 files changed

+43
-37
lines changed

src/checkpoints.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ namespace Checkpoints {
2525

2626
bool fEnabled = true;
2727

28-
bool CheckBlock(int nHeight, const uint256& hash)
28+
bool CheckBlock(const CCheckpointData& data, int nHeight, const uint256& hash)
2929
{
3030
if (!fEnabled)
3131
return true;
3232

33-
const MapCheckpoints& checkpoints = Params().Checkpoints().mapCheckpoints;
33+
const MapCheckpoints& checkpoints = data.mapCheckpoints;
3434

3535
MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
3636
if (i == checkpoints.end()) return true;
3737
return hash == i->second;
3838
}
3939

4040
//! Guess how far we are in the verification process at the given block index
41-
double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks) {
41+
double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex *pindex, bool fSigchecks) {
4242
if (pindex==NULL)
4343
return 0.0;
4444

@@ -50,8 +50,6 @@ namespace Checkpoints {
5050
// Work is defined as: 1.0 per transaction before the last checkpoint, and
5151
// fSigcheckVerificationFactor per transaction after.
5252

53-
const CCheckpointData &data = Params().Checkpoints();
54-
5553
if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
5654
double nCheapBefore = pindex->nChainTx;
5755
double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
@@ -69,22 +67,22 @@ namespace Checkpoints {
6967
return fWorkBefore / (fWorkBefore + fWorkAfter);
7068
}
7169

72-
int GetTotalBlocksEstimate()
70+
int GetTotalBlocksEstimate(const CCheckpointData& data)
7371
{
7472
if (!fEnabled)
7573
return 0;
7674

77-
const MapCheckpoints& checkpoints = Params().Checkpoints().mapCheckpoints;
75+
const MapCheckpoints& checkpoints = data.mapCheckpoints;
7876

7977
return checkpoints.rbegin()->first;
8078
}
8179

82-
CBlockIndex* GetLastCheckpoint()
80+
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data)
8381
{
8482
if (!fEnabled)
8583
return NULL;
8684

87-
const MapCheckpoints& checkpoints = Params().Checkpoints().mapCheckpoints;
85+
const MapCheckpoints& checkpoints = data.mapCheckpoints;
8886

8987
BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
9088
{

src/checkpoints.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ struct CCheckpointData {
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);
38+
double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex* pindex, bool fSigchecks = true);
3939

4040
extern bool fEnabled;
4141

src/main.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,8 +1204,9 @@ CAmount GetBlockValue(int nHeight, const CAmount& nFees)
12041204

12051205
bool IsInitialBlockDownload()
12061206
{
1207+
const CChainParams& chainParams = Params();
12071208
LOCK(cs_main);
1208-
if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate())
1209+
if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
12091210
return true;
12101211
static bool lockIBDState = false;
12111212
if (lockIBDState)
@@ -1709,7 +1710,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
17091710
return true;
17101711
}
17111712

1712-
bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate();
1713+
bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate(chainparams.Checkpoints());
17131714

17141715
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
17151716
// unless those are already completely spent.
@@ -1954,6 +1955,7 @@ void PruneAndFlush() {
19541955

19551956
/** Update chainActive and related internal data structures. */
19561957
void static UpdateTip(CBlockIndex *pindexNew) {
1958+
const CChainParams& chainParams = Params();
19571959
chainActive.SetTip(pindexNew);
19581960

19591961
// New best block
@@ -1963,7 +1965,7 @@ void static UpdateTip(CBlockIndex *pindexNew) {
19631965
LogPrintf("%s: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%u\n", __func__,
19641966
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
19651967
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
1966-
Checkpoints::GuessVerificationProgress(chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize());
1968+
Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize());
19671969

19681970
cvBlockChange.notify_all();
19691971

@@ -2247,6 +2249,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
22472249
bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
22482250
CBlockIndex *pindexNewTip = NULL;
22492251
CBlockIndex *pindexMostWork = NULL;
2252+
const CChainParams& chainParams = Params();
22502253
do {
22512254
boost::this_thread::interruption_point();
22522255

@@ -2271,7 +2274,7 @@ bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
22712274
if (!fInitialDownload) {
22722275
uint256 hashNewTip = pindexNewTip->GetBlockHash();
22732276
// Relay inventory, but don't relay old inventory during initial block download.
2274-
int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate();
2277+
int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints());
22752278
// Don't relay blocks if pruning -- could cause a peer to try to download, resulting
22762279
// in a stalled download if the block file is pruned before the request.
22772280
if (nLocalServices & NODE_NETWORK) {
@@ -2601,7 +2604,8 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
26012604

26022605
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
26032606
{
2604-
const Consensus::Params& consensusParams = Params().GetConsensus();
2607+
const CChainParams& chainParams = Params();
2608+
const Consensus::Params& consensusParams = chainParams.GetConsensus();
26052609
uint256 hash = block.GetHash();
26062610
if (hash == consensusParams.hashGenesisBlock)
26072611
return true;
@@ -2611,7 +2615,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
26112615
int nHeight = pindexPrev->nHeight+1;
26122616

26132617
// Check proof of work
2614-
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, Params().GetConsensus()))
2618+
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
26152619
return state.DoS(100, error("%s: incorrect proof of work", __func__),
26162620
REJECT_INVALID, "bad-diffbits");
26172621

@@ -2621,24 +2625,24 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
26212625
REJECT_INVALID, "time-too-old");
26222626

26232627
// Check that the block chain matches the known block chain up to a checkpoint
2624-
if (!Checkpoints::CheckBlock(nHeight, hash))
2628+
if (!Checkpoints::CheckBlock(chainParams.Checkpoints(), nHeight, hash))
26252629
return state.DoS(100, error("%s: rejected by checkpoint lock-in at %d", __func__, nHeight),
26262630
REJECT_CHECKPOINT, "checkpoint mismatch");
26272631

26282632
// Don't accept any forks from the main chain prior to last checkpoint
2629-
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint();
2633+
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainParams.Checkpoints());
26302634
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
26312635
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
26322636

26332637
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
2634-
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, Params().RejectBlockOutdatedMajority()))
2638+
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated))
26352639
{
26362640
return state.Invalid(error("%s: rejected nVersion=1 block", __func__),
26372641
REJECT_OBSOLETE, "bad-version");
26382642
}
26392643

26402644
// Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded:
2641-
if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, Params().RejectBlockOutdatedMajority()))
2645+
if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated))
26422646
{
26432647
return state.Invalid(error("%s : rejected nVersion=2 block", __func__),
26442648
REJECT_OBSOLETE, "bad-version");
@@ -3025,6 +3029,7 @@ CBlockIndex * InsertBlockIndex(uint256 hash)
30253029

30263030
bool static LoadBlockIndexDB()
30273031
{
3032+
const CChainParams& chainparams = Params();
30283033
if (!pblocktree->LoadBlockIndexGuts())
30293034
return false;
30303035

@@ -3127,7 +3132,7 @@ bool static LoadBlockIndexDB()
31273132
LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
31283133
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
31293134
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
3130-
Checkpoints::GuessVerificationProgress(chainActive.Tip()));
3135+
Checkpoints::GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
31313136

31323137
return true;
31333138
}

src/qt/clientmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ QDateTime ClientModel::getLastBlockDate() const
8989
double ClientModel::getVerificationProgress() const
9090
{
9191
LOCK(cs_main);
92-
return Checkpoints::GuessVerificationProgress(chainActive.Tip());
92+
return Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip());
9393
}
9494

9595
void ClientModel::updateTimer()

src/rpcblockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ Value getblockchaininfo(const Array& params, bool fHelp)
490490
obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
491491
obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
492492
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
493-
obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(chainActive.Tip())));
493+
obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip())));
494494
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
495495
return obj;
496496
}

src/test/Checkpoints_tests.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "uint256.h"
1212
#include "test/test_bitcoin.h"
13+
#include "chainparams.h"
1314

1415
#include <boost/test/unit_test.hpp>
1516

@@ -19,21 +20,22 @@ BOOST_FIXTURE_TEST_SUITE(Checkpoints_tests, BasicTestingSetup)
1920

2021
BOOST_AUTO_TEST_CASE(sanity)
2122
{
23+
const Checkpoints::CCheckpointData& checkpoints = Params(CBaseChainParams::MAIN).Checkpoints();
2224
uint256 p11111 = uint256S("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d");
2325
uint256 p134444 = uint256S("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe");
24-
BOOST_CHECK(Checkpoints::CheckBlock(11111, p11111));
25-
BOOST_CHECK(Checkpoints::CheckBlock(134444, p134444));
26+
BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 11111, p11111));
27+
BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 134444, p134444));
2628

2729

2830
// Wrong hashes at checkpoints should fail:
29-
BOOST_CHECK(!Checkpoints::CheckBlock(11111, p134444));
30-
BOOST_CHECK(!Checkpoints::CheckBlock(134444, p11111));
31+
BOOST_CHECK(!Checkpoints::CheckBlock(checkpoints, 11111, p134444));
32+
BOOST_CHECK(!Checkpoints::CheckBlock(checkpoints, 134444, p11111));
3133

3234
// ... but any hash not at a checkpoint should succeed:
33-
BOOST_CHECK(Checkpoints::CheckBlock(11111+1, p134444));
34-
BOOST_CHECK(Checkpoints::CheckBlock(134444+1, p11111));
35+
BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 11111+1, p134444));
36+
BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 134444+1, p11111));
3537

36-
BOOST_CHECK(Checkpoints::GetTotalBlocksEstimate() >= 134444);
38+
BOOST_CHECK(Checkpoints::GetTotalBlocksEstimate(checkpoints) >= 134444);
3739
}
3840

3941
BOOST_AUTO_TEST_SUITE_END()

src/wallet/wallet.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
10591059
{
10601060
int ret = 0;
10611061
int64_t nNow = GetTime();
1062+
const CChainParams& chainParams = Params();
10621063

10631064
CBlockIndex* pindex = pindexStart;
10641065
{
@@ -1070,12 +1071,12 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
10701071
pindex = chainActive.Next(pindex);
10711072

10721073
ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
1073-
double dProgressStart = Checkpoints::GuessVerificationProgress(pindex, false);
1074-
double dProgressTip = Checkpoints::GuessVerificationProgress(chainActive.Tip(), false);
1074+
double dProgressStart = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false);
1075+
double dProgressTip = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip(), false);
10751076
while (pindex)
10761077
{
10771078
if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
1078-
ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
1079+
ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
10791080

10801081
CBlock block;
10811082
ReadBlockFromDisk(block, pindex);
@@ -1087,7 +1088,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
10871088
pindex = chainActive.Next(pindex);
10881089
if (GetTime() >= nNow + 60) {
10891090
nNow = GetTime();
1090-
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, Checkpoints::GuessVerificationProgress(pindex));
1091+
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex));
10911092
}
10921093
}
10931094
ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI

0 commit comments

Comments
 (0)