PPCoin: RPC command 'sendcheckpoint'
[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     std::vector<unsigned char> vchMsg;
60     std::vector<unsigned char> vchSig;
61
62     CSyncCheckpoint()
63     {
64         SetNull();
65     }
66
67     IMPLEMENT_SERIALIZE
68     (
69         READWRITE(vchMsg);
70         READWRITE(vchSig);
71     )
72
73     void SetNull()
74     {
75         CUnsignedSyncCheckpoint::SetNull();
76         vchMsg.clear();
77         vchSig.clear();
78     }
79
80     bool IsNull() const
81     {
82         return (hashCheckpoint == 0);
83     }
84
85     uint256 GetHash() const
86     {
87         return SerializeHash(*this);
88     }
89
90     bool RelayTo(CNode* pnode) const
91     {
92         // returns true if wasn't already sent
93         if (pnode->hashCheckpointKnown != hashCheckpoint)
94         {
95             pnode->hashCheckpointKnown = hashCheckpoint;
96             pnode->PushMessage("checkpoint", *this);
97             return true;
98         }
99         return false;
100     }
101
102     bool CheckSignature()
103     {
104         CKey key;
105         if (!key.SetPubKey(ParseHex("04ea21daea8c15559870b5e93750ddc2f0c16bd0cb16636ba88c0746cfac07912ec7ad14111cc4aedda12c2687c920c7b7b62fd67ca14eed53f2d1704ec72362ce")))
106             return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed");
107         if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
108             return error("CSyncCheckpoint::CheckSignature() : verify signature failed");
109
110         // Now unserialize the data
111         CDataStream sMsg(vchMsg);
112         sMsg >> *(CUnsignedSyncCheckpoint*)this;
113         return true;
114     }
115
116     bool ProcessSyncCheckpoint(CNode* pfrom);
117 };
118
119 //
120 // Block-chain checkpoints are compiled-in sanity checks.
121 // They are updated every release or three.
122 //
123 namespace Checkpoints
124 {
125     // Returns true if block passes checkpoint checks
126     bool CheckHardened(int nHeight, const uint256& hash);
127
128     // Return conservative estimate of total number of blocks, 0 if unknown
129     int GetTotalBlocksEstimate();
130
131     // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
132     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
133
134     extern uint256 hashSyncCheckpoint;
135     extern CSyncCheckpoint checkpointMessage;
136     extern CCriticalSection cs_hashSyncCheckpoint;
137
138     bool AcceptPendingSyncCheckpoint(uint256 hashAcceptedBlock);
139
140     // ppcoin: automatic checkpoint
141     extern int nAutoCheckpoint;
142     extern int nBranchPoint;
143
144     bool CheckAuto(const CBlockIndex *pindex);
145     int  GetNextChainCheckpoint(const CBlockIndex *pindex);
146     int  GetNextAutoCheckpoint(int nCheckpoint);
147     void AdvanceAutoCheckpoint(int nCheckpoint);
148     bool ResetAutoCheckpoint(int nCheckpoint);
149 }
150
151 #endif