Skip to content

Commit e6ab88a

Browse files
committed
Merge #10916: add missing lock to crypter GetKeys()
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
2 parents 2f0d3e6 + fe09b01 commit e6ab88a

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

src/keystore.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CKeyStore
3030
//! Check whether a key corresponding to a given address is present in the store.
3131
virtual bool HaveKey(const CKeyID &address) const =0;
3232
virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
33-
virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
33+
virtual std::set<CKeyID> GetKeys() const =0;
3434
virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const =0;
3535

3636
//! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
@@ -71,18 +71,14 @@ class CBasicKeyStore : public CKeyStore
7171
}
7272
return result;
7373
}
74-
void GetKeys(std::set<CKeyID> &setAddress) const override
74+
std::set<CKeyID> GetKeys() const override
7575
{
76-
setAddress.clear();
77-
{
78-
LOCK(cs_KeyStore);
79-
KeyMap::const_iterator mi = mapKeys.begin();
80-
while (mi != mapKeys.end())
81-
{
82-
setAddress.insert((*mi).first);
83-
mi++;
84-
}
76+
LOCK(cs_KeyStore);
77+
std::set<CKeyID> set_address;
78+
for (const auto& mi : mapKeys) {
79+
set_address.insert(mi.first);
8580
}
81+
return set_address;
8682
}
8783
bool GetKey(const CKeyID &address, CKey &keyOut) const override
8884
{

src/wallet/crypter.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
1616
/**
1717
* Private key encryption is done based on a CMasterKey,
1818
* which holds a salt and random encryption key.
19-
*
19+
*
2020
* CMasterKeys are encrypted using AES-256-CBC using a key
2121
* derived using derivation method nDerivationMethod
2222
* (0 == EVP_sha512()) and derivation iterations nDeriveIterations.
2323
* vchOtherDerivationParameters is provided for alternative algorithms
2424
* which may require more parameters (such as scrypt).
25-
*
25+
*
2626
* Wallet Private Keys are then encrypted using AES-256-CBC
2727
* with the double-sha256 of the public key as the IV, and the
2828
* master key's key as the encryption key (see keystore.[ch]).
@@ -162,28 +162,26 @@ class CCryptoKeyStore : public CBasicKeyStore
162162
{
163163
{
164164
LOCK(cs_KeyStore);
165-
if (!IsCrypted())
165+
if (!IsCrypted()) {
166166
return CBasicKeyStore::HaveKey(address);
167+
}
167168
return mapCryptedKeys.count(address) > 0;
168169
}
169170
return false;
170171
}
171172
bool GetKey(const CKeyID &address, CKey& keyOut) const override;
172173
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
173-
void GetKeys(std::set<CKeyID> &setAddress) const override
174+
std::set<CKeyID> GetKeys() const override
174175
{
175-
if (!IsCrypted())
176-
{
177-
CBasicKeyStore::GetKeys(setAddress);
178-
return;
176+
LOCK(cs_KeyStore);
177+
if (!IsCrypted()) {
178+
return CBasicKeyStore::GetKeys();
179179
}
180-
setAddress.clear();
181-
CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
182-
while (mi != mapCryptedKeys.end())
183-
{
184-
setAddress.insert((*mi).first);
185-
mi++;
180+
std::set<CKeyID> set_address;
181+
for (const auto& mi : mapCryptedKeys) {
182+
set_address.insert(mi.first);
186183
}
184+
return set_address;
187185
}
188186

189187
/**

src/wallet/wallet.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3600,13 +3600,10 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
36003600
// map in which we'll infer heights of other keys
36013601
CBlockIndex *pindexMax = chainActive[std::max(0, chainActive.Height() - 144)]; // the tip can be reorganized; use a 144-block safety margin
36023602
std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
3603-
std::set<CKeyID> setKeys;
3604-
GetKeys(setKeys);
3605-
for (const CKeyID &keyid : setKeys) {
3603+
for (const CKeyID &keyid : GetKeys()) {
36063604
if (mapKeyBirth.count(keyid) == 0)
36073605
mapKeyFirstBlock[keyid] = pindexMax;
36083606
}
3609-
setKeys.clear();
36103607

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

0 commit comments

Comments
 (0)