Skip to content

Commit 3a53f19

Browse files
committed
Pushdown walletdb object through GenerateNewKey/DeriveNewChildKey.
This is needed but not sufficient for batching the wallet flushing when topping up the keypool.
1 parent 5cfdda2 commit 3a53f19

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/wallet/wallet.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
8787
return &(it->second);
8888
}
8989

90-
CPubKey CWallet::GenerateNewKey(bool internal)
90+
CPubKey CWallet::GenerateNewKey(CWalletDB &walletdb, bool internal)
9191
{
9292
AssertLockHeld(cs_wallet); // mapKeyMetadata
9393
bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
@@ -100,7 +100,7 @@ CPubKey CWallet::GenerateNewKey(bool internal)
100100

101101
// use HD key derivation if HD was enabled during wallet creation
102102
if (IsHDEnabled()) {
103-
DeriveNewChildKey(metadata, secret, (CanSupportFeature(FEATURE_HD_SPLIT) ? internal : false));
103+
DeriveNewChildKey(walletdb, metadata, secret, (CanSupportFeature(FEATURE_HD_SPLIT) ? internal : false));
104104
} else {
105105
secret.MakeNewKey(fCompressed);
106106
}
@@ -120,7 +120,7 @@ CPubKey CWallet::GenerateNewKey(bool internal)
120120
return pubkey;
121121
}
122122

123-
void CWallet::DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret, bool internal)
123+
void CWallet::DeriveNewChildKey(CWalletDB &walletdb, CKeyMetadata& metadata, CKey& secret, bool internal)
124124
{
125125
// for now we use a fixed keypath scheme of m/0'/0'/k
126126
CKey key; //master key seed (256bit)
@@ -162,7 +162,7 @@ void CWallet::DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret, bool inter
162162
secret = childKey.key;
163163
metadata.hdMasterKeyID = hdChain.masterKeyID;
164164
// update the chain model in the database
165-
if (!CWalletDB(*dbw).WriteHDChain(hdChain))
165+
if (!walletdb.WriteHDChain(hdChain))
166166
throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed");
167167
}
168168

@@ -3183,8 +3183,9 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
31833183
nEnd = std::max(nEnd, *(setExternalKeyPool.rbegin()) + 1);
31843184
}
31853185

3186-
if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey(internal), internal)))
3186+
if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey(walletdb, internal), internal))) {
31873187
throw std::runtime_error(std::string(__func__) + ": writing generated key failed");
3188+
}
31883189

31893190
if (internal) {
31903191
setInternalKeyPool.insert(nEnd);
@@ -3266,7 +3267,8 @@ bool CWallet::GetKeyFromPool(CPubKey& result, bool internal)
32663267
if (nIndex == -1)
32673268
{
32683269
if (IsLocked()) return false;
3269-
result = GenerateNewKey(internal);
3270+
CWalletDB walletdb(*dbw);
3271+
result = GenerateNewKey(walletdb, internal);
32703272
return true;
32713273
}
32723274
KeepKey(nIndex);

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
697697
CHDChain hdChain;
698698

699699
/* HD derive new child key (on internal or external chain) */
700-
void DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret, bool internal = false);
700+
void DeriveNewChildKey(CWalletDB &walletdb, CKeyMetadata& metadata, CKey& secret, bool internal = false);
701701

702702
std::set<int64_t> setInternalKeyPool;
703703
std::set<int64_t> setExternalKeyPool;
@@ -866,7 +866,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
866866
* keystore implementation
867867
* Generate a new key
868868
*/
869-
CPubKey GenerateNewKey(bool internal = false);
869+
CPubKey GenerateNewKey(CWalletDB& walletdb, bool internal = false);
870870
//! Adds a key to the store, and saves it to disk.
871871
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
872872
//! Adds a key to the store, without saving it to disk (used by LoadWallet)

0 commit comments

Comments
 (0)