@@ -3077,6 +3077,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
3077
3077
LOCK (cs_wallet);
3078
3078
setInternalKeyPool.clear ();
3079
3079
setExternalKeyPool.clear ();
3080
+ m_pool_key_to_index.clear ();
3080
3081
// Note: can't top-up keypool here, because wallet is locked.
3081
3082
// User will be prompted to unlock wallet the next operation
3082
3083
// that requires a new key.
@@ -3106,6 +3107,7 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
3106
3107
{
3107
3108
setInternalKeyPool.clear ();
3108
3109
setExternalKeyPool.clear ();
3110
+ m_pool_key_to_index.clear ();
3109
3111
// Note: can't top-up keypool here, because wallet is locked.
3110
3112
// User will be prompted to unlock wallet the next operation
3111
3113
// that requires a new key.
@@ -3132,6 +3134,7 @@ DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
3132
3134
LOCK (cs_wallet);
3133
3135
setInternalKeyPool.clear ();
3134
3136
setExternalKeyPool.clear ();
3137
+ m_pool_key_to_index.clear ();
3135
3138
// Note: can't top-up keypool here, because wallet is locked.
3136
3139
// User will be prompted to unlock wallet the next operation
3137
3140
// that requires a new key.
@@ -3226,6 +3229,8 @@ bool CWallet::NewKeyPool()
3226
3229
}
3227
3230
setExternalKeyPool.clear ();
3228
3231
3232
+ m_pool_key_to_index.clear ();
3233
+
3229
3234
if (!TopUpKeyPool ()) {
3230
3235
return false ;
3231
3236
}
@@ -3242,12 +3247,14 @@ size_t CWallet::KeypoolCountExternalKeys()
3242
3247
3243
3248
void CWallet::LoadKeyPool (int64_t nIndex, const CKeyPool &keypool)
3244
3249
{
3250
+ AssertLockHeld (cs_wallet);
3245
3251
if (keypool.fInternal ) {
3246
3252
setInternalKeyPool.insert (nIndex);
3247
3253
} else {
3248
3254
setExternalKeyPool.insert (nIndex);
3249
3255
}
3250
3256
m_max_keypool_index = std::max (m_max_keypool_index, nIndex);
3257
+ m_pool_key_to_index[keypool.vchPubKey .GetID ()] = nIndex;
3251
3258
3252
3259
// If no metadata exists yet, create a default with the pool key's
3253
3260
// creation time. Note that this may be overwritten by actually
@@ -3293,7 +3300,8 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
3293
3300
assert (m_max_keypool_index < std::numeric_limits<int64_t >::max ()); // How in the hell did you use so many keys?
3294
3301
int64_t index = ++m_max_keypool_index;
3295
3302
3296
- if (!walletdb.WritePool (index, CKeyPool (GenerateNewKey (walletdb, internal), internal))) {
3303
+ CPubKey pubkey (GenerateNewKey (walletdb, internal));
3304
+ if (!walletdb.WritePool (index, CKeyPool (pubkey, internal))) {
3297
3305
throw std::runtime_error (std::string (__func__) + " : writing generated key failed" );
3298
3306
}
3299
3307
@@ -3302,6 +3310,7 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
3302
3310
} else {
3303
3311
setExternalKeyPool.insert (index);
3304
3312
}
3313
+ m_pool_key_to_index[pubkey.GetID ()] = index;
3305
3314
}
3306
3315
if (missingInternal + missingExternal > 0 ) {
3307
3316
LogPrintf (" keypool added %d keys (%d internal), size=%u (%u internal)\n " , missingInternal + missingExternal, missingInternal, setInternalKeyPool.size () + setExternalKeyPool.size (), setInternalKeyPool.size ());
@@ -3343,6 +3352,7 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRe
3343
3352
}
3344
3353
3345
3354
assert (keypool.vchPubKey .IsValid ());
3355
+ m_pool_key_to_index.erase (keypool.vchPubKey .GetID ());
3346
3356
LogPrintf (" keypool reserve %d\n " , nIndex);
3347
3357
}
3348
3358
}
@@ -3355,7 +3365,7 @@ void CWallet::KeepKey(int64_t nIndex)
3355
3365
LogPrintf (" keypool keep %d\n " , nIndex);
3356
3366
}
3357
3367
3358
- void CWallet::ReturnKey (int64_t nIndex, bool fInternal )
3368
+ void CWallet::ReturnKey (int64_t nIndex, bool fInternal , const CPubKey& pubkey )
3359
3369
{
3360
3370
// Return to key pool
3361
3371
{
@@ -3365,6 +3375,7 @@ void CWallet::ReturnKey(int64_t nIndex, bool fInternal)
3365
3375
} else {
3366
3376
setExternalKeyPool.insert (nIndex);
3367
3377
}
3378
+ m_pool_key_to_index[pubkey.GetID ()] = nIndex;
3368
3379
}
3369
3380
LogPrintf (" keypool return %d\n " , nIndex);
3370
3381
}
@@ -3594,41 +3605,12 @@ void CReserveKey::KeepKey()
3594
3605
void CReserveKey::ReturnKey ()
3595
3606
{
3596
3607
if (nIndex != -1 ) {
3597
- pwallet->ReturnKey (nIndex, fInternal );
3608
+ pwallet->ReturnKey (nIndex, fInternal , vchPubKey );
3598
3609
}
3599
3610
nIndex = -1 ;
3600
3611
vchPubKey = CPubKey ();
3601
3612
}
3602
3613
3603
- static void LoadReserveKeysToSet (std::set<CKeyID>& setAddress, const std::set<int64_t >& setKeyPool, CWalletDB& walletdb) {
3604
- for (const int64_t & id : setKeyPool)
3605
- {
3606
- CKeyPool keypool;
3607
- if (!walletdb.ReadPool (id, keypool))
3608
- throw std::runtime_error (std::string (__func__) + " : read failed" );
3609
- assert (keypool.vchPubKey .IsValid ());
3610
- CKeyID keyID = keypool.vchPubKey .GetID ();
3611
- setAddress.insert (keyID);
3612
- }
3613
- }
3614
-
3615
- void CWallet::GetAllReserveKeys (std::set<CKeyID>& setAddress) const
3616
- {
3617
- setAddress.clear ();
3618
-
3619
- CWalletDB walletdb (*dbw);
3620
-
3621
- LOCK2 (cs_main, cs_wallet);
3622
- LoadReserveKeysToSet (setAddress, setInternalKeyPool, walletdb);
3623
- LoadReserveKeysToSet (setAddress, setExternalKeyPool, walletdb);
3624
-
3625
- for (const CKeyID& keyID : setAddress) {
3626
- if (!HaveKey (keyID)) {
3627
- throw std::runtime_error (std::string (__func__) + " : unknown key in key pool" );
3628
- }
3629
- }
3630
- }
3631
-
3632
3614
void CWallet::GetScriptForMining (std::shared_ptr<CReserveScript> &script)
3633
3615
{
3634
3616
std::shared_ptr<CReserveKey> rKey = std::make_shared<CReserveKey>(this );
0 commit comments