Skip to content

Commit c9cdb22

Browse files
committed
Lock cs_wallet in additional places.
- Declare CWallet::KeepKey as requiring the lock, and take it before all calls. This eliminates a tsan data race. Also, fields within CReserveKey may similarly contend during mining, declare that they are guarded by the same lock. - Add other cs_wallet uses per lock annotations. This eliminates some clang warnings: AddKeyPubKey requires holding mutex 'pwalletParent->cs_wallet' exclusively
1 parent 3140a94 commit c9cdb22

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

src/veil/ringct/anonwallet.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -4527,6 +4527,7 @@ bool AnonWallet::AddStealthDestinationMeta(const CKeyID& idStealth, const CKeyID
45274527

45284528
bool AnonWallet::AddKeyToParent(const CKey& keySharedSecret)
45294529
{
4530+
LOCK(pwalletParent->cs_wallet);
45304531
//Since the new key is not derived through Ext, for now keep it in CWallet keystore
45314532
bool fSuccess = true;
45324533
if (!pwalletParent->AddKeyPubKey(keySharedSecret, keySharedSecret.GetPubKey())) {

src/wallet/wallet.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -4647,6 +4647,7 @@ std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) co
46474647

46484648
bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool internal)
46494649
{
4650+
LOCK(pwallet->cs_wallet);
46504651
if (nIndex == -1)
46514652
{
46524653
CKeyPool keypool;
@@ -4663,14 +4664,17 @@ bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool internal)
46634664

46644665
void CReserveKey::KeepKey()
46654666
{
4666-
if (nIndex != -1)
4667+
LOCK(pwallet->cs_wallet);
4668+
if (nIndex != -1) {
46674669
pwallet->KeepKey(nIndex);
4670+
}
46684671
nIndex = -1;
46694672
vchPubKey = CPubKey();
46704673
}
46714674

46724675
void CReserveKey::ReturnKey()
46734676
{
4677+
LOCK(pwallet->cs_wallet);
46744678
if (nIndex != -1) {
46754679
pwallet->ReturnKey(nIndex, fInternal, vchPubKey);
46764680
}

src/wallet/wallet.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
11571157
* or external keypool
11581158
*/
11591159
bool ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRequestedInternal);
1160-
void KeepKey(int64_t nIndex);
1160+
void KeepKey(int64_t nIndex) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
11611161
void ReturnKey(int64_t nIndex, bool fInternal, const CPubKey& pubkey);
11621162
bool GetKeyFromPool(CPubKey &key, bool internal = false);
11631163
int64_t GetOldestKeyPoolTime();
@@ -1380,8 +1380,8 @@ class CReserveKey final : public CReserveScript
13801380
{
13811381
protected:
13821382
CWallet* pwallet;
1383-
int64_t nIndex;
1384-
CPubKey vchPubKey;
1383+
int64_t nIndex GUARDED_BY(pwallet->cs_wallet);
1384+
CPubKey vchPubKey GUARDED_BY(pwallet->cs_wallet);
13851385
bool fInternal;
13861386
public:
13871387
explicit CReserveKey(CWallet* pwalletIn)

0 commit comments

Comments
 (0)