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 uint256 hashInvalidCheckpoint = 0;
64 CCriticalSection cs_hashSyncCheckpoint;
66 // ppcoin: get last synchronized checkpoint
67 CBlockIndex* GetLastSyncCheckpoint()
69 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
71 if (!mapBlockIndex.count(hashSyncCheckpoint))
72 error("GetSyncCheckpoint: block index missing for current sync-checkpoint %s", hashSyncCheckpoint.ToString().c_str());
74 return mapBlockIndex[hashSyncCheckpoint];
79 // ppcoin: only descendant of current sync-checkpoint is allowed
80 bool ValidateSyncCheckpoint(uint256 hashCheckpoint)
82 if (!mapBlockIndex.count(hashSyncCheckpoint))
83 return error("ValidateSyncCheckpoint: block index missing for current sync-checkpoint %s", hashSyncCheckpoint.ToString().c_str());
84 if (!mapBlockIndex.count(hashCheckpoint))
85 return error("ValidateSyncCheckpoint: block index missing for received sync-checkpoint %s", hashCheckpoint.ToString().c_str());
87 CBlockIndex* pindexSyncCheckpoint = mapBlockIndex[hashSyncCheckpoint];
88 CBlockIndex* pindexCheckpointRecv = mapBlockIndex[hashCheckpoint];
90 if (pindexCheckpointRecv->nHeight <= pindexSyncCheckpoint->nHeight)
92 // Received an older checkpoint, trace back from current checkpoint
93 // to the same height of the received checkpoint to verify
94 // that current checkpoint should be a descendant block
95 CBlockIndex* pindex = pindexSyncCheckpoint;
96 while (pindex->nHeight > pindexCheckpointRecv->nHeight)
97 if (!(pindex = pindex->pprev))
98 return error("ValidateSyncCheckpoint: pprev1 null - block index structure failure");
99 if (pindex->GetBlockHash() != hashCheckpoint)
101 hashInvalidCheckpoint = hashCheckpoint;
102 return error("ValidateSyncCheckpoint: new sync-checkpoint %s is conflicting with current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str());
104 return false; // ignore older checkpoint
107 // Received checkpoint should be a descendant block of the current
108 // checkpoint. Trace back to the same height of current checkpoint
110 CBlockIndex* pindex = pindexCheckpointRecv;
111 while (pindex->nHeight > pindexSyncCheckpoint->nHeight)
112 if (!(pindex = pindex->pprev))
113 return error("ValidateSyncCheckpoint: pprev2 null - block index structure failure");
114 if (pindex->GetBlockHash() != hashSyncCheckpoint)
116 hashInvalidCheckpoint = hashCheckpoint;
117 return error("ValidateSyncCheckpoint: new sync-checkpoint %s is not a descendant of current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str());
122 bool AcceptPendingSyncCheckpoint()
124 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
126 if ((!checkpointMessagePending.IsNull()) && mapBlockIndex.count(checkpointMessagePending.hashCheckpoint))
128 if (!ValidateSyncCheckpoint(checkpointMessagePending.hashCheckpoint))
130 checkpointMessagePending.SetNull();
135 CBlockIndex* pindexCheckpoint = mapBlockIndex[checkpointMessagePending.hashCheckpoint];
136 if (!pindexCheckpoint->IsInMainChain())
139 if (!Reorganize(txdb, pindexCheckpoint))
142 hashInvalidCheckpoint = checkpointMessagePending.hashCheckpoint;
143 return error("ProcessSyncCheckpoint: Reorganize failed for sync checkpoint %s", checkpointMessagePending.hashCheckpoint.ToString().c_str());
148 if (!txdb.WriteSyncCheckpoint(checkpointMessagePending.hashCheckpoint))
151 return error("AcceptPendingSyncCheckpoint() : failed to write to db sync checkpoint %s\n", checkpointMessagePending.hashCheckpoint.ToString().c_str());
153 if (!txdb.TxnCommit())
154 return error("AcceptPendingSyncCheckpoint() : failed to commit to db sync checkpoint %s\n", checkpointMessagePending.hashCheckpoint.ToString().c_str());
157 hashSyncCheckpoint = checkpointMessagePending.hashCheckpoint;
158 checkpointMessage = checkpointMessagePending;
159 checkpointMessagePending.SetNull();
160 printf("AcceptPendingSyncCheckpoint : sync-checkpoint at %s\n", hashSyncCheckpoint.ToString().c_str());
161 // relay the checkpoint
162 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
163 BOOST_FOREACH(CNode* pnode, vNodes)
164 checkpointMessage.RelayTo(pnode);
172 uint256 AutoSelectSyncCheckpoint()
174 // select block roughly 8 hours ago
175 CBlockIndex *pindex = mapBlockIndex[hashSyncCheckpoint];
176 while (pindex->pnext && pindex->pnext->GetBlockTime() + AUTO_CHECKPOINT_MIN_SPAN <= GetAdjustedTime())
177 pindex = pindex->pnext;
178 return pindex->GetBlockHash();
181 // Check against synchronized checkpoint
182 bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev)
184 if (fTestNet) return true; // Testnet has no checkpoints
185 int nHeight = pindexPrev->nHeight + 1;
187 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
189 // sync-checkpoint should always be accepted block
190 assert(mapBlockIndex.count(hashSyncCheckpoint));
191 const CBlockIndex* pindexSync = mapBlockIndex[hashSyncCheckpoint];
193 if (nHeight > pindexSync->nHeight)
195 // trace back to same height as sync-checkpoint
196 const CBlockIndex* pindex = pindexPrev;
197 while (pindex->nHeight > pindexSync->nHeight)
198 if (!(pindex = pindex->pprev))
199 return error("CheckSync: pprev null - block index structure failure");
200 if (pindex->nHeight < pindexSync->nHeight || pindex->GetBlockHash() != hashSyncCheckpoint)
201 return false; // only descendant of sync-checkpoint can pass check
203 if (nHeight == pindexSync->nHeight && hashBlock != hashSyncCheckpoint)
204 return false; // same height with sync-checkpoint
205 if (nHeight < pindexSync->nHeight && !mapBlockIndex.count(hashBlock))
206 return false; // lower height than sync-checkpoint
211 bool WantedByPendingSyncCheckpoint(uint256 hashBlock)
213 CRITICAL_BLOCK(cs_hashSyncCheckpoint)
215 if (checkpointMessagePending.IsNull())
217 if (hashBlock == checkpointMessagePending.hashCheckpoint)
219 if (mapOrphanBlocks.count(checkpointMessagePending.hashCheckpoint)
220 && hashBlock == WantedByOrphan(mapOrphanBlocks[checkpointMessagePending.hashCheckpoint]))
227 // ppcoin: sync-checkpoint master key
228 const std::string CSyncCheckpoint::strMasterPubKey = "0424f20205e5da98ba632bbd278a11a6499585f62bfb2c782377ef59f0251daab8085fc31471bcb8180bc75ed0fa41bb50c7c084511d54015a3a5241d645c7268a";
230 // ppcoin: verify signature of sync-checkpoint message
231 bool CSyncCheckpoint::CheckSignature()
234 if (!key.SetPubKey(ParseHex(CSyncCheckpoint::strMasterPubKey)))
235 return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed");
236 if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
237 return error("CSyncCheckpoint::CheckSignature() : verify signature failed");
239 // Now unserialize the data
240 CDataStream sMsg(vchMsg);
241 sMsg >> *(CUnsignedSyncCheckpoint*)this;
245 // ppcoin: process synchronized checkpoint
246 bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom)
248 if (!CheckSignature())
251 CRITICAL_BLOCK(Checkpoints::cs_hashSyncCheckpoint)
253 if (!mapBlockIndex.count(hashCheckpoint))
255 // We haven't received the checkpoint chain, keep the checkpoint as pending
256 Checkpoints::checkpointMessagePending = *this;
257 printf("ProcessSyncCheckpoint: pending for sync-checkpoint %s\n", hashCheckpoint.ToString().c_str());
258 // Ask this guy to fill in what we're missing
261 pfrom->PushGetBlocks(pindexBest, hashCheckpoint);
262 // ask directly as well in case rejected earlier by duplicate
263 // proof-of-stake because getblocks may not get it this time
264 pfrom->AskFor(CInv(MSG_BLOCK, mapOrphanBlocks.count(hashCheckpoint)? WantedByOrphan(mapOrphanBlocks[hashCheckpoint]) : hashCheckpoint));
269 if (!Checkpoints::ValidateSyncCheckpoint(hashCheckpoint))
273 CBlockIndex* pindexCheckpoint = mapBlockIndex[hashCheckpoint];
274 if (!pindexCheckpoint->IsInMainChain())
276 // checkpoint chain received but not yet main chain
278 if (!Reorganize(txdb, pindexCheckpoint))
281 Checkpoints::hashInvalidCheckpoint = hashCheckpoint;
282 return error("ProcessSyncCheckpoint: Reorganize failed for sync checkpoint %s", hashCheckpoint.ToString().c_str());
287 if (!txdb.WriteSyncCheckpoint(hashCheckpoint))
290 return error("ProcessSyncCheckpoint(): failed to write to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
292 if (!txdb.TxnCommit())
293 return error("ProcessSyncCheckpoint(): failed to commit to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
296 Checkpoints::hashSyncCheckpoint = hashCheckpoint;
297 Checkpoints::checkpointMessage = *this;
298 Checkpoints::checkpointMessagePending.SetNull();
299 printf("ProcessSyncCheckpoint: sync-checkpoint at %s\n", hashCheckpoint.ToString().c_str());