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