Anoncoin  0.9.4
P2P Digital Currency
checkpoints.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "checkpoints.h"
7 
8 #include "main.h"
9 #include "uint256.h"
10 
11 #include <stdint.h>
12 
13 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
14 #include <boost/foreach.hpp>
15 
16 namespace Checkpoints
17 {
18  typedef std::map<int, uint256> MapCheckpoints;
19 
20  // How many times we expect transactions after the last checkpoint to
21  // be slower. This number is a compromise, as it can't be accurate for
22  // every system. When reindexing from a fast disk with a slow CPU, it
23  // can be up to 20, while when downloading from a slow network with a
24  // fast multicore CPU, it won't be much higher than 1.
25  static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
26 
27  struct CCheckpointData {
28  const MapCheckpoints *mapCheckpoints;
32  };
33 
34  bool fEnabled = true;
35 
36  // What makes a good checkpoint block?
37  // + Is surrounded by blocks with reasonable timestamps
38  // (no blocks before with a timestamp after, none after with
39  // timestamp before)
40  // + Contains no strange transactions
41  static MapCheckpoints mapCheckpoints =
42  boost::assign::map_list_of
43  ( 1, uint256("0x8fe2901fc0999bc86ea2668c58802ee87165438166d18154f1bd4f917bf25e0f"))
44  ( 7, uint256("0x4530df06d98fc77d04dab427630fc63b45f10d2b0ad3ad3a651883938986d629"))
45  ( 7777, uint256("0xae3094030b34a422c44b9832c84fe602d0d528449d6940374bd43b4472b4df5e"))
46  (15420, uint256("0xfded6a374d071f59d738a3009fc4d8461609052c3e7e91aa89146550d179c1b0"))
47  (16000, uint256("0x683517a8cae8530f39e636f010ecd1750665c3d91f57ba71d6556535972ab328"))
48  (77777, uint256("0xf5c98062cb1ad75c792a1851a388447f0edd7cb2271b67ef1241a03c673b7735"))
49  (77778, uint256("0xd13f93f9fdac82ea26ed8f90474ed2449c8c24be50a416e43c323a38573c30e5"))
50  (100000, uint256("0xcc4f0b11e9e17f7a406ac4a71e6e192b9b43e32b300ddecba229c789392497eb"))
51  (125000, uint256("0x52a933553dda61b4af6a418f10ce563f0c84df93619ec97f8f360f9ecc51cf5b"))
52  (150000, uint256("0xb1dc241da3e8e7d4ca2df75187d66f4f096840634667b987a7b60d5dde92853c"))
53  (210000, uint256("0xb9c2c5030199a87cb99255551b7f67bff6adf3ef2caf97258b93b417d14f9051"))
54  ;
55  static const CCheckpointData data = {
56  &mapCheckpoints,
57  1376885874, // * UNIX timestamp of last checkpoint block
58  1859062, // * total number of transactions between genesis and last checkpoint
59  // (the tx=... number in the SetBestChain debug.log lines)
60  7000.0 // * estimated number of transactions per day after checkpoint
61  };
62 
63  static MapCheckpoints mapCheckpointsTestnet =
64  boost::assign::map_list_of
65  ( 546, uint256("0xa0fea99a6897f531600c8ae53367b126824fd6a847b2b2b73817a95b8e27e602"))
66  ;
67  static const CCheckpointData dataTestnet = {
68  &mapCheckpointsTestnet,
69  1365458829,
70  547,
71  576
72  };
73 
74  static MapCheckpoints mapCheckpointsRegtest =
75  boost::assign::map_list_of
76  ( 0, uint256("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"))
77  ;
78  static const CCheckpointData dataRegtest = {
79  &mapCheckpointsRegtest,
80  0,
81  0,
82  0
83  };
84 
86  if (Params().NetworkID() == CChainParams::TESTNET)
87  return dataTestnet;
88  else if (Params().NetworkID() == CChainParams::MAIN)
89  return data;
90  else
91  return dataRegtest;
92  }
93 
94  bool CheckBlock(int nHeight, const uint256& hash)
95  {
96  if (!fEnabled)
97  return true;
98 
99  const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
100 
101  MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
102  if (i == checkpoints.end()) return true;
103  return hash == i->second;
104  }
105 
106  // Guess how far we are in the verification process at the given block index
107  double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks) {
108  if (pindex==NULL)
109  return 0.0;
110 
111  int64_t nNow = time(NULL);
112 
113  double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
114  double fWorkBefore = 0.0; // Amount of work done before pindex
115  double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
116  // Work is defined as: 1.0 per transaction before the last checkpoint, and
117  // fSigcheckVerificationFactor per transaction after.
118 
119  const CCheckpointData &data = Checkpoints();
120 
121  if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
122  double nCheapBefore = pindex->nChainTx;
123  double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
124  double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
125  fWorkBefore = nCheapBefore;
126  fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
127  } else {
128  double nCheapBefore = data.nTransactionsLastCheckpoint;
129  double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
130  double nExpensiveAfter = (nNow - pindex->nTime)/86400.0*data.fTransactionsPerDay;
131  fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
132  fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
133  }
134 
135  return fWorkBefore / (fWorkBefore + fWorkAfter);
136  }
137 
139  {
140  if (!fEnabled)
141  return 0;
142 
143  const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
144 
145  return checkpoints.rbegin()->first;
146  }
147 
148  CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
149  {
150  if (!fEnabled)
151  return NULL;
152 
153  const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
154 
155  BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
156  {
157  const uint256& hash = i.second;
158  std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
159  if (t != mapBlockIndex.end())
160  return t->second;
161  }
162  return NULL;
163  }
164 }
CBlockIndex * GetLastCheckpoint(const std::map< uint256, CBlockIndex * > &mapBlockIndex)
std::map< int, uint256 > MapCheckpoints
Definition: checkpoints.cpp:18
const MapCheckpoints * mapCheckpoints
Definition: checkpoints.cpp:28
unsigned int nChainTx
Definition: main.h:727
Block-chain checkpoints are compiled-in sanity checks.
Definition: checkpoints.cpp:16
unsigned int nTime
Definition: main.h:735
const CCheckpointData & Checkpoints()
Definition: checkpoints.cpp:85
int GetTotalBlocksEstimate()
bool CheckBlock(int nHeight, const uint256 &hash)
Definition: checkpoints.cpp:94
double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks)
256-bit unsigned integer
Definition: uint256.h:532
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:698
const CChainParams & Params()
Return the currently selected parameters.
map< uint256, CBlockIndex * > mapBlockIndex
Definition: main.cpp:42