Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
checkpoints.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2012 The Bitcoin developers
2 // Copyright (c) 2013 Feathercoin 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 <boost/assign/list_of.hpp> // for 'map_list_of()'
7 #include <boost/foreach.hpp>
8 
9 #include "checkpoints.h"
10 
11 #include "main.h"
12 #include "uint256.h"
13 
14 namespace Checkpoints
15 {
16  typedef std::map<int, uint256> MapCheckpoints;
17 
18  // How many times we expect transactions after the last checkpoint to
19  // be slower. This number is a compromise, as it can't be accurate for
20  // every system. When reindexing from a fast disk with a slow CPU, it
21  // can be up to 20, while when downloading from a slow network with a
22  // fast multicore CPU, it won't be much higher than 1.
23  static const double fSigcheckVerificationFactor = 5.0;
24 
25  struct CCheckpointData {
26  const MapCheckpoints *mapCheckpoints;
30  };
31 
32  // What makes a good checkpoint block?
33  // + Is surrounded by blocks with reasonable timestamps
34  // (no blocks before with a timestamp after, none after with
35  // timestamp before)
36  // + Contains no strange transactions
37  static MapCheckpoints mapCheckpoints =
38  boost::assign::map_list_of
39  ( 1, uint256("0xfdbe99b90c90bae7505796461471d89ae8388ab953997aa06a355bbda8d915cb"))
40  ( 22267, uint256("0x23dc7d871fc2a9b994112e978019f6370bab0b8979f557afe77a7ab620224b70"))
41  ( 22847, uint256("0x1450b80c150fee1e657ee8309819276342c021fab5e6a20ccf5407f5e2218d0f"))
42  ( 23453, uint256("0x7c25d3f9671e1d9400c9a1be2ff68e68db561ab85ae6b7020062d8d26da81e4c"))
43  ( 28230, uint256("0xd3feb71f92c63c682f994d7c615adc425226c669a83d46012b32fc8d518b08bb"))
44  ( 31846, uint256("0xba7d5c0e6d46f6448253290ce037e13975c13ca9c375ae854b6b2f85044fc0f9"))
45  ( 33918, uint256("0x023cf4acfd8bf0114090a7ce048e79ac28152de78bb41f1277742904494e6c49"))
46  ( 34000, uint256("0x082f5d9023af3f068733ab68cf81b741f58e3c75ae28d2a9bd07f30b74c38356"))
47  ( 41300, uint256("0x8c4e02f6c0d20e856fd7e952a147fee30ce145ca6932a284f354924362d2b408"))
48  ;
49  static const CCheckpointData data = {
50  &mapCheckpoints,
51  1372166807, // * UNIX timestamp of last checkpoint block
52  257285, // * total number of transactions between genesis and last checkpoint
53  // (the tx=... number in the SetBestChain debug.log lines)
54  3450.0 // * estimated number of transactions per day after checkpoint
55  };
56 
58  return data;
59  }
60 
61  bool CheckBlock(int nHeight, const uint256& hash)
62  {
63  if (fTestNet) return true; // Testnet has no checkpoints
64  if (!GetBoolArg("-checkpoints", true))
65  return true;
66 
67  const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
68 
69  MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
70  if (i == checkpoints.end()) return true;
71  return hash == i->second;
72  }
73 
74  // Guess how far we are in the verification process at the given block index
76  if (pindex==NULL)
77  return 0.0;
78 
79  int64 nNow = time(NULL);
80 
81  double fWorkBefore = 0.0; // Amount of work done before pindex
82  double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
83  // Work is defined as: 1.0 per transaction before the last checkoint, and
84  // fSigcheckVerificationFactor per transaction after.
85 
86  const CCheckpointData &data = Checkpoints();
87 
88  if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
89  double nCheapBefore = pindex->nChainTx;
90  double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
91  double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
92  fWorkBefore = nCheapBefore;
93  fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
94  } else {
95  double nCheapBefore = data.nTransactionsLastCheckpoint;
96  double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
97  double nExpensiveAfter = (nNow - pindex->nTime)/86400.0*data.fTransactionsPerDay;
98  fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
99  fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
100  }
101 
102  return fWorkBefore / (fWorkBefore + fWorkAfter);
103  }
104 
106  {
107  if (fTestNet) return 0; // Testnet has no checkpoints
108  if (!GetBoolArg("-checkpoints", true))
109  return 0;
110 
111  const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
112 
113  return checkpoints.rbegin()->first;
114  }
115 
116  CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
117  {
118  if (fTestNet) return NULL; // Testnet has no checkpoints
119  if (!GetBoolArg("-checkpoints", true))
120  return NULL;
121 
122  const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
123 
124  BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
125  {
126  const uint256& hash = i.second;
127  std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
128  if (t != mapBlockIndex.end())
129  return t->second;
130  }
131  return NULL;
132  }
133 
135  {
136  const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;
137  return (checkpoints.rbegin()->second);
138  }
139 }
CBlockIndex * GetLastCheckpoint(const std::map< uint256, CBlockIndex * > &mapBlockIndex)
std::map< int, uint256 > MapCheckpoints
Definition: checkpoints.cpp:16
const MapCheckpoints * mapCheckpoints
Definition: checkpoints.cpp:26
unsigned int nChainTx
Definition: main.h:1658
Block-chain checkpoints are compiled-in sanity checks.
Definition: checkpoints.cpp:14
unsigned int nTime
Definition: main.h:1666
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:600
bool fTestNet
Definition: util.cpp:81
const CCheckpointData & Checkpoints()
Definition: checkpoints.cpp:57
int GetTotalBlocksEstimate()
bool CheckBlock(int nHeight, const uint256 &hash)
Definition: checkpoints.cpp:61
uint256 GetLatestHardenedCheckpoint()
256-bit unsigned integer
Definition: uint256.h:537
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:1626
map< uint256, CBlockIndex * > mapBlockIndex
Definition: main.cpp:37
double GuessVerificationProgress(CBlockIndex *pindex)
Definition: checkpoints.cpp:75
uint32_t hash
Definition: cache.cc:34
long long int64
Definition: serialize.h:25