Skip to content

Commit

Permalink
[Refactor] Nuke zwalletMain global object
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Aug 23, 2020
1 parent f5f9df9 commit 49646b2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 30 deletions.
6 changes: 1 addition & 5 deletions src/init.cpp
Expand Up @@ -86,7 +86,6 @@


#ifdef ENABLE_WALLET
CzPIVWallet* zwalletMain = NULL;
int nWalletBackups = 10;
#endif
volatile bool fFeeEstimatesInitialized = false;
Expand Down Expand Up @@ -308,8 +307,6 @@ void Shutdown()
#ifdef ENABLE_WALLET
delete pwalletMain;
pwalletMain = NULL;
delete zwalletMain;
zwalletMain = NULL;
#endif
globalVerifyHandle.reset();
ECC_Stop();
Expand Down Expand Up @@ -1700,13 +1697,12 @@ bool AppInit2()
// ********************************************************* Step 8: load wallet
#ifdef ENABLE_WALLET
pwalletMain = nullptr;
zwalletMain = nullptr;
if (fDisableWallet) {
LogPrintf("Wallet disabled!\n");
} else {
std::string warningString;
std::string errorString;
pwalletMain = CWallet::InitLoadWallet(fDisableWallet, strWalletFile, warningString, errorString, zwalletMain);
pwalletMain = CWallet::InitLoadWallet(fDisableWallet, strWalletFile, warningString, errorString);
if (!warningString.empty())
UIWarning(warningString);
if (!errorString.empty()) {
Expand Down
2 changes: 0 additions & 2 deletions src/init.h
Expand Up @@ -18,8 +18,6 @@ namespace boost
class thread_group;
} // namespace boost

extern CzPIVWallet* zwalletMain;

void StartShutdown();
bool ShutdownRequested();
/** Interrupt threads */
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/rpcwallet.cpp
Expand Up @@ -4808,7 +4808,7 @@ UniValue generatemintlist(const JSONRPCRequest& request)

int nCount = request.params[0].get_int();
int nRange = request.params[1].get_int();
CzPIVWallet* zwallet = pwalletMain->zwalletMain;
CzPIVWallet* zwallet = pwalletMain->getZWallet();

UniValue arrRet(UniValue::VARR);
for (int i = nCount; i < nCount + nRange; i++) {
Expand Down Expand Up @@ -4837,7 +4837,7 @@ UniValue dzpivstate(const JSONRPCRequest& request) {
"\nExamples\n" +
HelpExampleCli("mintpoolstatus", "") + HelpExampleRpc("mintpoolstatus", ""));

CzPIVWallet* zwallet = pwalletMain->zwalletMain;
CzPIVWallet* zwallet = pwalletMain->getZWallet();
UniValue obj(UniValue::VOBJ);
int nCount, nCountLastUsed;
zwallet->GetState(nCount, nCountLastUsed);
Expand Down Expand Up @@ -4906,7 +4906,7 @@ UniValue searchdzpiv(const JSONRPCRequest& request)

int nThreads = request.params[2].get_int();

CzPIVWallet* zwallet = pwalletMain->zwalletMain;
CzPIVWallet* zwallet = pwalletMain->getZWallet();

boost::thread_group* dzpivThreads = new boost::thread_group();
int nRangePerThread = nRange / nThreads;
Expand Down
19 changes: 10 additions & 9 deletions src/wallet/wallet.cpp
Expand Up @@ -314,7 +314,7 @@ bool CWallet::Lock()
{
LOCK(cs_KeyStore);
vMasterKey.clear();
if (zwalletMain) zwalletMain->Lock();
if (zwallet) zwallet->Lock();
}

NotifyStatusChanged(this);
Expand Down Expand Up @@ -372,14 +372,14 @@ bool CWallet::Unlock(const CKeyingMaterial& vMasterKeyIn)
vMasterKey = vMasterKeyIn;
fDecryptionThoroughlyChecked = true;

if (zwalletMain) {
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.");
}
zwalletMain->SetMasterSeed(nSeed, false);
zwallet->SetMasterSeed(nSeed, false);
}
}
}
Expand Down Expand Up @@ -3816,7 +3816,7 @@ std::string CWallet::GetWalletHelpString(bool showDebug)
return strUsage;
}

CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWalletFile, std::string& warningString, std::string& errorString, CzPIVWallet* zwallet)
CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWalletFile, std::string& warningString, std::string& errorString)
{
// needed to restore wallet transaction meta data after -zapwallettxes
std::vector<CWalletTx> vWtx;
Expand Down Expand Up @@ -3934,8 +3934,8 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall

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

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

RegisterValidationInterface(walletInstance);

Expand Down Expand Up @@ -3985,14 +3985,14 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
}
fVerifyingBlocks = false;

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

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

return walletInstance;
Expand Down Expand Up @@ -4161,6 +4161,7 @@ CWallet::CWallet(std::string strWalletFileIn)

CWallet::~CWallet()
{
delete zwallet;
delete pwalletdbEncryption;
delete pStakerStatus;
}
Expand Down
6 changes: 4 additions & 2 deletions src/wallet/wallet.h
Expand Up @@ -278,6 +278,9 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface

bool IsKeyUsed(const CPubKey& vchPubKey);

// Zerocoin wallet
CzPIVWallet* zwallet{nullptr};

public:

static const CAmount DEFAULT_STAKE_SPLIT_THRESHOLD = 500 * COIN;
Expand Down Expand Up @@ -651,7 +654,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
static std::string GetWalletHelpString(bool showDebug);

/* initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
static CWallet* InitLoadWallet(bool fDisableWallet, const std::string& strWalletFile, std::string& warningString, std::string& errorString, CzPIVWallet* zwallet);
static CWallet* InitLoadWallet(bool fDisableWallet, const std::string& strWalletFile, std::string& warningString, std::string& errorString);

/**
* Wallet post-init setup
Expand Down Expand Up @@ -720,7 +723,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
std::map<libzerocoin::CoinDenomination, CAmount> GetMyZerocoinDistribution() const;

// zPIV wallet
CzPIVWallet* zwalletMain{nullptr};
std::unique_ptr<CzPIVTracker> zpivTracker{nullptr};
void setZWallet(CzPIVWallet* zwallet);
CzPIVWallet* getZWallet();
Expand Down
16 changes: 8 additions & 8 deletions src/wallet/wallet_zerocoin.cpp
Expand Up @@ -230,15 +230,15 @@ bool CWallet::CreateZPIVOutPut(libzerocoin::CoinDenomination denomination, CTxOu
{
// mint a new coin (create Pedersen Commitment) and extract PublicCoin that is shareable from it
libzerocoin::PrivateCoin coin(Params().GetConsensus().Zerocoin_Params(false), denomination, false);
zwalletMain->GenerateDeterministicZPIV(denomination, coin, dMint);
zwallet->GenerateDeterministicZPIV(denomination, coin, dMint);

libzerocoin::PublicCoin pubCoin = coin.getPublicCoin();

// Validate
if(!pubCoin.validate())
return error("%s: newly created pubcoin is not valid", __func__);

zwalletMain->UpdateCount();
zwallet->UpdateCount();

CScript scriptSerializedCoin = CScript() << OP_ZEROCOINMINT << pubCoin.getValue().getvch().size() << pubCoin.getValue().getvch();
outMint = CTxOut(libzerocoin::ZerocoinDenominationToAmount(denomination), scriptSerializedCoin);
Expand Down Expand Up @@ -812,13 +812,13 @@ std::map<libzerocoin::CoinDenomination, CAmount> CWallet::GetMyZerocoinDistribut

void CWallet::setZWallet(CzPIVWallet* zwallet)
{
zwalletMain = zwallet;
this->zwallet = zwallet;
zpivTracker = std::unique_ptr<CzPIVTracker>(new CzPIVTracker(this));
}

CzPIVWallet* CWallet::getZWallet()
{
return zwalletMain;
return zwallet;
}

bool CWallet::IsMyZerocoinSpend(const CBigNum& bnSerial) const
Expand All @@ -831,7 +831,7 @@ bool CWallet::IsMyMint(const CBigNum& bnValue) const
if (zpivTracker->HasPubcoin(bnValue))
return true;

return zwalletMain->IsInMintPool(bnValue);
return zwallet->IsInMintPool(bnValue);
}

bool IsMintInChain(const uint256& hashPubcoin, uint256& txid, int& nHeight)
Expand Down Expand Up @@ -910,7 +910,7 @@ bool CWallet::GetMint(const uint256& hashSerial, CZerocoinMint& mint)
CDeterministicMint dMint;
if (!walletdb.ReadDeterministicMint(meta.hashPubcoin, dMint))
return error("%s: failed to read deterministic mint", __func__);
if (!zwalletMain->RegenerateMint(dMint, mint))
if (!zwallet->RegenerateMint(dMint, mint))
return error("%s: failed to generate mint", __func__);

return true;
Expand Down Expand Up @@ -944,8 +944,8 @@ bool CWallet::UpdateMint(const CBigNum& bnValue, const int& nHeight, const uint2
return zpivTracker->UpdateState(meta);
} else {
//Check if this mint is one that is in our mintpool (a potential future mint from our deterministic generation)
if (zwalletMain->IsInMintPool(bnValue)) {
if (zwalletMain->SetMintSeen(bnValue, nHeight, txid, denom))
if (zwallet->IsInMintPool(bnValue)) {
if (zwallet->SetMintSeen(bnValue, nHeight, txid, denom))
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/zpiv/zpivtracker.cpp
Expand Up @@ -449,7 +449,7 @@ std::set<CMintMeta> CzPIVTracker::ListMints(bool fUnusedOnly, bool fMatureOnly,
for (auto& dMint : listDeterministicDB) {
if (fExcludeV1 && dMint.GetVersion() < 2)
continue;
Add(dMint, false, false, wallet->zwalletMain);
Add(dMint, false, false, wallet->getZWallet());
}
LogPrint(BCLog::LEGACYZC, "%s: added %d dzpiv from DB\n", __func__, listDeterministicDB.size());
}
Expand Down

0 comments on commit 49646b2

Please sign in to comment.