PPCoin: Ask for pending sync-checkpoint block
[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     void AskForPendingSyncCheckpoint(CNode* pfrom);
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
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