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