您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

checkpoints.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2009-2012 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 <boost/assign/list_of.hpp> // for 'map_list_of()'
  5. #include <boost/foreach.hpp>
  6. #include "checkpoints.h"
  7. #include "main.h"
  8. #include "uint256.h"
  9. namespace Checkpoints
  10. {
  11. typedef std::map<int, uint256> MapCheckpoints;
  12. // How many times we expect transactions after the last checkpoint to
  13. // be slower. This number is a compromise, as it can't be accurate for
  14. // every system. When reindexing from a fast disk with a slow CPU, it
  15. // can be up to 20, while when downloading from a slow network with a
  16. // fast multicore CPU, it won't be much higher than 1.
  17. static const double fSigcheckVerificationFactor = 5.0;
  18. struct CCheckpointData {
  19. const MapCheckpoints *mapCheckpoints;
  20. int64 nTimeLastCheckpoint;
  21. int64 nTransactionsLastCheckpoint;
  22. double fTransactionsPerDay;
  23. };
  24. bool fEnabled = true;
  25. // What makes a good checkpoint block?
  26. // + Is surrounded by blocks with reasonable timestamps
  27. // (no blocks before with a timestamp after, none after with
  28. // timestamp before)
  29. // + Contains no strange transactions
  30. static MapCheckpoints mapCheckpoints =
  31. boost::assign::map_list_of
  32. ( 11111, uint256("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"))
  33. ( 33333, uint256("0x000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6"))
  34. ( 74000, uint256("0x0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20"))
  35. (105000, uint256("0x00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97"))
  36. (134444, uint256("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe"))
  37. (168000, uint256("0x000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763"))
  38. (193000, uint256("0x000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317"))
  39. (210000, uint256("0x000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e"))
  40. (216116, uint256("0x00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e"))
  41. (225430, uint256("0x00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932"))
  42. ;
  43. static const CCheckpointData data = {
  44. &mapCheckpoints,
  45. 1363044259, // * UNIX timestamp of last checkpoint block
  46. 14264869, // * total number of transactions between genesis and last checkpoint
  47. // (the tx=... number in the SetBestChain debug.log lines)
  48. 60000.0 // * estimated number of transactions per day after checkpoint
  49. };
  50. static MapCheckpoints mapCheckpointsTestnet =
  51. boost::assign::map_list_of
  52. ( 546, uint256("000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70"))
  53. ;
  54. static const CCheckpointData dataTestnet = {
  55. &mapCheckpointsTestnet,
  56. 1338180505,
  57. 16341,
  58. 300
  59. };
  60. const CCheckpointData &Checkpoints() {
  61. if (TestNet())
  62. return dataTestnet;
  63. else
  64. return data;
  65. }
  66. bool CheckBlock(int nHeight, const uint256& hash)
  67. {
  68. if (!fEnabled)
  69. return true;
  70. const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
  71. MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
  72. if (i == checkpoints.end()) return true;
  73. return hash == i->second;
  74. }
  75. // Guess how far we are in the verification process at the given block index
  76. double GuessVerificationProgress(CBlockIndex *pindex) {
  77. if (pindex==NULL)
  78. return 0.0;
  79. int64 nNow = time(NULL);
  80. double fWorkBefore = 0.0; // Amount of work done before pindex
  81. double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
  82. // Work is defined as: 1.0 per transaction before the last checkoint, and
  83. // fSigcheckVerificationFactor per transaction after.
  84. const CCheckpointData &data = Checkpoints();
  85. if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
  86. double nCheapBefore = pindex->nChainTx;
  87. double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
  88. double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
  89. fWorkBefore = nCheapBefore;
  90. fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
  91. } else {
  92. double nCheapBefore = data.nTransactionsLastCheckpoint;
  93. double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
  94. double nExpensiveAfter = (nNow - pindex->nTime)/86400.0*data.fTransactionsPerDay;
  95. fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
  96. fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
  97. }
  98. return fWorkBefore / (fWorkBefore + fWorkAfter);
  99. }
  100. int GetTotalBlocksEstimate()
  101. {
  102. if (!fEnabled)
  103. return 0;
  104. const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
  105. return checkpoints.rbegin()->first;
  106. }
  107. CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
  108. {
  109. if (!fEnabled)
  110. return NULL;
  111. const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
  112. BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
  113. {
  114. const uint256& hash = i.second;
  115. std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
  116. if (t != mapBlockIndex.end())
  117. return t->second;
  118. }
  119. return NULL;
  120. }
  121. }