1 // Copyright (c) 2011 The Bitcoin developers
2 // Copyright (c) 2011 The PPCoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
6 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
7 #include <boost/foreach.hpp>
10 #include "checkpoints.h"
14 typedef std::map<int, uint256> MapCheckpoints; // hardened checkpoints
17 // What makes a good checkpoint block?
18 // + Is surrounded by blocks with reasonable timestamps
19 // (no blocks before with a timestamp after, none after with
21 // + Contains no strange transactions
23 static MapCheckpoints mapCheckpoints =
24 boost::assign::map_list_of
25 ( 0, hashGenesisBlock )
26 ; // ppcoin: no checkpoint yet; to be created in future releases
28 // ppcoin: automatic checkpoint (represented by height of checkpoint)
29 int nAutoCheckpoint = 0;
31 bool CheckHardened(int nHeight, const uint256& hash)
33 if (fTestNet) return true; // Testnet has no checkpoints
35 MapCheckpoints::const_iterator i = mapCheckpoints.find(nHeight);
36 if (i == mapCheckpoints.end()) return true;
37 return hash == i->second;
40 // ppcoin: check automatic checkpoint
42 // - All ancestors (including the block itself) have block index already
43 // - The immediate ancestor in main chain must not have height less than
45 bool CheckAuto(const CBlockIndex *pindex)
49 if (pindex->IsInMainChain())
51 if (pindex->nHeight >= nAutoCheckpoint)
54 return error("Checkpoints: new block on alternative branch at height=%d before auto checkpoint at height=%d", pindex->nHeight, nAutoCheckpoint);
57 pindex = pindex->pprev;
59 return error("Checkpoints: failed to find any ancestor on main chain for the new block - internal error");
62 // ppcoin: get next auto checkpoint in the chain
63 int GetNextChainCheckpoint(const CBlockIndex *pindexLast)
66 CBigNum bnTargetMax = 0; // max target of all blocks since checkpoint
67 CBigNum bnTargetMin = 0; // min target of all candidate checkpoints
68 int nMinTargetHeight = 0; // min target height since checkpoint
69 int nCheckpointMin = 0; // minimum candidate checkpoint
70 int nCheckpointMax = 0; // maximum candidate checkpoint
71 int nDepth = pindexLast->nHeight - pindexLast->nCheckpoint;
72 const CBlockIndex *pindex = pindexLast;
73 while (nDepth >= 0 && pindex)
75 bnTarget.SetCompact(pindex->nBits);
76 if (bnTarget > bnTargetMax)
77 bnTargetMax = bnTarget;
78 if (nCheckpointMax > 0 && bnTarget < bnTargetMin)
80 bnTargetMin = bnTarget;
81 nMinTargetHeight = pindex->nHeight;
83 if (nCheckpointMax == 0 && pindexLast->GetBlockTime() - pindex->GetBlockTime() > AUTO_CHECKPOINT_MIN_SPAN)
85 nCheckpointMax = pindex->nHeight;
86 bnTargetMin.SetCompact(pindex->nBits);
87 nMinTargetHeight = pindex->nHeight;
89 if (pindexLast->GetBlockTime() - pindex->GetBlockTime() < AUTO_CHECKPOINT_MAX_SPAN)
90 nCheckpointMin = pindex->nHeight;
91 pindex = pindex->pprev;
95 assert (nDepth == -1); // arrive at chain checkpoint now
97 printf("Checkpoints: min=%d max=%d tminheight=%d tmin=0x%08x tmax=0x%08x\n",
98 nCheckpointMin, nCheckpointMax, nMinTargetHeight,
99 bnTargetMin.GetCompact(), bnTargetMax.GetCompact());
100 if (nCheckpointMax == 0) // checkpoint stays if max candidate not found
101 return pindexLast->nCheckpoint;
103 if (bnTargetMin * 100 > bnTargetMax * 90)
104 return nCheckpointMax;
105 if (bnTarget * 100 > bnTargetMax * 90)
106 return std::min(nCheckpointMax, nMinTargetHeight);
108 return nCheckpointMin;
111 // ppcoin: advance to next automatic checkpoint
112 void AdvanceAutoCheckpoint(int nCheckpoint)
114 nAutoCheckpoint = std::max(nAutoCheckpoint, nCheckpoint);
115 printf("Checkpoints: auto checkpoint now at height=%d\n", nAutoCheckpoint);
118 int GetTotalBlocksEstimate()
120 if (fTestNet) return 0;
122 return mapCheckpoints.rbegin()->first;
125 CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
127 if (fTestNet) return NULL;
130 BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
132 const uint256& hash = i.second;
133 std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
134 if (t != mapBlockIndex.end())