| 3682 | } |
| 3683 | |
| 3684 | void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool internal) |
| 3685 | { |
| 3686 | nIndex = -1; |
| 3687 | keypool.vchPubKey = CPubKey(); |
| 3688 | { |
| 3689 | LOCK(cs_wallet); |
| 3690 | |
| 3691 | if (!IsLocked()) |
| 3692 | TopUpKeyPool(); |
| 3693 | |
| 3694 | internal = internal && IsHDEnabled(); |
| 3695 | std::set<int64_t>& setKeyPool = internal ? setInternalKeyPool : setExternalKeyPool; |
| 3696 | |
| 3697 | // Get the oldest key |
| 3698 | if (setKeyPool.empty()) |
| 3699 | return; |
| 3700 | |
| 3701 | CWalletDB walletdb(strWalletFile); |
| 3702 | |
| 3703 | nIndex = *setKeyPool.begin(); |
| 3704 | setKeyPool.erase(nIndex); |
| 3705 | if (!walletdb.ReadPool(nIndex, keypool)) { |
| 3706 | LogPrintf("%s: read failed\n", __func__); |
| 3707 | return; |
| 3708 | } |
| 3709 | if (!HaveKey(keypool.vchPubKey.GetID())) { |
| 3710 | LogPrintf("%s: unknown key in key pool\n", __func__); |
| 3711 | return; |
| 3712 | } |
| 3713 | // If the key was pre-split keypool, we don't care about what type it is |
| 3714 | if (keypool.internal != internal) { |
| 3715 | LogPrintf("%s: keypool entry misclassified\n", __func__); |
| 3716 | return; |
| 3717 | } |
| 3718 | if (!keypool.vchPubKey.IsValid()) { |
| 3719 | LogPrintf("%s: keypool entry invalid\n", __func__); |
| 3720 | return; |
| 3721 | } |
| 3722 | |
| 3723 | // m_pool_key_to_index.erase(keypool.vchPubKey.GetID()); |
| 3724 | LogPrintf("keypool reserve %d\n", nIndex); |
| 3725 | } |
| 3726 | } |
| 3727 | |
| 3728 | void CWallet::KeepKey(int64_t nIndex) |
| 3729 | { |