Get rid of fTrickle parameter.
[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     // Returns true if block passes banlist checks
44     bool CheckBanned(const uint256 &nHash);
45
46     // Return conservative estimate of total number of blocks, 0 if unknown
47     int GetTotalBlocksEstimate();
48
49     // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
50     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
51
52     // Returns last checkpoint timestamp
53     unsigned int GetLastCheckpointTime();
54
55     extern uint256 hashSyncCheckpoint;
56     extern CSyncCheckpoint checkpointMessage;
57     extern uint256 hashInvalidCheckpoint;
58     extern CCriticalSection cs_hashSyncCheckpoint;
59
60     CBlockIndex* GetLastSyncCheckpoint();
61     bool WriteSyncCheckpoint(const uint256& hashCheckpoint);
62     bool AcceptPendingSyncCheckpoint();
63     uint256 AutoSelectSyncCheckpoint();
64     bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
65     bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
66     bool ResetSyncCheckpoint();
67     void AskForPendingSyncCheckpoint(CNode* pfrom);
68     bool SetCheckpointPrivKey(std::string strPrivKey);
69     bool SendSyncCheckpoint(uint256 hashCheckpoint);
70     bool IsMatureSyncCheckpoint();
71 }
72
73 // ppcoin: synchronized checkpoint
74 class CUnsignedSyncCheckpoint
75 {
76 public:
77     int nVersion;
78     uint256 hashCheckpoint;      // checkpoint block
79
80     IMPLEMENT_SERIALIZE
81     (
82         READWRITE(this->nVersion);
83         nVersion = this->nVersion;
84         READWRITE(hashCheckpoint);
85     )
86
87     void SetNull()
88     {
89         nVersion = 1;
90         hashCheckpoint = 0;
91     }
92
93     std::string ToString() const
94     {
95         return strprintf(
96                 "CSyncCheckpoint(\n"
97                 "    nVersion       = %d\n"
98                 "    hashCheckpoint = %s\n"
99                 ")\n",
100             nVersion,
101             hashCheckpoint.ToString().c_str());
102     }
103
104     void print() const
105     {
106         printf("%s", ToString().c_str());
107     }
108 };
109
110 class CSyncCheckpoint : public CUnsignedSyncCheckpoint
111 {
112 public:
113     static const std::string strMasterPubKey;
114     static std::string strMasterPrivKey;
115
116     std::vector<unsigned char> vchMsg;
117     std::vector<unsigned char> vchSig;
118
119     CSyncCheckpoint()
120     {
121         SetNull();
122     }
123
124     IMPLEMENT_SERIALIZE
125     (
126         READWRITE(vchMsg);
127         READWRITE(vchSig);
128     )
129
130     void SetNull()
131     {
132         CUnsignedSyncCheckpoint::SetNull();
133         vchMsg.clear();
134         vchSig.clear();
135     }
136
137     bool IsNull() const
138     {
139         return (hashCheckpoint == 0);
140     }
141
142     uint256 GetHash() const
143     {
144         return SerializeHash(*this);
145     }
146
147     bool RelayTo(CNode* pnode) const
148     {
149         // returns true if wasn't already sent
150         if (pnode->hashCheckpointKnown != hashCheckpoint)
151         {
152             pnode->hashCheckpointKnown = hashCheckpoint;
153             pnode->PushMessage("checkpoint", *this);
154             return true;
155         }
156         return false;
157     }
158
159     bool CheckSignature();
160     bool ProcessSyncCheckpoint(CNode* pfrom);
161 };
162
163 #endif