aboutsummaryrefslogtreecommitdiff
path: root/src/keystore.h
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2011-08-26 14:37:23 -0400
committerGavin Andresen <gavinandresen@gmail.com>2011-08-31 12:55:16 -0400
commit6cc4a62c0e696dcb9d90ba0504f688e4f644a10f (patch)
tree5b365769be7c8be7caf6c31c4bdb1b4798be9ef4 /src/keystore.h
parentb0243da77c6ee8d8ca59b4423f333a179bff02cf (diff)
Fix rpc-hanging deadlocks
Collapsed multiple wallet mutexes to a single cs_wallet, to avoid deadlocks with wallet methods that acquired locks in different order. Also change master RPC call handler to acquire cs_main and cs_wallet locks before executing RPC calls; requiring each RPC call to acquire the right set of locks in the right order was too error-prone.
Diffstat (limited to 'src/keystore.h')
-rw-r--r--src/keystore.h44
1 files changed, 27 insertions, 17 deletions
diff --git a/src/keystore.h b/src/keystore.h
index 0324cc6e1b..d1e4985d5d 100644
--- a/src/keystore.h
+++ b/src/keystore.h
@@ -9,9 +9,10 @@
class CKeyStore
{
-public:
+protected:
mutable CCriticalSection cs_KeyStore;
+public:
virtual bool AddKey(const CKey& key) =0;
virtual bool HaveKey(const CBitcoinAddress &address) const =0;
virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
@@ -30,15 +31,21 @@ public:
bool AddKey(const CKey& key);
bool HaveKey(const CBitcoinAddress &address) const
{
- return (mapKeys.count(address) > 0);
+ bool result;
+ CRITICAL_BLOCK(cs_KeyStore)
+ result = (mapKeys.count(address) > 0);
+ return result;
}
bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
{
- KeyMap::const_iterator mi = mapKeys.find(address);
- if (mi != mapKeys.end())
+ CRITICAL_BLOCK(cs_KeyStore)
{
- keyOut.SetSecret((*mi).second);
- return true;
+ KeyMap::const_iterator mi = mapKeys.find(address);
+ if (mi != mapKeys.end())
+ {
+ keyOut.SetSecret((*mi).second);
+ return true;
+ }
}
return false;
}
@@ -74,8 +81,6 @@ protected:
bool Unlock(const CKeyingMaterial& vMasterKeyIn);
public:
- mutable CCriticalSection cs_vMasterKey; //No guarantees master key wont get locked before you can use it, so lock this first
-
CCryptoKeyStore() : fUseCrypto(false)
{
}
@@ -89,18 +94,20 @@ public:
{
if (!IsCrypted())
return false;
- return vMasterKey.empty();
+ bool result;
+ CRITICAL_BLOCK(cs_KeyStore)
+ result = vMasterKey.empty();
+ return result;
}
bool Lock()
{
- CRITICAL_BLOCK(cs_vMasterKey)
- {
- if (!SetCrypted())
- return false;
+ if (!SetCrypted())
+ return false;
+ CRITICAL_BLOCK(cs_KeyStore)
vMasterKey.clear();
- }
+
return true;
}
@@ -109,9 +116,12 @@ public:
bool AddKey(const CKey& key);
bool HaveKey(const CBitcoinAddress &address) const
{
- if (!IsCrypted())
- return CBasicKeyStore::HaveKey(address);
- return mapCryptedKeys.count(address) > 0;
+ CRITICAL_BLOCK(cs_KeyStore)
+ {
+ if (!IsCrypted())
+ return CBasicKeyStore::HaveKey(address);
+ return mapCryptedKeys.count(address) > 0;
+ }
}
bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;