PPCoin: Reset sync-checkpoint when checkpoint master key changes
[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 class CSyncCheckpoint;
19
20 //
21 // Block-chain checkpoints are compiled-in sanity checks.
22 // They are updated every release or three.
23 //
24 namespace Checkpoints
25 {
26     // Returns true if block passes checkpoint checks
27     bool CheckHardened(int nHeight, const uint256& hash);
28
29     // Return conservative estimate of total number of blocks, 0 if unknown
30     int GetTotalBlocksEstimate();
31
32     // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
33     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
34
35     extern uint256 hashSyncCheckpoint;
36     extern CSyncCheckpoint checkpointMessage;
37     extern uint256 hashInvalidCheckpoint;
38     extern CCriticalSection cs_hashSyncCheckpoint;
39
40     CBlockIndex* GetLastSyncCheckpoint();
41     bool WriteSyncCheckpoint(const uint256& hashCheckpoint);
42     bool AcceptPendingSyncCheckpoint();
43     uint256 AutoSelectSyncCheckpoint();
44     bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
45     bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
46     bool ResetSyncCheckpoint();
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
91     std::vector<unsigned char> vchMsg;
92     std::vector<unsigned char> vchSig;
93
94     CSyncCheckpoint()
95     {
96         SetNull();
97     }
98
99     IMPLEMENT_SERIALIZE
100     (
101         READWRITE(vchMsg);
102         READWRITE(vchSig);
103     )
104
105     void SetNull()
106     {
107         CUnsignedSyncCheckpoint::SetNull();
108         vchMsg.clear();
109         vchSig.clear();
110     }
111
112     bool IsNull() const
113     {
114         return (hashCheckpoint == 0);
115     }
116
117     uint256 GetHash() const
118     {
119         return SerializeHash(*this);
120     }
121
122     bool RelayTo(CNode* pnode) const
123     {
124         // returns true if wasn't already sent
125         if (pnode->hashCheckpointKnown != hashCheckpoint)
126         {
127             pnode->hashCheckpointKnown = hashCheckpoint;
128             pnode->PushMessage("checkpoint", *this);
129             return true;
130         }
131         return false;
132     }
133
134     bool CheckSignature();
135     bool ProcessSyncCheckpoint(CNode* pfrom);
136 };
137
138 #endif