diff options
author | furszy <matiasfurszyfer@protonmail.com> | 2023-07-20 18:20:00 -0300 |
---|---|---|
committer | furszy <matiasfurszyfer@protonmail.com> | 2023-11-21 23:01:30 -0300 |
commit | 075aa44ceba41fa82bb3ce2295e2962e5fd0508e (patch) | |
tree | 933399b754090b9dc6cd028c04f0a5891f85a7d9 /src/wallet/scriptpubkeyman.cpp | |
parent | bb4554c81e0d819d74996f89cbb9c00476aedf8c (diff) | |
download | bitcoin-075aa44ceba41fa82bb3ce2295e2962e5fd0508e.tar.xz |
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.
Diffstat (limited to 'src/wallet/scriptpubkeyman.cpp')
-rw-r--r-- | src/wallet/scriptpubkeyman.cpp | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index d2b2801aa8..997348d371 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -2136,6 +2136,15 @@ std::map<CKeyID, CKey> DescriptorScriptPubKeyMan::GetKeys() const 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); unsigned int target_size; if (size > 0) { @@ -2157,7 +2166,6 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size) FlatSigningProvider provider; provider.keys = GetKeys(); - WalletBatch batch(m_storage.GetDatabase()); uint256 id = GetID(); for (int32_t i = m_max_cached_index + 1; i < new_range_end; ++i) { FlatSigningProvider out_keys; @@ -2326,6 +2334,8 @@ bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(const CExtKey& master_ // Store the master private key, and descriptor 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())) { 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(); + TopUpWithDB(batch); m_storage.UnsetBlankWalletFlag(batch); + + if (!batch.TxnCommit()) throw std::runtime_error(std::string(__func__) + ": error committing db transaction"); return true; } |