Merge bitcoin v0.5.1 into ppcoin
[novacoin.git] / src / checkpoints.cpp
1 // Copyright (c) 2011 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
6 #include <boost/foreach.hpp>
7
8 #include "headers.h"
9 #include "checkpoints.h"
10
11 namespace Checkpoints
12 {
13     typedef std::map<int, uint256> MapCheckpoints;
14
15     //
16     // What makes a good checkpoint block?
17     // + Is surrounded by blocks with reasonable timestamps
18     //   (no blocks before with a timestamp after, none after with
19     //    timestamp before)
20     // + Contains no strange transactions
21     //
22     static MapCheckpoints mapCheckpoints =
23         boost::assign::map_list_of
24         ( 0, hashGenesisBlock )
25         ; // ppcoin: no checkpoint yet; to be created in future releases
26
27     bool CheckBlock(int nHeight, const uint256& hash)
28     {
29         if (fTestNet) return true; // Testnet has no checkpoints
30
31         MapCheckpoints::const_iterator i = mapCheckpoints.find(nHeight);
32         if (i == mapCheckpoints.end()) return true;
33         return hash == i->second;
34     }
35
36     int GetTotalBlocksEstimate()
37     {
38         if (fTestNet) return 0;
39
40         return mapCheckpoints.rbegin()->first;
41     }
42
43     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
44     {
45         if (fTestNet) return NULL;
46
47         int64 nResult;
48         BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
49         {
50             const uint256& hash = i.second;
51             std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
52             if (t != mapBlockIndex.end())
53                 return t->second;
54         }
55         return NULL;
56     }
57 }