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