Fix error while building on non-c++0x compilers
[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 <map>
8 #include "net.h"
9 #include "util.h"
10
11 #define CHECKPOINT_MAX_SPAN (60 * 60 * 4) // max 4 hours before latest block
12
13 class uint256;
14 class CBlockIndex;
15 class CSyncCheckpoint;
16
17 /** Block-chain checkpoints are compiled-in sanity checks.
18  * They are updated every release or three.
19  */
20 namespace Checkpoints
21 {
22     /** Checkpointing mode */
23     enum CPMode
24     {
25         // Scrict checkpoints policy, perform conflicts verification and resolve conflicts
26         STRICT = 0,
27         // Advisory checkpoints policy, perform conflicts verification but don't try to resolve them
28         ADVISORY = 1,
29         // Permissive checkpoints policy, don't perform any checking
30         PERMISSIVE = 2
31     };
32
33     // Returns true if block passes checkpoint checks
34     bool CheckHardened(int nHeight, const uint256& hash);
35
36     // Return conservative estimate of total number of blocks, 0 if unknown
37     int GetTotalBlocksEstimate();
38
39     // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
40     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
41
42     // Returns last checkpoint timestamp
43     int GetLastCheckpointTime();
44
45     extern uint256 hashSyncCheckpoint;
46     extern CSyncCheckpoint checkpointMessage;
47     extern uint256 hashInvalidCheckpoint;
48     extern CCriticalSection cs_hashSyncCheckpoint;
49
50     CBlockIndex* GetLastSyncCheckpoint();
51     bool WriteSyncCheckpoint(const uint256& hashCheckpoint);
52     bool AcceptPendingSyncCheckpoint();
53     uint256 AutoSelectSyncCheckpoint();
54     bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
55     bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
56     bool ResetSyncCheckpoint();
57     void AskForPendingSyncCheckpoint(CNode* pfrom);
58     bool SetCheckpointPrivKey(std::string strPrivKey);
59     bool SendSyncCheckpoint(uint256 hashCheckpoint);
60     bool IsMatureSyncCheckpoint();
61     bool IsSyncCheckpointTooOld(unsigned int nSeconds);
62 }
63
64 // ppcoin: synchronized checkpoint
65 class CUnsignedSyncCheckpoint
66 {
67 public:
68     int nVersion;
69     uint256 hashCheckpoint;      // checkpoint block
70
71     IMPLEMENT_SERIALIZE
72     (
73         READWRITE(this->nVersion);
74         nVersion = this->nVersion;
75         READWRITE(hashCheckpoint);
76     )
77
78     void SetNull()
79     {
80         nVersion = 1;
81         hashCheckpoint = 0;
82     }
83
84     std::string ToString() const
85     {
86         return strprintf(
87                 "CSyncCheckpoint(\n"
88                 "    nVersion       = %d\n"
89                 "    hashCheckpoint = %s\n"
90                 ")\n",
91             nVersion,
92             hashCheckpoint.ToString().c_str());
93     }
94
95     void print() const
96     {
97         printf("%s", ToString().c_str());
98     }
99 };
100
101 class CSyncCheckpoint : public CUnsignedSyncCheckpoint
102 {
103 public:
104     static const std::string strMasterPubKey;
105     static std::string strMasterPrivKey;
106
107     std::vector<unsigned char> vchMsg;
108     std::vector<unsigned char> vchSig;
109
110     CSyncCheckpoint()
111     {
112         SetNull();
113     }
114
115     IMPLEMENT_SERIALIZE
116     (
117         READWRITE(vchMsg);
118         READWRITE(vchSig);
119     )
120
121     void SetNull()
122     {
123         CUnsignedSyncCheckpoint::SetNull();
124         vchMsg.clear();
125         vchSig.clear();
126     }
127
128     bool IsNull() const
129     {
130         return (hashCheckpoint == 0);
131     }
132
133     uint256 GetHash() const
134     {
135         return SerializeHash(*this);
136     }
137
138     bool RelayTo(CNode* pnode) const
139     {
140         // returns true if wasn't already sent
141         if (pnode->hashCheckpointKnown != hashCheckpoint)
142         {
143             pnode->hashCheckpointKnown = hashCheckpoint;
144             pnode->PushMessage("checkpoint", *this);
145             return true;
146         }
147         return false;
148     }
149
150     bool CheckSignature();
151     bool ProcessSyncCheckpoint(CNode* pfrom);
152 };
153
154 #endif