]> Git Repo - VerusCoin.git/blob - src/checkpoints.cpp
Merge pull request #5997
[VerusCoin.git] / src / checkpoints.cpp
1 // Copyright (c) 2009-2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "checkpoints.h"
6
7 #include "chainparams.h"
8 #include "main.h"
9 #include "uint256.h"
10
11 #include <stdint.h>
12
13 #include <boost/foreach.hpp>
14
15 namespace Checkpoints {
16
17     /**
18      * How many times we expect transactions after the last checkpoint to
19      * be slower. This number is a compromise, as it can't be accurate for
20      * every system. When reindexing from a fast disk with a slow CPU, it
21      * can be up to 20, while when downloading from a slow network with a
22      * fast multicore CPU, it won't be much higher than 1.
23      */
24     static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
25
26     bool fEnabled = true;
27
28     bool CheckBlock(int nHeight, const uint256& hash)
29     {
30         if (!fEnabled)
31             return true;
32
33         const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
34
35         MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
36         if (i == checkpoints.end()) return true;
37         return hash == i->second;
38     }
39
40     //! Guess how far we are in the verification process at the given block index
41     double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks) {
42         if (pindex==NULL)
43             return 0.0;
44
45         int64_t nNow = time(NULL);
46
47         double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
48         double fWorkBefore = 0.0; // Amount of work done before pindex
49         double fWorkAfter = 0.0;  // Amount of work left after pindex (estimated)
50         // Work is defined as: 1.0 per transaction before the last checkpoint, and
51         // fSigcheckVerificationFactor per transaction after.
52
53         const CCheckpointData &data = Params().Checkpoints();
54
55         if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
56             double nCheapBefore = pindex->nChainTx;
57             double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
58             double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
59             fWorkBefore = nCheapBefore;
60             fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
61         } else {
62             double nCheapBefore = data.nTransactionsLastCheckpoint;
63             double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
64             double nExpensiveAfter = (nNow - pindex->GetBlockTime())/86400.0*data.fTransactionsPerDay;
65             fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
66             fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
67         }
68
69         return fWorkBefore / (fWorkBefore + fWorkAfter);
70     }
71
72     int GetTotalBlocksEstimate()
73     {
74         if (!fEnabled)
75             return 0;
76
77         const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
78
79         return checkpoints.rbegin()->first;
80     }
81
82     CBlockIndex* GetLastCheckpoint()
83     {
84         if (!fEnabled)
85             return NULL;
86
87         const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
88
89         BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
90         {
91             const uint256& hash = i.second;
92             BlockMap::const_iterator t = mapBlockIndex.find(hash);
93             if (t != mapBlockIndex.end())
94                 return t->second;
95         }
96         return NULL;
97     }
98
99 } // namespace Checkpoints
This page took 0.027 seconds and 4 git commands to generate.