Skip to content

Commit 0165a56

Browse files
committed
Refactor ZapWalletTxes to avoid layer vialotions
1 parent 71148b8 commit 0165a56

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/wallet/wallet.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,12 +2846,16 @@ DBErrors CWallet::ZapSelectTx(vector<uint256>& vHashIn, vector<uint256>& vHashOu
28462846
{
28472847
if (!fFileBacked)
28482848
return DB_LOAD_OK;
2849-
DBErrors nZapSelectTxRet = CWalletDB(strWalletFile,"cr+").ZapSelectTx(this, vHashIn, vHashOut);
2849+
AssertLockHeld(cs_wallet); // mapWallet
2850+
vchDefaultKey = CPubKey();
2851+
DBErrors nZapSelectTxRet = CWalletDB(strWalletFile,"cr+").ZapSelectTx(vHashIn, vHashOut);
2852+
for (uint256 hash : vHashOut)
2853+
mapWallet.erase(hash);
2854+
28502855
if (nZapSelectTxRet == DB_NEED_REWRITE)
28512856
{
28522857
if (CDB::Rewrite(strWalletFile, "\x04pool"))
28532858
{
2854-
LOCK(cs_wallet);
28552859
setKeyPool.clear();
28562860
// Note: can't top-up keypool here, because wallet is locked.
28572861
// User will be prompted to unlock wallet the next operation
@@ -2872,7 +2876,8 @@ DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
28722876
{
28732877
if (!fFileBacked)
28742878
return DB_LOAD_OK;
2875-
DBErrors nZapWalletTxRet = CWalletDB(strWalletFile,"cr+").ZapWalletTx(this, vWtx);
2879+
vchDefaultKey = CPubKey();
2880+
DBErrors nZapWalletTxRet = CWalletDB(strWalletFile,"cr+").ZapWalletTx(vWtx);
28762881
if (nZapWalletTxRet == DB_NEED_REWRITE)
28772882
{
28782883
if (CDB::Rewrite(strWalletFile, "\x04pool"))

src/wallet/walletdb.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -646,20 +646,17 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
646646
return result;
647647
}
648648

649-
DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash, vector<CWalletTx>& vWtx)
649+
DBErrors CWalletDB::FindWalletTx(vector<uint256>& vTxHash, vector<CWalletTx>& vWtx)
650650
{
651-
pwallet->vchDefaultKey = CPubKey();
652651
bool fNoncriticalErrors = false;
653652
DBErrors result = DB_LOAD_OK;
654653

655654
try {
656-
LOCK(pwallet->cs_wallet);
657655
int nMinVersion = 0;
658656
if (Read((string)"minversion", nMinVersion))
659657
{
660658
if (nMinVersion > CLIENT_VERSION)
661659
return DB_TOO_NEW;
662-
pwallet->LoadMinVersion(nMinVersion);
663660
}
664661

665662
// Get cursor
@@ -712,12 +709,12 @@ DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash, vec
712709
return result;
713710
}
714711

715-
DBErrors CWalletDB::ZapSelectTx(CWallet* pwallet, vector<uint256>& vTxHashIn, vector<uint256>& vTxHashOut)
712+
DBErrors CWalletDB::ZapSelectTx(vector<uint256>& vTxHashIn, vector<uint256>& vTxHashOut)
716713
{
717714
// build list of wallet TXs and hashes
718715
vector<uint256> vTxHash;
719716
vector<CWalletTx> vWtx;
720-
DBErrors err = FindWalletTx(pwallet, vTxHash, vWtx);
717+
DBErrors err = FindWalletTx(vTxHash, vWtx);
721718
if (err != DB_LOAD_OK) {
722719
return err;
723720
}
@@ -736,7 +733,6 @@ DBErrors CWalletDB::ZapSelectTx(CWallet* pwallet, vector<uint256>& vTxHashIn, ve
736733
break;
737734
}
738735
else if ((*it) == hash) {
739-
pwallet->mapWallet.erase(hash);
740736
if(!EraseTx(hash)) {
741737
LogPrint("db", "Transaction was found for deletion but returned database error: %s\n", hash.GetHex());
742738
delerror = true;
@@ -751,11 +747,11 @@ DBErrors CWalletDB::ZapSelectTx(CWallet* pwallet, vector<uint256>& vTxHashIn, ve
751747
return DB_LOAD_OK;
752748
}
753749

754-
DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet, vector<CWalletTx>& vWtx)
750+
DBErrors CWalletDB::ZapWalletTx(vector<CWalletTx>& vWtx)
755751
{
756752
// build list of wallet TXs
757753
vector<uint256> vTxHash;
758-
DBErrors err = FindWalletTx(pwallet, vTxHash, vWtx);
754+
DBErrors err = FindWalletTx(vTxHash, vWtx);
759755
if (err != DB_LOAD_OK)
760756
return err;
761757

src/wallet/walletdb.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ class CWalletDB : public CDB
167167
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
168168

169169
DBErrors LoadWallet(CWallet* pwallet);
170-
DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
171-
DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
172-
DBErrors ZapSelectTx(CWallet* pwallet, std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
170+
DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
171+
DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);
172+
DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
173173
static bool Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKeys);
174174
static bool Recover(CDBEnv& dbenv, const std::string& filename);
175175

0 commit comments

Comments
 (0)