Skip to content

Commit

Permalink
Test performance optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
riordant committed Jun 3, 2020
1 parent 25028ff commit a7a6d1c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
16 changes: 8 additions & 8 deletions src/hdmint/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ bool CHDMintTracker::UnArchive(const uint256& hashPubcoin, bool isDeterministic)
CHDMint dMint;
if (!walletdb.UnarchiveHDMint(hashPubcoin, dMint))
return error("%s: failed to unarchive deterministic mint", __func__);
Add(dMint, false);
Add(walletdb, dMint, false);
} else {
CSigmaEntry sigma;
if (!walletdb.UnarchiveSigmaMint(hashPubcoin, sigma))
return error("%s: failed to unarchivesigma mint", __func__);
Add(sigma, false);
Add(walletdb, sigma, false);
}

LogPrintf("%s: unarchived %s\n", __func__, hashPubcoin.GetHex());
Expand Down Expand Up @@ -283,7 +283,7 @@ bool CHDMintTracker::UpdateState(const CMintMeta& meta)
* @param isArchived set to true if this mint is archived, used to set meta object correctly
* @return success
*/
void CHDMintTracker::Add(const CHDMint& dMint, bool isNew, bool isArchived)
void CHDMintTracker::Add(CWalletDB& walletdb, const CHDMint& dMint, bool isNew, bool isArchived)
{
CMintMeta meta;
meta.SetPubCoinValue(dMint.GetPubcoinValue());
Expand All @@ -305,10 +305,10 @@ void CHDMintTracker::Add(const CHDMint& dMint, bool isNew, bool isArchived)
CT_UPDATED);

if (isNew)
CWalletDB(strWalletFile).WriteHDMint(dMint);
walletdb.WriteHDMint(dMint);
}

void CHDMintTracker::Add(const CSigmaEntry& sigma, bool isNew, bool isArchived)
void CHDMintTracker::Add(CWalletDB& walletdb, const CSigmaEntry& sigma, bool isNew, bool isArchived)
{
CMintMeta meta;
meta.SetPubCoinValue(sigma.value);
Expand All @@ -324,7 +324,7 @@ void CHDMintTracker::Add(const CSigmaEntry& sigma, bool isNew, bool isArchived)
mapSerialHashes[meta.hashSerial] = meta;

if (isNew)
CWalletDB(strWalletFile).WriteSigmaEntry(sigma);
walletdb.WriteSigmaEntry(sigma);
}

/**
Expand Down Expand Up @@ -720,13 +720,13 @@ std::vector<CMintMeta> CHDMintTracker::ListMints(bool fUnusedOnly, bool fMatureO
std::list<CSigmaEntry> listMintsDB;
walletdb.ListSigmaPubCoin(listMintsDB);
for (auto& mint : listMintsDB){
Add(mint);
Add(walletdb, mint);
}
LogPrint("zero", "%s: added %d sigmamints from DB\n", __func__, listMintsDB.size());

std::list<CHDMint> listDeterministicDB = walletdb.ListHDMints();
for (auto& dMint : listDeterministicDB) {
Add(dMint, false, false);
Add(walletdb, dMint, false, false);
}
LogPrint("zero", "%s: added %d hdmint from DB\n", __func__, listDeterministicDB.size());
}
Expand Down
5 changes: 3 additions & 2 deletions src/hdmint/tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "primitives/zerocoin.h"
#include "hdmint/mintpool.h"
#include "wallet/walletdb.h"
#include <list>

class CHDMint;
Expand All @@ -25,8 +26,8 @@ class CHDMintTracker
public:
CHDMintTracker(std::string strWalletFile);
~CHDMintTracker();
void Add(const CHDMint& dMint, bool isNew = false, bool isArchived = false);
void Add(const CSigmaEntry& sigma, bool isNew = false, bool isArchived = false);
void Add(CWalletDB& walletdb, const CHDMint& dMint, bool isNew = false, bool isArchived = false);
void Add(CWalletDB& walletdb, const CSigmaEntry& sigma, bool isNew = false, bool isArchived = false);
bool Archive(CMintMeta& meta);
bool HasPubcoinHash(const uint256& hashPubcoin) const;
bool HasSerialHash(const uint256& hashSerial) const;
Expand Down
8 changes: 6 additions & 2 deletions src/hdmint/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ void CHDMintWallet::GenerateMintPool(CWalletDB& walletdb, int32_t nIndex)
LogPrintf("%s : hashSeedMaster=%s hashPubcoin=%s seedId=%d count=%d\n", __func__, hashSeedMaster.GetHex(), hashPubcoin.GetHex(), seedId.GetHex(), nLastCount);
}

// write hdchain back to database
if (!walletdb.WriteHDChain(pwalletMain->GetHDChain()))
throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed");

// Update local + DB entries for count last generated
nCountNextGenerate = nLastCount;
walletdb.WriteMintSeedCount(nCountNextGenerate);
Expand Down Expand Up @@ -433,7 +437,7 @@ bool CHDMintWallet::SetMintSeedSeen(CWalletDB& walletdb, std::pair<uint256,MintP

LogPrintf("%s: Adding mint to tracker.. \n", __func__);
// Add to tracker which also adds to database
tracker.Add(dMint, true);
tracker.Add(walletdb, dMint, true);

return true;
}
Expand Down Expand Up @@ -526,7 +530,7 @@ bool CHDMintWallet::CreateMintSeed(CWalletDB& walletdb, uint512& mintSeed, const
int32_t chainIndex = pwalletMain->GetHDChain().nExternalChainCounters[BIP44_MINT_INDEX];
if(nCount==chainIndex){
// If chainIndex is the same as n (ie. we are generating next available key), generate a new key.
pubKey = pwalletMain->GenerateNewKey(BIP44_MINT_INDEX);
pubKey = pwalletMain->GenerateNewKey(BIP44_MINT_INDEX, nWriteChain);
}
else if(nCount<chainIndex){
// if it's less than the current chain index, we are regenerating the mintpool. get the key at n
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3760,7 +3760,7 @@ UniValue resetsigmamint(const JSONRPCRequest& request) {
}
dMint.SetUsed(false);
dMint.SetHeight(-1);
pwallet->zwallet->GetTracker().Add(dMint, true);
pwallet->zwallet->GetTracker().Add(walletdb, dMint, true);
}

return NullUniValue;
Expand Down Expand Up @@ -4075,10 +4075,10 @@ UniValue setsigmamintstatus(const JSONRPCRequest& request) {

if(!mint.isDeterministic){
zerocoinItem.IsUsed = fStatus;
pwallet->zwallet->GetTracker().Add(zerocoinItem, true);
pwallet->zwallet->GetTracker().Add(walletdb, zerocoinItem, true);
}else{
dMint.SetUsed(fStatus);
pwallet->zwallet->GetTracker().Add(dMint, true);
pwallet->zwallet->GetTracker().Add(walletdb, dMint, true);
}

if (!fStatus) {
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/test/sigma_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static void GenerateBlockWithCoins(const std::vector<std::pair<sigma::CoinDenomi
block->second.nHeight = block->second.pprev->nHeight + 1;

// generate coins
CWalletDB walletdb(pwalletMain->strWalletFile);
CHDMint dMint;
for (auto& coin : coins) {
for (int i = 0; i < coin.second; i++) {
Expand All @@ -89,7 +90,7 @@ static void GenerateBlockWithCoins(const std::vector<std::pair<sigma::CoinDenomi
block->second.sigmaMintedPubCoins[std::make_pair(coin.first, 1)].push_back(pub);

if (addToWallet) {
pwalletMain->zwallet->GetTracker().Add(dMint, true);
pwalletMain->zwallet->GetTracker().Add(walletdb, dMint, true);
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ CPubKey CWallet::GetKeyFromKeypath(uint32_t nChange, uint32_t nChild) {
return pubkey;
}

CPubKey CWallet::GenerateNewKey(uint32_t nChange)
CPubKey CWallet::GenerateNewKey(uint32_t nChange, bool nWriteChain)
{
AssertLockHeld(cs_wallet); // mapKeyMetadata
bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
Expand Down Expand Up @@ -242,8 +242,10 @@ CPubKey CWallet::GenerateNewKey(uint32_t nChange)
secret = childKey.key;

// update the chain model in the database
if (!CWalletDB(strWalletFile).WriteHDChain(hdChain))
throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed");
if(nWriteChain){
if (!CWalletDB(strWalletFile).WriteHDChain(hdChain))
throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed");
}
/* bitcoin 0.14:
if (IsHDEnabled()) {
DeriveNewChildKey(metadata, secret);
Expand Down Expand Up @@ -4204,7 +4206,7 @@ bool CWallet::CreateSigmaMintModel(string &stringError, const string& denomAmoun
return false;

dMint.SetTxHash(wtx.GetHash());
zwallet->GetTracker().Add(dMint, true);
zwallet->GetTracker().Add(walletdb, dMint, true);

LogPrintf("CreateZerocoinMintModel() -> NotifyZerocoinChanged\n");
LogPrintf("pubcoin=%s, isUsed=%s\n", newCoin.getPublicCoin().getValue().GetHex(), dMint.IsUsed());
Expand Down Expand Up @@ -4523,7 +4525,7 @@ bool CWallet::CreateZerocoinToSigmaRemintModel(string &stringError, int version,
//update mints with full transaction hash and then database them
for (CHDMint hdMint : vHDMints) {
hdMint.SetTxHash(wtxNew.GetHash());
zwallet->GetTracker().Add(hdMint, true);
zwallet->GetTracker().Add(walletdb, hdMint, true);
NotifyZerocoinChanged(this,
hdMint.GetPubcoinValue().GetHex(),
"New (" + std::to_string(hdMint.GetDenominationValue()) + " mint)",
Expand Down Expand Up @@ -6581,7 +6583,7 @@ string CWallet::MintAndStoreSigma(const vector<CRecipient>& vecSend,
CWalletDB walletdb(pwalletMain->strWalletFile);
for (CHDMint dMint : vDMints) {
dMint.SetTxHash(wtxNew.GetHash());
zwallet->GetTracker().Add(dMint, true);
zwallet->GetTracker().Add(walletdb, dMint, true);
NotifyZerocoinChanged(this,
dMint.GetPubcoinValue().GetHex(),
"New (" + std::to_string(dMint.GetDenominationValue()) + " mint)",
Expand Down Expand Up @@ -7038,7 +7040,7 @@ bool CWallet::CommitSigmaTransaction(CWalletTx& wtxNew, std::vector<CSigmaEntry>

for (auto& change : changes) {
change.SetTxHash(wtxNew.GetHash());
zwallet->GetTracker().Add(change, true);
zwallet->GetTracker().Add(db, change, true);

// raise event
NotifyZerocoinChanged(this,
Expand All @@ -7048,7 +7050,7 @@ bool CWallet::CommitSigmaTransaction(CWalletTx& wtxNew, std::vector<CSigmaEntry>
}

// Update nCountNextUse in HDMint wallet database
zwallet->UpdateCountDB(walletdb);
zwallet->UpdateCountDB(db);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
* keystore implementation
* Generate a new key
*/
CPubKey GenerateNewKey(uint32_t nChange=0);
CPubKey GenerateNewKey(uint32_t nChange=0, bool nWriteChain=true);
void DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret);
//! Adds a key to the store, and saves it to disk.
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
Expand Down

0 comments on commit a7a6d1c

Please sign in to comment.