Skip to content

Commit 5cb3da0

Browse files
author
Marko Bencun
committed
keystore GetKeys(): return result instead of writing to reference
Issue: #10905 By returning the result, a few useless lines can be removed. Return-value-optimization means there should be no copy.
1 parent 0c173a1 commit 5cb3da0

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-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: 11 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,25 @@ 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+
if (!IsCrypted()) {
177+
return CBasicKeyStore::GetKeys();
179178
}
180-
setAddress.clear();
181-
CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
182-
while (mi != mapCryptedKeys.end())
183-
{
184-
setAddress.insert((*mi).first);
185-
mi++;
179+
std::set<CKeyID> set_address;
180+
for (const auto& mi : mapCryptedKeys) {
181+
set_address.insert(mi.first);
186182
}
183+
return set_address;
187184
}
188185

189186
/**

src/wallet/wallet.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3660,13 +3660,10 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
36603660
// map in which we'll infer heights of other keys
36613661
CBlockIndex *pindexMax = chainActive[std::max(0, chainActive.Height() - 144)]; // the tip can be reorganized; use a 144-block safety margin
36623662
std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
3663-
std::set<CKeyID> setKeys;
3664-
GetKeys(setKeys);
3665-
for (const CKeyID &keyid : setKeys) {
3663+
for (const CKeyID &keyid : GetKeys()) {
36663664
if (mapKeyBirth.count(keyid) == 0)
36673665
mapKeyFirstBlock[keyid] = pindexMax;
36683666
}
3669-
setKeys.clear();
36703667

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

0 commit comments

Comments
 (0)