1 // Copyright (c) 2009-2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php .
5 #include "checkpoints.h"
7 #include "chainparams.h"
13 #include <boost/foreach.hpp>
15 namespace Checkpoints {
18 * How many times slower we expect checking transactions after the last
19 * checkpoint to be (from checking signatures, which is skipped up to the
20 * last checkpoint). This number is a compromise, as it can't be accurate
21 * for every system. When reindexing from a fast disk with a slow CPU, it
22 * can be up to 20, while when downloading from a slow network with a
23 * fast multicore CPU, it won't be much higher than 1.
25 static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
27 //! Guess how far we are in the verification process at the given block index
28 double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex *pindex, bool fSigchecks) {
32 int64_t nNow = time(NULL);
34 double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
35 double fWorkBefore = 0.0; // Amount of work done before pindex
36 double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
37 // Work is defined as: 1.0 per transaction before the last checkpoint, and
38 // fSigcheckVerificationFactor per transaction after.
40 if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
41 double nCheapBefore = pindex->nChainTx;
42 double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
43 double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
44 fWorkBefore = nCheapBefore;
45 fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
47 double nCheapBefore = data.nTransactionsLastCheckpoint;
48 double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
49 double nExpensiveAfter = (nNow - pindex->GetBlockTime())/86400.0*data.fTransactionsPerDay;
50 fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
51 fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
54 return fWorkBefore / (fWorkBefore + fWorkAfter);
57 int GetTotalBlocksEstimate(const CCheckpointData& data)
59 const MapCheckpoints& checkpoints = data.mapCheckpoints;
61 if (checkpoints.empty())
64 return checkpoints.rbegin()->first;
67 CBlockIndex* GetLastCheckpoint(const CCheckpointData& data)
69 const MapCheckpoints& checkpoints = data.mapCheckpoints;
71 BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
73 const uint256& hash = i.second;
74 BlockMap::const_iterator t = mapBlockIndex.find(hash);
75 if (t != mapBlockIndex.end())
81 } // namespace Checkpoints