PPCoin: RPC 'makekeypair' limits loop to avoid hang
[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 #define STAKE_MIN_AGE (60 * 60 * 24)      // minimum age for coin age
12 #define CHECKPOINT_MIN_SPAN (60 * 60 * 4) // 4 hours checkpoint
13
14 class uint256;
15 class CBlockIndex;
16 class CSyncCheckpoint;
17
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 }
47
48 // ppcoin: synchronized checkpoint
49 class CUnsignedSyncCheckpoint
50 {
51 public:
52     int nVersion;
53     uint256 hashCheckpoint;      // checkpoint block
54
55     IMPLEMENT_SERIALIZE
56     (
57         READWRITE(this->nVersion);
58         nVersion = this->nVersion;
59         READWRITE(hashCheckpoint);
60     )
61
62     void SetNull()
63     {
64         nVersion = 1;
65         hashCheckpoint = 0;
66     }
67
68     std::string ToString() const
69     {
70         return strprintf(
71                 "CSyncCheckpoint(\n"
72                 "    nVersion       = %d\n"
73                 "    hashCheckpoint = %s\n"
74                 ")\n",
75             nVersion,
76             hashCheckpoint.ToString().c_str());
77     }
78
79     void print() const
80     {
81         printf("%s", ToString().c_str());
82     }
83 };
84
85 class CSyncCheckpoint : public CUnsignedSyncCheckpoint
86 {
87 public:
88     static const std::string strMasterPubKey;
89
90     std::vector<unsigned char> vchMsg;
91     std::vector<unsigned char> vchSig;
92
93     CSyncCheckpoint()
94     {
95         SetNull();
96     }
97
98     IMPLEMENT_SERIALIZE
99     (
100         READWRITE(vchMsg);
101         READWRITE(vchSig);
102     )
103
104     void SetNull()
105     {
106         CUnsignedSyncCheckpoint::SetNull();
107         vchMsg.clear();
108         vchSig.clear();
109     }
110
111     bool IsNull() const
112     {
113         return (hashCheckpoint == 0);
114     }
115
116     uint256 GetHash() const
117     {
118         return SerializeHash(*this);
119     }
120
121     bool RelayTo(CNode* pnode) const
122     {
123         // returns true if wasn't already sent
124         if (pnode->hashCheckpointKnown != hashCheckpoint)
125         {
126             pnode->hashCheckpointKnown = hashCheckpoint;
127             pnode->PushMessage("checkpoint", *this);
128             return true;
129         }
130         return false;
131     }
132
133     bool CheckSignature();
134     bool ProcessSyncCheckpoint(CNode* pfrom);
135 };
136
137 #endif