Skip to content

Commit

Permalink
Merge #10916: add missing lock to crypter GetKeys()
Browse files Browse the repository at this point in the history
fe09b01 add missing lock to crypter GetKeys() (Marko Bencun)
5cb3da0 keystore GetKeys(): return result instead of writing to reference (Marko Bencun)

Pull request description:

  Issue: #10905

  First commit makes GetKeys() return the result instead of writing to a reference to remove some useless lines.

Tree-SHA512: bb51255b5a6cf5488c3d5dee89f539d41f0717f018441d120047f877e0a705a133fb3b7a97d1cf8f73b5d2ed93dd2dbdfcd6f394e40105af2a12e01d397cb402
  • Loading branch information
laanwj committed Sep 7, 2017
2 parents 2f0d3e6 + fe09b01 commit e6ab88a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
18 changes: 7 additions & 11 deletions src/keystore.h
Expand Up @@ -30,7 +30,7 @@ class CKeyStore
//! Check whether a key corresponding to a given address is present in the store.
virtual bool HaveKey(const CKeyID &address) const =0;
virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
virtual std::set<CKeyID> GetKeys() const =0;
virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const =0;

//! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
Expand Down Expand Up @@ -71,18 +71,14 @@ class CBasicKeyStore : public CKeyStore
}
return result;
}
void GetKeys(std::set<CKeyID> &setAddress) const override
std::set<CKeyID> GetKeys() const override
{
setAddress.clear();
{
LOCK(cs_KeyStore);
KeyMap::const_iterator mi = mapKeys.begin();
while (mi != mapKeys.end())
{
setAddress.insert((*mi).first);
mi++;
}
LOCK(cs_KeyStore);
std::set<CKeyID> set_address;
for (const auto& mi : mapKeys) {
set_address.insert(mi.first);
}
return set_address;
}
bool GetKey(const CKeyID &address, CKey &keyOut) const override
{
Expand Down
26 changes: 12 additions & 14 deletions src/wallet/crypter.h
Expand Up @@ -16,13 +16,13 @@ const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
/**
* Private key encryption is done based on a CMasterKey,
* which holds a salt and random encryption key.
*
*
* CMasterKeys are encrypted using AES-256-CBC using a key
* derived using derivation method nDerivationMethod
* (0 == EVP_sha512()) and derivation iterations nDeriveIterations.
* vchOtherDerivationParameters is provided for alternative algorithms
* which may require more parameters (such as scrypt).
*
*
* Wallet Private Keys are then encrypted using AES-256-CBC
* with the double-sha256 of the public key as the IV, and the
* master key's key as the encryption key (see keystore.[ch]).
Expand Down Expand Up @@ -162,28 +162,26 @@ class CCryptoKeyStore : public CBasicKeyStore
{
{
LOCK(cs_KeyStore);
if (!IsCrypted())
if (!IsCrypted()) {
return CBasicKeyStore::HaveKey(address);
}
return mapCryptedKeys.count(address) > 0;
}
return false;
}
bool GetKey(const CKeyID &address, CKey& keyOut) const override;
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
void GetKeys(std::set<CKeyID> &setAddress) const override
std::set<CKeyID> GetKeys() const override
{
if (!IsCrypted())
{
CBasicKeyStore::GetKeys(setAddress);
return;
LOCK(cs_KeyStore);
if (!IsCrypted()) {
return CBasicKeyStore::GetKeys();
}
setAddress.clear();
CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
while (mi != mapCryptedKeys.end())
{
setAddress.insert((*mi).first);
mi++;
std::set<CKeyID> set_address;
for (const auto& mi : mapCryptedKeys) {
set_address.insert(mi.first);
}
return set_address;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/wallet/wallet.cpp
Expand Up @@ -3600,13 +3600,10 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
// map in which we'll infer heights of other keys
CBlockIndex *pindexMax = chainActive[std::max(0, chainActive.Height() - 144)]; // the tip can be reorganized; use a 144-block safety margin
std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
std::set<CKeyID> setKeys;
GetKeys(setKeys);
for (const CKeyID &keyid : setKeys) {
for (const CKeyID &keyid : GetKeys()) {
if (mapKeyBirth.count(keyid) == 0)
mapKeyFirstBlock[keyid] = pindexMax;
}
setKeys.clear();

// if there are no such keys, we're done
if (mapKeyFirstBlock.empty())
Expand Down

0 comments on commit e6ab88a

Please sign in to comment.