@@ -3427,7 +3427,7 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
3427
3427
return true ;
3428
3428
}
3429
3429
3430
- void CWallet::ReserveKeyFromKeyPool (int64_t & nIndex, CKeyPool& keypool, bool fRequestedInternal )
3430
+ bool CWallet::ReserveKeyFromKeyPool (int64_t & nIndex, CKeyPool& keypool, bool fRequestedInternal )
3431
3431
{
3432
3432
nIndex = -1 ;
3433
3433
keypool.vchPubKey = CPubKey ();
@@ -3438,11 +3438,13 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRe
3438
3438
TopUpKeyPool ();
3439
3439
3440
3440
bool fReturningInternal = IsHDEnabled () && CanSupportFeature (FEATURE_HD_SPLIT) && fRequestedInternal ;
3441
- std::set<int64_t >& setKeyPool = set_pre_split_keypool.empty () ? (fReturningInternal ? setInternalKeyPool : setExternalKeyPool) : set_pre_split_keypool;
3441
+ bool use_split_keypool = set_pre_split_keypool.empty ();
3442
+ std::set<int64_t >& setKeyPool = use_split_keypool ? (fReturningInternal ? setInternalKeyPool : setExternalKeyPool) : set_pre_split_keypool;
3442
3443
3443
3444
// Get the oldest key
3444
- if (setKeyPool.empty ())
3445
- return ;
3445
+ if (setKeyPool.empty ()) {
3446
+ return false ;
3447
+ }
3446
3448
3447
3449
WalletBatch batch (*database);
3448
3450
@@ -3456,14 +3458,17 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRe
3456
3458
throw std::runtime_error (std::string (__func__) + " : unknown key in key pool" );
3457
3459
}
3458
3460
// If the key was pre-split keypool, we don't care about what type it is
3459
- if (set_pre_split_keypool. size () == 0 && keypool.fInternal != fReturningInternal ) {
3461
+ if (use_split_keypool && keypool.fInternal != fReturningInternal ) {
3460
3462
throw std::runtime_error (std::string (__func__) + " : keypool entry misclassified" );
3461
3463
}
3464
+ if (!keypool.vchPubKey .IsValid ()) {
3465
+ throw std::runtime_error (std::string (__func__) + " : keypool entry invalid" );
3466
+ }
3462
3467
3463
- assert (keypool.vchPubKey .IsValid ());
3464
3468
m_pool_key_to_index.erase (keypool.vchPubKey .GetID ());
3465
3469
LogPrintf (" keypool reserve %d\n " , nIndex);
3466
3470
}
3471
+ return true ;
3467
3472
}
3468
3473
3469
3474
void CWallet::KeepKey (int64_t nIndex)
@@ -3496,10 +3501,8 @@ bool CWallet::GetKeyFromPool(CPubKey& result, bool internal)
3496
3501
CKeyPool keypool;
3497
3502
{
3498
3503
LOCK (cs_wallet);
3499
- int64_t nIndex = 0 ;
3500
- ReserveKeyFromKeyPool (nIndex, keypool, internal);
3501
- if (nIndex == -1 )
3502
- {
3504
+ int64_t nIndex;
3505
+ if (!ReserveKeyFromKeyPool (nIndex, keypool, internal)) {
3503
3506
if (IsLocked ()) return false ;
3504
3507
WalletBatch batch (*database);
3505
3508
result = GenerateNewKey (batch, internal);
@@ -3701,12 +3704,10 @@ bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool internal)
3701
3704
if (nIndex == -1 )
3702
3705
{
3703
3706
CKeyPool keypool;
3704
- pwallet->ReserveKeyFromKeyPool (nIndex, keypool, internal);
3705
- if (nIndex != -1 )
3706
- vchPubKey = keypool.vchPubKey ;
3707
- else {
3707
+ if (!pwallet->ReserveKeyFromKeyPool (nIndex, keypool, internal)) {
3708
3708
return false ;
3709
3709
}
3710
+ vchPubKey = keypool.vchPubKey ;
3710
3711
fInternal = keypool.fInternal ;
3711
3712
}
3712
3713
assert (vchPubKey.IsValid ());
0 commit comments