Skip to content

Commit f2541d0

Browse files
committed
wallet: batch MigrateToDescriptor() db transactions
Grouping all db writes into a single atomic write operation. Speeding up the flow and preventing inconsistent states.
1 parent 66c9936 commit f2541d0

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,12 @@ std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor()
18071807
keyid_it++;
18081808
}
18091809

1810+
WalletBatch batch(m_storage.GetDatabase());
1811+
if (!batch.TxnBegin()) {
1812+
LogPrintf("Error generating descriptors for migration, cannot initialize db transaction\n");
1813+
return std::nullopt;
1814+
}
1815+
18101816
// keyids is now all non-HD keys. Each key will have its own combo descriptor
18111817
for (const CKeyID& keyid : keyids) {
18121818
CKey key;
@@ -1837,8 +1843,8 @@ std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor()
18371843

18381844
// Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
18391845
auto desc_spk_man = std::make_unique<DescriptorScriptPubKeyMan>(m_storage, w_desc, /*keypool_size=*/0);
1840-
desc_spk_man->AddDescriptorKey(key, key.GetPubKey());
1841-
desc_spk_man->TopUp();
1846+
WITH_LOCK(desc_spk_man->cs_desc_man, desc_spk_man->AddDescriptorKeyWithDB(batch, key, key.GetPubKey()));
1847+
desc_spk_man->TopUpWithDB(batch);
18421848
auto desc_spks = desc_spk_man->GetScriptPubKeys();
18431849

18441850
// Remove the scriptPubKeys from our current set
@@ -1883,8 +1889,8 @@ std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor()
18831889

18841890
// Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
18851891
auto desc_spk_man = std::make_unique<DescriptorScriptPubKeyMan>(m_storage, w_desc, /*keypool_size=*/0);
1886-
desc_spk_man->AddDescriptorKey(master_key.key, master_key.key.GetPubKey());
1887-
desc_spk_man->TopUp();
1892+
WITH_LOCK(desc_spk_man->cs_desc_man, desc_spk_man->AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey()));
1893+
desc_spk_man->TopUpWithDB(batch);
18881894
auto desc_spks = desc_spk_man->GetScriptPubKeys();
18891895

18901896
// Remove the scriptPubKeys from our current set
@@ -1950,9 +1956,9 @@ std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor()
19501956
if (!GetKey(keyid, key)) {
19511957
continue;
19521958
}
1953-
desc_spk_man->AddDescriptorKey(key, key.GetPubKey());
1959+
WITH_LOCK(desc_spk_man->cs_desc_man, desc_spk_man->AddDescriptorKeyWithDB(batch, key, key.GetPubKey()));
19541960
}
1955-
desc_spk_man->TopUp();
1961+
desc_spk_man->TopUpWithDB(batch);
19561962
auto desc_spks_set = desc_spk_man->GetScriptPubKeys();
19571963
desc_spks.insert(desc_spks.end(), desc_spks_set.begin(), desc_spks_set.end());
19581964

@@ -2019,6 +2025,13 @@ std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor()
20192025

20202026
// Make sure that we have accounted for all scriptPubKeys
20212027
assert(spks.size() == 0);
2028+
2029+
// Finalize transaction
2030+
if (!batch.TxnCommit()) {
2031+
LogPrintf("Error generating descriptors for migration, cannot commit db transaction\n");
2032+
return std::nullopt;
2033+
}
2034+
20222035
return out;
20232036
}
20242037

src/wallet/scriptpubkeyman.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ class LegacySigningProvider : public SigningProvider
582582

583583
class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
584584
{
585+
friend class LegacyDataSPKM;
585586
private:
586587
using ScriptPubKeyMap = std::map<CScript, int32_t>; // Map of scripts to descriptor range index
587588
using PubKeyMap = std::map<CPubKey, int32_t>; // Map of pubkeys involved in scripts to descriptor range index

0 commit comments

Comments
 (0)