0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-01 09:35:52 -05:00

wallet: provide WalletBatch to 'SetupDescriptorScriptPubKeyMans'

So it can be used within an external db txn context.
This commit is contained in:
furszy 2024-07-03 17:22:00 -03:00
parent 34bf0795fc
commit 9ef20e86d7
No known key found for this signature in database
GPG key ID: 5DD23CCC686AA623
2 changed files with 12 additions and 16 deletions

View file

@ -3747,25 +3747,17 @@ DescriptorScriptPubKeyMan& CWallet::SetupDescriptorScriptPubKeyMan(WalletBatch&
return *out;
}
void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
void CWallet::SetupDescriptorScriptPubKeyMans(WalletBatch& batch, const CExtKey& master_key)
{
AssertLockHeld(cs_wallet);
// Create single batch txn
WalletBatch batch(GetDatabase());
if (!batch.TxnBegin()) throw std::runtime_error("Error: cannot create db transaction for descriptors setup");
for (bool internal : {false, true}) {
for (OutputType t : OUTPUT_TYPES) {
SetupDescriptorScriptPubKeyMan(batch, master_key, t, internal);
}
}
// Ensure information is committed to disk
if (!batch.TxnCommit()) throw std::runtime_error("Error: cannot commit db transaction for descriptors setup");
}
void CWallet::SetupOwnDescriptorScriptPubKeyMans()
void CWallet::SetupOwnDescriptorScriptPubKeyMans(WalletBatch& batch)
{
AssertLockHeld(cs_wallet);
assert(!IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER));
@ -3778,7 +3770,7 @@ void CWallet::SetupOwnDescriptorScriptPubKeyMans()
CExtKey master_key;
master_key.SetSeed(seed_key);
SetupDescriptorScriptPubKeyMans(master_key);
SetupDescriptorScriptPubKeyMans(batch, master_key);
}
void CWallet::SetupDescriptorScriptPubKeyMans()
@ -3786,7 +3778,10 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
AssertLockHeld(cs_wallet);
if (!IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
SetupOwnDescriptorScriptPubKeyMans();
if (!RunWithinTxn(GetDatabase(), /*process_desc=*/"setup descriptors", [&](WalletBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet){
SetupOwnDescriptorScriptPubKeyMans(batch);
return true;
})) throw std::runtime_error("Error: cannot process db transaction for descriptors setup");
} else {
ExternalSigner signer = ExternalSignerScriptPubKeyMan::GetExternalSigner();
@ -4113,12 +4108,13 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
// Setup new descriptors
SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
if (!IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
WalletBatch local_wallet_batch(GetDatabase());
// Use the existing master key if we have it
if (data.master_key.key.IsValid()) {
SetupDescriptorScriptPubKeyMans(data.master_key);
SetupDescriptorScriptPubKeyMans(local_wallet_batch, data.master_key);
} else {
// Setup with a new seed if we don't.
SetupOwnDescriptorScriptPubKeyMans();
SetupOwnDescriptorScriptPubKeyMans(local_wallet_batch);
}
}

View file

@ -1022,11 +1022,11 @@ public:
//! Create new DescriptorScriptPubKeyMan and add it to the wallet
DescriptorScriptPubKeyMan& SetupDescriptorScriptPubKeyMan(WalletBatch& batch, const CExtKey& master_key, const OutputType& output_type, bool internal) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
//! Create new DescriptorScriptPubKeyMans and add them to the wallet
void SetupDescriptorScriptPubKeyMans(const CExtKey& master_key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void SetupDescriptorScriptPubKeyMans(WalletBatch& batch, const CExtKey& master_key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void SetupDescriptorScriptPubKeyMans() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
//! Create new seed and default DescriptorScriptPubKeyMans for this wallet
void SetupOwnDescriptorScriptPubKeyMans() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void SetupOwnDescriptorScriptPubKeyMans(WalletBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
//! Return the DescriptorScriptPubKeyMan for a WalletDescriptor if it is already in the wallet
DescriptorScriptPubKeyMan* GetDescriptorScriptPubKeyMan(const WalletDescriptor& desc) const;