PPCoin: Additional block/coinbase timestamp rules; various minor cleanups
[novacoin.git] / src / checkpoints.cpp
1 // Copyright (c) 2011 The Bitcoin developers
2 // Copyright (c) 2011 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
31     bool CheckHardened(int nHeight, const uint256& hash)
32     {
33         if (fTestNet) return true; // Testnet has no checkpoints
34
35         MapCheckpoints::const_iterator i = mapCheckpoints.find(nHeight);
36         if (i == mapCheckpoints.end()) return true;
37         return hash == i->second;
38     }
39
40     // ppcoin: check automatic checkpoint
41     // To pass the check:
42     //   - All ancestors (including the block itself) have block index already
43     //   - The immediate ancestor in main chain must not have height less than
44     //     checkpoint height
45     bool CheckAuto(const CBlockIndex *pindex)
46     {
47         while (pindex)
48         {
49             if (pindex->IsInMainChain())
50             {
51                 if (pindex->nHeight >= nAutoCheckpoint)
52                     return true;
53                 else
54                     return error("Checkpoints: new block on alternative branch at height=%d before auto checkpoint at height=%d", pindex->nHeight, nAutoCheckpoint);
55             }
56             else
57                 pindex = pindex->pprev;
58         }
59         return error("Checkpoints: failed to find any ancestor on main chain for the new block - internal error");
60     }
61
62     // ppcoin: get next chain checkpoint
63     int GetNextChainCheckpoint(const CBlockIndex *pindexLast)
64     {
65         CBigNum bnTarget;
66         CBigNum bnTargetMax = 0;  // max target of all blocks since checkpoint
67         CBigNum bnTargetMin = 0;  // min target of all candidate checkpoints
68         int nMinTargetHeight = 0; // min target height of candidate checkpoints
69         int nCheckpointMin = 0;   // minimum candidate checkpoint
70         int nCheckpointMax = 0;   // maximum candidate checkpoint
71         int nDepth = pindexLast->nHeight - pindexLast->nCheckpoint;
72         const CBlockIndex *pindex = pindexLast;
73         while (nDepth >= 0 && pindex)
74         {
75             bnTarget.SetCompact(pindex->nBits);
76             if (bnTarget > bnTargetMax)
77                 bnTargetMax = bnTarget;
78             if (nCheckpointMax > 0 && bnTarget < bnTargetMin)
79             {
80                 bnTargetMin = bnTarget;
81                 nMinTargetHeight = pindex->nHeight;
82             }
83             if (nCheckpointMax == 0 && pindexLast->GetBlockTime() - pindex->GetBlockTime() > AUTO_CHECKPOINT_MIN_SPAN)
84             {
85                 nCheckpointMax = pindex->nHeight;
86                 bnTargetMin.SetCompact(pindex->nBits);
87                 nMinTargetHeight = pindex->nHeight;
88             }
89             if (pindexLast->GetBlockTime() - pindex->GetBlockTime() < AUTO_CHECKPOINT_MAX_SPAN)
90                 nCheckpointMin = pindex->nHeight;
91             pindex = pindex->pprev;
92             nDepth--;
93         }
94
95         assert (nDepth == -1);  // arrive at chain checkpoint now
96
97         printf("Checkpoints: min=%d max=%d tminheight=%d tmin=0x%08x tmax=0x%08x\n",
98             nCheckpointMin, nCheckpointMax, nMinTargetHeight,
99             bnTargetMin.GetCompact(), bnTargetMax.GetCompact());
100         if (nCheckpointMax == 0) // checkpoint stays if max candidate not found
101             return pindexLast->nCheckpoint;
102
103         if (bnTargetMin * 100 > bnTargetMax * 90)
104             return nCheckpointMax;
105         if (bnTarget * 100 > bnTargetMax * 90)
106             return nMinTargetHeight;
107         else
108             return nCheckpointMin;
109     }
110
111     // ppcoin: get next auto checkpoint from the new chain checkpoint
112     int GetNextAutoCheckpoint(int nCheckpoint)
113     {
114         return (std::max(nAutoCheckpoint, nCheckpoint));
115     }
116
117     // ppcoin: advance to next automatic checkpoint
118     void AdvanceAutoCheckpoint(int nCheckpoint)
119     {
120         nAutoCheckpoint = GetNextAutoCheckpoint(nCheckpoint);
121         printf("Checkpoints: auto checkpoint now at height=%d\n", nAutoCheckpoint);
122     }
123
124     int GetTotalBlocksEstimate()
125     {
126         if (fTestNet) return 0;
127
128         return mapCheckpoints.rbegin()->first;
129     }
130
131     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
132     {
133         if (fTestNet) return NULL;
134
135         int64 nResult;
136         BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
137         {
138             const uint256& hash = i.second;
139             std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
140             if (t != mapBlockIndex.end())
141                 return t->second;
142         }
143         return NULL;
144     }
145 }