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