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, hashGenesisBlockOfficial )
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)
47 std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hashGenesisBlock);
48 if (t != mapBlockIndex.end())
54 BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
56 const uint256& hash = i.second;
57 std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
58 if (t != mapBlockIndex.end())
64 // ppcoin: synchronized checkpoint (centrally broadcasted)
65 uint256 hashSyncCheckpoint = 0;
66 uint256 hashPendingCheckpoint = 0;
67 CSyncCheckpoint checkpointMessage;
68 CSyncCheckpoint checkpointMessagePending;
69 uint256 hashInvalidCheckpoint = 0;
70 CCriticalSection cs_hashSyncCheckpoint;
72 // ppcoin: get last synchronized checkpoint
73 CBlockIndex* GetLastSyncCheckpoint()
75 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
77 if (!mapBlockIndex.count(hashSyncCheckpoint))
78 error("GetSyncCheckpoint: block index missing for current sync-checkpoint %s", hashSyncCheckpoint.ToString().c_str());
80 return mapBlockIndex[hashSyncCheckpoint];
85 // ppcoin: only descendant of current sync-checkpoint is allowed
86 bool ValidateSyncCheckpoint(uint256 hashCheckpoint)
88 if (!mapBlockIndex.count(hashSyncCheckpoint))
89 return error("ValidateSyncCheckpoint: block index missing for current sync-checkpoint %s", hashSyncCheckpoint.ToString().c_str());
90 if (!mapBlockIndex.count(hashCheckpoint))
91 return error("ValidateSyncCheckpoint: block index missing for received sync-checkpoint %s", hashCheckpoint.ToString().c_str());
93 CBlockIndex* pindexSyncCheckpoint = mapBlockIndex[hashSyncCheckpoint];
94 CBlockIndex* pindexCheckpointRecv = mapBlockIndex[hashCheckpoint];
96 if (pindexCheckpointRecv->nHeight <= pindexSyncCheckpoint->nHeight)
98 // Received an older checkpoint, trace back from current checkpoint
99 // to the same height of the received checkpoint to verify
100 // that current checkpoint should be a descendant block
101 CBlockIndex* pindex = pindexSyncCheckpoint;
102 while (pindex->nHeight > pindexCheckpointRecv->nHeight)
103 if (!(pindex = pindex->pprev))
104 return error("ValidateSyncCheckpoint: pprev1 null - block index structure failure");
105 if (pindex->GetBlockHash() != hashCheckpoint)
107 hashInvalidCheckpoint = hashCheckpoint;
108 return error("ValidateSyncCheckpoint: new sync-checkpoint %s is conflicting with current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str());
110 return false; // ignore older checkpoint
113 // Received checkpoint should be a descendant block of the current
114 // checkpoint. Trace back to the same height of current checkpoint
116 CBlockIndex* pindex = pindexCheckpointRecv;
117 while (pindex->nHeight > pindexSyncCheckpoint->nHeight)
118 if (!(pindex = pindex->pprev))
119 return error("ValidateSyncCheckpoint: pprev2 null - block index structure failure");
120 if (pindex->GetBlockHash() != hashSyncCheckpoint)
122 hashInvalidCheckpoint = hashCheckpoint;
123 return error("ValidateSyncCheckpoint: new sync-checkpoint %s is not a descendant of current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str());
128 bool WriteSyncCheckpoint(const uint256& hashCheckpoint)
132 if (!txdb.WriteSyncCheckpoint(hashCheckpoint))
135 return error("WriteSyncCheckpoint(): failed to write to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
137 if (!txdb.TxnCommit())
138 return error("WriteSyncCheckpoint(): failed to commit to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
141 Checkpoints::hashSyncCheckpoint = hashCheckpoint;
145 bool AcceptPendingSyncCheckpoint()
147 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
149 if (hashPendingCheckpoint != 0 && mapBlockIndex.count(hashPendingCheckpoint))
151 if (!ValidateSyncCheckpoint(hashPendingCheckpoint))
153 hashPendingCheckpoint = 0;
154 checkpointMessagePending.SetNull();
159 CBlockIndex* pindexCheckpoint = mapBlockIndex[hashPendingCheckpoint];
160 if (!pindexCheckpoint->IsInMainChain())
163 if (!Reorganize(txdb, pindexCheckpoint))
166 hashInvalidCheckpoint = hashPendingCheckpoint;
167 return error("ProcessSyncCheckpoint: Reorganize failed for sync checkpoint %s", hashPendingCheckpoint.ToString().c_str());
172 if (!WriteSyncCheckpoint(hashPendingCheckpoint))
173 return error("AcceptPendingSyncCheckpoint(): failed to write sync checkpoint %s", hashPendingCheckpoint.ToString().c_str());
174 hashPendingCheckpoint = 0;
175 checkpointMessage = checkpointMessagePending;
176 checkpointMessagePending.SetNull();
177 printf("AcceptPendingSyncCheckpoint : sync-checkpoint at %s\n", hashSyncCheckpoint.ToString().c_str());
178 // relay the checkpoint
179 if (!checkpointMessage.IsNull())
180 BOOST_FOREACH(CNode* pnode, vNodes)
181 checkpointMessage.RelayTo(pnode);
189 uint256 AutoSelectSyncCheckpoint()
191 // select a block some time ago
192 CBlockIndex *pindex = mapBlockIndex[hashSyncCheckpoint];
193 while (pindex->pnext && pindex->pnext->GetBlockTime() + CHECKPOINT_MIN_SPAN <= GetAdjustedTime())
194 pindex = pindex->pnext;
195 return pindex->GetBlockHash();
198 // Check against synchronized checkpoint
199 bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev)
201 if (fTestNet) return true; // Testnet has no checkpoints
202 int nHeight = pindexPrev->nHeight + 1;
204 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
206 // sync-checkpoint should always be accepted block
207 assert(mapBlockIndex.count(hashSyncCheckpoint));
208 const CBlockIndex* pindexSync = mapBlockIndex[hashSyncCheckpoint];
210 if (nHeight > pindexSync->nHeight)
212 // trace back to same height as sync-checkpoint
213 const CBlockIndex* pindex = pindexPrev;
214 while (pindex->nHeight > pindexSync->nHeight)
215 if (!(pindex = pindex->pprev))
216 return error("CheckSync: pprev null - block index structure failure");
217 if (pindex->nHeight < pindexSync->nHeight || pindex->GetBlockHash() != hashSyncCheckpoint)
218 return false; // only descendant of sync-checkpoint can pass check
220 if (nHeight == pindexSync->nHeight && hashBlock != hashSyncCheckpoint)
221 return false; // same height with sync-checkpoint
222 if (nHeight < pindexSync->nHeight && !mapBlockIndex.count(hashBlock))
223 return false; // lower height than sync-checkpoint
228 bool WantedByPendingSyncCheckpoint(uint256 hashBlock)
230 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
232 if (hashPendingCheckpoint == 0)
234 if (hashBlock == hashPendingCheckpoint)
236 if (mapOrphanBlocks.count(hashPendingCheckpoint)
237 && hashBlock == WantedByOrphan(mapOrphanBlocks[hashPendingCheckpoint]))
243 // ppcoin: reset synchronized checkpoint to last hardened checkpoint
244 bool ResetSyncCheckpoint()
246 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
248 const uint256& hash = mapCheckpoints.rbegin()->second;
249 if (mapBlockIndex.count(hash) && !mapBlockIndex[hash]->IsInMainChain())
251 // checkpoint block accepted but not yet in main chain
252 printf("ResetSyncCheckpoint: Reorganize to hardened checkpoint %s\n", hash.ToString().c_str());
255 if (!Reorganize(txdb, mapBlockIndex[hash]))
258 return error("ResetSyncCheckpoint: Reorganize failed for hardened checkpoint %s", hash.ToString().c_str());
262 else if(!mapBlockIndex.count(hash))
264 // checkpoint block not yet accepted
265 hashPendingCheckpoint = hash;
266 checkpointMessagePending.SetNull();
267 printf("ResetSyncCheckpoint: pending for sync-checkpoint %s\n", hashPendingCheckpoint.ToString().c_str());
270 BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
272 const uint256& hash = i.second;
273 if (mapBlockIndex.count(hash) && mapBlockIndex[hash]->IsInMainChain())
275 if (!WriteSyncCheckpoint(hash))
276 return error("ResetSyncCheckpoint: failed to write sync checkpoint %s", hash.ToString().c_str());
277 printf("ResetSyncCheckpoint: sync-checkpoint reset to %s\n", hashSyncCheckpoint.ToString().c_str());
286 void AskForPendingSyncCheckpoint(CNode* pfrom)
288 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
289 if (pfrom && hashPendingCheckpoint != 0 && (!mapBlockIndex.count(hashPendingCheckpoint)) && (!mapOrphanBlocks.count(hashPendingCheckpoint)))
290 pfrom->AskFor(CInv(MSG_BLOCK, hashPendingCheckpoint));
294 // ppcoin: sync-checkpoint master key
295 const std::string CSyncCheckpoint::strMasterPubKey = "0424f20205e5da98ba632bbd278a11a6499585f62bfb2c782377ef59f0251daab8085fc31471bcb8180bc75ed0fa41bb50c7c084511d54015a3a5241d645c7268a";
297 // ppcoin: verify signature of sync-checkpoint message
298 bool CSyncCheckpoint::CheckSignature()
301 if (!key.SetPubKey(ParseHex(CSyncCheckpoint::strMasterPubKey)))
302 return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed");
303 if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
304 return error("CSyncCheckpoint::CheckSignature() : verify signature failed");
306 // Now unserialize the data
307 CDataStream sMsg(vchMsg);
308 sMsg >> *(CUnsignedSyncCheckpoint*)this;
312 // ppcoin: process synchronized checkpoint
313 bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom)
315 if (!CheckSignature())
318 CRITICAL_BLOCK(Checkpoints::cs_hashSyncCheckpoint)
320 if (!mapBlockIndex.count(hashCheckpoint))
322 // We haven't received the checkpoint chain, keep the checkpoint as pending
323 Checkpoints::hashPendingCheckpoint = hashCheckpoint;
324 Checkpoints::checkpointMessagePending = *this;
325 printf("ProcessSyncCheckpoint: pending for sync-checkpoint %s\n", hashCheckpoint.ToString().c_str());
326 // Ask this guy to fill in what we're missing
329 pfrom->PushGetBlocks(pindexBest, hashCheckpoint);
330 // ask directly as well in case rejected earlier by duplicate
331 // proof-of-stake because getblocks may not get it this time
332 pfrom->AskFor(CInv(MSG_BLOCK, mapOrphanBlocks.count(hashCheckpoint)? WantedByOrphan(mapOrphanBlocks[hashCheckpoint]) : hashCheckpoint));
337 if (!Checkpoints::ValidateSyncCheckpoint(hashCheckpoint))
341 CBlockIndex* pindexCheckpoint = mapBlockIndex[hashCheckpoint];
342 if (!pindexCheckpoint->IsInMainChain())
344 // checkpoint chain received but not yet main chain
346 if (!Reorganize(txdb, pindexCheckpoint))
349 Checkpoints::hashInvalidCheckpoint = hashCheckpoint;
350 return error("ProcessSyncCheckpoint: Reorganize failed for sync checkpoint %s", hashCheckpoint.ToString().c_str());
355 if (!Checkpoints::WriteSyncCheckpoint(hashCheckpoint))
356 return error("ProcessSyncCheckpoint(): failed to write sync checkpoint %s", hashCheckpoint.ToString().c_str());
357 Checkpoints::checkpointMessage = *this;
358 Checkpoints::hashPendingCheckpoint = 0;
359 Checkpoints::checkpointMessagePending.SetNull();
360 printf("ProcessSyncCheckpoint: sync-checkpoint at %s\n", hashCheckpoint.ToString().c_str());