701e253ea4c1b78a3ff7c308b3803ff40726be5b
[novacoin.git] / src / checkpoints.h
1 // Copyright (c) 2009-2012 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef BITCOIN_CHECKPOINT_H
5 #define  BITCOIN_CHECKPOINT_H
6
7 #include "util.h"
8 #include "sync.h"
9 #include "uint256.h"
10
11 #include <map>
12
13 // max 1 hour before latest block
14 static const int64_t CHECKPOINT_MAX_SPAN = nOneHour;
15
16 #ifdef WIN32
17 #undef STRICT
18 #undef PERMISSIVE
19 #undef ADVISORY
20 #endif
21
22 class CBlockIndex;
23 class CSyncCheckpoint;
24 class CNode;
25
26 /** Block-chain checkpoints are compiled-in sanity checks.
27  * They are updated every release or three.
28  */
29 namespace Checkpoints
30 {
31     /** Checkpointing mode */
32     enum CPMode
33     {
34         // Scrict checkpoints policy, perform conflicts verification and resolve conflicts
35         STRICT = 0,
36         // Advisory checkpoints policy, perform conflicts verification but don't try to resolve them
37         ADVISORY = 1,
38         // Permissive checkpoints policy, don't perform any checking
39         PERMISSIVE = 2
40     };
41
42     // Returns true if block passes checkpoint checks
43     bool CheckHardened(int nHeight, const uint256& hash);
44
45     // Returns true if block passes banlist checks
46     bool CheckBanned(const uint256 &nHash);
47
48     // Return conservative estimate of total number of blocks, 0 if unknown
49     int GetTotalBlocksEstimate();
50
51     // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
52     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
53
54     // Returns last checkpoint timestamp
55     unsigned int GetLastCheckpointTime();
56
57     extern uint256 hashSyncCheckpoint;
58     extern CSyncCheckpoint checkpointMessage;
59     extern uint256 hashInvalidCheckpoint;
60     extern CCriticalSection cs_hashSyncCheckpoint;
61
62     CBlockIndex* GetLastSyncCheckpoint();
63     bool WriteSyncCheckpoint(const uint256& hashCheckpoint);
64     bool AcceptPendingSyncCheckpoint();
65     uint256 AutoSelectSyncCheckpoint();
66     bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
67     bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
68     bool ResetSyncCheckpoint();
69     void AskForPendingSyncCheckpoint(CNode* pfrom);
70     bool SetCheckpointPrivKey(std::string strPrivKey);
71     bool SendSyncCheckpoint(uint256 hashCheckpoint);
72     bool IsMatureSyncCheckpoint();
73 }
74
75 // ppcoin: synchronized checkpoint
76 class CUnsignedSyncCheckpoint
77 {
78 public:
79     int nVersion;
80     uint256 hashCheckpoint;      // checkpoint block
81
82     IMPLEMENT_SERIALIZE
83     (
84         READWRITE(this->nVersion);
85         nVersion = this->nVersion;
86         READWRITE(hashCheckpoint);
87     )
88
89     void SetNull()
90     {
91         nVersion = 1;
92         hashCheckpoint = 0;
93     }
94
95     std::string ToString() const
96     {
97         return strprintf(
98                 "CSyncCheckpoint(\n"
99                 "    nVersion       = %d\n"
100                 "    hashCheckpoint = %s\n"
101                 ")\n",
102             nVersion,
103             hashCheckpoint.ToString().c_str());
104     }
105 };
106
107 class CSyncCheckpoint : public CUnsignedSyncCheckpoint
108 {
109 public:
110     static const std::string strMasterPubKey;
111     static std::string strMasterPrivKey;
112
113     std::vector<unsigned char> vchMsg;
114     std::vector<unsigned char> vchSig;
115
116     CSyncCheckpoint();
117
118     IMPLEMENT_SERIALIZE
119     (
120         READWRITE(vchMsg);
121         READWRITE(vchSig);
122     )
123
124     void SetNull();
125     bool IsNull() const;
126     uint256 GetHash() const;
127     bool RelayTo(CNode* pnode) const;
128     bool CheckSignature();
129     bool ProcessSyncCheckpoint(CNode* pfrom);
130 };
131
132 #endif