Skip to content

Commit

Permalink
[Cleanup][DB] Remove DB functions for zerocoin precomputing
Browse files Browse the repository at this point in the history
Github-Pull: #1237
Rebased-From: bd3eebb
  • Loading branch information
random-zebra authored and Fuzzbawls committed Jan 11, 2020
1 parent 1e93feb commit 7084840
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 153 deletions.
1 change: 0 additions & 1 deletion src/rpc/server.cpp
Expand Up @@ -477,7 +477,6 @@ static const CRPCCommand vRPCCommands[] =
{"zerocoin", "generatemintlist", &generatemintlist, false, false, true},
{"zerocoin", "searchdzpiv", &searchdzpiv, false, false, true},
{"zerocoin", "dzpivstate", &dzpivstate, false, false, true},
{"zerocoin", "clearspendcache", &clearspendcache, false, false, true}

#endif // ENABLE_WALLET
};
Expand Down
1 change: 0 additions & 1 deletion src/rpc/server.h
Expand Up @@ -281,7 +281,6 @@ extern UniValue getzpivseed(const UniValue& params, bool fHelp);
extern UniValue generatemintlist(const UniValue& params, bool fHelp);
extern UniValue searchdzpiv(const UniValue& params, bool fHelp);
extern UniValue dzpivstate(const UniValue& params, bool fHelp);
extern UniValue clearspendcache(const UniValue& params, bool fHelp);
extern UniValue enableautomintaddress(const UniValue& params, bool fHelp);
extern UniValue createautomintaddress(const UniValue& params, bool fHelp);

Expand Down
35 changes: 0 additions & 35 deletions src/wallet/rpcwallet.cpp
Expand Up @@ -4425,38 +4425,3 @@ UniValue spendrawzerocoin(const UniValue& params, bool fHelp)
return DoZpivSpend(mint.GetDenominationAsAmount(), false, true, vMintsSelected, address_str, isPublicSpend);
}

UniValue clearspendcache(const UniValue& params, bool fHelp)
{
if(fHelp || params.size() != 0)
throw std::runtime_error(
"clearspendcache\n"
"\nClear the pre-computed zPIV spend cache, and database.\n" +
HelpRequiringPassphrase() + "\n"

"\nExamples\n" +
HelpExampleCli("clearspendcache", "") + HelpExampleRpc("clearspendcache", ""));

EnsureWalletIsUnlocked();

CzPIVTracker* zpivTracker = pwalletMain->zpivTracker.get();

{
int nTries = 0;
while (nTries < 100) {
TRY_LOCK(zpivTracker->cs_spendcache, fLocked);
if (fLocked) {
if (zpivTracker->ClearSpendCache()) {
fClearSpendCache = true;
CWalletDB walletdb("precomputes.dat", "cr+");
walletdb.EraseAllPrecomputes();
return "Successfully Cleared the Precompute Spend Cache and Database";
}
} else {
fGlobalUnlockSpendCache = true;
nTries++;
MilliSleep(100);
}
}
}
throw JSONRPCError(RPC_WALLET_ERROR, "Error: Spend cache not cleared!");
}
105 changes: 0 additions & 105 deletions src/wallet/walletdb.cpp
Expand Up @@ -1373,111 +1373,6 @@ bool CWalletDB::WriteMintPoolPair(const uint256& hashMasterSeed, const uint256&
return Write(std::make_pair(std::string("mintpool"), hashPubcoin), std::make_pair(hashMasterSeed, nCount));
}

void CWalletDB::LoadPrecomputes(std::list<std::pair<uint256, CoinWitnessCacheData> >& itemList, std::map<uint256, std::list<std::pair<uint256, CoinWitnessCacheData> >::iterator>& itemMap)
{

Dbc* pcursor = GetCursor();
if (!pcursor)
throw std::runtime_error(std::string(__func__)+" : cannot create DB cursor");
unsigned int fFlags = DB_SET_RANGE;
for (;;)
{
// Read next record
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
if (fFlags == DB_SET_RANGE)
ssKey << std::make_pair(std::string("precompute"), uint256(0));
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
fFlags = DB_NEXT;
if (ret == DB_NOTFOUND)
break;
else if (ret != 0)
{
pcursor->close();
throw std::runtime_error(std::string(__func__)+" : error scanning precompute DB");
}

// Unserialize
std::string strType;
ssKey >> strType;
if (strType != "precompute")
break;

uint256 hash;
ssKey >> hash;

CoinWitnessCacheData cacheData;
ssValue >> cacheData;

itemList.push_front(std::make_pair(hash, cacheData));
itemMap.insert(std::make_pair(hash, itemList.begin()));

if (itemMap.size() == PRECOMPUTE_LRU_CACHE_SIZE)
break;
}

pcursor->close();
}

void CWalletDB::LoadPrecomputes(std::set<uint256> setHashes)
{
Dbc* pcursor = GetCursor();
if (!pcursor)
throw std::runtime_error(std::string(__func__)+" : cannot create DB cursor");
unsigned int fFlags = DB_SET_RANGE;
for (;;)
{
// Read next record
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
if (fFlags == DB_SET_RANGE)
ssKey << make_pair(std::string("precompute"), uint256(0));
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
fFlags = DB_NEXT;
if (ret == DB_NOTFOUND)
break;
else if (ret != 0)
{
pcursor->close();
throw std::runtime_error(std::string(__func__)+" : error scanning precompute DB");
}

// Unserialize
std::string strType;
ssKey >> strType;
if (strType != "precompute")
break;

uint256 hash;
ssKey >> hash;

setHashes.insert(hash);
}

pcursor->close();
}

void CWalletDB::EraseAllPrecomputes()
{
std::set<uint256> setHashes;
LoadPrecomputes(setHashes);

for (auto hash : setHashes)
ErasePrecompute(hash);
}

bool CWalletDB::WritePrecompute(const uint256& hash, const CoinWitnessCacheData& data)
{
return Write(std::make_pair(std::string("precompute"), hash), data);
}
bool CWalletDB::ReadPrecompute(const uint256& hash, CoinWitnessCacheData& data)
{
return Read(std::make_pair(std::string("precompute"), hash), data);
}
bool CWalletDB::ErasePrecompute(const uint256& hash)
{
return Erase(std::make_pair(std::string("precompute"), hash));
}

//! map with hashMasterSeed as the key, paired with vector of hashPubcoins and their count
std::map<uint256, std::vector<std::pair<uint256, uint32_t> > > CWalletDB::MapMintPool()
Expand Down
7 changes: 0 additions & 7 deletions src/wallet/walletdb.h
Expand Up @@ -188,13 +188,6 @@ class CWalletDB : public CDB
std::map<uint256, std::vector<std::pair<uint256, uint32_t> > > MapMintPool();
bool WriteMintPoolPair(const uint256& hashMasterSeed, const uint256& hashPubcoin, const uint32_t& nCount);

void LoadPrecomputes(std::list<std::pair<uint256, CoinWitnessCacheData> >& itemList, std::map<uint256, std::list<std::pair<uint256, CoinWitnessCacheData> >::iterator>& itemMap);
void LoadPrecomputes(std::set<uint256> setHashes);
void EraseAllPrecomputes();
bool WritePrecompute(const uint256& hash, const CoinWitnessCacheData& data);
bool ReadPrecompute(const uint256& hash, CoinWitnessCacheData& data);
bool ErasePrecompute(const uint256& hash);

private:
CWalletDB(const CWalletDB&);
void operator=(const CWalletDB&);
Expand Down
4 changes: 0 additions & 4 deletions src/zpiv/witness.h
Expand Up @@ -11,10 +11,6 @@
#include "zerocoin.h"
#include "serialize.h"

#define PRECOMPUTE_LRU_CACHE_SIZE 1000
#define PRECOMPUTE_MAX_DIRTY_CACHE_SIZE 100
#define PRECOMPUTE_FLUSH_TIME 300 // 5 minutes

class CoinWitnessCacheData;

class CoinWitnessData
Expand Down

0 comments on commit 7084840

Please sign in to comment.