From a30481b154f49cbb9ab8387575f7141ca30c7c1a Mon Sep 17 00:00:00 2001 From: Peter Shugalev Date: Sat, 13 May 2023 12:37:42 +0200 Subject: [PATCH] Use single instance of CWalletDB in CSparkWallet --- src/spark/sparkwallet.cpp | 48 +++++++++++++++------------------------ src/spark/sparkwallet.h | 20 ++++++++-------- src/wallet/rpcwallet.cpp | 4 ++-- src/wallet/wallet.cpp | 2 +- src/wallet/wallet.h | 1 - 5 files changed, 31 insertions(+), 44 deletions(-) diff --git a/src/spark/sparkwallet.cpp b/src/spark/sparkwallet.cpp index 7c4d6531f6..082e0b2b4f 100644 --- a/src/spark/sparkwallet.cpp +++ b/src/spark/sparkwallet.cpp @@ -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); @@ -66,11 +62,11 @@ CSparkWallet::~CSparkWallet() { delete (ParallelOpThreadPool*)threadPool; } -void CSparkWallet::resetDiversifierFromDB(CWalletDB& walletdb) { +void CSparkWallet::resetDiversifierFromDB() { walletdb.readDiversifier(lastDiversifier); } -void CSparkWallet::updatetDiversifierInDB(CWalletDB& walletdb) { +void CSparkWallet::updatetDiversifierInDB() { walletdb.writeDiversifier(lastDiversifier); } @@ -173,8 +169,7 @@ spark::Address CSparkWallet::generateNewAddress() { spark::Address address(viewKey, lastDiversifier); addresses[lastDiversifier] = address; - CWalletDB walletdb(strWalletFile); - updatetDiversifierInDB(walletdb); + updatetDiversifierInDB(); return address; } @@ -281,9 +276,8 @@ std::vector CSparkWallet::ListSparkMints(bool fUnusedOnly, bool return setMints; } -std::list CSparkWallet::ListSparkSpends() const { +std::list CSparkWallet::ListSparkSpends() { std::list result; - CWalletDB walletdb(strWalletFile); walletdb.ListSparkSpends(result); return result; } @@ -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); @@ -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) { @@ -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); } } @@ -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( @@ -502,7 +494,7 @@ CAmount CSparkWallet::getMySpendAmount(const std::vector& lTags) c return result; } -void CSparkWallet::UpdateMintState(const std::vector& coins, const uint256& txHash, CWalletDB& walletdb) { +void CSparkWallet::UpdateMintState(const std::vector& coins, const uint256& txHash) { spark::CSparkState *sparkState = spark::CSparkState::GetState(); for (auto coin : coins) { try { @@ -528,7 +520,7 @@ void CSparkWallet::UpdateMintState(const std::vector& coins, const } uint256 lTagHash = primitives::GetLTagHash(recoveredCoinData.T); - addOrUpdateMint(mintMeta, lTagHash, walletdb); + addOrUpdateMint(mintMeta, lTagHash); if (mintMeta.isUsed) { uint256 spendTxHash; @@ -553,8 +545,7 @@ void CSparkWallet::UpdateMintState(const std::vector& coins, const void CSparkWallet::UpdateMintStateFromMempool(const std::vector& coins, const uint256& txHash) { ((ParallelOpThreadPool*)threadPool)->PostTask([=]() mutable { LOCK(cs_spark_wallet); - CWalletDB walletdb(strWalletFile); - UpdateMintState(coins, txHash, walletdb); + UpdateMintState(coins, txHash); }); } @@ -563,12 +554,11 @@ void CSparkWallet::UpdateMintStateFromBlock(const CBlock& block) { ((ParallelOpThreadPool*)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); } } }); @@ -580,10 +570,9 @@ void CSparkWallet::RemoveSparkMints(const std::vector& 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; } @@ -598,8 +587,7 @@ void CSparkWallet::RemoveSparkSpends(const std::unordered_map 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); } } diff --git a/src/spark/sparkwallet.h b/src/spark/sparkwallet.h index 7153f0b360..32b56bf78a 100644 --- a/src/spark/sparkwallet.h +++ b/src/spark/sparkwallet.h @@ -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); @@ -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 ListSparkMints(bool fUnusedOnly = false, bool fMatureOnly = false) const; - std::list ListSparkSpends() const; + std::list ListSparkSpends(); std::unordered_map getMintMap() const; // generate spark Coin from meta data spark::Coin getCoinFromMeta(const CSparkMintMeta& meta) const; @@ -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); @@ -87,7 +87,7 @@ class CSparkWallet { void UpdateSpendState(const GroupElement& lTag, const uint256& txHash, bool fUpdateMint = true); void UpdateSpendStateFromMempool(const std::vector& lTags, const uint256& txHash, bool fUpdateMint = true); void UpdateSpendStateFromBlock(const CBlock& block); - void UpdateMintState(const std::vector& coins, const uint256& txHash, CWalletDB& walletdb); + void UpdateMintState(const std::vector& coins, const uint256& txHash); void UpdateMintStateFromMempool(const std::vector& coins, const uint256& txHash); void UpdateMintStateFromBlock(const CBlock& block); void RemoveSparkMints(const std::vector& mints); @@ -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; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index aab51b2bca..1db1d2d455 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -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; @@ -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; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index c29a7d11e3..e9a9f98a87 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -7658,7 +7658,7 @@ void CWallet::HandleSparkTransaction(CWalletTx const & wtx) { // get spark coins and add into wallet std::vector 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) diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 33148312bc..4d983de711 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -755,7 +755,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface unsigned int nMasterKeyMaxID; std::unique_ptr zwallet; - std::unique_ptr sparkWallet; CWallet()