Skip to content

Commit 386a994

Browse files
committed
Key pool: Change ReturnDestination interface to take address instead of key
In order for ScriptPubKeyMan to be generic and work with future ScriptPubKeyMans, ScriptPubKeyMan::ReturnDestination is changed to take a CTxDestination instead of a CPubKey. Since LegacyScriptPubKeyMan still deals with keys internally, a new map m_reserved_key_to_index is added in order to track the keypool indexes that have been reserved. The CPubKey argument of KeepDestination is also removed so that it is more generic. Instead of taking a CPubKey or a CTxDestination, we just use the nIndex given to find the pubkey.
1 parent ba41aa4 commit 386a994

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,16 +1087,20 @@ void LegacyScriptPubKeyMan::AddKeypoolPubkeyWithDB(const CPubKey& pubkey, const
10871087
m_pool_key_to_index[pubkey.GetID()] = index;
10881088
}
10891089

1090-
void LegacyScriptPubKeyMan::KeepDestination(int64_t nIndex, const OutputType& type, const CPubKey& pubkey)
1090+
void LegacyScriptPubKeyMan::KeepDestination(int64_t nIndex, const OutputType& type)
10911091
{
10921092
// Remove from key pool
10931093
WalletBatch batch(m_storage.GetDatabase());
10941094
batch.ErasePool(nIndex);
1095+
CPubKey pubkey;
1096+
bool have_pk = GetPubKey(m_index_to_reserved_key.at(nIndex), pubkey);
1097+
assert(have_pk);
10951098
LearnRelatedScripts(pubkey, type);
1099+
m_index_to_reserved_key.erase(nIndex);
10961100
WalletLogPrintf("keypool keep %d\n", nIndex);
10971101
}
10981102

1099-
void LegacyScriptPubKeyMan::ReturnDestination(int64_t nIndex, bool fInternal, const CPubKey& pubkey)
1103+
void LegacyScriptPubKeyMan::ReturnDestination(int64_t nIndex, bool fInternal, const CTxDestination&)
11001104
{
11011105
// Return to key pool
11021106
{
@@ -1108,7 +1112,9 @@ void LegacyScriptPubKeyMan::ReturnDestination(int64_t nIndex, bool fInternal, co
11081112
} else {
11091113
setExternalKeyPool.insert(nIndex);
11101114
}
1111-
m_pool_key_to_index[pubkey.GetID()] = nIndex;
1115+
CKeyID& pubkey_id = m_index_to_reserved_key.at(nIndex);
1116+
m_pool_key_to_index[pubkey_id] = nIndex;
1117+
m_index_to_reserved_key.erase(nIndex);
11121118
NotifyCanGetAddressesChanged();
11131119
}
11141120
WalletLogPrintf("keypool return %d\n", nIndex);
@@ -1130,7 +1136,7 @@ bool LegacyScriptPubKeyMan::GetKeyFromPool(CPubKey& result, const OutputType typ
11301136
result = GenerateNewKey(batch, internal);
11311137
return true;
11321138
}
1133-
KeepDestination(nIndex, type, keypool.vchPubKey);
1139+
KeepDestination(nIndex, type);
11341140
result = keypool.vchPubKey;
11351141
}
11361142
return true;
@@ -1175,6 +1181,8 @@ bool LegacyScriptPubKeyMan::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& key
11751181
throw std::runtime_error(std::string(__func__) + ": keypool entry invalid");
11761182
}
11771183

1184+
assert(m_index_to_reserved_key.count(nIndex) == 0);
1185+
m_index_to_reserved_key[nIndex] = keypool.vchPubKey.GetID();
11781186
m_pool_key_to_index.erase(keypool.vchPubKey.GetID());
11791187
WalletLogPrintf("keypool reserve %d\n", nIndex);
11801188
}

src/wallet/scriptpubkeyman.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ class ScriptPubKeyMan
151151
virtual isminetype IsMine(const CScript& script) const { return ISMINE_NO; }
152152

153153
virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool) { return false; }
154-
virtual void KeepDestination(int64_t index, const OutputType& type, const CPubKey& pubkey) {}
155-
virtual void ReturnDestination(int64_t index, bool internal, const CPubKey& pubkey) {}
154+
virtual void KeepDestination(int64_t index, const OutputType& type) {}
155+
virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) {}
156156

157157
virtual bool TopUp(unsigned int size = 0) { return false; }
158158

@@ -246,6 +246,8 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
246246
std::set<int64_t> set_pre_split_keypool GUARDED_BY(cs_wallet);
247247
int64_t m_max_keypool_index GUARDED_BY(cs_wallet) = 0;
248248
std::map<CKeyID, int64_t> m_pool_key_to_index;
249+
// Tracks keypool indexes to CKeyIDs of keys that have been taken out of the keypool but may be returned to it
250+
std::map<int64_t, CKeyID> m_index_to_reserved_key;
249251

250252
//! Fetches a key from the keypool
251253
bool GetKeyFromPool(CPubKey &key, const OutputType type, bool internal = false);
@@ -274,8 +276,8 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
274276
bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
275277

276278
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool) override;
277-
void KeepDestination(int64_t index, const OutputType& type, const CPubKey& pubkey) override;
278-
void ReturnDestination(int64_t index, bool internal, const CPubKey& pubkey) override;
279+
void KeepDestination(int64_t index, const OutputType& type) override;
280+
void ReturnDestination(int64_t index, bool internal, const CTxDestination&) override;
279281

280282
bool TopUp(unsigned int size = 0) override;
281283

src/wallet/wallet.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,31 +3305,27 @@ bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool inter
33053305
if (!m_spk_man->GetReservedDestination(type, internal, address, nIndex, keypool)) {
33063306
return false;
33073307
}
3308-
vchPubKey = keypool.vchPubKey;
33093308
fInternal = keypool.fInternal;
33103309
}
3311-
assert(vchPubKey.IsValid());
33123310
dest = address;
33133311
return true;
33143312
}
33153313

33163314
void ReserveDestination::KeepDestination()
33173315
{
33183316
if (nIndex != -1) {
3319-
m_spk_man->KeepDestination(nIndex, type, vchPubKey);
3317+
m_spk_man->KeepDestination(nIndex, type);
33203318
}
33213319
nIndex = -1;
3322-
vchPubKey = CPubKey();
33233320
address = CNoDestination();
33243321
}
33253322

33263323
void ReserveDestination::ReturnDestination()
33273324
{
33283325
if (nIndex != -1) {
3329-
m_spk_man->ReturnDestination(nIndex, fInternal, vchPubKey);
3326+
m_spk_man->ReturnDestination(nIndex, fInternal, address);
33303327
}
33313328
nIndex = -1;
3332-
vchPubKey = CPubKey();
33333329
address = CNoDestination();
33343330
}
33353331

src/wallet/wallet.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ class ReserveDestination
146146
OutputType const type;
147147
//! The index of the address's key in the keypool
148148
int64_t nIndex{-1};
149-
//! The public key for the address
150-
CPubKey vchPubKey;
151149
//! The destination
152150
CTxDestination address;
153151
//! Whether this is from the internal (change output) keypool

0 commit comments

Comments
 (0)