Minor optimization
[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     // Returns last checkpoint timestamp
32     int GetLastCheckpointTime();
33
34     extern uint256 hashSyncCheckpoint;
35     extern CSyncCheckpoint checkpointMessage;
36     extern uint256 hashInvalidCheckpoint;
37     extern CCriticalSection cs_hashSyncCheckpoint;
38
39     CBlockIndex* GetLastSyncCheckpoint();
40     bool WriteSyncCheckpoint(const uint256& hashCheckpoint);
41     bool AcceptPendingSyncCheckpoint();
42     uint256 AutoSelectSyncCheckpoint();
43     bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
44     bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
45     bool ResetSyncCheckpoint();
46     void AskForPendingSyncCheckpoint(CNode* pfrom);
47     bool SetCheckpointPrivKey(std::string strPrivKey);
48     bool SendSyncCheckpoint(uint256 hashCheckpoint);
49     bool IsMatureSyncCheckpoint();
50     bool IsSyncCheckpointTooOld(unsigned int nSeconds);
51 }
52
53 // ppcoin: synchronized checkpoint
54 class CUnsignedSyncCheckpoint
55 {
56 public:
57     int nVersion;
58     uint256 hashCheckpoint;      // checkpoint block
59
60     IMPLEMENT_SERIALIZE
61     (
62         READWRITE(this->nVersion);
63         nVersion = this->nVersion;
64         READWRITE(hashCheckpoint);
65     )
66
67     void SetNull()
68     {
69         nVersion = 1;
70         hashCheckpoint = 0;
71     }
72
73     std::string ToString() const
74     {
75         return strprintf(
76                 "CSyncCheckpoint(\n"
77                 "    nVersion       = %d\n"
78                 "    hashCheckpoint = %s\n"
79                 ")\n",
80             nVersion,
81             hashCheckpoint.ToString().c_str());
82     }
83
84     void print() const
85     {
86         printf("%s", ToString().c_str());
87     }
88 };
89
90 class CSyncCheckpoint : public CUnsignedSyncCheckpoint
91 {
92 public:
93     static const std::string strMasterPubKey;
94     static std::string strMasterPrivKey;
95
96     std::vector<unsigned char> vchMsg;
97     std::vector<unsigned char> vchSig;
98
99     CSyncCheckpoint()
100     {
101         SetNull();
102     }
103
104     IMPLEMENT_SERIALIZE
105     (
106         READWRITE(vchMsg);
107         READWRITE(vchSig);
108     )
109
110     void SetNull()
111     {
112         CUnsignedSyncCheckpoint::SetNull();
113         vchMsg.clear();
114         vchSig.clear();
115     }
116
117     bool IsNull() const
118     {
119         return (hashCheckpoint == 0);
120     }
121
122     uint256 GetHash() const
123     {
124         return SerializeHash(*this);
125     }
126
127     bool RelayTo(CNode* pnode) const
128     {
129         // returns true if wasn't already sent
130         if (pnode->hashCheckpointKnown != hashCheckpoint)
131         {
132             pnode->hashCheckpointKnown = hashCheckpoint;
133             pnode->PushMessage("checkpoint", *this);
134             return true;
135         }
136         return false;
137     }
138
139     bool CheckSignature();
140     bool ProcessSyncCheckpoint(CNode* pfrom);
141 };
142
143 #endif