]> Git Repo - VerusCoin.git/blob - src/checkpoints.cpp
Auto merge of #1116 - ebfull:performance-check-valid-pour-fix, r=ebfull
[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 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.
24      */
25     static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
26
27     bool CheckBlock(const CCheckpointData& data, int nHeight, const uint256& hash)
28     {
29         const MapCheckpoints& checkpoints = data.mapCheckpoints;
30
31         MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
32         if (i == checkpoints.end()) return true;
33         return hash == i->second;
34     }
35
36     //! Guess how far we are in the verification process at the given block index
37     double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex *pindex, bool fSigchecks) {
38         if (pindex==NULL)
39             return 0.0;
40
41         int64_t nNow = time(NULL);
42
43         double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
44         double fWorkBefore = 0.0; // Amount of work done before pindex
45         double fWorkAfter = 0.0;  // Amount of work left after pindex (estimated)
46         // Work is defined as: 1.0 per transaction before the last checkpoint, and
47         // fSigcheckVerificationFactor per transaction after.
48
49         if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
50             double nCheapBefore = pindex->nChainTx;
51             double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
52             double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
53             fWorkBefore = nCheapBefore;
54             fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
55         } else {
56             double nCheapBefore = data.nTransactionsLastCheckpoint;
57             double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
58             double nExpensiveAfter = (nNow - pindex->GetBlockTime())/86400.0*data.fTransactionsPerDay;
59             fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
60             fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
61         }
62
63         return fWorkBefore / (fWorkBefore + fWorkAfter);
64     }
65
66     int GetTotalBlocksEstimate(const CCheckpointData& data)
67     {
68         const MapCheckpoints& checkpoints = data.mapCheckpoints;
69
70         if (checkpoints.empty())
71             return 0;
72
73         return checkpoints.rbegin()->first;
74     }
75
76     CBlockIndex* GetLastCheckpoint(const CCheckpointData& data)
77     {
78         const MapCheckpoints& checkpoints = data.mapCheckpoints;
79
80         BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
81         {
82             const uint256& hash = i.second;
83             BlockMap::const_iterator t = mapBlockIndex.find(hash);
84             if (t != mapBlockIndex.end())
85                 return t->second;
86         }
87         return NULL;
88     }
89
90 } // namespace Checkpoints
This page took 0.04576 seconds and 4 git commands to generate.