Skip to content

Commit 3d0893a

Browse files
committed
[RPC] spendzerocoin command can now create old zc spends, only for regression tests
1 parent f46b80e commit 3d0893a

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

src/rpc/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ extern std::string HelpExampleCli(std::string methodname, std::string args);
179179
extern std::string HelpExampleRpc(std::string methodname, std::string args);
180180

181181
extern void EnsureWalletIsUnlocked(bool fAllowAnonOnly = false);
182-
extern UniValue DoZpivSpend(const CAmount nAmount, bool fMintChange, bool fMinimizeChange, vector<CZerocoinMint>& vMintsSelected, std::string address_str);
182+
extern UniValue DoZpivSpend(const CAmount nAmount, bool fMintChange, bool fMinimizeChange, vector<CZerocoinMint>& vMintsSelected, std::string address_str, bool isPublicSpend = true);
183183

184184
extern UniValue getconnectioncount(const UniValue& params, bool fHelp); // in rpc/net.cpp
185185
extern UniValue getpeerinfo(const UniValue& params, bool fHelp);

src/wallet/rpcwallet.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,6 +2853,7 @@ UniValue spendzerocoin(const UniValue& params, bool fHelp)
28532853
"3. minimizechange (boolean, required) Try to minimize the returning change [false]\n"
28542854
"4. \"address\" (string, optional, default=change) Send to specified address or to a new change address.\n"
28552855
" If there is change then an address is required\n"
2856+
"5. ispublicspend (boolean, optional, default=true) create a public zc spend instead of use the old code (only for regression tests)"
28562857

28572858
"\nResult:\n"
28582859
"{\n"
@@ -2892,10 +2893,15 @@ UniValue spendzerocoin(const UniValue& params, bool fHelp)
28922893
bool fMintChange = params[1].get_bool(); // Mint change to zPIV
28932894
bool fMinimizeChange = params[2].get_bool(); // Minimize change
28942895
std::string address_str = params.size() > 3 ? params[3].get_str() : "";
2896+
bool ispublicspend = params.size() > 4 ? params[3].get_bool() : true;
28952897

28962898
vector<CZerocoinMint> vMintsSelected;
28972899

2898-
return DoZpivSpend(nAmount, fMintChange, fMinimizeChange, vMintsSelected, address_str);
2900+
if (!ispublicspend && Params().NetworkID() != CBaseChainParams::REGTEST) {
2901+
throw JSONRPCError(RPC_WALLET_ERROR, "zPIV old spend only available in regtest for tests purposes");
2902+
}
2903+
2904+
return DoZpivSpend(nAmount, fMintChange, fMinimizeChange, vMintsSelected, address_str, ispublicspend);
28992905
}
29002906

29012907

@@ -2993,7 +2999,7 @@ UniValue spendzerocoinmints(const UniValue& params, bool fHelp)
29932999
}
29943000

29953001

2996-
extern UniValue DoZpivSpend(const CAmount nAmount, bool fMintChange, bool fMinimizeChange, vector<CZerocoinMint>& vMintsSelected, std::string address_str)
3002+
extern UniValue DoZpivSpend(const CAmount nAmount, bool fMintChange, bool fMinimizeChange, vector<CZerocoinMint>& vMintsSelected, std::string address_str, bool ispublicspend)
29973003
{
29983004
int64_t nTimeStart = GetTimeMillis();
29993005
CBitcoinAddress address = CBitcoinAddress(); // Optional sending address. Dummy initialization here.
@@ -3005,9 +3011,9 @@ extern UniValue DoZpivSpend(const CAmount nAmount, bool fMintChange, bool fMinim
30053011
address = CBitcoinAddress(address_str);
30063012
if(!address.IsValid())
30073013
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address");
3008-
fSuccess = pwalletMain->SpendZerocoin(nAmount, wtx, receipt, vMintsSelected, fMintChange, fMinimizeChange, &address);
3014+
fSuccess = pwalletMain->SpendZerocoin(nAmount, wtx, receipt, vMintsSelected, fMintChange, fMinimizeChange, &address, ispublicspend);
30093015
} else // Spend to newly generated local address
3010-
fSuccess = pwalletMain->SpendZerocoin(nAmount, wtx, receipt, vMintsSelected, fMintChange, fMinimizeChange);
3016+
fSuccess = pwalletMain->SpendZerocoin(nAmount, wtx, receipt, vMintsSelected, fMintChange, fMinimizeChange, nullptr, ispublicspend);
30113017

30123018
if (!fSuccess)
30133019
throw JSONRPCError(RPC_WALLET_ERROR, receipt.GetStatusMessage());

src/wallet/wallet.cpp

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4706,14 +4706,28 @@ bool CWallet::CheckCoinSpend(libzerocoin::CoinSpend& spend, libzerocoin::Accumul
47064706
return true;
47074707
}
47084708

4709-
bool CWallet::MintToTxIn(CZerocoinMint mint, const uint256& hashTxOut, CTxIn& newTxIn, CZerocoinSpendReceipt& receipt, libzerocoin::SpendType spendType, CBlockIndex* pindexCheckpoint)
4709+
bool CWallet::MintToTxIn(
4710+
CZerocoinMint mint,
4711+
const uint256& hashTxOut,
4712+
CTxIn& newTxIn,
4713+
CZerocoinSpendReceipt& receipt,
4714+
libzerocoin::SpendType spendType,
4715+
CBlockIndex* pindexCheckpoint,
4716+
bool publicCoinSpend)
47104717
{
47114718
std::map<CBigNum, CZerocoinMint> mapMints;
47124719
mapMints.insert(std::make_pair(mint.GetValue(), mint));
47134720
std::vector<CTxIn> vin;
4714-
if (MintsToInputVectorPublicSpend(mapMints, hashTxOut, vin, receipt, spendType, pindexCheckpoint)) {
4715-
newTxIn = vin[0];
4716-
return true;
4721+
if (publicCoinSpend) {
4722+
if (MintsToInputVectorPublicSpend(mapMints, hashTxOut, vin, receipt, spendType, pindexCheckpoint)) {
4723+
newTxIn = vin[0];
4724+
return true;
4725+
}
4726+
} else {
4727+
if (MintsToInputVector(mapMints, hashTxOut, vin, receipt, spendType, pindexCheckpoint)) {
4728+
newTxIn = vin[0];
4729+
return true;
4730+
}
47174731
}
47184732

47194733
return false;
@@ -4901,7 +4915,17 @@ bool CWallet::MintsToInputVectorPublicSpend(std::map<CBigNum, CZerocoinMint>& ma
49014915
return true;
49024916
}
49034917

4904-
bool CWallet::CreateZerocoinSpendTransaction(CAmount nValue, CWalletTx& wtxNew, CReserveKey& reserveKey, CZerocoinSpendReceipt& receipt, vector<CZerocoinMint>& vSelectedMints, vector<CDeterministicMint>& vNewMints, bool fMintChange, bool fMinimizeChange, CBitcoinAddress* address)
4918+
bool CWallet::CreateZerocoinSpendTransaction(
4919+
CAmount nValue,
4920+
CWalletTx& wtxNew,
4921+
CReserveKey& reserveKey,
4922+
CZerocoinSpendReceipt& receipt,
4923+
vector<CZerocoinMint>& vSelectedMints,
4924+
vector<CDeterministicMint>& vNewMints,
4925+
bool fMintChange,
4926+
bool fMinimizeChange,
4927+
CBitcoinAddress* address,
4928+
bool isPublicSpend)
49054929
{
49064930
// Check available funds
49074931
int nStatus = ZPIV_TRX_FUNDS_PROBLEMS;
@@ -5095,8 +5119,15 @@ bool CWallet::CreateZerocoinSpendTransaction(CAmount nValue, CWalletTx& wtxNew,
50955119

50965120
//add all of the mints to the transaction as inputs
50975121
std::vector<CTxIn> vin;
5098-
if (!MintsToInputVectorPublicSpend(mapSelectedMints, hashTxOut, vin, receipt, libzerocoin::SpendType::SPEND, pindexCheckpoint))
5099-
return false;
5122+
if (isPublicSpend) {
5123+
if (!MintsToInputVectorPublicSpend(mapSelectedMints, hashTxOut, vin, receipt,
5124+
libzerocoin::SpendType::SPEND, pindexCheckpoint))
5125+
return false;
5126+
} else {
5127+
if (!MintsToInputVector(mapSelectedMints, hashTxOut, vin, receipt,
5128+
libzerocoin::SpendType::SPEND, pindexCheckpoint))
5129+
return false;
5130+
}
51005131
txNew.vin = vin;
51015132

51025133
// Limit size
@@ -5402,7 +5433,7 @@ string CWallet::MintZerocoin(CAmount nValue, CWalletTx& wtxNew, vector<CDetermin
54025433
return "";
54035434
}
54045435

5405-
bool CWallet::SpendZerocoin(CAmount nAmount, CWalletTx& wtxNew, CZerocoinSpendReceipt& receipt, vector<CZerocoinMint>& vMintsSelected, bool fMintChange, bool fMinimizeChange, CBitcoinAddress* addressTo)
5436+
bool CWallet::SpendZerocoin(CAmount nAmount, CWalletTx& wtxNew, CZerocoinSpendReceipt& receipt, vector<CZerocoinMint>& vMintsSelected, bool fMintChange, bool fMinimizeChange, CBitcoinAddress* addressTo, bool isPublicSpend)
54065437
{
54075438
// Default: assume something goes wrong. Depending on the problem this gets more specific below
54085439
int nStatus = ZPIV_SPEND_ERROR;
@@ -5414,7 +5445,7 @@ bool CWallet::SpendZerocoin(CAmount nAmount, CWalletTx& wtxNew, CZerocoinSpendRe
54145445

54155446
CReserveKey reserveKey(this);
54165447
vector<CDeterministicMint> vNewMints;
5417-
if (!CreateZerocoinSpendTransaction(nAmount, wtxNew, reserveKey, receipt, vMintsSelected, vNewMints, fMintChange, fMinimizeChange, addressTo)) {
5448+
if (!CreateZerocoinSpendTransaction(nAmount, wtxNew, reserveKey, receipt, vMintsSelected, vNewMints, fMintChange, fMinimizeChange, addressTo, isPublicSpend)) {
54185449
return false;
54195450
}
54205451

src/wallet/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
209209

210210
// Zerocoin additions
211211
bool CreateZerocoinMintTransaction(const CAmount nValue, CMutableTransaction& txNew, vector<CDeterministicMint>& vDMints, CReserveKey* reservekey, int64_t& nFeeRet, std::string& strFailReason, const CCoinControl* coinControl = NULL, const bool isZCSpendChange = false);
212-
bool CreateZerocoinSpendTransaction(CAmount nValue, CWalletTx& wtxNew, CReserveKey& reserveKey, CZerocoinSpendReceipt& receipt, vector<CZerocoinMint>& vSelectedMints, vector<CDeterministicMint>& vNewMints, bool fMintChange, bool fMinimizeChange, CBitcoinAddress* address = NULL);
212+
bool CreateZerocoinSpendTransaction(CAmount nValue, CWalletTx& wtxNew, CReserveKey& reserveKey, CZerocoinSpendReceipt& receipt, vector<CZerocoinMint>& vSelectedMints, vector<CDeterministicMint>& vNewMints, bool fMintChange, bool fMinimizeChange, CBitcoinAddress* address = NULL, bool isPublicSpend = true);
213213
bool CheckCoinSpend(libzerocoin::CoinSpend& spend, libzerocoin::Accumulator& accumulator, CZerocoinSpendReceipt& receipt);
214-
bool MintToTxIn(CZerocoinMint mint, const uint256& hashTxOut, CTxIn& newTxIn, CZerocoinSpendReceipt& receipt, libzerocoin::SpendType spendType, CBlockIndex* pindexCheckpoint = nullptr);
214+
bool MintToTxIn(CZerocoinMint mint, const uint256& hashTxOut, CTxIn& newTxIn, CZerocoinSpendReceipt& receipt, libzerocoin::SpendType spendType, CBlockIndex* pindexCheckpoint = nullptr, bool publicCoinSpend = true);
215215
bool MintsToInputVector(std::map<CBigNum, CZerocoinMint>& mapMintsSelected, const uint256& hashTxOut, std::vector<CTxIn>& vin,
216216
CZerocoinSpendReceipt& receipt, libzerocoin::SpendType spendType, CBlockIndex* pindexCheckpoint = nullptr);
217217

@@ -221,7 +221,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
221221

222222
std::string MintZerocoinFromOutPoint(CAmount nValue, CWalletTx& wtxNew, std::vector<CDeterministicMint>& vDMints, const vector<COutPoint> vOutpts);
223223
std::string MintZerocoin(CAmount nValue, CWalletTx& wtxNew, vector<CDeterministicMint>& vDMints, const CCoinControl* coinControl = NULL);
224-
bool SpendZerocoin(CAmount nValue, CWalletTx& wtxNew, CZerocoinSpendReceipt& receipt, vector<CZerocoinMint>& vMintsSelected, bool fMintChange, bool fMinimizeChange, CBitcoinAddress* addressTo = NULL);
224+
bool SpendZerocoin(CAmount nValue, CWalletTx& wtxNew, CZerocoinSpendReceipt& receipt, vector<CZerocoinMint>& vMintsSelected, bool fMintChange, bool fMinimizeChange, CBitcoinAddress* addressTo = NULL, bool isPublicSpend = true);
225225
std::string ResetMintZerocoin();
226226
std::string ResetSpentZerocoin();
227227
void ReconsiderZerocoins(std::list<CZerocoinMint>& listMintsRestored, std::list<CDeterministicMint>& listDMintsRestored);

0 commit comments

Comments
 (0)