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

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