wallet: batch external signer descriptor import

Co-authored-by: furszy <matiasfurszyfer@protonmail.com>
This commit is contained in:
Sjors Provoost 2023-11-21 23:07:00 -03:00 committed by furszy
parent 1f65241b73
commit f053024273
No known key found for this signature in database
GPG Key ID: 5DD23CCC686AA623
4 changed files with 12 additions and 11 deletions

View File

@ -16,7 +16,7 @@
#include <vector> #include <vector>
namespace wallet { namespace wallet {
bool ExternalSignerScriptPubKeyMan::SetupDescriptor(std::unique_ptr<Descriptor> desc) bool ExternalSignerScriptPubKeyMan::SetupDescriptor(WalletBatch& batch, std::unique_ptr<Descriptor> desc)
{ {
LOCK(cs_desc_man); LOCK(cs_desc_man);
assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
@ -29,13 +29,12 @@ bool ExternalSignerScriptPubKeyMan::SetupDescriptor(std::unique_ptr<Descriptor>
m_wallet_descriptor = w_desc; m_wallet_descriptor = w_desc;
// Store the descriptor // Store the descriptor
WalletBatch batch(m_storage.GetDatabase());
if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) { if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
throw std::runtime_error(std::string(__func__) + ": writing descriptor failed"); throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
} }
// TopUp // TopUp
TopUp(); TopUpWithDB(batch);
m_storage.UnsetBlankWalletFlag(batch); m_storage.UnsetBlankWalletFlag(batch);
return true; return true;

View File

@ -23,7 +23,7 @@ class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan
/** Provide a descriptor at setup time /** Provide a descriptor at setup time
* Returns false if already setup or setup fails, true if setup is successful * Returns false if already setup or setup fails, true if setup is successful
*/ */
bool SetupDescriptor(std::unique_ptr<Descriptor>desc); bool SetupDescriptor(WalletBatch& batch, std::unique_ptr<Descriptor>desc);
static ExternalSigner GetExternalSigner(); static ExternalSigner GetExternalSigner();

View File

@ -623,11 +623,6 @@ public:
//! Setup descriptors based on the given CExtkey //! Setup descriptors based on the given CExtkey
bool SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal); bool SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal);
/** Provide a descriptor at setup time
* Returns false if already setup or setup fails, true if setup is successful
*/
bool SetupDescriptor(std::unique_ptr<Descriptor>desc);
bool HavePrivateKeys() const override; bool HavePrivateKeys() const override;
std::optional<int64_t> GetOldestKeyPoolTime() const override; std::optional<int64_t> GetOldestKeyPoolTime() const override;

View File

@ -3585,6 +3585,10 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
UniValue signer_res = signer.GetDescriptors(account); UniValue signer_res = signer.GetDescriptors(account);
if (!signer_res.isObject()) throw std::runtime_error(std::string(__func__) + ": Unexpected result"); if (!signer_res.isObject()) throw std::runtime_error(std::string(__func__) + ": Unexpected result");
WalletBatch batch(GetDatabase());
if (!batch.TxnBegin()) throw std::runtime_error("Error: cannot create db transaction for descriptors import");
for (bool internal : {false, true}) { for (bool internal : {false, true}) {
const UniValue& descriptor_vals = signer_res.find_value(internal ? "internal" : "receive"); const UniValue& descriptor_vals = signer_res.find_value(internal ? "internal" : "receive");
if (!descriptor_vals.isArray()) throw std::runtime_error(std::string(__func__) + ": Unexpected result"); if (!descriptor_vals.isArray()) throw std::runtime_error(std::string(__func__) + ": Unexpected result");
@ -3601,12 +3605,15 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
} }
OutputType t = *desc->GetOutputType(); OutputType t = *desc->GetOutputType();
auto spk_manager = std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, m_keypool_size)); auto spk_manager = std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, m_keypool_size));
spk_manager->SetupDescriptor(std::move(desc)); spk_manager->SetupDescriptor(batch, std::move(desc));
uint256 id = spk_manager->GetID(); uint256 id = spk_manager->GetID();
AddScriptPubKeyMan(id, std::move(spk_manager)); AddScriptPubKeyMan(id, std::move(spk_manager));
AddActiveScriptPubKeyMan(id, t, internal); AddActiveScriptPubKeyManWithDb(batch, id, t, internal);
} }
} }
// Ensure imported descriptors are committed to disk
if (!batch.TxnCommit()) throw std::runtime_error("Error: cannot commit db transaction for descriptors import");
} }
} }