mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
wallet: batch descriptor spkm TopUp
Instead of performing multiple atomic write operations per descriptor setup call, batch them all within a single atomic db txn.
This commit is contained in:
parent
bb4554c81e
commit
075aa44ceb
2 changed files with 18 additions and 3 deletions
|
@ -2135,6 +2135,15 @@ std::map<CKeyID, CKey> DescriptorScriptPubKeyMan::GetKeys() const
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
|
bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
|
||||||
|
{
|
||||||
|
WalletBatch batch(m_storage.GetDatabase());
|
||||||
|
if (!batch.TxnBegin()) return false;
|
||||||
|
bool res = TopUpWithDB(batch, size);
|
||||||
|
if (!batch.TxnCommit()) throw std::runtime_error(strprintf("Error during descriptors keypool top up. Cannot commit changes for wallet %s", m_storage.GetDisplayName()));
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int size)
|
||||||
{
|
{
|
||||||
LOCK(cs_desc_man);
|
LOCK(cs_desc_man);
|
||||||
unsigned int target_size;
|
unsigned int target_size;
|
||||||
|
@ -2157,7 +2166,6 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
|
||||||
FlatSigningProvider provider;
|
FlatSigningProvider provider;
|
||||||
provider.keys = GetKeys();
|
provider.keys = GetKeys();
|
||||||
|
|
||||||
WalletBatch batch(m_storage.GetDatabase());
|
|
||||||
uint256 id = GetID();
|
uint256 id = GetID();
|
||||||
for (int32_t i = m_max_cached_index + 1; i < new_range_end; ++i) {
|
for (int32_t i = m_max_cached_index + 1; i < new_range_end; ++i) {
|
||||||
FlatSigningProvider out_keys;
|
FlatSigningProvider out_keys;
|
||||||
|
@ -2326,6 +2334,8 @@ bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(const CExtKey& master_
|
||||||
|
|
||||||
// Store the master private key, and descriptor
|
// Store the master private key, and descriptor
|
||||||
WalletBatch batch(m_storage.GetDatabase());
|
WalletBatch batch(m_storage.GetDatabase());
|
||||||
|
if (!batch.TxnBegin()) throw std::runtime_error(std::string(__func__) + ": cannot start db transaction");
|
||||||
|
|
||||||
if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
|
if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
|
||||||
throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed");
|
throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed");
|
||||||
}
|
}
|
||||||
|
@ -2334,9 +2344,11 @@ bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(const CExtKey& master_
|
||||||
}
|
}
|
||||||
|
|
||||||
// TopUp
|
// TopUp
|
||||||
TopUp();
|
TopUpWithDB(batch);
|
||||||
|
|
||||||
m_storage.UnsetBlankWalletFlag(batch);
|
m_storage.UnsetBlankWalletFlag(batch);
|
||||||
|
|
||||||
|
if (!batch.TxnCommit()) throw std::runtime_error(std::string(__func__) + ": error committing db transaction");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -583,7 +583,10 @@ private:
|
||||||
std::unique_ptr<FlatSigningProvider> GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
|
std::unique_ptr<FlatSigningProvider> GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);
|
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);
|
||||||
|
|
||||||
|
//! Same as 'TopUp' but designed for use within a batch transaction context
|
||||||
|
bool TopUpWithDB(WalletBatch& batch, unsigned int size = 0);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size)
|
DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size)
|
||||||
|
|
Loading…
Add table
Reference in a new issue