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.5KB

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