Skip to content

Commit f053024

Browse files
Sjorsfurszy
andcommitted
wallet: batch external signer descriptor import
Co-authored-by: furszy <matiasfurszyfer@protonmail.com>
1 parent 1f65241 commit f053024

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

src/wallet/external_signer_scriptpubkeyman.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <vector>
1717

1818
namespace wallet {
19-
bool ExternalSignerScriptPubKeyMan::SetupDescriptor(std::unique_ptr<Descriptor> desc)
19+
bool ExternalSignerScriptPubKeyMan::SetupDescriptor(WalletBatch& batch, std::unique_ptr<Descriptor> desc)
2020
{
2121
LOCK(cs_desc_man);
2222
assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
@@ -29,13 +29,12 @@ bool ExternalSignerScriptPubKeyMan::SetupDescriptor(std::unique_ptr<Descriptor>
2929
m_wallet_descriptor = w_desc;
3030

3131
// Store the descriptor
32-
WalletBatch batch(m_storage.GetDatabase());
3332
if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
3433
throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
3534
}
3635

3736
// TopUp
38-
TopUp();
37+
TopUpWithDB(batch);
3938

4039
m_storage.UnsetBlankWalletFlag(batch);
4140
return true;

src/wallet/external_signer_scriptpubkeyman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan
2323
/** Provide a descriptor at setup time
2424
* Returns false if already setup or setup fails, true if setup is successful
2525
*/
26-
bool SetupDescriptor(std::unique_ptr<Descriptor>desc);
26+
bool SetupDescriptor(WalletBatch& batch, std::unique_ptr<Descriptor>desc);
2727

2828
static ExternalSigner GetExternalSigner();
2929

src/wallet/scriptpubkeyman.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,6 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
623623
//! Setup descriptors based on the given CExtkey
624624
bool SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal);
625625

626-
/** Provide a descriptor at setup time
627-
* Returns false if already setup or setup fails, true if setup is successful
628-
*/
629-
bool SetupDescriptor(std::unique_ptr<Descriptor>desc);
630-
631626
bool HavePrivateKeys() const override;
632627

633628
std::optional<int64_t> GetOldestKeyPoolTime() const override;

src/wallet/wallet.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3585,6 +3585,10 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
35853585
UniValue signer_res = signer.GetDescriptors(account);
35863586

35873587
if (!signer_res.isObject()) throw std::runtime_error(std::string(__func__) + ": Unexpected result");
3588+
3589+
WalletBatch batch(GetDatabase());
3590+
if (!batch.TxnBegin()) throw std::runtime_error("Error: cannot create db transaction for descriptors import");
3591+
35883592
for (bool internal : {false, true}) {
35893593
const UniValue& descriptor_vals = signer_res.find_value(internal ? "internal" : "receive");
35903594
if (!descriptor_vals.isArray()) throw std::runtime_error(std::string(__func__) + ": Unexpected result");
@@ -3601,12 +3605,15 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
36013605
}
36023606
OutputType t = *desc->GetOutputType();
36033607
auto spk_manager = std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, m_keypool_size));
3604-
spk_manager->SetupDescriptor(std::move(desc));
3608+
spk_manager->SetupDescriptor(batch, std::move(desc));
36053609
uint256 id = spk_manager->GetID();
36063610
AddScriptPubKeyMan(id, std::move(spk_manager));
3607-
AddActiveScriptPubKeyMan(id, t, internal);
3611+
AddActiveScriptPubKeyManWithDb(batch, id, t, internal);
36083612
}
36093613
}
3614+
3615+
// Ensure imported descriptors are committed to disk
3616+
if (!batch.TxnCommit()) throw std::runtime_error("Error: cannot commit db transaction for descriptors import");
36103617
}
36113618
}
36123619

0 commit comments

Comments
 (0)