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