From 5cb3da04b8882ca975b4e3d6c089c64bbaf67d0d Mon Sep 17 00:00:00 2001 From: Marko Bencun Date: Sun, 23 Jul 2017 23:32:57 +0200 Subject: [PATCH 1/2] 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. --- src/keystore.h | 18 +++++++----------- src/wallet/crypter.h | 25 +++++++++++-------------- src/wallet/wallet.cpp | 5 +---- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/keystore.h b/src/keystore.h index 965ae0c79ad86..059a657dc2039 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -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 &setAddress) const =0; + virtual std::set 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 @@ -71,18 +71,14 @@ class CBasicKeyStore : public CKeyStore } return result; } - void GetKeys(std::set &setAddress) const override + std::set 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 set_address; + for (const auto& mi : mapKeys) { + set_address.insert(mi.first); } + return set_address; } bool GetKey(const CKeyID &address, CKey &keyOut) const override { diff --git a/src/wallet/crypter.h b/src/wallet/crypter.h index 1dc44e424f631..d6f3e27721c60 100644 --- a/src/wallet/crypter.h +++ b/src/wallet/crypter.h @@ -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]). @@ -162,28 +162,25 @@ 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 &setAddress) const override + std::set GetKeys() const override { - if (!IsCrypted()) - { - CBasicKeyStore::GetKeys(setAddress); - return; + if (!IsCrypted()) { + return CBasicKeyStore::GetKeys(); } - setAddress.clear(); - CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin(); - while (mi != mapCryptedKeys.end()) - { - setAddress.insert((*mi).first); - mi++; + std::set set_address; + for (const auto& mi : mapCryptedKeys) { + set_address.insert(mi.first); } + return set_address; } /** diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 223790aa40c57..fcb679e75446c 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3660,13 +3660,10 @@ void CWallet::GetKeyBirthTimes(std::map &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 mapKeyFirstBlock; - std::set 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()) From fe09b0197c20dc3c0a614c1a94dac708ef206743 Mon Sep 17 00:00:00 2001 From: Marko Bencun Date: Sun, 23 Jul 2017 23:37:56 +0200 Subject: [PATCH 2/2] add missing lock to crypter GetKeys() Issue: #10905 --- src/wallet/crypter.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/crypter.h b/src/wallet/crypter.h index d6f3e27721c60..0295004cb382e 100644 --- a/src/wallet/crypter.h +++ b/src/wallet/crypter.h @@ -173,6 +173,7 @@ class CCryptoKeyStore : public CBasicKeyStore bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override; std::set GetKeys() const override { + LOCK(cs_KeyStore); if (!IsCrypted()) { return CBasicKeyStore::GetKeys(); }