1 // Copyright (c) 2011 The Bitcoin developers
2 // Copyright (c) 2011-2012 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 bool CheckHardened(int nHeight, const uint256& hash)
30 if (fTestNet) return true; // Testnet has no checkpoints
32 MapCheckpoints::const_iterator i = mapCheckpoints.find(nHeight);
33 if (i == mapCheckpoints.end()) return true;
34 return hash == i->second;
37 int GetTotalBlocksEstimate()
39 if (fTestNet) return 0;
41 return mapCheckpoints.rbegin()->first;
44 CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
46 if (fTestNet) return NULL;
49 BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
51 const uint256& hash = i.second;
52 std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
53 if (t != mapBlockIndex.end())
59 // ppcoin: synchronized checkpoint (centrally broadcasted)
60 uint256 hashSyncCheckpoint;
61 CSyncCheckpoint checkpointMessage;
62 CSyncCheckpoint checkpointMessagePending;
63 CCriticalSection cs_hashSyncCheckpoint;
65 bool CSyncCheckpoint::ProcessSyncCheckpoint()
67 if (!CheckSignature())
70 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
72 if (!mapBlockIndex.count(hashCheckpoint))
74 // TODO: we don't have this block yet, so ask for it
75 checkpointMessagePending = *this;
79 if (!mapBlockIndex.count(hashSyncCheckpoint))
80 return error("ProcessSyncCheckpoint: block index missing for synchronized checkpoint %s", hashSyncCheckpoint.ToString().c_str());
82 CBlockIndex* pindexSyncCheckpoint = mapBlockIndex[hashSyncCheckpoint];
83 CBlockIndex* pindexCheckpointPending = mapBlockIndex[hashCheckpoint];
84 if (pindexCheckpointPending->nHeight <= pindexSyncCheckpoint->nHeight)
85 return false; // this is an older checkpoint, ignore
87 CBlockIndex* pindex = pindexCheckpointPending;
88 while (pindex->nHeight > pindexSyncCheckpoint->nHeight)
89 pindex = pindex->pprev;
90 if (pindex->GetBlockHash() != hashSyncCheckpoint)
91 return error("ProcessSyncCheckpoint: new sync-checkpoint %s is not a descendant of current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str());
92 hashSyncCheckpoint = this->hashCheckpoint;
93 checkpointMessage = *this;
98 // ppcoin: automatic checkpoint (represented by height of checkpoint)
99 int nAutoCheckpoint = 0;
100 int nBranchPoint = 0; // branch point to alternative branch
102 // ppcoin: check automatic checkpoint
103 // To pass the check:
104 // - All ancestors (including the block itself) have block index already
105 // - The immediate ancestor in main chain must not have height less than
107 bool CheckAuto(const CBlockIndex *pindex)
111 if (pindex->IsInMainChain())
113 if (pindex->nHeight >= nAutoCheckpoint)
117 nBranchPoint = pindex->nHeight;
118 return error("Checkpoints: new block on alternative branch at height=%d before auto checkpoint at height=%d", pindex->nHeight, nAutoCheckpoint);
122 pindex = pindex->pprev;
124 return error("Checkpoints: failed to find any ancestor on main chain for the new block - internal error");
127 // ppcoin: get next chain checkpoint
128 int GetNextChainCheckpoint(const CBlockIndex *pindexLast)
131 CBigNum bnTargetMax = 0; // max target of all blocks since checkpoint
132 CBigNum bnTargetMin = 0; // min target of all candidate checkpoints
133 int nMinTargetHeight = 0; // min target height of candidate checkpoints
134 int nCheckpointMin = 0; // minimum candidate checkpoint
135 int nCheckpointMax = 0; // maximum candidate checkpoint
136 int nDepth = pindexLast->nHeight - pindexLast->nCheckpoint;
137 const CBlockIndex *pindex = pindexLast;
138 while (nDepth >= 0 && pindex)
140 bnTarget.SetCompact(pindex->nBits);
141 if (bnTarget > bnTargetMax)
142 bnTargetMax = bnTarget;
143 if (nCheckpointMax > 0 && bnTarget < bnTargetMin)
145 bnTargetMin = bnTarget;
146 nMinTargetHeight = pindex->nHeight;
148 if (nCheckpointMax == 0 && pindexLast->GetBlockTime() - pindex->GetBlockTime() > AUTO_CHECKPOINT_MIN_SPAN)
150 nCheckpointMax = pindex->nHeight;
151 bnTargetMin.SetCompact(pindex->nBits);
152 nMinTargetHeight = pindex->nHeight;
154 if (pindexLast->GetBlockTime() - pindex->GetBlockTime() < AUTO_CHECKPOINT_MAX_SPAN)
155 nCheckpointMin = pindex->nHeight;
156 pindex = pindex->pprev;
160 assert (nDepth == -1); // arrive at chain checkpoint now
162 printf("Checkpoints: min=%d max=%d tminheight=%d tmin=0x%08x tmax=0x%08x\n",
163 nCheckpointMin, nCheckpointMax, nMinTargetHeight,
164 bnTargetMin.GetCompact(), bnTargetMax.GetCompact());
165 if (nCheckpointMax == 0) // checkpoint stays if max candidate not found
166 return pindexLast->nCheckpoint;
168 if (bnTargetMin * 100 > bnTargetMax * 90)
169 return nCheckpointMax;
170 if (bnTarget * 100 > bnTargetMax * 90)
171 return nMinTargetHeight;
173 return nCheckpointMin;
176 // ppcoin: get next auto checkpoint from the new chain checkpoint
177 int GetNextAutoCheckpoint(int nCheckpoint)
179 return (std::max(nAutoCheckpoint, nCheckpoint));
182 // ppcoin: advance to next automatic checkpoint
183 void AdvanceAutoCheckpoint(int nCheckpoint)
185 nAutoCheckpoint = GetNextAutoCheckpoint(nCheckpoint);
186 printf("Checkpoints: auto checkpoint now at height=%d\n", nAutoCheckpoint);
189 // ppcoin: reset auto checkpoint
190 bool ResetAutoCheckpoint(int nCheckpoint)
192 if (nCheckpoint <= 0 || nCheckpoint > nBestHeight)
193 return error("ResetAutoCheckpoint() : new checkpoint invalid");
194 if (nCheckpoint >= nAutoCheckpoint)
195 return error("ResetAutoCheckpoint() : new checkpoint not earlier than current auto checkpoint");
198 if (!txdb.WriteAutoCheckpoint(nCheckpoint, true))
199 return error("ResetAutoCheckpoint() : database write failed");
200 if (!txdb.TxnCommit())
201 return error("ResetAutoCheckpoint() : database commit failed");
202 nAutoCheckpoint = nCheckpoint;
203 nBranchPoint = 0; // clear branch point
205 // clear ban list to accept alternative branches
206 CRITICAL_BLOCK(cs_vNodes)
208 BOOST_FOREACH(CNode* pnode, vNodes)
209 pnode->ClearBanned();