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