3dec98bae8172773ba1b5c62a1431c48173863bd
[novacoin.git] / src / checkpoints.h
1 // Copyright (c) 2009-2012 The Bitcoin developers
2 // Copyright (c) 2011-2012 The PPCoin developers
3 // Copyright (c) 2012-2013 The NovaCoin developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_CHECKPOINT_H
7 #define  BITCOIN_CHECKPOINT_H
8
9 #include <map>
10 #include "net.h"
11 #include "util.h"
12
13 #define STAKE_MIN_AGE (60 * 60 * 24 * 30) // minimum age 30 days for coin age
14 #define STAKE_MAX_AGE (60 * 60 * 24 * 90) // stake age for full weight
15 #define CHECKPOINT_MAX_SPAN (60 * 60 * 4) // max 4 hours before latest block
16
17 class uint256;
18 class CBlockIndex;
19 class CSyncCheckpoint;
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     bool SetCheckpointPrivKey(std::string strPrivKey);
49     bool SendSyncCheckpoint(uint256 hashCheckpoint);
50     bool IsMatureSyncCheckpoint();
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