aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/wallet.cpp
diff options
context:
space:
mode:
authorMatt Corallo <git@bluematt.me>2017-07-11 12:15:02 -0400
committerMatt Corallo <git@bluematt.me>2017-07-17 12:12:48 -0400
commit1fc8c3de0cff2971cc1f9903bfc3d03a982f2fab (patch)
tree90ef1ee9bbdcc8f90231635a71b63b9234e2a7d7 /src/wallet/wallet.cpp
parent0b019357ff09e7a522307fc271d6b60562a7b890 (diff)
No longer ever reuse keypool indexes
This fixes an issue where you could reserve a keypool entry, then top up the keypool, writing out a new key at the given index, then return they key from the pool. This isnt likely to cause issues, but given there is no reason to ever re-use keypool indexes (they're 64 bits...), best to avoid it alltogether.
Diffstat (limited to 'src/wallet/wallet.cpp')
-rw-r--r--src/wallet/wallet.cpp14
1 files changed, 5 insertions, 9 deletions
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 6f1894d430..5317502589 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -3211,21 +3211,17 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
internal = true;
}
- if (!setInternalKeyPool.empty()) {
- nEnd = *(setInternalKeyPool.rbegin()) + 1;
- }
- if (!setExternalKeyPool.empty()) {
- nEnd = std::max(nEnd, *(setExternalKeyPool.rbegin()) + 1);
- }
+ assert(m_max_keypool_index < std::numeric_limits<int64_t>::max()); // How in the hell did you use so many keys?
+ int64_t index = ++m_max_keypool_index;
- if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey(walletdb, internal), internal))) {
+ if (!walletdb.WritePool(index, CKeyPool(GenerateNewKey(walletdb, internal), internal))) {
throw std::runtime_error(std::string(__func__) + ": writing generated key failed");
}
if (internal) {
- setInternalKeyPool.insert(nEnd);
+ setInternalKeyPool.insert(index);
} else {
- setExternalKeyPool.insert(nEnd);
+ setExternalKeyPool.insert(index);
}
}
if (missingInternal + missingExternal > 0) {