From 5a5094bbb5ba7a9b1ac993e5a238fcea4914cdaa Mon Sep 17 00:00:00 2001 From: Larry Ruane Date: Mon, 1 Apr 2019 10:15:16 -0600 Subject: [PATCH] simplify locking, merge cs_SpendingKeyStore into cs_KeyStore --- src/keystore.cpp | 31 ++++++++++++++++--------------- src/keystore.h | 15 +++++++-------- src/wallet/crypter.cpp | 26 +++++++++++++------------- src/wallet/crypter.h | 4 ++-- src/wallet/wallet.cpp | 4 ++-- 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/keystore.cpp b/src/keystore.cpp index 6c94bf05a..ebdae0dca 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -25,7 +25,7 @@ bool CKeyStore::AddKey(const CKey &key) { bool CBasicKeyStore::SetHDSeed(const HDSeed& seed) { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!hdSeed.IsNull()) { // Don't allow an existing seed to be changed. We can maybe relax this // restriction later once we have worked out the UX implications. @@ -37,13 +37,13 @@ bool CBasicKeyStore::SetHDSeed(const HDSeed& seed) bool CBasicKeyStore::HaveHDSeed() const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); return !hdSeed.IsNull(); } bool CBasicKeyStore::GetHDSeed(HDSeed& seedOut) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (hdSeed.IsNull()) { return false; } else { @@ -115,7 +115,7 @@ bool CBasicKeyStore::HaveWatchOnly() const bool CBasicKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk) { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); auto address = sk.address(); mapSproutSpendingKeys[address] = sk; mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(sk.receiving_key()))); @@ -127,7 +127,7 @@ bool CBasicKeyStore::AddSaplingSpendingKey( const libzcash::SaplingExtendedSpendingKey &sk, const libzcash::SaplingPaymentAddress &defaultAddr) { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); auto fvk = sk.expsk.full_viewing_key(); // if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it @@ -142,7 +142,7 @@ bool CBasicKeyStore::AddSaplingSpendingKey( bool CBasicKeyStore::AddSproutViewingKey(const libzcash::SproutViewingKey &vk) { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); auto address = vk.address(); mapSproutViewingKeys[address] = vk; mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(vk.sk_enc))); @@ -153,7 +153,7 @@ bool CBasicKeyStore::AddSaplingFullViewingKey( const libzcash::SaplingFullViewingKey &fvk, const libzcash::SaplingPaymentAddress &defaultAddr) { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); auto ivk = fvk.in_viewing_key(); mapSaplingFullViewingKeys[ivk] = fvk; @@ -167,7 +167,7 @@ bool CBasicKeyStore::AddSaplingIncomingViewingKey( const libzcash::SaplingIncomingViewingKey &ivk, const libzcash::SaplingPaymentAddress &addr) { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); // Add addr -> SaplingIncomingViewing to SaplingIncomingViewingKeyMap mapSaplingIncomingViewingKeys[addr] = ivk; @@ -177,26 +177,26 @@ bool CBasicKeyStore::AddSaplingIncomingViewingKey( bool CBasicKeyStore::RemoveSproutViewingKey(const libzcash::SproutViewingKey &vk) { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); mapSproutViewingKeys.erase(vk.address()); return true; } bool CBasicKeyStore::HaveSproutViewingKey(const libzcash::SproutPaymentAddress &address) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); return mapSproutViewingKeys.count(address) > 0; } bool CBasicKeyStore::HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); return mapSaplingFullViewingKeys.count(ivk) > 0; } bool CBasicKeyStore::HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); return mapSaplingIncomingViewingKeys.count(addr) > 0; } @@ -204,7 +204,7 @@ bool CBasicKeyStore::GetSproutViewingKey( const libzcash::SproutPaymentAddress &address, libzcash::SproutViewingKey &vkOut) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); SproutViewingKeyMap::const_iterator mi = mapSproutViewingKeys.find(address); if (mi != mapSproutViewingKeys.end()) { vkOut = mi->second; @@ -216,7 +216,7 @@ bool CBasicKeyStore::GetSproutViewingKey( bool CBasicKeyStore::GetSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk, libzcash::SaplingFullViewingKey &fvkOut) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); SaplingFullViewingKeyMap::const_iterator mi = mapSaplingFullViewingKeys.find(ivk); if (mi != mapSaplingFullViewingKeys.end()) { fvkOut = mi->second; @@ -228,7 +228,7 @@ bool CBasicKeyStore::GetSaplingFullViewingKey(const libzcash::SaplingIncomingVie bool CBasicKeyStore::GetSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr, libzcash::SaplingIncomingViewingKey &ivkOut) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); SaplingIncomingViewingKeyMap::const_iterator mi = mapSaplingIncomingViewingKeys.find(addr); if (mi != mapSaplingIncomingViewingKeys.end()) { ivkOut = mi->second; @@ -242,6 +242,7 @@ bool CBasicKeyStore::GetSaplingExtendedSpendingKey(const libzcash::SaplingPaymen libzcash::SaplingIncomingViewingKey ivk; libzcash::SaplingFullViewingKey fvk; + LOCK(cs_KeyStore); return GetSaplingIncomingViewingKey(addr, ivk) && GetSaplingFullViewingKey(ivk, fvk) && GetSaplingSpendingKey(fvk, extskOut); diff --git a/src/keystore.h b/src/keystore.h index 7c78895dc..4c9607ef7 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -23,7 +23,6 @@ class CKeyStore { protected: mutable CCriticalSection cs_KeyStore; - mutable CCriticalSection cs_SpendingKeyStore; public: virtual ~CKeyStore() {} @@ -185,7 +184,7 @@ public: { bool result; { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); result = (mapSproutSpendingKeys.count(address) > 0); } return result; @@ -193,7 +192,7 @@ public: bool GetSproutSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey &skOut) const { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); SproutSpendingKeyMap::const_iterator mi = mapSproutSpendingKeys.find(address); if (mi != mapSproutSpendingKeys.end()) { @@ -206,7 +205,7 @@ public: bool GetNoteDecryptor(const libzcash::SproutPaymentAddress &address, ZCNoteDecryption &decOut) const { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); NoteDecryptorMap::const_iterator mi = mapNoteDecryptors.find(address); if (mi != mapNoteDecryptors.end()) { @@ -220,7 +219,7 @@ public: { setAddress.clear(); { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); SproutSpendingKeyMap::const_iterator mi = mapSproutSpendingKeys.begin(); while (mi != mapSproutSpendingKeys.end()) { @@ -244,7 +243,7 @@ public: { bool result; { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); result = (mapSaplingSpendingKeys.count(fvk) > 0); } return result; @@ -252,7 +251,7 @@ public: bool GetSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk, libzcash::SaplingExtendedSpendingKey &skOut) const { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); SaplingSpendingKeyMap::const_iterator mi = mapSaplingSpendingKeys.find(fvk); if (mi != mapSaplingSpendingKeys.end()) @@ -288,7 +287,7 @@ public: { setAddress.clear(); { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); auto mi = mapSaplingIncomingViewingKeys.begin(); while (mi != mapSaplingIncomingViewingKeys.end()) { diff --git a/src/wallet/crypter.cpp b/src/wallet/crypter.cpp index 3cd91abe4..3ebc7bbc8 100644 --- a/src/wallet/crypter.cpp +++ b/src/wallet/crypter.cpp @@ -187,7 +187,7 @@ static bool DecryptSaplingSpendingKey(const CKeyingMaterial& vMasterKey, bool CCryptoKeyStore::SetCrypted() { - LOCK2(cs_KeyStore, cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (fUseCrypto) return true; if (!(mapKeys.empty() && mapSproutSpendingKeys.empty() && mapSaplingSpendingKeys.empty())) @@ -213,7 +213,7 @@ bool CCryptoKeyStore::Lock() bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) { { - LOCK2(cs_KeyStore, cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!SetCrypted()) return false; @@ -290,7 +290,7 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) bool CCryptoKeyStore::SetHDSeed(const HDSeed& seed) { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) { return CBasicKeyStore::SetHDSeed(seed); } @@ -317,7 +317,7 @@ bool CCryptoKeyStore::SetCryptedHDSeed( const std::vector& vchCryptedSecret) { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) { return false; } @@ -335,7 +335,7 @@ bool CCryptoKeyStore::SetCryptedHDSeed( bool CCryptoKeyStore::HaveHDSeed() const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) return CBasicKeyStore::HaveHDSeed(); @@ -344,7 +344,7 @@ bool CCryptoKeyStore::HaveHDSeed() const bool CCryptoKeyStore::GetHDSeed(HDSeed& seedOut) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) return CBasicKeyStore::GetHDSeed(seedOut); @@ -426,7 +426,7 @@ bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) co bool CCryptoKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk) { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) return CBasicKeyStore::AddSproutSpendingKey(sk); @@ -452,7 +452,7 @@ bool CCryptoKeyStore::AddSaplingSpendingKey( const libzcash::SaplingPaymentAddress &defaultAddr) { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) { return CBasicKeyStore::AddSaplingSpendingKey(sk, defaultAddr); } @@ -483,7 +483,7 @@ bool CCryptoKeyStore::AddCryptedSproutSpendingKey( const std::vector &vchCryptedSecret) { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!SetCrypted()) return false; @@ -499,7 +499,7 @@ bool CCryptoKeyStore::AddCryptedSaplingSpendingKey( const libzcash::SaplingPaymentAddress &defaultAddr) { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!SetCrypted()) { return false; } @@ -517,7 +517,7 @@ bool CCryptoKeyStore::AddCryptedSaplingSpendingKey( bool CCryptoKeyStore::GetSproutSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey &skOut) const { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) return CBasicKeyStore::GetSproutSpendingKey(address, skOut); @@ -534,7 +534,7 @@ bool CCryptoKeyStore::GetSproutSpendingKey(const libzcash::SproutPaymentAddress bool CCryptoKeyStore::GetSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk, libzcash::SaplingExtendedSpendingKey &skOut) const { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) return CBasicKeyStore::GetSaplingSpendingKey(fvk, skOut); @@ -551,7 +551,7 @@ bool CCryptoKeyStore::GetSaplingSpendingKey(const libzcash::SaplingFullViewingKe bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) { { - LOCK2(cs_KeyStore, cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!mapCryptedKeys.empty() || IsCrypted()) return false; diff --git a/src/wallet/crypter.h b/src/wallet/crypter.h index a5c11a527..2e4fd0795 100644 --- a/src/wallet/crypter.h +++ b/src/wallet/crypter.h @@ -216,7 +216,7 @@ public: bool HaveSproutSpendingKey(const libzcash::SproutPaymentAddress &address) const { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) return CBasicKeyStore::HaveSproutSpendingKey(address); return mapCryptedSproutSpendingKeys.count(address) > 0; @@ -250,7 +250,7 @@ public: bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const { { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); if (!IsCrypted()) return CBasicKeyStore::HaveSaplingSpendingKey(fvk); for (auto entry : mapCryptedSaplingSpendingKeys) { diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index f79ce4b1d..718d389ca 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1802,7 +1802,7 @@ boost::optional CWallet::GetSproutNoteNullifier(const JSDescription &js */ mapSproutNoteData_t CWallet::FindMySproutNotes(const CTransaction &tx) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); uint256 hash = tx.GetHash(); mapSproutNoteData_t noteData; @@ -1850,7 +1850,7 @@ mapSproutNoteData_t CWallet::FindMySproutNotes(const CTransaction &tx) const */ std::pair CWallet::FindMySaplingNotes(const CTransaction &tx) const { - LOCK(cs_SpendingKeyStore); + LOCK(cs_KeyStore); uint256 hash = tx.GetHash(); mapSaplingNoteData_t noteData;