PPCoin: Process and relay synchronized checkpoint
[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 //
20 // Block-chain checkpoints are compiled-in sanity checks.
21 // They are updated every release or three.
22 //
23 namespace Checkpoints
24 {
25     // Returns true if block passes checkpoint checks
26     bool CheckHardened(int nHeight, const uint256& hash);
27
28     // Return conservative estimate of total number of blocks, 0 if unknown
29     int GetTotalBlocksEstimate();
30
31     // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
32     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
33
34     // ppcoin: synchronized checkpoint
35     class CUnsignedSyncCheckpoint
36     {
37     public:
38         int nVersion;
39         uint256 hashCheckpoint;      // checkpoint block
40
41         IMPLEMENT_SERIALIZE
42         (
43             READWRITE(this->nVersion);
44             nVersion = this->nVersion;
45             READWRITE(hashCheckpoint);
46         )
47
48         void SetNull()
49         {
50             nVersion = 1;
51             hashCheckpoint = 0;
52         }
53
54         std::string ToString() const
55         {
56             return strprintf(
57                     "CSyncCheckpoint(\n"
58                     "    nVersion       = %d\n"
59                     "    hashCheckpoint = %s\n"
60                     ")\n",
61                 nVersion,
62                 hashCheckpoint.ToString().c_str());
63         }
64
65         void print() const
66         {
67             printf("%s", ToString().c_str());
68         }
69     };
70
71     class CSyncCheckpoint : public CUnsignedSyncCheckpoint
72     {
73     public:
74         std::vector<unsigned char> vchMsg;
75         std::vector<unsigned char> vchSig;
76
77         CSyncCheckpoint()
78         {
79             SetNull();
80         }
81
82         IMPLEMENT_SERIALIZE
83         (
84             READWRITE(vchMsg);
85             READWRITE(vchSig);
86         )
87
88         void SetNull()
89         {
90             CUnsignedSyncCheckpoint::SetNull();
91             vchMsg.clear();
92             vchSig.clear();
93         }
94
95         bool IsNull() const
96         {
97             return (hashCheckpoint == 0);
98         }
99
100         uint256 GetHash() const
101         {
102             return SerializeHash(*this);
103         }
104
105         bool RelayTo(CNode* pnode) const
106         {
107             // returns true if wasn't already sent
108             if (pnode->hashCheckpointKnown != hashCheckpoint)
109             {
110                 pnode->hashCheckpointKnown = hashCheckpoint;
111                 pnode->PushMessage("checkpoint", *this);
112                 return true;
113             }
114             return false;
115         }
116
117         bool CheckSignature()
118         {
119             CKey key;
120             if (!key.SetPubKey(ParseHex("0487ca85b6ae9d311f996c7616d20d0c88a5b4f07d25e78f419019f35cce6522acf978b2d99f0e7a58db1f120439e5c1889266927854aa57c93956c2569188a539")))
121                 return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed");
122             if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
123                 return error("CSyncCheckpoint::CheckSignature() : verify signature failed");
124
125             // Now unserialize the data
126             CDataStream sMsg(vchMsg);
127             sMsg >> *(CUnsignedSyncCheckpoint*)this;
128             return true;
129         }
130
131         bool ProcessSyncCheckpoint();
132     };
133
134     extern uint256 hashSyncCheckpoint;
135     extern CSyncCheckpoint checkpointMessage;
136     extern CCriticalSection cs_hashSyncCheckpoint;
137
138     // ppcoin: automatic checkpoint
139     extern int nAutoCheckpoint;
140     extern int nBranchPoint;
141
142     bool CheckAuto(const CBlockIndex *pindex);
143     int  GetNextChainCheckpoint(const CBlockIndex *pindex);
144     int  GetNextAutoCheckpoint(int nCheckpoint);
145     void AdvanceAutoCheckpoint(int nCheckpoint);
146     bool ResetAutoCheckpoint(int nCheckpoint);
147 }
148
149 #endif