Skip to content

Commit 65d90f5

Browse files
committed
Merge #9143: Refactor ZapWalletTxes to avoid layer violations
0165a56 Refactor ZapWalletTxes to avoid layer vialotions (Jonas Schnelli) Tree-SHA512: 51e3abbb866185817d14d685a65cb1070a7a60aa3db692670f3a5fdd328aad59afa765c5a6b13d9e8f6d219e735487f8c87998f2fb38fdd911c1fe19bea373b9
2 parents 0496e15 + 0165a56 commit 65d90f5

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
@@ -2870,12 +2870,16 @@ DBErrors CWallet::ZapSelectTx(vector<uint256>& vHashIn, vector<uint256>& vHashOu
28702870
{
28712871
if (!fFileBacked)
28722872
return DB_LOAD_OK;
2873-
DBErrors nZapSelectTxRet = CWalletDB(strWalletFile,"cr+").ZapSelectTx(this, vHashIn, vHashOut);
2873+
AssertLockHeld(cs_wallet); // mapWallet
2874+
vchDefaultKey = CPubKey();
2875+
DBErrors nZapSelectTxRet = CWalletDB(strWalletFile,"cr+").ZapSelectTx(vHashIn, vHashOut);
2876+
for (uint256 hash : vHashOut)
2877+
mapWallet.erase(hash);
2878+
28742879
if (nZapSelectTxRet == DB_NEED_REWRITE)
28752880
{
28762881
if (CDB::Rewrite(strWalletFile, "\x04pool"))
28772882
{
2878-
LOCK(cs_wallet);
28792883
setKeyPool.clear();
28802884
// Note: can't top-up keypool here, because wallet is locked.
28812885
// User will be prompted to unlock wallet the next operation
@@ -2896,7 +2900,8 @@ DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
28962900
{
28972901
if (!fFileBacked)
28982902
return DB_LOAD_OK;
2899-
DBErrors nZapWalletTxRet = CWalletDB(strWalletFile,"cr+").ZapWalletTx(this, vWtx);
2903+
vchDefaultKey = CPubKey();
2904+
DBErrors nZapWalletTxRet = CWalletDB(strWalletFile,"cr+").ZapWalletTx(vWtx);
29002905
if (nZapWalletTxRet == DB_NEED_REWRITE)
29012906
{
29022907
if (CDB::Rewrite(strWalletFile, "\x04pool"))

src/wallet/walletdb.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -659,20 +659,17 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
659659
return result;
660660
}
661661

662-
DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash, vector<CWalletTx>& vWtx)
662+
DBErrors CWalletDB::FindWalletTx(vector<uint256>& vTxHash, vector<CWalletTx>& vWtx)
663663
{
664-
pwallet->vchDefaultKey = CPubKey();
665664
bool fNoncriticalErrors = false;
666665
DBErrors result = DB_LOAD_OK;
667666

668667
try {
669-
LOCK(pwallet->cs_wallet);
670668
int nMinVersion = 0;
671669
if (Read((string)"minversion", nMinVersion))
672670
{
673671
if (nMinVersion > CLIENT_VERSION)
674672
return DB_TOO_NEW;
675-
pwallet->LoadMinVersion(nMinVersion);
676673
}
677674

678675
// Get cursor
@@ -725,12 +722,12 @@ DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash, vec
725722
return result;
726723
}
727724

728-
DBErrors CWalletDB::ZapSelectTx(CWallet* pwallet, vector<uint256>& vTxHashIn, vector<uint256>& vTxHashOut)
725+
DBErrors CWalletDB::ZapSelectTx(vector<uint256>& vTxHashIn, vector<uint256>& vTxHashOut)
729726
{
730727
// build list of wallet TXs and hashes
731728
vector<uint256> vTxHash;
732729
vector<CWalletTx> vWtx;
733-
DBErrors err = FindWalletTx(pwallet, vTxHash, vWtx);
730+
DBErrors err = FindWalletTx(vTxHash, vWtx);
734731
if (err != DB_LOAD_OK) {
735732
return err;
736733
}
@@ -749,7 +746,6 @@ DBErrors CWalletDB::ZapSelectTx(CWallet* pwallet, vector<uint256>& vTxHashIn, ve
749746
break;
750747
}
751748
else if ((*it) == hash) {
752-
pwallet->mapWallet.erase(hash);
753749
if(!EraseTx(hash)) {
754750
LogPrint("db", "Transaction was found for deletion but returned database error: %s\n", hash.GetHex());
755751
delerror = true;
@@ -764,11 +760,11 @@ DBErrors CWalletDB::ZapSelectTx(CWallet* pwallet, vector<uint256>& vTxHashIn, ve
764760
return DB_LOAD_OK;
765761
}
766762

767-
DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet, vector<CWalletTx>& vWtx)
763+
DBErrors CWalletDB::ZapWalletTx(vector<CWalletTx>& vWtx)
768764
{
769765
// build list of wallet TXs
770766
vector<uint256> vTxHash;
771-
DBErrors err = FindWalletTx(pwallet, vTxHash, vWtx);
767+
DBErrors err = FindWalletTx(vTxHash, vWtx);
772768
if (err != DB_LOAD_OK)
773769
return err;
774770

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)