PPCoin: Define synchronized checkpoint
[novacoin.git] / src / checkpoints.cpp
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.
5
6 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
7 #include <boost/foreach.hpp>
8
9 #include "headers.h"
10 #include "checkpoints.h"
11
12 namespace Checkpoints
13 {
14     typedef std::map<int, uint256> MapCheckpoints;   // hardened checkpoints
15
16     //
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
20     //    timestamp before)
21     // + Contains no strange transactions
22     //
23     static MapCheckpoints mapCheckpoints =
24         boost::assign::map_list_of
25         ( 0, hashGenesisBlock )
26         ; // ppcoin: no checkpoint yet; to be created in future releases
27
28     bool CheckHardened(int nHeight, const uint256& hash)
29     {
30         if (fTestNet) return true; // Testnet has no checkpoints
31
32         MapCheckpoints::const_iterator i = mapCheckpoints.find(nHeight);
33         if (i == mapCheckpoints.end()) return true;
34         return hash == i->second;
35     }
36
37     int GetTotalBlocksEstimate()
38     {
39         if (fTestNet) return 0;
40
41         return mapCheckpoints.rbegin()->first;
42     }
43
44     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
45     {
46         if (fTestNet) return NULL;
47
48         int64 nResult;
49         BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
50         {
51             const uint256& hash = i.second;
52             std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
53             if (t != mapBlockIndex.end())
54                 return t->second;
55         }
56         return NULL;
57     }
58
59     // ppcoin: synchronized checkpoint (centrally broadcasted)
60     uint256 hashSyncCheckpoint;
61
62     bool AcceptNewSyncCheckpoint(uint256 hashCheckpoint)
63     {
64     }
65
66     // ppcoin: automatic checkpoint (represented by height of checkpoint)
67     int nAutoCheckpoint = 0;
68     int nBranchPoint = 0;    // branch point to alternative branch
69
70     // ppcoin: check automatic checkpoint
71     // To pass the check:
72     //   - All ancestors (including the block itself) have block index already
73     //   - The immediate ancestor in main chain must not have height less than
74     //     checkpoint height
75     bool CheckAuto(const CBlockIndex *pindex)
76     {
77         while (pindex)
78         {
79             if (pindex->IsInMainChain())
80             {
81                 if (pindex->nHeight >= nAutoCheckpoint)
82                     return true;
83                 else
84                 {
85                     nBranchPoint = pindex->nHeight;
86                     return error("Checkpoints: new block on alternative branch at height=%d before auto checkpoint at height=%d", pindex->nHeight, nAutoCheckpoint);
87                 }
88             }
89             else
90                 pindex = pindex->pprev;
91         }
92         return error("Checkpoints: failed to find any ancestor on main chain for the new block - internal error");
93     }
94
95     // ppcoin: get next chain checkpoint
96     int GetNextChainCheckpoint(const CBlockIndex *pindexLast)
97     {
98         CBigNum bnTarget;
99         CBigNum bnTargetMax = 0;  // max target of all blocks since checkpoint
100         CBigNum bnTargetMin = 0;  // min target of all candidate checkpoints
101         int nMinTargetHeight = 0; // min target height of candidate checkpoints
102         int nCheckpointMin = 0;   // minimum candidate checkpoint
103         int nCheckpointMax = 0;   // maximum candidate checkpoint
104         int nDepth = pindexLast->nHeight - pindexLast->nCheckpoint;
105         const CBlockIndex *pindex = pindexLast;
106         while (nDepth >= 0 && pindex)
107         {
108             bnTarget.SetCompact(pindex->nBits);
109             if (bnTarget > bnTargetMax)
110                 bnTargetMax = bnTarget;
111             if (nCheckpointMax > 0 && bnTarget < bnTargetMin)
112             {
113                 bnTargetMin = bnTarget;
114                 nMinTargetHeight = pindex->nHeight;
115             }
116             if (nCheckpointMax == 0 && pindexLast->GetBlockTime() - pindex->GetBlockTime() > AUTO_CHECKPOINT_MIN_SPAN)
117             {
118                 nCheckpointMax = pindex->nHeight;
119                 bnTargetMin.SetCompact(pindex->nBits);
120                 nMinTargetHeight = pindex->nHeight;
121             }
122             if (pindexLast->GetBlockTime() - pindex->GetBlockTime() < AUTO_CHECKPOINT_MAX_SPAN)
123                 nCheckpointMin = pindex->nHeight;
124             pindex = pindex->pprev;
125             nDepth--;
126         }
127
128         assert (nDepth == -1);  // arrive at chain checkpoint now
129
130         printf("Checkpoints: min=%d max=%d tminheight=%d tmin=0x%08x tmax=0x%08x\n",
131             nCheckpointMin, nCheckpointMax, nMinTargetHeight,
132             bnTargetMin.GetCompact(), bnTargetMax.GetCompact());
133         if (nCheckpointMax == 0) // checkpoint stays if max candidate not found
134             return pindexLast->nCheckpoint;
135
136         if (bnTargetMin * 100 > bnTargetMax * 90)
137             return nCheckpointMax;
138         if (bnTarget * 100 > bnTargetMax * 90)
139             return nMinTargetHeight;
140         else
141             return nCheckpointMin;
142     }
143
144     // ppcoin: get next auto checkpoint from the new chain checkpoint
145     int GetNextAutoCheckpoint(int nCheckpoint)
146     {
147         return (std::max(nAutoCheckpoint, nCheckpoint));
148     }
149
150     // ppcoin: advance to next automatic checkpoint
151     void AdvanceAutoCheckpoint(int nCheckpoint)
152     {
153         nAutoCheckpoint = GetNextAutoCheckpoint(nCheckpoint);
154         printf("Checkpoints: auto checkpoint now at height=%d\n", nAutoCheckpoint);
155     }
156
157     // ppcoin: reset auto checkpoint
158     bool ResetAutoCheckpoint(int nCheckpoint)
159     {
160         if (nCheckpoint <= 0 || nCheckpoint > nBestHeight)
161             return error("ResetAutoCheckpoint() : new checkpoint invalid");
162         if (nCheckpoint >= nAutoCheckpoint)
163             return error("ResetAutoCheckpoint() : new checkpoint not earlier than current auto checkpoint");
164         CTxDB txdb;
165         txdb.TxnBegin();
166         if (!txdb.WriteAutoCheckpoint(nCheckpoint, true))
167             return error("ResetAutoCheckpoint() : database write failed");
168         if (!txdb.TxnCommit())
169             return error("ResetAutoCheckpoint() : database commit failed");
170         nAutoCheckpoint = nCheckpoint;
171         nBranchPoint = 0;  // clear branch point
172
173         // clear ban list to accept alternative branches
174         CRITICAL_BLOCK(cs_vNodes)
175         {
176             BOOST_FOREACH(CNode* pnode, vNodes)
177                 pnode->ClearBanned();
178         }
179
180         return true;
181     }
182 }