Skip to content

Commit

Permalink
Cleanup: Remove zerocoin wallet and tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Dec 2, 2020
1 parent c1d56b8 commit e0b8196
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 268 deletions.
1 change: 0 additions & 1 deletion src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

class CScheduler;
class CWallet;
class CzPIVWallet;

namespace boost
{
Expand Down
9 changes: 1 addition & 8 deletions src/legacy/validation_zerocoin_legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "libzerocoin/CoinSpend.h"
#include "wallet/wallet.h"
#include "zpivchain.h"
#include "zpiv/zpivmodule.h"

bool AcceptToMemoryPoolZerocoin(const CTransaction& tx, CAmount& nValueIn, int chainHeight, CValidationState& state, const Consensus::Params& consensus)
{
Expand Down Expand Up @@ -71,14 +72,6 @@ bool DisconnectZerocoinTx(const CTransaction& tx, CAmount& nValueIn, CZerocoinDB

if (!zerocoinDB->EraseCoinSpend(serial))
return error("failed to erase spent zerocoin in block");

//if this was our spend, then mark it unspent now
if (pwalletMain) {
if (pwalletMain->IsMyZerocoinSpend(serial)) {
if (!pwalletMain->SetMintUnspent(serial))
LogPrintf("%s: failed to automatically reset mint", __func__);
}
}
}

}
Expand Down
1 change: 1 addition & 0 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "invalid.h"
#include "policy/policy.h"
#include "zpivchain.h"
#include "zpiv/zpivmodule.h"


#include <boost/thread.hpp>
Expand Down
45 changes: 1 addition & 44 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "validationinterface.h"
#include "zpivchain.h"
#include "zpiv/zerocoin.h"
#include "zpiv/zpivmodule.h"

#include <boost/algorithm/string/replace.hpp>
#include <boost/thread.hpp>
Expand Down Expand Up @@ -1325,12 +1326,6 @@ DisconnectResult DisconnectBlock(CBlock& block, CBlockIndex* pindex, CCoinsViewC
return DISCONNECT_FAILED;
}

//Track zPIV money supply
if (!UpdateZPIVSupplyDisconnect(block, pindex)) {
error("%s: Failed to calculate new zPIV supply", __func__);
return DISCONNECT_FAILED;
}

// undo transactions in reverse order
for (int i = block.vtx.size() - 1; i >= 0; i--) {
const CTransaction& tx = *block.vtx[i];
Expand Down Expand Up @@ -1744,41 +1739,9 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
setDirtyBlockIndex.insert(pindex);
}

//Record zPIV serials
if (pwalletMain) {
std::set<uint256> setAddedTx;
for (const std::pair<libzerocoin::CoinSpend, uint256>& pSpend : vSpends) {
// Send signal to wallet if this is ours
if (pwalletMain->IsMyZerocoinSpend(pSpend.first.getCoinSerialNumber())) {
LogPrintf("%s: %s detected zerocoinspend in transaction %s \n", __func__,
pSpend.first.getCoinSerialNumber().GetHex(), pSpend.second.GetHex());
pwalletMain->NotifyZerocoinChanged(pwalletMain, pSpend.first.getCoinSerialNumber().GetHex(), "Used",
CT_UPDATED);

//Don't add the same tx multiple times
if (setAddedTx.count(pSpend.second))
continue;

//Search block for matching tx, turn into wtx, set merkle branch, add to wallet
int posInBlock = 0;
for (posInBlock = 0; posInBlock < (int)block.vtx.size(); posInBlock++) {
auto& tx = *block.vtx[posInBlock];
if (tx.GetHash() == pSpend.second) {
CWalletTx wtx(pwalletMain, tx);
wtx.nTimeReceived = pindex->GetBlockTime();
wtx.SetMerkleBranch(pindex->GetBlockHash(), posInBlock);
pwalletMain->AddToWallet(wtx);
setAddedTx.insert(pSpend.second);
}
}
}
}
}

// Flush spend/mint info to disk
if (!vSpends.empty() && !zerocoinDB->WriteCoinSpendBatch(vSpends))
return AbortNode(state, "Failed to record coin serials to database");

if (!vMints.empty() && !zerocoinDB->WriteCoinMintBatch(vMints))
return AbortNode(state, "Failed to record new mints to database");

Expand All @@ -1789,12 +1752,6 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
// add this block to the view's block chain
view.SetBestBlock(pindex->GetBlockHash());

// Update zPIV money supply map
if (!UpdateZPIVSupplyConnect(block, pindex, fJustCheck)) {
return state.DoS(100, error("%s: Failed to calculate new zPIV supply for block=%s height=%d", __func__,
block.GetHash().GetHex(), pindex->nHeight), REJECT_INVALID);
}

int64_t nTime3 = GetTimeMicros();
nTimeIndex += nTime3 - nTime2;
LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeIndex * 0.000001);
Expand Down
32 changes: 0 additions & 32 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ bool CWallet::Lock()
{
LOCK(cs_KeyStore);
vMasterKey.clear();
if (zwallet) zwallet->Lock();
}

NotifyStatusChanged(this);
Expand Down Expand Up @@ -384,17 +383,6 @@ bool CWallet::Unlock(const CKeyingMaterial& vMasterKeyIn)

vMasterKey = vMasterKeyIn;
fDecryptionThoroughlyChecked = true;

if (zwallet) {
uint256 hashSeed;
if (CWalletDB(strWalletFile).ReadCurrentSeedHash(hashSeed)) {
uint256 nSeed;
if (!GetDeterministicSeed(hashSeed, nSeed)) {
return error("Failed to read zPIV seed from DB. Wallet is probably corrupt.");
}
zwallet->SetMasterSeed(nSeed, false);
}
}
}

NotifyStatusChanged(this);
Expand Down Expand Up @@ -1687,9 +1675,6 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate, b
{
int ret = 0;
int64_t nNow = GetTime();
bool fCheckZPIV = gArgs.GetBoolArg("-zapwallettxes", false);
if (fCheckZPIV)
zpivTracker->Init();

const Consensus::Params& consensus = Params().GetConsensus();

Expand Down Expand Up @@ -1739,9 +1724,6 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate, b
}
}

// Will try to rescan it if zPIV upgrade is active.
doZPivRescan(pindex, block, setAddedToWallet, consensus, fCheckZPIV);

pindex = chainActive.Next(pindex);
if (GetTime() >= nNow + 60) {
nNow = GetTime();
Expand Down Expand Up @@ -4106,9 +4088,6 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)

LogPrintf("Wallet completed loading in %15dms\n", GetTimeMillis() - nStart);

CzPIVWallet* zwalletInstance = new CzPIVWallet(walletInstance);
walletInstance->setZWallet(zwalletInstance);

RegisterValidationInterface(walletInstance);

CBlockIndex* pindexRescan = chainActive.Tip();
Expand Down Expand Up @@ -4156,16 +4135,6 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
}
fVerifyingBlocks = false;

if (!zwalletInstance->GetMasterSeed().IsNull()) {
//Inititalize zPIVWallet
uiInterface.InitMessage(_("Syncing zPIV wallet..."));

//Load zerocoin mint hashes to memory
walletInstance->zpivTracker->Init();
zwalletInstance->LoadMintPoolFromDB();
zwalletInstance->SyncWithChain();
}

return walletInstance;
}

Expand Down Expand Up @@ -4311,7 +4280,6 @@ CWallet::CWallet(std::string strWalletFileIn)

CWallet::~CWallet()
{
delete zwallet;
delete pwalletdbEncryption;
delete pStakerStatus;
}
Expand Down
79 changes: 1 addition & 78 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "sapling/address.hpp"
#include "zpiv/zerocoin.h"
#include "guiinterface.h"
#include "util.h"
#include "util/memory.h"
#include "utilstrencodings.h"
#include "validationinterface.h"
#include "script/ismine.h"
#include "wallet/scriptpubkeyman.h"
#include "sapling/saplingscriptpubkeyman.h"
#include "validation.h"
#include "wallet/walletdb.h"
#include "zpiv/zpivmodule.h"
#include "zpiv/zpivwallet.h"
#include "zpiv/zpivtracker.h"

#include <algorithm>
#include <atomic>
Expand Down Expand Up @@ -130,27 +127,6 @@ enum AvailableCoinsType {
STAKEABLE_COINS = 6 // UTXO's that are valid for staking
};

// Possible states for zPIV send
enum ZerocoinSpendStatus {
ZPIV_SPEND_OKAY = 0, // No error
ZPIV_SPEND_ERROR = 1, // Unspecified class of errors, more details are (hopefully) in the returning text
ZPIV_WALLET_LOCKED = 2, // Wallet was locked
ZPIV_COMMIT_FAILED = 3, // Commit failed, reset status
ZPIV_ERASE_SPENDS_FAILED = 4, // Erasing spends during reset failed
ZPIV_ERASE_NEW_MINTS_FAILED = 5, // Erasing new mints during reset failed
ZPIV_TRX_FUNDS_PROBLEMS = 6, // Everything related to available funds
ZPIV_TRX_CREATE = 7, // Everything related to create the transaction
ZPIV_TRX_CHANGE = 8, // Everything related to transaction change
ZPIV_TXMINT_GENERAL = 9, // General errors in MintsToInputVectorPublicSpend
ZPIV_INVALID_COIN = 10, // Selected mint coin is not valid
ZPIV_FAILED_ACCUMULATOR_INITIALIZATION = 11, // Failed to initialize witness
ZPIV_INVALID_WITNESS = 12, // Spend coin transaction did not verify
ZPIV_BAD_SERIALIZATION = 13, // Transaction verification failed
ZPIV_SPENT_USED_ZPIV = 14, // Coin has already been spend
ZPIV_TX_TOO_LARGE = 15, // The transaction is larger than the max tx size
ZPIV_SPEND_V1_SEC_LEVEL // Spend is V1 and security level is not set to 100
};

/** A key pool entry */
class CKeyPool
{
Expand Down Expand Up @@ -345,9 +321,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
const bool fIncludeDelegated,
const bool fIncludeLocked) const;

// Zerocoin wallet
CzPIVWallet* zwallet{nullptr};

//! Destination --> label/purpose mapping.
std::map<CWDestination, AddressBook::CAddressBookData> mapAddressBook;

Expand Down Expand Up @@ -817,56 +790,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface

/** notify wallet file backed up */
boost::signals2::signal<void (const bool& fSuccess, const std::string& filename)> NotifyWalletBacked;


/* Legacy ZC - implementations in wallet_zerocoin.cpp */

bool GetDeterministicSeed(const uint256& hashSeed, uint256& seed);
bool AddDeterministicSeed(const uint256& seed);

// Par of the tx rescan process
void doZPivRescan(const CBlockIndex* pindex, const CBlock& block, std::set<uint256>& setAddedToWallet, const Consensus::Params& consensus, bool fCheckZPIV);

//- ZC Mints (Only for regtest)
std::string MintZerocoin(CAmount nValue, CWalletTx& wtxNew, std::vector<CDeterministicMint>& vDMints, const CCoinControl* coinControl = NULL);
std::string MintZerocoinFromOutPoint(CAmount nValue, CWalletTx& wtxNew, std::vector<CDeterministicMint>& vDMints, const std::vector<COutPoint> vOutpts);
bool CreateZPIVOutPut(libzerocoin::CoinDenomination denomination, CTxOut& outMint, CDeterministicMint& dMint);
bool CreateZerocoinMintTransaction(const CAmount nValue,
CMutableTransaction& txNew,
std::vector<CDeterministicMint>& vDMints,
CReserveKey* reservekey,
std::string& strFailReason,
const CCoinControl* coinControl = NULL);

// - ZC PublicSpends
bool SpendZerocoin(CAmount nAmount, CWalletTx& wtxNew, CZerocoinSpendReceipt& receipt, std::vector<CZerocoinMint>& vMintsSelected, std::list<std::pair<CTxDestination,CAmount>> addressesTo, CTxDestination* changeAddress = nullptr);
bool MintsToInputVectorPublicSpend(std::map<CBigNum, CZerocoinMint>& mapMintsSelected, const uint256& hashTxOut, std::vector<CTxIn>& vin, CZerocoinSpendReceipt& receipt, libzerocoin::SpendType spendType, CBlockIndex* pindexCheckpoint = nullptr);
bool CreateZCPublicSpendTransaction(
CAmount nValue,
CWalletTx& wtxNew,
CReserveKey& reserveKey,
CZerocoinSpendReceipt& receipt,
std::vector<CZerocoinMint>& vSelectedMints,
std::vector<CDeterministicMint>& vNewMints,
std::list<std::pair<CTxDestination,CAmount>> addressesTo,
CTxDestination* changeAddress = nullptr);

// - ZC Balances
CAmount GetZerocoinBalance() const;
std::map<libzerocoin::CoinDenomination, CAmount> GetMyZerocoinDistribution() const;

// zPIV wallet
std::unique_ptr<CzPIVTracker> zpivTracker{nullptr};
void setZWallet(CzPIVWallet* zwallet);
CzPIVWallet* getZWallet();
bool IsMyZerocoinSpend(const CBigNum& bnSerial) const;
bool IsMyMint(const CBigNum& bnValue) const;
void ReconsiderZerocoins(std::list<CZerocoinMint>& listMintsRestored, std::list<CDeterministicMint>& listDMintsRestored);
bool GetMint(const uint256& hashSerial, CZerocoinMint& mint);
bool SetMintUnspent(const CBigNum& bnSerial);
bool UpdateMint(const CBigNum& bnValue, const int& nHeight, const uint256& txid, const libzerocoin::CoinDenomination& denom);
// Zerocoin entry changed. (called with lock cs_wallet held)
boost::signals2::signal<void(CWallet* wallet, const std::string& pubCoin, const std::string& isUsed, ChangeType status)> NotifyZerocoinChanged;
};

/** A key allocated from the key pool. */
Expand Down
1 change: 0 additions & 1 deletion src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "zpiv/zerocoin.h"
#include "libzerocoin/Accumulator.h"
#include "libzerocoin/Denominations.h"
#include "zpiv/zpivtracker.h"

#include <list>
#include <stdint.h>
Expand Down
Loading

0 comments on commit e0b8196

Please sign in to comment.