Skip to content

Commit 1db7c59

Browse files
committed
[UNIT TEST] publicCoinSpend valid input creation and verification.
[UNIT TEST] publicCoinSpend creation + validation completed.
1 parent ca86660 commit 1db7c59

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/test/zerocoin_transactions_tests.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include "libzerocoin/Denominations.h"
6+
#include "libzerocoin/Coin.h"
67
#include "amount.h"
78
#include "chainparams.h"
89
#include "coincontrol.h"
910
#include "main.h"
1011
#include "wallet/wallet.h"
1112
#include "wallet/walletdb.h"
1213
#include "txdb.h"
14+
#include "zpiv/zpivmodule.h"
1315
#include <boost/test/unit_test.hpp>
1416
#include <iostream>
1517

@@ -53,4 +55,62 @@ BOOST_AUTO_TEST_CASE(zerocoin_spend_test)
5355

5456
}
5557

58+
BOOST_AUTO_TEST_CASE(zerocoin_public_spend_test)
59+
{
60+
SelectParams(CBaseChainParams::MAIN);
61+
ZerocoinParams *ZCParams = Params().Zerocoin_Params(false);
62+
(void)ZCParams;
63+
64+
ZPIVModule zpivModule;
65+
66+
PrivateCoin privCoin(ZCParams, libzerocoin::CoinDenomination::ZQ_ONE, true);
67+
const CPrivKey privKey = privCoin.getPrivKey();
68+
69+
CZerocoinMint mint = CZerocoinMint(
70+
privCoin.getPublicCoin().getDenomination(),
71+
privCoin.getPublicCoin().getValue(),
72+
privCoin.getRandomness(),
73+
privCoin.getSerialNumber(),
74+
false,
75+
privCoin.getVersion(),
76+
nullptr);
77+
mint.SetPrivKey(privKey);
78+
79+
80+
// Mint tx
81+
CTransaction prevTx;
82+
83+
CScript scriptSerializedCoin = CScript()
84+
<< OP_ZEROCOINMINT << privCoin.getPublicCoin().getValue().getvch().size() << privCoin.getPublicCoin().getValue().getvch();
85+
CTxOut out = CTxOut(libzerocoin::ZerocoinDenominationToAmount(privCoin.getPublicCoin().getDenomination()), scriptSerializedCoin);
86+
prevTx.vout.push_back(out);
87+
88+
mint.SetOutputIndex(0);
89+
mint.SetTxHash(prevTx.GetHash());
90+
91+
// Spend tx
92+
CMutableTransaction tx;
93+
tx.vout.resize(1);
94+
tx.vout[0].nValue = 1*CENT;
95+
tx.vout[0].scriptPubKey = GetScriptForDestination(CBitcoinAddress("D9Ti4LEhF1n6dR2hGd2SyNADD51AVgva6q").Get());
96+
97+
CTxIn in;
98+
if (!zpivModule.createInput(in, mint, tx.GetHash())){
99+
BOOST_CHECK_MESSAGE(false, "Failed to create zc input");
100+
}
101+
102+
PublicCoinSpend publicSpend(ZCParams);
103+
if (!zpivModule.validateInput(in, out, tx, publicSpend)){
104+
BOOST_CHECK_MESSAGE(false, "Failed to validate zc input");
105+
}
106+
107+
PublicCoinSpend publicSpendTest(ZCParams);
108+
BOOST_CHECK_MESSAGE(zpivModule.parseCoinSpend(in, tx, out, publicSpendTest), "Failed to parse public spend");
109+
libzerocoin::CoinSpend *spend = &publicSpendTest;
110+
111+
BOOST_CHECK_MESSAGE(publicSpendTest.HasValidSignature(), "Failed to validate public spend signature");
112+
BOOST_CHECK_MESSAGE(spend->HasValidSignature(), "Failed to validate spend signature");
113+
114+
}
115+
56116
BOOST_AUTO_TEST_SUITE_END()

src/zpiv/zerocoin.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class CZerocoinMint
4343
CBigNum randomness;
4444
CBigNum serialNumber;
4545
uint256 txid;
46+
int outputIndex;
4647
CPrivKey privkey;
4748
uint8_t version;
4849
bool isUsed;
@@ -104,6 +105,9 @@ class CZerocoinMint
104105
void SetPrivKey(const CPrivKey& privkey) { this->privkey = privkey; }
105106
bool GetKeyPair(CKey& key) const;
106107

108+
unsigned int GetOutputIndex() { return this->outputIndex; }
109+
void SetOutputIndex(unsigned int index) {this->outputIndex = index; }
110+
107111
inline bool operator <(const CZerocoinMint& a) const { return GetHeight() < a.GetHeight(); }
108112

109113
CZerocoinMint(const CZerocoinMint& other) {
@@ -186,6 +190,7 @@ class CZerocoinSpend
186190
libzerocoin::CoinDenomination denomination;
187191
unsigned int nAccumulatorChecksum;
188192
int nMintCount; //memory only - the amount of mints that belong to the accumulator this is spent from
193+
bool publicSpend = false;
189194

190195
public:
191196
CZerocoinSpend()
@@ -202,6 +207,16 @@ class CZerocoinSpend
202207
this->nAccumulatorChecksum = nAccumulatorChecksum;
203208
}
204209

210+
CZerocoinSpend(CBigNum coinSerial, uint256 hashTx, CBigNum pubCoin, libzerocoin::CoinDenomination denomination, unsigned int nAccumulatorChecksum, bool isPublicSpend)
211+
{
212+
this->coinSerial = coinSerial;
213+
this->hashTx = hashTx;
214+
this->pubCoin = pubCoin;
215+
this->denomination = denomination;
216+
this->nAccumulatorChecksum = nAccumulatorChecksum;
217+
this->publicSpend = isPublicSpend;
218+
}
219+
205220
void SetNull()
206221
{
207222
coinSerial = 0;
@@ -219,6 +234,7 @@ class CZerocoinSpend
219234
uint256 GetHash() const;
220235
void SetMintCount(int nMintsAdded) { this->nMintCount = nMintsAdded; }
221236
int GetMintCount() const { return nMintCount; }
237+
bool IsPublicSpend() const { return publicSpend; }
222238

223239
ADD_SERIALIZE_METHODS;
224240

0 commit comments

Comments
 (0)