Skip to content

Commit a16ad1c

Browse files
committed
Merge pull request #3704 from gavinandresen/wallet_lock_fixes
Wallet locking fixes for -DDEBUG_LOCKORDER
2 parents beabca2 + ca4cf5c commit a16ad1c

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

qa/rpc-tests/txnmall.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ if [ $# -lt 1 ]; then
88
exit 1
99
fi
1010

11+
set -f
12+
1113
BITCOIND=${1}/bitcoind
1214
CLI=${1}/bitcoin-cli
1315

@@ -23,13 +25,13 @@ D=$(mktemp -d test.XXXXX)
2325

2426
D1=${D}/node1
2527
CreateDataDir $D1 port=11000 rpcport=11001
26-
B1ARGS="-datadir=$D1 -debug"
28+
B1ARGS="-datadir=$D1"
2729
$BITCOIND $B1ARGS &
2830
B1PID=$!
2931

3032
D2=${D}/node2
3133
CreateDataDir $D2 port=11010 rpcport=11011
32-
B2ARGS="-datadir=$D2 -debug"
34+
B2ARGS="-datadir=$D2"
3335
$BITCOIND $B2ARGS &
3436
B2PID=$!
3537

src/sync.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ void AssertLockHeldInternal(const char *pszName, const char* pszFile, int nLine,
140140
{
141141
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)&i, *lockstack)
142142
if (i.first == cs) return;
143-
LogPrintf("Lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
144-
assert(0);
143+
fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s",
144+
pszName, pszFile, nLine, LocksHeld().c_str());
145+
abort();
145146
}
146147

147148
#endif /* DEBUG_LOCKORDER */

src/wallet.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void CWallet::SetBestChain(const CBlockLocator& loc)
192192

193193
bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
194194
{
195-
AssertLockHeld(cs_wallet); // nWalletVersion
195+
LOCK(cs_wallet); // nWalletVersion
196196
if (nWalletVersion >= nVersion)
197197
return true;
198198

@@ -219,7 +219,7 @@ bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn,
219219

220220
bool CWallet::SetMaxVersion(int nVersion)
221221
{
222-
AssertLockHeld(cs_wallet); // nWalletVersion, nWalletMaxVersion
222+
LOCK(cs_wallet); // nWalletVersion, nWalletMaxVersion
223223
// cannot downgrade below current version
224224
if (nWalletVersion > nVersion)
225225
return false;
@@ -1621,14 +1621,17 @@ DBErrors CWallet::ZapWalletTx()
16211621

16221622
bool CWallet::SetAddressBook(const CTxDestination& address, const string& strName, const string& strPurpose)
16231623
{
1624-
AssertLockHeld(cs_wallet); // mapAddressBook
1625-
std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address);
1626-
mapAddressBook[address].name = strName;
1627-
if (!strPurpose.empty()) /* update purpose only if requested */
1628-
mapAddressBook[address].purpose = strPurpose;
1624+
bool fUpdated = false;
1625+
{
1626+
LOCK(cs_wallet); // mapAddressBook
1627+
std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address);
1628+
fUpdated = mi != mapAddressBook.end();
1629+
mapAddressBook[address].name = strName;
1630+
if (!strPurpose.empty()) /* update purpose only if requested */
1631+
mapAddressBook[address].purpose = strPurpose;
1632+
}
16291633
NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address),
1630-
mapAddressBook[address].purpose,
1631-
(mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED);
1634+
strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) );
16321635
if (!fFileBacked)
16331636
return false;
16341637
if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(CBitcoinAddress(address).ToString(), strPurpose))
@@ -1638,21 +1641,23 @@ bool CWallet::SetAddressBook(const CTxDestination& address, const string& strNam
16381641

16391642
bool CWallet::DelAddressBook(const CTxDestination& address)
16401643
{
1641-
1642-
AssertLockHeld(cs_wallet); // mapAddressBook
1643-
1644-
if(fFileBacked)
16451644
{
1646-
// Delete destdata tuples associated with address
1647-
std::string strAddress = CBitcoinAddress(address).ToString();
1648-
BOOST_FOREACH(const PAIRTYPE(string, string) &item, mapAddressBook[address].destdata)
1645+
LOCK(cs_wallet); // mapAddressBook
1646+
1647+
if(fFileBacked)
16491648
{
1650-
CWalletDB(strWalletFile).EraseDestData(strAddress, item.first);
1649+
// Delete destdata tuples associated with address
1650+
std::string strAddress = CBitcoinAddress(address).ToString();
1651+
BOOST_FOREACH(const PAIRTYPE(string, string) &item, mapAddressBook[address].destdata)
1652+
{
1653+
CWalletDB(strWalletFile).EraseDestData(strAddress, item.first);
1654+
}
16511655
}
1656+
mapAddressBook.erase(address);
16521657
}
16531658

1654-
mapAddressBook.erase(address);
16551659
NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), "", CT_DELETED);
1660+
16561661
if (!fFileBacked)
16571662
return false;
16581663
CWalletDB(strWalletFile).ErasePurpose(CBitcoinAddress(address).ToString());

src/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class CWallet : public CCryptoKeyStore, public CWalletInterface
363363
bool SetMaxVersion(int nVersion);
364364

365365
// get the current wallet format (the oldest client version guaranteed to understand this wallet)
366-
int GetVersion() { AssertLockHeld(cs_wallet); return nWalletVersion; }
366+
int GetVersion() { LOCK(cs_wallet); return nWalletVersion; }
367367

368368
// Get wallet transactions that conflict with given transaction (spend same outputs)
369369
std::set<uint256> GetConflicts(const uint256& txid) const;

0 commit comments

Comments
 (0)