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 // ppcoin: only descendant of current sync-checkpoint is allowed
66 bool ValidateSyncCheckpoint(uint256 hashCheckpoint)
68 if (!mapBlockIndex.count(hashSyncCheckpoint))
69 return error("ValidateSyncCheckpoint: block index missing for current sync-checkpoint %s", hashSyncCheckpoint.ToString().c_str());
70 if (!mapBlockIndex.count(hashCheckpoint))
71 return error("ValidateSyncCheckpoint: block index missing for received sync-checkpoint %s", hashCheckpoint.ToString().c_str());
73 CBlockIndex* pindexSyncCheckpoint = mapBlockIndex[hashSyncCheckpoint];
74 CBlockIndex* pindexCheckpointRecv = mapBlockIndex[hashCheckpoint];
75 if (pindexCheckpointRecv->nHeight <= pindexSyncCheckpoint->nHeight)
76 return false; // this is an older checkpoint, ignore
78 CBlockIndex* pindex = pindexCheckpointRecv;
79 while (pindex->nHeight > pindexSyncCheckpoint->nHeight)
80 pindex = pindex->pprev;
81 if (pindex->GetBlockHash() != hashSyncCheckpoint)
82 return error("ValidateSyncCheckpoint: new sync-checkpoint %s is not a descendant of current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str());
86 bool AcceptPendingSyncCheckpoint(uint256 hashAcceptedBlock)
88 if (!mapBlockIndex.count(hashAcceptedBlock))
91 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
92 if ((!checkpointMessagePending.IsNull()) && checkpointMessagePending.hashCheckpoint == hashAcceptedBlock)
94 if (!ValidateSyncCheckpoint(checkpointMessagePending.hashCheckpoint))
96 checkpointMessagePending.SetNull();
99 hashSyncCheckpoint = checkpointMessagePending.hashCheckpoint;
100 checkpointMessage = checkpointMessagePending;
101 checkpointMessagePending.SetNull();
102 printf("AcceptPendingSyncCheckpoint : sync-checkpoint at %s\n", hashSyncCheckpoint.ToString().c_str());
103 // relay the checkpoint
104 BOOST_FOREACH(CNode* pnode, vNodes)
105 checkpointMessage.RelayTo(pnode);
112 // ppcoin: automatic checkpoint (represented by height of checkpoint)
113 int nAutoCheckpoint = 0;
114 int nBranchPoint = 0; // branch point to alternative branch
116 // ppcoin: check automatic checkpoint
117 // To pass the check:
118 // - All ancestors (including the block itself) have block index already
119 // - The immediate ancestor in main chain must not have height less than
121 bool CheckAuto(const CBlockIndex *pindex)
125 if (pindex->IsInMainChain())
127 if (pindex->nHeight >= nAutoCheckpoint)
131 nBranchPoint = pindex->nHeight;
132 return error("Checkpoints: new block on alternative branch at height=%d before auto checkpoint at height=%d", pindex->nHeight, nAutoCheckpoint);
136 pindex = pindex->pprev;
138 return error("Checkpoints: failed to find any ancestor on main chain for the new block - internal error");
141 // ppcoin: get next chain checkpoint
142 int GetNextChainCheckpoint(const CBlockIndex *pindexLast)
145 CBigNum bnTargetMax = 0; // max target of all blocks since checkpoint
146 CBigNum bnTargetMin = 0; // min target of all candidate checkpoints
147 int nMinTargetHeight = 0; // min target height of candidate checkpoints
148 int nCheckpointMin = 0; // minimum candidate checkpoint
149 int nCheckpointMax = 0; // maximum candidate checkpoint
150 int nDepth = pindexLast->nHeight - pindexLast->nCheckpoint;
151 const CBlockIndex *pindex = pindexLast;
152 while (nDepth >= 0 && pindex)
154 bnTarget.SetCompact(pindex->nBits);
155 if (bnTarget > bnTargetMax)
156 bnTargetMax = bnTarget;
157 if (nCheckpointMax > 0 && bnTarget < bnTargetMin)
159 bnTargetMin = bnTarget;
160 nMinTargetHeight = pindex->nHeight;
162 if (nCheckpointMax == 0 && pindexLast->GetBlockTime() - pindex->GetBlockTime() > AUTO_CHECKPOINT_MIN_SPAN)
164 nCheckpointMax = pindex->nHeight;
165 bnTargetMin.SetCompact(pindex->nBits);
166 nMinTargetHeight = pindex->nHeight;
168 if (pindexLast->GetBlockTime() - pindex->GetBlockTime() < AUTO_CHECKPOINT_MAX_SPAN)
169 nCheckpointMin = pindex->nHeight;
170 pindex = pindex->pprev;
174 assert (nDepth == -1); // arrive at chain checkpoint now
176 printf("Checkpoints: min=%d max=%d tminheight=%d tmin=0x%08x tmax=0x%08x\n",
177 nCheckpointMin, nCheckpointMax, nMinTargetHeight,
178 bnTargetMin.GetCompact(), bnTargetMax.GetCompact());
179 if (nCheckpointMax == 0) // checkpoint stays if max candidate not found
180 return pindexLast->nCheckpoint;
182 if (bnTargetMin * 100 > bnTargetMax * 90)
183 return nCheckpointMax;
184 if (bnTarget * 100 > bnTargetMax * 90)
185 return nMinTargetHeight;
187 return nCheckpointMin;
190 // ppcoin: get next auto checkpoint from the new chain checkpoint
191 int GetNextAutoCheckpoint(int nCheckpoint)
193 return (std::max(nAutoCheckpoint, nCheckpoint));
196 // ppcoin: advance to next automatic checkpoint
197 void AdvanceAutoCheckpoint(int nCheckpoint)
199 nAutoCheckpoint = GetNextAutoCheckpoint(nCheckpoint);
200 printf("Checkpoints: auto checkpoint now at height=%d\n", nAutoCheckpoint);
203 // ppcoin: reset auto checkpoint
204 bool ResetAutoCheckpoint(int nCheckpoint)
206 if (nCheckpoint <= 0 || nCheckpoint > nBestHeight)
207 return error("ResetAutoCheckpoint() : new checkpoint invalid");
208 if (nCheckpoint >= nAutoCheckpoint)
209 return error("ResetAutoCheckpoint() : new checkpoint not earlier than current auto checkpoint");
212 if (!txdb.WriteAutoCheckpoint(nCheckpoint, true))
213 return error("ResetAutoCheckpoint() : database write failed");
214 if (!txdb.TxnCommit())
215 return error("ResetAutoCheckpoint() : database commit failed");
216 nAutoCheckpoint = nCheckpoint;
217 nBranchPoint = 0; // clear branch point
219 // clear ban list to accept alternative branches
220 CRITICAL_BLOCK(cs_vNodes)
222 BOOST_FOREACH(CNode* pnode, vNodes)
223 pnode->ClearBanned();
230 // ppcoin: process synchronized checkpoint
231 bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom)
233 if (!CheckSignature())
236 CRITICAL_BLOCK(Checkpoints::cs_hashSyncCheckpoint)
238 if (!mapBlockIndex.count(hashCheckpoint))
240 // We haven't accepted this block, keep the checkpoint as pending
241 Checkpoints::checkpointMessagePending = *this;
242 printf("ProcessSyncCheckpoint : pending for sync-checkpoint %s\n", hashCheckpoint.ToString().c_str());
243 // Ask this guy to fill in what we're missing
245 pfrom->PushGetBlocks(pindexBest, hashCheckpoint);
248 if (!Checkpoints::ValidateSyncCheckpoint(hashCheckpoint))
250 Checkpoints::hashSyncCheckpoint = this->hashCheckpoint;
251 Checkpoints::checkpointMessage = *this;
252 Checkpoints::checkpointMessagePending.SetNull();
253 printf("ProcessSyncCheckpoint : sync-checkpoint at %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());