Skip to content

Commit 9ef20e8

Browse files
committed
wallet: provide WalletBatch to 'SetupDescriptorScriptPubKeyMans'
So it can be used within an external db txn context.
1 parent 34bf079 commit 9ef20e8

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/wallet/wallet.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3747,25 +3747,17 @@ DescriptorScriptPubKeyMan& CWallet::SetupDescriptorScriptPubKeyMan(WalletBatch&
37473747
return *out;
37483748
}
37493749

3750-
void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
3750+
void CWallet::SetupDescriptorScriptPubKeyMans(WalletBatch& batch, const CExtKey& master_key)
37513751
{
37523752
AssertLockHeld(cs_wallet);
3753-
3754-
// Create single batch txn
3755-
WalletBatch batch(GetDatabase());
3756-
if (!batch.TxnBegin()) throw std::runtime_error("Error: cannot create db transaction for descriptors setup");
3757-
37583753
for (bool internal : {false, true}) {
37593754
for (OutputType t : OUTPUT_TYPES) {
37603755
SetupDescriptorScriptPubKeyMan(batch, master_key, t, internal);
37613756
}
37623757
}
3763-
3764-
// Ensure information is committed to disk
3765-
if (!batch.TxnCommit()) throw std::runtime_error("Error: cannot commit db transaction for descriptors setup");
37663758
}
37673759

3768-
void CWallet::SetupOwnDescriptorScriptPubKeyMans()
3760+
void CWallet::SetupOwnDescriptorScriptPubKeyMans(WalletBatch& batch)
37693761
{
37703762
AssertLockHeld(cs_wallet);
37713763
assert(!IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER));
@@ -3778,15 +3770,18 @@ void CWallet::SetupOwnDescriptorScriptPubKeyMans()
37783770
CExtKey master_key;
37793771
master_key.SetSeed(seed_key);
37803772

3781-
SetupDescriptorScriptPubKeyMans(master_key);
3773+
SetupDescriptorScriptPubKeyMans(batch, master_key);
37823774
}
37833775

37843776
void CWallet::SetupDescriptorScriptPubKeyMans()
37853777
{
37863778
AssertLockHeld(cs_wallet);
37873779

37883780
if (!IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
3789-
SetupOwnDescriptorScriptPubKeyMans();
3781+
if (!RunWithinTxn(GetDatabase(), /*process_desc=*/"setup descriptors", [&](WalletBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet){
3782+
SetupOwnDescriptorScriptPubKeyMans(batch);
3783+
return true;
3784+
})) throw std::runtime_error("Error: cannot process db transaction for descriptors setup");
37903785
} else {
37913786
ExternalSigner signer = ExternalSignerScriptPubKeyMan::GetExternalSigner();
37923787

@@ -4113,12 +4108,13 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
41134108
// Setup new descriptors
41144109
SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
41154110
if (!IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
4111+
WalletBatch local_wallet_batch(GetDatabase());
41164112
// Use the existing master key if we have it
41174113
if (data.master_key.key.IsValid()) {
4118-
SetupDescriptorScriptPubKeyMans(data.master_key);
4114+
SetupDescriptorScriptPubKeyMans(local_wallet_batch, data.master_key);
41194115
} else {
41204116
// Setup with a new seed if we don't.
4121-
SetupOwnDescriptorScriptPubKeyMans();
4117+
SetupOwnDescriptorScriptPubKeyMans(local_wallet_batch);
41224118
}
41234119
}
41244120

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,11 +1022,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10221022
//! Create new DescriptorScriptPubKeyMan and add it to the wallet
10231023
DescriptorScriptPubKeyMan& SetupDescriptorScriptPubKeyMan(WalletBatch& batch, const CExtKey& master_key, const OutputType& output_type, bool internal) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10241024
//! Create new DescriptorScriptPubKeyMans and add them to the wallet
1025-
void SetupDescriptorScriptPubKeyMans(const CExtKey& master_key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1025+
void SetupDescriptorScriptPubKeyMans(WalletBatch& batch, const CExtKey& master_key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10261026
void SetupDescriptorScriptPubKeyMans() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10271027

10281028
//! Create new seed and default DescriptorScriptPubKeyMans for this wallet
1029-
void SetupOwnDescriptorScriptPubKeyMans() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1029+
void SetupOwnDescriptorScriptPubKeyMans(WalletBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10301030

10311031
//! Return the DescriptorScriptPubKeyMan for a WalletDescriptor if it is already in the wallet
10321032
DescriptorScriptPubKeyMan* GetDescriptorScriptPubKeyMan(const WalletDescriptor& desc) const;

0 commit comments

Comments
 (0)