From f994822a18754342d1edd4e7ea9cbaaac73b5a7d Mon Sep 17 00:00:00 2001 From: dimxy Date: Tue, 28 Mar 2023 01:31:48 +0500 Subject: [PATCH] remove long running call to CWalletdb from OrderedTxItems in a loop --- src/wallet/wallet.cpp | 10 ++++++---- src/wallet/wallet.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index f5707a14313..05fe037808b 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1315,10 +1315,9 @@ int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb) return nRet; } -CWallet::TxItems CWallet::OrderedTxItems(std::list& acentries, std::string strAccount) +CWallet::TxItems CWallet::OrderedTxItems(std::list& acentries, std::string strAccount, CWalletDB *pwalletdbIn) { AssertLockHeld(cs_wallet); // mapWallet - CWalletDB walletdb(strWalletFile); // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap. TxItems txOrdered; @@ -1331,12 +1330,15 @@ CWallet::TxItems CWallet::OrderedTxItems(std::list& acentries, txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0))); //fprintf(stderr,"ordered iter.%d %s\n",(int32_t)wtx->nOrderPos,wtx->GetHash().GetHex().c_str()); } + + CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile); acentries.clear(); - walletdb.ListAccountCreditDebit(strAccount, acentries); + pwalletdb->ListAccountCreditDebit(strAccount, acentries); BOOST_FOREACH(CAccountingEntry& entry, acentries) { txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry))); } + if (!pwalletdbIn) delete pwalletdb; return txOrdered; } @@ -1509,7 +1511,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletD // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future int64_t latestTolerated = latestNow + 300; std::list acentries; - TxItems txOrdered = OrderedTxItems(acentries); + TxItems txOrdered = OrderedTxItems(acentries, "", pwalletdb); for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) { CWalletTx *const pwtx = (*it).second.first; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index ac45158daa8..9ac0c5f630b 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1127,7 +1127,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface * @return multimap of ordered transactions and accounting entries * @warning Returned pointers are *only* valid within the scope of passed acentries */ - TxItems OrderedTxItems(std::list& acentries, std::string strAccount = ""); + TxItems OrderedTxItems(std::list& acentries, std::string strAccount = "", CWalletDB *pwalletdbIn = nullptr); void MarkDirty(); bool UpdateNullifierNoteMap();