forked from heliumchain/squorum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzpivchain.cpp
376 lines (317 loc) · 13.2 KB
/
zpivchain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright (c) 2018 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "zpivchain.h"
#include "invalid.h"
#include "main.h"
#include "txdb.h"
#include "ui_interface.h"
// 6 comes from OPCODE (1) + vch.size() (1) + BIGNUM size (4)
#define SCRIPT_OFFSET 6
// For Script size (BIGNUM/Uint256 size)
#define BIGNUM_SIZE 4
bool BlockToMintValueVector(const CBlock& block, const libzerocoin::CoinDenomination denom, vector<CBigNum>& vValues)
{
for (const CTransaction tx : block.vtx) {
if(!tx.IsZerocoinMint())
continue;
for (const CTxOut txOut : tx.vout) {
if(!txOut.scriptPubKey.IsZerocoinMint())
continue;
CValidationState state;
libzerocoin::PublicCoin coin(Params().Zerocoin_Params(false));
if(!TxOutToPublicCoin(txOut, coin, state))
return false;
if (coin.getDenomination() != denom)
continue;
vValues.push_back(coin.getValue());
}
}
return true;
}
bool BlockToPubcoinList(const CBlock& block, std::list<libzerocoin::PublicCoin>& listPubcoins, bool fFilterInvalid)
{
for (const CTransaction tx : block.vtx) {
if(!tx.IsZerocoinMint())
continue;
/* NOTE: GJH Inappropriate for Securus
// Filter out mints that have used invalid outpoints
if (fFilterInvalid) {
bool fValid = true;
for (const CTxIn in : tx.vin) {
if (!ValidOutPoint(in.prevout, INT_MAX)) {
fValid = false;
break;
}
}
if (!fValid)
continue;
}
uint256 txHash = tx.GetHash();
*/
for (unsigned int i = 0; i < tx.vout.size(); i++) {
/* NOTE: GJH Inappropriate for Securus
//Filter out mints that use invalid outpoints - edge case: invalid spend with minted change
if (fFilterInvalid && !ValidOutPoint(COutPoint(txHash, i), INT_MAX))
break;
*/
const CTxOut txOut = tx.vout[i];
if(!txOut.scriptPubKey.IsZerocoinMint())
continue;
CValidationState state;
libzerocoin::PublicCoin pubCoin(Params().Zerocoin_Params(false));
if(!TxOutToPublicCoin(txOut, pubCoin, state))
return false;
listPubcoins.emplace_back(pubCoin);
}
}
return true;
}
//return a list of zerocoin mints contained in a specific block
bool BlockToZerocoinMintList(const CBlock& block, std::list<CZerocoinMint>& vMints, bool fFilterInvalid)
{
for (const CTransaction& tx : block.vtx) {
if(!tx.IsZerocoinMint())
continue;
/* NOTE: GJH Inappropriate for Securus
// Filter out mints that have used invalid outpoints
if (fFilterInvalid) {
bool fValid = true;
for (const CTxIn in : tx.vin) {
if (!ValidOutPoint(in.prevout, INT_MAX)) {
fValid = false;
break;
}
}
if (!fValid)
continue;
}
uint256 txHash = tx.GetHash();
*/
for (unsigned int i = 0; i < tx.vout.size(); i++) {
/* NOTE: GJH Inappropriate for Securus
//Filter out mints that use invalid outpoints - edge case: invalid spend with minted change
if (fFilterInvalid && !ValidOutPoint(COutPoint(txHash, i), INT_MAX))
break;
*/
const CTxOut txOut = tx.vout[i];
if(!txOut.scriptPubKey.IsZerocoinMint())
continue;
CValidationState state;
libzerocoin::PublicCoin pubCoin(Params().Zerocoin_Params(false));
if(!TxOutToPublicCoin(txOut, pubCoin, state))
return false;
//version should not actually matter here since it is just a reference to the pubcoin, not to the privcoin
uint8_t version = 1;
CZerocoinMint mint = CZerocoinMint(pubCoin.getDenomination(), pubCoin.getValue(), 0, 0, false, version, nullptr);
mint.SetTxHash(tx.GetHash());
vMints.push_back(mint);
}
}
return true;
}
void FindMints(std::vector<CMintMeta> vMintsToFind, std::vector<CMintMeta>& vMintsToUpdate, std::vector<CMintMeta>& vMissingMints)
{
// see which mints are in our public zerocoin database. The mint should be here if it exists, unless
// something went wrong
for (CMintMeta meta : vMintsToFind) {
uint256 txHash;
if (!zerocoinDB->ReadCoinMint(meta.hashPubcoin, txHash)) {
vMissingMints.push_back(meta);
continue;
}
// make sure the txhash and block height meta data are correct for this mint
CTransaction tx;
uint256 hashBlock;
if (!GetTransaction(txHash, tx, hashBlock, true)) {
LogPrintf("%s : cannot find tx %s\n", __func__, txHash.GetHex());
vMissingMints.push_back(meta);
continue;
}
if (!mapBlockIndex.count(hashBlock)) {
LogPrintf("%s : cannot find block %s\n", __func__, hashBlock.GetHex());
vMissingMints.push_back(meta);
continue;
}
//see if this mint is spent
uint256 hashTxSpend = 0;
bool fSpent = zerocoinDB->ReadCoinSpend(meta.hashSerial, hashTxSpend);
//if marked as spent, check that it actually made it into the chain
CTransaction txSpend;
uint256 hashBlockSpend;
if (fSpent && !GetTransaction(hashTxSpend, txSpend, hashBlockSpend, true)) {
LogPrintf("%s : cannot find spend tx %s\n", __func__, hashTxSpend.GetHex());
meta.isUsed = false;
vMintsToUpdate.push_back(meta);
continue;
}
//The mint has been incorrectly labelled as spent in zerocoinDB and needs to be undone
int nHeightTx = 0;
uint256 hashSerial = meta.hashSerial;
uint256 txidSpend;
if (fSpent && !IsSerialInBlockchain(hashSerial, nHeightTx, txidSpend)) {
LogPrintf("%s : cannot find block %s. Erasing coinspend from zerocoinDB.\n", __func__, hashBlockSpend.GetHex());
meta.isUsed = false;
vMintsToUpdate.push_back(meta);
continue;
}
// is the denomination correct?
for (auto& out : tx.vout) {
if (!out.IsZerocoinMint())
continue;
libzerocoin::PublicCoin pubcoin(Params().Zerocoin_Params(meta.nVersion < libzerocoin::PrivateCoin::PUBKEY_VERSION));
CValidationState state;
TxOutToPublicCoin(out, pubcoin, state);
if (GetPubCoinHash(pubcoin.getValue()) == meta.hashPubcoin && pubcoin.getDenomination() != meta.denom) {
LogPrintf("%s: found mismatched denom pubcoinhash = %s\n", __func__, meta.hashPubcoin.GetHex());
meta.denom = pubcoin.getDenomination();
vMintsToUpdate.emplace_back(meta);
}
}
// if meta data is correct, then no need to update
if (meta.txid == txHash && meta.nHeight == mapBlockIndex[hashBlock]->nHeight && meta.isUsed == fSpent)
continue;
//mark this mint for update
meta.txid = txHash;
meta.nHeight = mapBlockIndex[hashBlock]->nHeight;
meta.isUsed = fSpent;
LogPrintf("%s: found updates for pubcoinhash = %s\n", __func__, meta.hashPubcoin.GetHex());
vMintsToUpdate.push_back(meta);
}
}
int GetZerocoinStartHeight()
{
return Params().Zerocoin_StartHeight();
}
bool GetZerocoinMint(const CBigNum& bnPubcoin, uint256& txHash)
{
txHash = 0;
return zerocoinDB->ReadCoinMint(bnPubcoin, txHash);
}
bool IsPubcoinInBlockchain(const uint256& hashPubcoin, uint256& txid)
{
txid = 0;
return zerocoinDB->ReadCoinMint(hashPubcoin, txid);
}
bool IsSerialKnown(const CBigNum& bnSerial)
{
uint256 txHash = 0;
return zerocoinDB->ReadCoinSpend(bnSerial, txHash);
}
bool IsSerialInBlockchain(const CBigNum& bnSerial, int& nHeightTx)
{
uint256 txHash = 0;
// if not in zerocoinDB then its not in the blockchain
if (!zerocoinDB->ReadCoinSpend(bnSerial, txHash))
return false;
return IsTransactionInChain(txHash, nHeightTx);
}
bool IsSerialInBlockchain(const uint256& hashSerial, int& nHeightTx, uint256& txidSpend)
{
CTransaction tx;
return IsSerialInBlockchain(hashSerial, nHeightTx, txidSpend, tx);
}
bool IsSerialInBlockchain(const uint256& hashSerial, int& nHeightTx, uint256& txidSpend, CTransaction& tx)
{
txidSpend = 0;
// if not in zerocoinDB then its not in the blockchain
if (!zerocoinDB->ReadCoinSpend(hashSerial, txidSpend))
return false;
return IsTransactionInChain(txidSpend, nHeightTx, tx);
}
std::string ReindexZerocoinDB()
{
if (!zerocoinDB->WipeCoins("spends") || !zerocoinDB->WipeCoins("mints")) {
return _("Failed to wipe zerocoinDB");
}
CBlockIndex* pindex = chainActive[Params().Zerocoin_StartHeight()];
while (pindex) {
if (pindex->nHeight % 1000 == 0)
LogPrintf("Reindexing zerocoin : block %d...\n", pindex->nHeight);
CBlock block;
if (!ReadBlockFromDisk(block, pindex)) {
return _("Reindexing zerocoin failed");
}
for (const CTransaction& tx : block.vtx) {
for (unsigned int i = 0; i < tx.vin.size(); i++) {
if (tx.IsCoinBase())
break;
if (tx.ContainsZerocoins()) {
uint256 txid = tx.GetHash();
//Record Serials
if (tx.IsZerocoinSpend()) {
for (auto& in : tx.vin) {
if (!in.scriptSig.IsZerocoinSpend())
continue;
libzerocoin::CoinSpend spend = TxInToZerocoinSpend(in);
zerocoinDB->WriteCoinSpend(spend.getCoinSerialNumber(), txid);
}
}
//Record mints
if (tx.IsZerocoinMint()) {
for (auto& out : tx.vout) {
if (!out.IsZerocoinMint())
continue;
CValidationState state;
libzerocoin::PublicCoin coin(Params().Zerocoin_Params(pindex->nHeight < Params().Zerocoin_Block_V2_Start()));
TxOutToPublicCoin(out, coin, state);
zerocoinDB->WriteCoinMint(coin, txid);
}
}
}
}
}
pindex = chainActive.Next(pindex);
}
return "";
}
bool RemoveSerialFromDB(const CBigNum& bnSerial)
{
return zerocoinDB->EraseCoinSpend(bnSerial);
}
libzerocoin::CoinSpend TxInToZerocoinSpend(const CTxIn& txin)
{
// extract the CoinSpend from the txin
std::vector<char, zero_after_free_allocator<char> > dataTxIn;
dataTxIn.insert(dataTxIn.end(), txin.scriptSig.begin() + BIGNUM_SIZE, txin.scriptSig.end());
CDataStream serializedCoinSpend(dataTxIn, SER_NETWORK, PROTOCOL_VERSION);
libzerocoin::ZerocoinParams* paramsAccumulator = Params().Zerocoin_Params(chainActive.Height() < Params().Zerocoin_Block_V2_Start());
libzerocoin::CoinSpend spend(Params().Zerocoin_Params(true), paramsAccumulator, serializedCoinSpend);
return spend;
}
bool TxOutToPublicCoin(const CTxOut& txout, libzerocoin::PublicCoin& pubCoin, CValidationState& state)
{
CBigNum publicZerocoin;
vector<unsigned char> vchZeroMint;
vchZeroMint.insert(vchZeroMint.end(), txout.scriptPubKey.begin() + SCRIPT_OFFSET,
txout.scriptPubKey.begin() + txout.scriptPubKey.size());
publicZerocoin.setvch(vchZeroMint);
libzerocoin::CoinDenomination denomination = libzerocoin::AmountToZerocoinDenomination(txout.nValue);
LogPrint("zero", "%s ZCPRINT denomination %d pubcoin %s\n", __func__, denomination, publicZerocoin.GetHex());
if (denomination == libzerocoin::ZQ_ERROR)
return state.DoS(100, error("TxOutToPublicCoin : txout.nValue is not correct"));
libzerocoin::PublicCoin checkPubCoin(Params().Zerocoin_Params(false), publicZerocoin, denomination);
pubCoin = checkPubCoin;
return true;
}
//return a list of zerocoin spends contained in a specific block, list may have many denominations
std::list<libzerocoin::CoinDenomination> ZerocoinSpendListFromBlock(const CBlock& block, bool fFilterInvalid)
{
std::list<libzerocoin::CoinDenomination> vSpends;
for (const CTransaction tx : block.vtx) {
if (!tx.IsZerocoinSpend())
continue;
for (const CTxIn txin : tx.vin) {
if (!txin.scriptSig.IsZerocoinSpend())
continue;
if (fFilterInvalid) {
libzerocoin::CoinSpend spend = TxInToZerocoinSpend(txin);
if (invalid_out::ContainsSerial(spend.getCoinSerialNumber()))
continue;
}
libzerocoin::CoinDenomination c = libzerocoin::IntToZerocoinDenomination(txin.nSequence);
vSpends.push_back(c);
}
}
return vSpends;
}