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