|
5 | 5 | #ifndef PIVX_ZEROCOIN_H |
6 | 6 | #define PIVX_ZEROCOIN_H |
7 | 7 |
|
8 | | -#include <amount.h> |
9 | | -#include <limits.h> |
10 | | -#include <chainparams.h> |
| 8 | +#include "uint256.h" |
11 | 9 | #include "libzerocoin/bignum.h" |
12 | | -#include "libzerocoin/Denominations.h" |
13 | | -#include "key.h" |
14 | | -#include "serialize.h" |
15 | 10 |
|
16 | | -//struct that is safe to store essential mint data, without holding any information that allows for actual spending (serial, randomness, private key) |
17 | | -struct CMintMeta |
18 | | -{ |
19 | | - int nHeight; |
20 | | - uint256 hashSerial; |
21 | | - uint256 hashPubcoin; |
22 | | - uint256 hashStake; //requires different hashing method than hashSerial above |
23 | | - uint8_t nVersion; |
24 | | - libzerocoin::CoinDenomination denom; |
25 | | - uint256 txid; |
26 | | - bool isUsed; |
27 | | - bool isArchived; |
28 | | - bool isDeterministic; |
29 | | - bool isSeedCorrect; |
30 | | - |
31 | | - bool operator <(const CMintMeta& a) const; |
32 | | -}; |
33 | | - |
34 | | -uint256 GetSerialHash(const CBigNum& bnSerial); |
35 | 11 | uint256 GetPubCoinHash(const CBigNum& bnValue); |
36 | 12 |
|
37 | | -class CZerocoinMint |
38 | | -{ |
39 | | -private: |
40 | | - libzerocoin::CoinDenomination denomination; |
41 | | - int nHeight; |
42 | | - CBigNum value; |
43 | | - CBigNum randomness; |
44 | | - CBigNum serialNumber; |
45 | | - uint256 txid; |
46 | | - int outputIndex = -1; |
47 | | - CPrivKey privkey; |
48 | | - uint8_t version; |
49 | | - bool isUsed; |
50 | | - |
51 | | -public: |
52 | | - static const int STAKABLE_VERSION = 2; |
53 | | - static const int CURRENT_VERSION = 2; |
54 | | - |
55 | | - CZerocoinMint() |
56 | | - { |
57 | | - SetNull(); |
58 | | - } |
59 | | - |
60 | | - CZerocoinMint(libzerocoin::CoinDenomination denom, const CBigNum& value, const CBigNum& randomness, const CBigNum& serialNumber, bool isUsed, const uint8_t& nVersion, CPrivKey* privkey = nullptr) |
61 | | - { |
62 | | - SetNull(); |
63 | | - this->denomination = denom; |
64 | | - this->value = value; |
65 | | - this->randomness = randomness; |
66 | | - this->serialNumber = serialNumber; |
67 | | - this->isUsed = isUsed; |
68 | | - this->version = nVersion; |
69 | | - if (nVersion >= 2 && privkey) |
70 | | - this->privkey = *privkey; |
71 | | - } |
72 | | - |
73 | | - void SetNull() |
74 | | - { |
75 | | - isUsed = false; |
76 | | - randomness = 0; |
77 | | - value = 0; |
78 | | - denomination = libzerocoin::ZQ_ERROR; |
79 | | - nHeight = 0; |
80 | | - txid.SetNull(); |
81 | | - version = 1; |
82 | | - privkey.clear(); |
83 | | - } |
84 | | - |
85 | | - uint256 GetHash() const; |
86 | | - |
87 | | - CBigNum GetValue() const { return value; } |
88 | | - void SetValue(CBigNum value){ this->value = value; } |
89 | | - libzerocoin::CoinDenomination GetDenomination() const { return denomination; } |
90 | | - int64_t GetDenominationAsAmount() const { return denomination * COIN; } |
91 | | - void SetDenomination(libzerocoin::CoinDenomination denom){ this->denomination = denom; } |
92 | | - int GetHeight() const { return nHeight; } |
93 | | - void SetHeight(int nHeight){ this->nHeight = nHeight; } |
94 | | - bool IsUsed() const { return this->isUsed; } |
95 | | - void SetUsed(bool isUsed){ this->isUsed = isUsed; } |
96 | | - CBigNum GetRandomness() const{ return randomness; } |
97 | | - void SetRandomness(CBigNum rand){ this->randomness = rand; } |
98 | | - CBigNum GetSerialNumber() const { return serialNumber; } |
99 | | - void SetSerialNumber(CBigNum serial){ this->serialNumber = serial; } |
100 | | - uint256 GetTxHash() const { return this->txid; } |
101 | | - void SetTxHash(uint256 txid) { this->txid = txid; } |
102 | | - uint8_t GetVersion() const { return this->version; } |
103 | | - void SetVersion(const uint8_t nVersion) { this->version = nVersion; } |
104 | | - CPrivKey GetPrivKey() const { return this->privkey; } |
105 | | - void SetPrivKey(const CPrivKey& privkey) { this->privkey = privkey; } |
106 | | - bool GetKeyPair(CKey& key) const; |
107 | | - |
108 | | - int GetOutputIndex() { return this->outputIndex; } |
109 | | - void SetOutputIndex(int index) { this->outputIndex = index; } |
110 | | - |
111 | | - inline bool operator <(const CZerocoinMint& a) const { return GetHeight() < a.GetHeight(); } |
112 | | - |
113 | | - CZerocoinMint(const CZerocoinMint& other) { |
114 | | - denomination = other.GetDenomination(); |
115 | | - nHeight = other.GetHeight(); |
116 | | - value = other.GetValue(); |
117 | | - randomness = other.GetRandomness(); |
118 | | - serialNumber = other.GetSerialNumber(); |
119 | | - txid = other.GetTxHash(); |
120 | | - isUsed = other.IsUsed(); |
121 | | - version = other.GetVersion(); |
122 | | - privkey = other.privkey; |
123 | | - } |
124 | | - |
125 | | - std::string ToString() const; |
126 | | - |
127 | | - bool operator == (const CZerocoinMint& other) const |
128 | | - { |
129 | | - return this->GetValue() == other.GetValue(); |
130 | | - } |
131 | | - |
132 | | - // Copy another CZerocoinMint |
133 | | - inline CZerocoinMint& operator=(const CZerocoinMint& other) { |
134 | | - denomination = other.GetDenomination(); |
135 | | - nHeight = other.GetHeight(); |
136 | | - value = other.GetValue(); |
137 | | - randomness = other.GetRandomness(); |
138 | | - serialNumber = other.GetSerialNumber(); |
139 | | - txid = other.GetTxHash(); |
140 | | - isUsed = other.IsUsed(); |
141 | | - version = other.GetVersion(); |
142 | | - privkey = other.GetPrivKey(); |
143 | | - return *this; |
144 | | - } |
145 | | - |
146 | | - // why 6 below (SPOCK) |
147 | | - inline bool checkUnused(int denom, int Height) const { |
148 | | - if (IsUsed() == false && GetDenomination() == denomination && GetRandomness() != 0 && GetSerialNumber() != 0 && GetHeight() != -1 && GetHeight() != INT_MAX && GetHeight() >= 1 && (GetHeight() + 6 <= Height)) { |
149 | | - return true; |
150 | | - } else { |
151 | | - return false; |
152 | | - } |
153 | | - } |
154 | | - |
155 | | - ADD_SERIALIZE_METHODS; |
156 | | - |
157 | | - template <typename Stream, typename Operation> |
158 | | - inline void SerializationOp(Stream& s, Operation ser_action) { |
159 | | - READWRITE(isUsed); |
160 | | - READWRITE(randomness); |
161 | | - READWRITE(serialNumber); |
162 | | - READWRITE(value); |
163 | | - READWRITE(denomination); |
164 | | - READWRITE(nHeight); |
165 | | - READWRITE(txid); |
166 | | - |
167 | | - bool fVersionedMint = true; |
168 | | - try { |
169 | | - READWRITE(version); |
170 | | - } catch (...) { |
171 | | - fVersionedMint = false; |
172 | | - } |
173 | | - |
174 | | - if (version > CURRENT_VERSION) { |
175 | | - version = 1; |
176 | | - fVersionedMint = false; |
177 | | - } |
178 | | - |
179 | | - if (fVersionedMint) |
180 | | - READWRITE(privkey); |
181 | | - }; |
182 | | -}; |
183 | | - |
184 | | -class CZerocoinSpend |
185 | | -{ |
186 | | -private: |
187 | | - CBigNum coinSerial; |
188 | | - uint256 hashTx; |
189 | | - CBigNum pubCoin; |
190 | | - libzerocoin::CoinDenomination denomination; |
191 | | - unsigned int nAccumulatorChecksum; |
192 | | - int nMintCount; //memory only - the amount of mints that belong to the accumulator this is spent from |
193 | | - |
194 | | -public: |
195 | | - CZerocoinSpend() |
196 | | - { |
197 | | - SetNull(); |
198 | | - } |
199 | | - |
200 | | - CZerocoinSpend(CBigNum coinSerial, uint256 hashTx, CBigNum pubCoin, libzerocoin::CoinDenomination denomination, unsigned int nAccumulatorChecksum) |
201 | | - { |
202 | | - this->coinSerial = coinSerial; |
203 | | - this->hashTx = hashTx; |
204 | | - this->pubCoin = pubCoin; |
205 | | - this->denomination = denomination; |
206 | | - this->nAccumulatorChecksum = nAccumulatorChecksum; |
207 | | - } |
208 | | - |
209 | | - void SetNull() |
210 | | - { |
211 | | - coinSerial = 0; |
212 | | - hashTx.SetNull(); |
213 | | - pubCoin = 0; |
214 | | - denomination = libzerocoin::ZQ_ERROR; |
215 | | - } |
216 | | - |
217 | | - CBigNum GetSerial() const { return coinSerial; } |
218 | | - uint256 GetTxHash() const { return hashTx; } |
219 | | - void SetTxHash(uint256 hash) { this->hashTx = hash; } |
220 | | - CBigNum GetPubCoin() const { return pubCoin; } |
221 | | - libzerocoin::CoinDenomination GetDenomination() const { return denomination; } |
222 | | - unsigned int GetAccumulatorChecksum() const { return this->nAccumulatorChecksum; } |
223 | | - uint256 GetHash() const; |
224 | | - void SetMintCount(int nMintsAdded) { this->nMintCount = nMintsAdded; } |
225 | | - int GetMintCount() const { return nMintCount; } |
226 | | - |
227 | | - ADD_SERIALIZE_METHODS; |
228 | | - |
229 | | - template <typename Stream, typename Operation> |
230 | | - inline void SerializationOp(Stream& s, Operation ser_action) { |
231 | | - READWRITE(coinSerial); |
232 | | - READWRITE(hashTx); |
233 | | - READWRITE(pubCoin); |
234 | | - READWRITE(denomination); |
235 | | - READWRITE(nAccumulatorChecksum); |
236 | | - }; |
237 | | -}; |
238 | | - |
239 | | -class CZerocoinSpendReceipt |
240 | | -{ |
241 | | -private: |
242 | | - std::string strStatusMessage; |
243 | | - int nStatus; |
244 | | - int nNeededSpends; |
245 | | - std::vector<CZerocoinSpend> vSpends; |
246 | | - |
247 | | -public: |
248 | | - void AddSpend(const CZerocoinSpend& spend); |
249 | | - std::vector<CZerocoinSpend> GetSpends(); |
250 | | - void SetStatus(std::string strStatus, int nStatus, int nNeededSpends = 0); |
251 | | - std::string GetStatusMessage(); |
252 | | - int GetStatus(); |
253 | | - int GetNeededSpends(); |
254 | | -}; |
255 | | - |
256 | | -/** |
257 | | - * Wrapped serials attack inflation, only for mainnet. |
258 | | - * FUTURE: Move this to another file.. |
259 | | - * @param denom |
260 | | - * @return |
261 | | - */ |
262 | | -int GetWrapppedSerialInflation(libzerocoin::CoinDenomination denom); |
263 | | - |
264 | | -int64_t GetWrapppedSerialInflationAmount(); |
265 | | - |
266 | 13 | #endif //PIVX_ZEROCOIN_H |
0 commit comments