Skip to content

Commit

Permalink
Use single instance of CWalletDB in CSparkWallet
Browse files Browse the repository at this point in the history
  • Loading branch information
psolstice committed May 22, 2023
1 parent bccaab1 commit a30481b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 44 deletions.
48 changes: 18 additions & 30 deletions src/spark/sparkwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@

const uint32_t DEFAULT_SPARK_NCOUNT = 1;

CSparkWallet::CSparkWallet(const std::string& strWalletFile) {

CWalletDB walletdb(strWalletFile);
this->strWalletFile = strWalletFile;

CSparkWallet::CSparkWallet(const std::string &strWalletFile) : walletdb(strWalletFile) {
const spark::Params* params = spark::Params::get_default();

fullViewKey = spark::FullViewKey(params);
Expand Down Expand Up @@ -66,11 +62,11 @@ CSparkWallet::~CSparkWallet() {
delete (ParallelOpThreadPool<void>*)threadPool;
}

void CSparkWallet::resetDiversifierFromDB(CWalletDB& walletdb) {
void CSparkWallet::resetDiversifierFromDB() {
walletdb.readDiversifier(lastDiversifier);
}

void CSparkWallet::updatetDiversifierInDB(CWalletDB& walletdb) {
void CSparkWallet::updatetDiversifierInDB() {
walletdb.writeDiversifier(lastDiversifier);
}

Expand Down Expand Up @@ -173,8 +169,7 @@ spark::Address CSparkWallet::generateNewAddress() {
spark::Address address(viewKey, lastDiversifier);

addresses[lastDiversifier] = address;
CWalletDB walletdb(strWalletFile);
updatetDiversifierInDB(walletdb);
updatetDiversifierInDB();
return address;
}

Expand Down Expand Up @@ -281,9 +276,8 @@ std::vector<CSparkMintMeta> CSparkWallet::ListSparkMints(bool fUnusedOnly, bool
return setMints;
}

std::list<CSparkSpendEntry> CSparkWallet::ListSparkSpends() const {
std::list<CSparkSpendEntry> CSparkWallet::ListSparkSpends() {
std::list<CSparkSpendEntry> result;
CWalletDB walletdb(strWalletFile);
walletdb.ListSparkSpends(result);
return result;
}
Expand Down Expand Up @@ -316,7 +310,7 @@ spark::Coin CSparkWallet::getCoinFromLTag(const GroupElement& lTag) const {
}


void CSparkWallet::clearAllMints(CWalletDB& walletdb) {
void CSparkWallet::clearAllMints() {
LOCK(cs_spark_wallet);
for (auto& itr : coinMeta) {
walletdb.EraseSparkMint(itr.first);
Expand All @@ -327,13 +321,13 @@ void CSparkWallet::clearAllMints(CWalletDB& walletdb) {
walletdb.writeDiversifier(lastDiversifier);
}

void CSparkWallet::eraseMint(const uint256& hash, CWalletDB& walletdb) {
void CSparkWallet::eraseMint(const uint256& hash) {
LOCK(cs_spark_wallet);
walletdb.EraseSparkMint(hash);
coinMeta.erase(hash);
}

void CSparkWallet::addOrUpdateMint(const CSparkMintMeta& mint, const uint256& lTagHash, CWalletDB& walletdb) {
void CSparkWallet::addOrUpdateMint(const CSparkMintMeta& mint, const uint256& lTagHash) {
LOCK(cs_spark_wallet);

if (mint.i > lastDiversifier) {
Expand All @@ -344,24 +338,23 @@ void CSparkWallet::addOrUpdateMint(const CSparkMintMeta& mint, const uint256& lT
walletdb.WriteSparkMint(lTagHash, mint);
}

void CSparkWallet::updateMint(const CSparkMintMeta& mint, CWalletDB& walletdb) {
void CSparkWallet::updateMint(const CSparkMintMeta& mint) {
LOCK(cs_spark_wallet);
for (const auto& coin : coinMeta) {
if (mint == coin.second) {
addOrUpdateMint(mint, coin.first, walletdb);
addOrUpdateMint(mint, coin.first);
}
}
}

void CSparkWallet::setCoinUnused(const GroupElement& lTag) {
LOCK(cs_spark_wallet);
CWalletDB walletdb(strWalletFile);
uint256 lTagHash = primitives::GetLTagHash(lTag);
CSparkMintMeta coinMeta = getMintMeta(lTagHash);

if (coinMeta != CSparkMintMeta()) {
coinMeta.isUsed = false;
updateMint(coinMeta, walletdb);
updateMint(coinMeta);
}
}

Expand Down Expand Up @@ -402,12 +395,11 @@ void CSparkWallet::UpdateSpendState(const GroupElement& lTag, const uint256& lTa
spendEntry.hashTx = txHash;
spendEntry.amount = mintMeta.v;

CWalletDB walletdb(strWalletFile);
walletdb.WriteSparkSpendEntry(spendEntry);

if (fUpdateMint) {
mintMeta.isUsed = true;
addOrUpdateMint(mintMeta, lTagHash, walletdb);
addOrUpdateMint(mintMeta, lTagHash);
}

// pwalletMain->NotifyZerocoinChanged(
Expand Down Expand Up @@ -502,7 +494,7 @@ CAmount CSparkWallet::getMySpendAmount(const std::vector<GroupElement>& lTags) c
return result;
}

void CSparkWallet::UpdateMintState(const std::vector<spark::Coin>& coins, const uint256& txHash, CWalletDB& walletdb) {
void CSparkWallet::UpdateMintState(const std::vector<spark::Coin>& coins, const uint256& txHash) {
spark::CSparkState *sparkState = spark::CSparkState::GetState();
for (auto coin : coins) {
try {
Expand All @@ -528,7 +520,7 @@ void CSparkWallet::UpdateMintState(const std::vector<spark::Coin>& coins, const
}

uint256 lTagHash = primitives::GetLTagHash(recoveredCoinData.T);
addOrUpdateMint(mintMeta, lTagHash, walletdb);
addOrUpdateMint(mintMeta, lTagHash);

if (mintMeta.isUsed) {
uint256 spendTxHash;
Expand All @@ -553,8 +545,7 @@ void CSparkWallet::UpdateMintState(const std::vector<spark::Coin>& coins, const
void CSparkWallet::UpdateMintStateFromMempool(const std::vector<spark::Coin>& coins, const uint256& txHash) {
((ParallelOpThreadPool<void>*)threadPool)->PostTask([=]() mutable {
LOCK(cs_spark_wallet);
CWalletDB walletdb(strWalletFile);
UpdateMintState(coins, txHash, walletdb);
UpdateMintState(coins, txHash);
});
}

Expand All @@ -563,12 +554,11 @@ void CSparkWallet::UpdateMintStateFromBlock(const CBlock& block) {

((ParallelOpThreadPool<void>*)threadPool)->PostTask([=] () mutable {
LOCK(cs_spark_wallet);
CWalletDB walletdb(strWalletFile);
for (const auto& tx : transactions) {
if (tx->IsSparkTransaction()) {
auto coins = spark::GetSparkMintCoins(*tx);
uint256 txHash = tx->GetHash();
UpdateMintState(coins, txHash, walletdb);
UpdateMintState(coins, txHash);
}
}
});
Expand All @@ -580,10 +570,9 @@ void CSparkWallet::RemoveSparkMints(const std::vector<spark::Coin>& mints) {
spark::IdentifiedCoinData identifiedCoinData = coin.identify(this->viewKey);
spark::RecoveredCoinData recoveredCoinData = coin.recover(this->fullViewKey, identifiedCoinData);

CWalletDB walletdb(strWalletFile);
uint256 lTagHash = primitives::GetLTagHash(recoveredCoinData.T);

eraseMint(lTagHash, walletdb);
eraseMint(lTagHash);
} catch (const std::runtime_error &e) {
continue;
}
Expand All @@ -598,8 +587,7 @@ void CSparkWallet::RemoveSparkSpends(const std::unordered_map<GroupElement, int>
if (coinMeta.count(lTagHash)) {
auto mintMeta = coinMeta[lTagHash];
mintMeta.isUsed = false;
CWalletDB walletdb(strWalletFile);
addOrUpdateMint(mintMeta, lTagHash, walletdb);
addOrUpdateMint(mintMeta, lTagHash);
walletdb.EraseSparkSpendEntry(spend.first);
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/spark/sparkwallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ const uint32_t BIP44_SPARK_INDEX = 0x6;

class CSparkWallet {
public:
CSparkWallet(const std::string& strWalletFile);
CSparkWallet(const std::string &strWalletFile);
~CSparkWallet();
// increment diversifier and generate address for that
spark::Address generateNextAddress();
spark::Address generateNewAddress();
spark::Address getDefaultAddress();
// assign difersifier to the value from db
void resetDiversifierFromDB(CWalletDB& walletdb);
void resetDiversifierFromDB();
// assign diversifier in to to current value
void updatetDiversifierInDB(CWalletDB& walletdb);
void updatetDiversifierInDB();

// functions for key set generation
spark::SpendKey generateSpendKey(const spark::Params* params);
Expand All @@ -45,7 +45,7 @@ class CSparkWallet {

// list spark mint, mint metadata in memory and in db should be the same at this moment, so get from memory
std::vector<CSparkMintMeta> ListSparkMints(bool fUnusedOnly = false, bool fMatureOnly = false) const;
std::list<CSparkSpendEntry> ListSparkSpends() const;
std::list<CSparkSpendEntry> ListSparkSpends();
std::unordered_map<uint256, CSparkMintMeta> getMintMap() const;
// generate spark Coin from meta data
spark::Coin getCoinFromMeta(const CSparkMintMeta& meta) const;
Expand All @@ -62,12 +62,12 @@ class CSparkWallet {
CAmount getAddressUnconfirmedBalance(const spark::Address& address);

// function to be used for zap wallet
void clearAllMints(CWalletDB& walletdb);
void clearAllMints();
// erase mint meta data from memory and from db
void eraseMint(const uint256& hash, CWalletDB& walletdb);
void eraseMint(const uint256& hash);
// add mint meta data to memory and to db
void addOrUpdateMint(const CSparkMintMeta& mint, const uint256& lTagHash, CWalletDB& walletdb);
void updateMint(const CSparkMintMeta& mint, CWalletDB& walletdb);
void addOrUpdateMint(const CSparkMintMeta& mint, const uint256& lTagHash);
void updateMint(const CSparkMintMeta& mint);

void setCoinUnused(const GroupElement& lTag);

Expand All @@ -87,7 +87,7 @@ class CSparkWallet {
void UpdateSpendState(const GroupElement& lTag, const uint256& txHash, bool fUpdateMint = true);
void UpdateSpendStateFromMempool(const std::vector<GroupElement>& lTags, const uint256& txHash, bool fUpdateMint = true);
void UpdateSpendStateFromBlock(const CBlock& block);
void UpdateMintState(const std::vector<spark::Coin>& coins, const uint256& txHash, CWalletDB& walletdb);
void UpdateMintState(const std::vector<spark::Coin>& coins, const uint256& txHash);
void UpdateMintStateFromMempool(const std::vector<spark::Coin>& coins, const uint256& txHash);
void UpdateMintStateFromBlock(const CBlock& block);
void RemoveSparkMints(const std::vector<spark::Coin>& mints);
Expand Down Expand Up @@ -136,7 +136,7 @@ class CSparkWallet {
mutable CCriticalSection cs_spark_wallet;

private:
std::string strWalletFile;
CWalletDB walletdb;
// this is latest used diversifier
int32_t lastDiversifier;

Expand Down
4 changes: 2 additions & 2 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3467,7 +3467,7 @@ UniValue resetsparkmints(const JSONRPCRequest& request) {
BOOST_FOREACH(CSparkMintMeta& mint, listMints) {
mint.isUsed = false;
mint.nHeight = -1;
pwallet->sparkWallet->updateMint(mint, walletdb);
pwallet->sparkWallet->updateMint(mint);
}

return NullUniValue;
Expand Down Expand Up @@ -3498,7 +3498,7 @@ UniValue setsparkmintstatus(const JSONRPCRequest& request) {

if (coinMeta != CSparkMintMeta()) {
coinMeta.isUsed = fStatus;
pwallet->sparkWallet->updateMint(coinMeta, walletdb);
pwallet->sparkWallet->updateMint(coinMeta);
}

return NullUniValue;
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7658,7 +7658,7 @@ void CWallet::HandleSparkTransaction(CWalletTx const & wtx) {

// get spark coins and add into wallet
std::vector<spark::Coin> coins = spark::GetSparkMintCoins(*wtx.tx);
sparkWallet->UpdateMintState(coins, txHash, walletdb);
sparkWallet->UpdateMintState(coins, txHash);
}

void CWallet::LabelSendingPcode(bip47::CPaymentCode const & pcode_, std::string const & label, bool remove)
Expand Down
1 change: 0 additions & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
unsigned int nMasterKeyMaxID;

std::unique_ptr<CHDMintWallet> zwallet;

std::unique_ptr<CSparkWallet> sparkWallet;

CWallet()
Expand Down

0 comments on commit a30481b

Please sign in to comment.