PPCoin: Checkpoint master key as class constant
[novacoin.git] / src / checkpoints.h
1 // Copyright (c) 2011 The Bitcoin developers
2 // Copyright (c) 2011-2012 The PPCoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_CHECKPOINT_H
6 #define  BITCOIN_CHECKPOINT_H
7
8 #include <map>
9 #include "util.h"
10
11 // ppcoin: auto checkpoint min at 8 hours; max at 16 hours
12 #define AUTO_CHECKPOINT_MIN_SPAN   (60 * 60 * 8)
13 #define AUTO_CHECKPOINT_MAX_SPAN   (60 * 60 * 16)
14 #define AUTO_CHECKPOINT_TRUST_SPAN (60 * 60 * 24)
15
16 class uint256;
17 class CBlockIndex;
18
19 // ppcoin: synchronized checkpoint
20 class CUnsignedSyncCheckpoint
21 {
22 public:
23     int nVersion;
24     uint256 hashCheckpoint;      // checkpoint block
25
26     IMPLEMENT_SERIALIZE
27     (
28         READWRITE(this->nVersion);
29         nVersion = this->nVersion;
30         READWRITE(hashCheckpoint);
31     )
32
33     void SetNull()
34     {
35         nVersion = 1;
36         hashCheckpoint = 0;
37     }
38
39     std::string ToString() const
40     {
41         return strprintf(
42                 "CSyncCheckpoint(\n"
43                 "    nVersion       = %d\n"
44                 "    hashCheckpoint = %s\n"
45                 ")\n",
46             nVersion,
47             hashCheckpoint.ToString().c_str());
48     }
49
50     void print() const
51     {
52         printf("%s", ToString().c_str());
53     }
54 };
55
56 class CSyncCheckpoint : public CUnsignedSyncCheckpoint
57 {
58 public:
59     static const std::string strMasterPubKey;
60
61     std::vector<unsigned char> vchMsg;
62     std::vector<unsigned char> vchSig;
63
64     CSyncCheckpoint()
65     {
66         SetNull();
67     }
68
69     IMPLEMENT_SERIALIZE
70     (
71         READWRITE(vchMsg);
72         READWRITE(vchSig);
73     )
74
75     void SetNull()
76     {
77         CUnsignedSyncCheckpoint::SetNull();
78         vchMsg.clear();
79         vchSig.clear();
80     }
81
82     bool IsNull() const
83     {
84         return (hashCheckpoint == 0);
85     }
86
87     uint256 GetHash() const
88     {
89         return SerializeHash(*this);
90     }
91
92     bool RelayTo(CNode* pnode) const
93     {
94         // returns true if wasn't already sent
95         if (pnode->hashCheckpointKnown != hashCheckpoint)
96         {
97             pnode->hashCheckpointKnown = hashCheckpoint;
98             pnode->PushMessage("checkpoint", *this);
99             return true;
100         }
101         return false;
102     }
103
104     bool CheckSignature();
105     bool ProcessSyncCheckpoint(CNode* pfrom);
106 };
107
108 //
109 // Block-chain checkpoints are compiled-in sanity checks.
110 // They are updated every release or three.
111 //
112 namespace Checkpoints
113 {
114     // Returns true if block passes checkpoint checks
115     bool CheckHardened(int nHeight, const uint256& hash);
116
117     // Return conservative estimate of total number of blocks, 0 if unknown
118     int GetTotalBlocksEstimate();
119
120     // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
121     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
122
123     extern uint256 hashSyncCheckpoint;
124     extern CSyncCheckpoint checkpointMessage;
125     extern uint256 hashInvalidCheckpoint;
126     extern CCriticalSection cs_hashSyncCheckpoint;
127
128     bool AcceptPendingSyncCheckpoint();
129     uint256 AutoSelectSyncCheckpoint();
130     bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
131     bool IsPendingSyncCheckpoint(uint256 hashBlock);
132
133     // ppcoin: automatic checkpoint
134     extern int nAutoCheckpoint;
135     extern int nBranchPoint;
136
137     bool CheckAuto(const CBlockIndex *pindex);
138     int  GetNextChainCheckpoint(const CBlockIndex *pindex);
139     int  GetNextAutoCheckpoint(int nCheckpoint);
140     void AdvanceAutoCheckpoint(int nCheckpoint);
141     bool ResetAutoCheckpoint(int nCheckpoint);
142 }
143
144 #endif