PPCoin: Define synchronized checkpoint message
[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     extern uint256 hashSyncCheckpoint;
36
37     class CUnsignedSyncCheckpoint
38     {
39     public:
40         int nVersion;
41         uint256 hashCheckpoint;      // checkpoint block
42
43         IMPLEMENT_SERIALIZE
44         (
45             READWRITE(this->nVersion);
46             nVersion = this->nVersion;
47             READWRITE(hashCheckpoint);
48         )
49
50         void SetNull()
51         {
52             nVersion = 1;
53             hashCheckpoint = 0;
54         }
55
56         std::string ToString() const
57         {
58             return strprintf(
59                     "CSyncCheckpoint(\n"
60                     "    nVersion       = %d\n"
61                     "    hashCheckpoint = %s\n"
62                     ")\n",
63                 nVersion,
64                 hashCheckpoint.ToString().c_str());
65         }
66
67         void print() const
68         {
69             printf("%s", ToString().c_str());
70         }
71     };
72
73     class CSyncCheckpoint : public CUnsignedSyncCheckpoint
74     {
75     public:
76         std::vector<unsigned char> vchMsg;
77         std::vector<unsigned char> vchSig;
78
79         CSyncCheckpoint()
80         {
81             SetNull();
82         }
83
84         IMPLEMENT_SERIALIZE
85         (
86             READWRITE(vchMsg);
87             READWRITE(vchSig);
88         )
89
90         void SetNull()
91         {
92             CUnsignedSyncCheckpoint::SetNull();
93             vchMsg.clear();
94             vchSig.clear();
95         }
96
97         bool IsNull() const
98         {
99             return (hashCheckpoint == 0);
100         }
101
102         uint256 GetHash() const
103         {
104             return SerializeHash(*this);
105         }
106
107         bool RelayTo(CNode* pnode) const
108         {
109             // returns true if wasn't already sent
110             if (pnode->hashCheckpointKnown != hashCheckpoint)
111             {
112                 pnode->hashCheckpointKnown = hashCheckpoint;
113                 pnode->PushMessage("checkpoint", *this);
114                 return true;
115             }
116             return false;
117         }
118
119         bool CheckSignature()
120         {
121             CKey key;
122             if (!key.SetPubKey(ParseHex("0487ca85b6ae9d311f996c7616d20d0c88a5b4f07d25e78f419019f35cce6522acf978b2d99f0e7a58db1f120439e5c1889266927854aa57c93956c2569188a539")))
123                 return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed");
124             if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
125                 return error("CSyncCheckpoint::CheckSignature() : verify signature failed");
126
127             // Now unserialize the data
128             CDataStream sMsg(vchMsg);
129             sMsg >> *(CUnsignedSyncCheckpoint*)this;
130             return true;
131         }
132
133         bool ProcessSyncCheckpoint();
134     };
135
136     // ppcoin: automatic checkpoint
137     extern int nAutoCheckpoint;
138     extern int nBranchPoint;
139
140     bool CheckAuto(const CBlockIndex *pindex);
141     int  GetNextChainCheckpoint(const CBlockIndex *pindex);
142     int  GetNextAutoCheckpoint(int nCheckpoint);
143     void AdvanceAutoCheckpoint(int nCheckpoint);
144     bool ResetAutoCheckpoint(int nCheckpoint);
145 }
146
147 #endif