You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

checkpoints.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "checkpoints.h"
  5. #include "chainparams.h"
  6. #include "main.h"
  7. #include "uint256.h"
  8. #include <stdint.h>
  9. #include <boost/foreach.hpp>
  10. namespace Checkpoints {
  11. /**
  12. * How many times slower we expect checking transactions after the last
  13. * checkpoint to be (from checking signatures, which is skipped up to the
  14. * last checkpoint). This number is a compromise, as it can't be accurate
  15. * for every system. When reindexing from a fast disk with a slow CPU, it
  16. * can be up to 20, while when downloading from a slow network with a
  17. * fast multicore CPU, it won't be much higher than 1.
  18. */
  19. static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
  20. bool fEnabled = true;
  21. bool CheckBlock(int nHeight, const uint256& hash)
  22. {
  23. if (!fEnabled)
  24. return true;
  25. const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
  26. MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
  27. if (i == checkpoints.end()) return true;
  28. return hash == i->second;
  29. }
  30. //! Guess how far we are in the verification process at the given block index
  31. double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks) {
  32. if (pindex==NULL)
  33. return 0.0;
  34. int64_t nNow = time(NULL);
  35. double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
  36. double fWorkBefore = 0.0; // Amount of work done before pindex
  37. double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
  38. // Work is defined as: 1.0 per transaction before the last checkpoint, and
  39. // fSigcheckVerificationFactor per transaction after.
  40. const CCheckpointData &data = Params().Checkpoints();
  41. if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
  42. double nCheapBefore = pindex->nChainTx;
  43. double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
  44. double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
  45. fWorkBefore = nCheapBefore;
  46. fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
  47. } else {
  48. double nCheapBefore = data.nTransactionsLastCheckpoint;
  49. double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
  50. double nExpensiveAfter = (nNow - pindex->GetBlockTime())/86400.0*data.fTransactionsPerDay;
  51. fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
  52. fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
  53. }
  54. return fWorkBefore / (fWorkBefore + fWorkAfter);
  55. }
  56. int GetTotalBlocksEstimate()
  57. {
  58. if (!fEnabled)
  59. return 0;
  60. const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
  61. return checkpoints.rbegin()->first;
  62. }
  63. CBlockIndex* GetLastCheckpoint()
  64. {
  65. if (!fEnabled)
  66. return NULL;
  67. const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
  68. BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
  69. {
  70. const uint256& hash = i.second;
  71. BlockMap::const_iterator t = mapBlockIndex.find(hash);
  72. if (t != mapBlockIndex.end())
  73. return t->second;
  74. }
  75. return NULL;
  76. }
  77. } // namespace Checkpoints