Browse Source

checkpoints: Decouple checkpoints from Params

Pass checkpoint data in as necessary
0.13
Cory Fields 10 years ago
parent
commit
11982d366d
  1. 16
      src/checkpoints.cpp
  2. 8
      src/checkpoints.h
  3. 27
      src/main.cpp
  4. 2
      src/qt/clientmodel.cpp
  5. 2
      src/rpcblockchain.cpp
  6. 16
      src/test/Checkpoints_tests.cpp
  7. 9
      src/wallet/wallet.cpp

16
src/checkpoints.cpp

@ -25,12 +25,12 @@ namespace Checkpoints {
bool fEnabled = true; bool fEnabled = true;
bool CheckBlock(int nHeight, const uint256& hash) bool CheckBlock(const CCheckpointData& data, int nHeight, const uint256& hash)
{ {
if (!fEnabled) if (!fEnabled)
return true; return true;
const MapCheckpoints& checkpoints = Params().Checkpoints().mapCheckpoints; const MapCheckpoints& checkpoints = data.mapCheckpoints;
MapCheckpoints::const_iterator i = checkpoints.find(nHeight); MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
if (i == checkpoints.end()) return true; if (i == checkpoints.end()) return true;
@ -38,7 +38,7 @@ namespace Checkpoints {
} }
//! Guess how far we are in the verification process at the given block index //! Guess how far we are in the verification process at the given block index
double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks) { double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex *pindex, bool fSigchecks) {
if (pindex==NULL) if (pindex==NULL)
return 0.0; return 0.0;
@ -50,8 +50,6 @@ namespace Checkpoints {
// Work is defined as: 1.0 per transaction before the last checkpoint, and // Work is defined as: 1.0 per transaction before the last checkpoint, and
// fSigcheckVerificationFactor per transaction after. // fSigcheckVerificationFactor per transaction after.
const CCheckpointData &data = Params().Checkpoints();
if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) { if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
double nCheapBefore = pindex->nChainTx; double nCheapBefore = pindex->nChainTx;
double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx; double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
@ -69,22 +67,22 @@ namespace Checkpoints {
return fWorkBefore / (fWorkBefore + fWorkAfter); return fWorkBefore / (fWorkBefore + fWorkAfter);
} }
int GetTotalBlocksEstimate() int GetTotalBlocksEstimate(const CCheckpointData& data)
{ {
if (!fEnabled) if (!fEnabled)
return 0; return 0;
const MapCheckpoints& checkpoints = Params().Checkpoints().mapCheckpoints; const MapCheckpoints& checkpoints = data.mapCheckpoints;
return checkpoints.rbegin()->first; return checkpoints.rbegin()->first;
} }
CBlockIndex* GetLastCheckpoint() CBlockIndex* GetLastCheckpoint(const CCheckpointData& data)
{ {
if (!fEnabled) if (!fEnabled)
return NULL; return NULL;
const MapCheckpoints& checkpoints = Params().Checkpoints().mapCheckpoints; const MapCheckpoints& checkpoints = data.mapCheckpoints;
BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints) BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
{ {

8
src/checkpoints.h

@ -27,15 +27,15 @@ struct CCheckpointData {
}; };
//! Returns true if block passes checkpoint checks //! Returns true if block passes checkpoint checks
bool CheckBlock(int nHeight, const uint256& hash); bool CheckBlock(const CCheckpointData& data, int nHeight, const uint256& hash);
//! Return conservative estimate of total number of blocks, 0 if unknown //! Return conservative estimate of total number of blocks, 0 if unknown
int GetTotalBlocksEstimate(); int GetTotalBlocksEstimate(const CCheckpointData& data);
//! Returns last CBlockIndex* in mapBlockIndex that is a checkpoint //! Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
CBlockIndex* GetLastCheckpoint(); CBlockIndex* GetLastCheckpoint(const CCheckpointData& data);
double GuessVerificationProgress(CBlockIndex* pindex, bool fSigchecks = true); double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex* pindex, bool fSigchecks = true);
extern bool fEnabled; extern bool fEnabled;

27
src/main.cpp

@ -1204,8 +1204,9 @@ CAmount GetBlockValue(int nHeight, const CAmount& nFees)
bool IsInitialBlockDownload() bool IsInitialBlockDownload()
{ {
const CChainParams& chainParams = Params();
LOCK(cs_main); LOCK(cs_main);
if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate()) if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
return true; return true;
static bool lockIBDState = false; static bool lockIBDState = false;
if (lockIBDState) if (lockIBDState)
@ -1709,7 +1710,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
return true; return true;
} }
bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate(); bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate(chainparams.Checkpoints());
// Do not allow blocks that contain transactions which 'overwrite' older transactions, // Do not allow blocks that contain transactions which 'overwrite' older transactions,
// unless those are already completely spent. // unless those are already completely spent.
@ -1954,6 +1955,7 @@ void PruneAndFlush() {
/** Update chainActive and related internal data structures. */ /** Update chainActive and related internal data structures. */
void static UpdateTip(CBlockIndex *pindexNew) { void static UpdateTip(CBlockIndex *pindexNew) {
const CChainParams& chainParams = Params();
chainActive.SetTip(pindexNew); chainActive.SetTip(pindexNew);
// New best block // New best block
@ -1963,7 +1965,7 @@ void static UpdateTip(CBlockIndex *pindexNew) {
LogPrintf("%s: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%u\n", __func__, LogPrintf("%s: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%u\n", __func__,
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx, chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()), DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
Checkpoints::GuessVerificationProgress(chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize()); Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize());
cvBlockChange.notify_all(); cvBlockChange.notify_all();
@ -2247,6 +2249,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
bool ActivateBestChain(CValidationState &state, CBlock *pblock) { bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
CBlockIndex *pindexNewTip = NULL; CBlockIndex *pindexNewTip = NULL;
CBlockIndex *pindexMostWork = NULL; CBlockIndex *pindexMostWork = NULL;
const CChainParams& chainParams = Params();
do { do {
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
@ -2271,7 +2274,7 @@ bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
if (!fInitialDownload) { if (!fInitialDownload) {
uint256 hashNewTip = pindexNewTip->GetBlockHash(); uint256 hashNewTip = pindexNewTip->GetBlockHash();
// Relay inventory, but don't relay old inventory during initial block download. // Relay inventory, but don't relay old inventory during initial block download.
int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(); int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints());
// Don't relay blocks if pruning -- could cause a peer to try to download, resulting // Don't relay blocks if pruning -- could cause a peer to try to download, resulting
// in a stalled download if the block file is pruned before the request. // in a stalled download if the block file is pruned before the request.
if (nLocalServices & NODE_NETWORK) { if (nLocalServices & NODE_NETWORK) {
@ -2601,7 +2604,8 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev) bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
{ {
const Consensus::Params& consensusParams = Params().GetConsensus(); const CChainParams& chainParams = Params();
const Consensus::Params& consensusParams = chainParams.GetConsensus();
uint256 hash = block.GetHash(); uint256 hash = block.GetHash();
if (hash == consensusParams.hashGenesisBlock) if (hash == consensusParams.hashGenesisBlock)
return true; return true;
@ -2611,7 +2615,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
int nHeight = pindexPrev->nHeight+1; int nHeight = pindexPrev->nHeight+1;
// Check proof of work // Check proof of work
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, Params().GetConsensus())) if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
return state.DoS(100, error("%s: incorrect proof of work", __func__), return state.DoS(100, error("%s: incorrect proof of work", __func__),
REJECT_INVALID, "bad-diffbits"); REJECT_INVALID, "bad-diffbits");
@ -2621,24 +2625,24 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
REJECT_INVALID, "time-too-old"); REJECT_INVALID, "time-too-old");
// Check that the block chain matches the known block chain up to a checkpoint // Check that the block chain matches the known block chain up to a checkpoint
if (!Checkpoints::CheckBlock(nHeight, hash)) if (!Checkpoints::CheckBlock(chainParams.Checkpoints(), nHeight, hash))
return state.DoS(100, error("%s: rejected by checkpoint lock-in at %d", __func__, nHeight), return state.DoS(100, error("%s: rejected by checkpoint lock-in at %d", __func__, nHeight),
REJECT_CHECKPOINT, "checkpoint mismatch"); REJECT_CHECKPOINT, "checkpoint mismatch");
// Don't accept any forks from the main chain prior to last checkpoint // Don't accept any forks from the main chain prior to last checkpoint
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(); CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainParams.Checkpoints());
if (pcheckpoint && nHeight < pcheckpoint->nHeight) if (pcheckpoint && nHeight < pcheckpoint->nHeight)
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight)); return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded: // Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, Params().RejectBlockOutdatedMajority())) if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated))
{ {
return state.Invalid(error("%s: rejected nVersion=1 block", __func__), return state.Invalid(error("%s: rejected nVersion=1 block", __func__),
REJECT_OBSOLETE, "bad-version"); REJECT_OBSOLETE, "bad-version");
} }
// Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded: // Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded:
if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, Params().RejectBlockOutdatedMajority())) if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated))
{ {
return state.Invalid(error("%s : rejected nVersion=2 block", __func__), return state.Invalid(error("%s : rejected nVersion=2 block", __func__),
REJECT_OBSOLETE, "bad-version"); REJECT_OBSOLETE, "bad-version");
@ -3025,6 +3029,7 @@ CBlockIndex * InsertBlockIndex(uint256 hash)
bool static LoadBlockIndexDB() bool static LoadBlockIndexDB()
{ {
const CChainParams& chainparams = Params();
if (!pblocktree->LoadBlockIndexGuts()) if (!pblocktree->LoadBlockIndexGuts())
return false; return false;
@ -3127,7 +3132,7 @@ bool static LoadBlockIndexDB()
LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__, LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()), DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
Checkpoints::GuessVerificationProgress(chainActive.Tip())); Checkpoints::GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
return true; return true;
} }

2
src/qt/clientmodel.cpp

@ -89,7 +89,7 @@ QDateTime ClientModel::getLastBlockDate() const
double ClientModel::getVerificationProgress() const double ClientModel::getVerificationProgress() const
{ {
LOCK(cs_main); LOCK(cs_main);
return Checkpoints::GuessVerificationProgress(chainActive.Tip()); return Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip());
} }
void ClientModel::updateTimer() void ClientModel::updateTimer()

2
src/rpcblockchain.cpp

@ -490,7 +490,7 @@ Value getblockchaininfo(const Array& params, bool fHelp)
obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1)); obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex())); obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
obj.push_back(Pair("difficulty", (double)GetDifficulty())); obj.push_back(Pair("difficulty", (double)GetDifficulty()));
obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(chainActive.Tip()))); obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip())));
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex())); obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
return obj; return obj;
} }

16
src/test/Checkpoints_tests.cpp

@ -10,6 +10,7 @@
#include "uint256.h" #include "uint256.h"
#include "test/test_bitcoin.h" #include "test/test_bitcoin.h"
#include "chainparams.h"
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
@ -19,21 +20,22 @@ BOOST_FIXTURE_TEST_SUITE(Checkpoints_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(sanity) BOOST_AUTO_TEST_CASE(sanity)
{ {
const Checkpoints::CCheckpointData& checkpoints = Params(CBaseChainParams::MAIN).Checkpoints();
uint256 p11111 = uint256S("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"); uint256 p11111 = uint256S("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d");
uint256 p134444 = uint256S("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe"); uint256 p134444 = uint256S("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe");
BOOST_CHECK(Checkpoints::CheckBlock(11111, p11111)); BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 11111, p11111));
BOOST_CHECK(Checkpoints::CheckBlock(134444, p134444)); BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 134444, p134444));
// Wrong hashes at checkpoints should fail: // Wrong hashes at checkpoints should fail:
BOOST_CHECK(!Checkpoints::CheckBlock(11111, p134444)); BOOST_CHECK(!Checkpoints::CheckBlock(checkpoints, 11111, p134444));
BOOST_CHECK(!Checkpoints::CheckBlock(134444, p11111)); BOOST_CHECK(!Checkpoints::CheckBlock(checkpoints, 134444, p11111));
// ... but any hash not at a checkpoint should succeed: // ... but any hash not at a checkpoint should succeed:
BOOST_CHECK(Checkpoints::CheckBlock(11111+1, p134444)); BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 11111+1, p134444));
BOOST_CHECK(Checkpoints::CheckBlock(134444+1, p11111)); BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 134444+1, p11111));
BOOST_CHECK(Checkpoints::GetTotalBlocksEstimate() >= 134444); BOOST_CHECK(Checkpoints::GetTotalBlocksEstimate(checkpoints) >= 134444);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

9
src/wallet/wallet.cpp

@ -1059,6 +1059,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
{ {
int ret = 0; int ret = 0;
int64_t nNow = GetTime(); int64_t nNow = GetTime();
const CChainParams& chainParams = Params();
CBlockIndex* pindex = pindexStart; CBlockIndex* pindex = pindexStart;
{ {
@ -1070,12 +1071,12 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
pindex = chainActive.Next(pindex); pindex = chainActive.Next(pindex);
ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
double dProgressStart = Checkpoints::GuessVerificationProgress(pindex, false); double dProgressStart = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false);
double dProgressTip = Checkpoints::GuessVerificationProgress(chainActive.Tip(), false); double dProgressTip = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip(), false);
while (pindex) while (pindex)
{ {
if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0) if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100)))); ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
CBlock block; CBlock block;
ReadBlockFromDisk(block, pindex); ReadBlockFromDisk(block, pindex);
@ -1087,7 +1088,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
pindex = chainActive.Next(pindex); pindex = chainActive.Next(pindex);
if (GetTime() >= nNow + 60) { if (GetTime() >= nNow + 60) {
nNow = GetTime(); nNow = GetTime();
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, Checkpoints::GuessVerificationProgress(pindex)); LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex));
} }
} }
ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI

Loading…
Cancel
Save