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