Skip to content

Commit b1a46b2

Browse files
committed
Merge bitcoin/bitcoin#26008: wallet: cache IsMine scriptPubKeys to improve performance of descriptor wallets
e041ed9 wallet: Retrieve ID from loaded DescSPKM directly (Ava Chow) 39640dd wallet: Use scriptPubKeyCache in GetSolvingProvider (Ava Chow) b410f68 wallet: Use scriptPubKey cache in GetScriptPubKeyMans (Ava Chow) edf4e73 wallet: Use scriptPubKey cache in IsMine (Ava Chow) 3723233 wallet: Cache scriptPubKeys for all DescriptorSPKMs (Ava Chow) 99a0cdd wallet: Introduce a callback called after TopUp completes (Ava Chow) b276825 bench: Add a benchmark for ismine (Ava Chow) Pull request description: Wallets that have a ton of non-ranged descriptors (such as a migrated non-HD wallet) perform fairly poorly due to looping through all of the wallet's `ScriptPubKeyMan`s. This is done in various places, such as `IsMine`, and helper functions for fetching a `ScriptPubKeyMan` and a `SolvingProvider`. This also has a bit of a performance impact on standard descriptor wallets, although less noticeable due to the small number of SPKMs. As these functions are based on doing `IsMine` for each `ScriptPubKeyMan`, we can improve this performance by caching `IsMine` scriptPubKeys for all descriptors and use that to determine which `ScriptPubKeyMan` to actually use for those things. This cache is used exclusively and we no longer iterate the SPKMs. Also added a benchmark for `IsMine`. ACKs for top commit: ryanofsky: Code review ACK e041ed9. Just suggested changes since last review josibake: ACK bitcoin/bitcoin@e041ed9 furszy: Code review ACK e041ed9 Tree-SHA512: 8e7081991a025e682e9dea838b4543b0d179832d1c47397fb9fe7a97fa01eb699c15a5d5a785634926844fc83a46e6ac07ef753119f39d84423220ef8a548894
2 parents c265aad + e041ed9 commit b1a46b2

File tree

7 files changed

+157
-21
lines changed

7 files changed

+157
-21
lines changed

src/Makefile.bench.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ bench_bench_bitcoin_SOURCES += bench/wallet_balance.cpp
8888
bench_bench_bitcoin_SOURCES += bench/wallet_create.cpp
8989
bench_bench_bitcoin_SOURCES += bench/wallet_loading.cpp
9090
bench_bench_bitcoin_SOURCES += bench/wallet_create_tx.cpp
91+
bench_bench_bitcoin_SOURCES += bench/wallet_ismine.cpp
92+
9193
bench_bench_bitcoin_LDADD += $(BDB_LIBS) $(SQLITE_LIBS)
9294
endif
9395

src/bench/wallet_ismine.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <bench/bench.h>
6+
#include <interfaces/chain.h>
7+
#include <key.h>
8+
#include <key_io.h>
9+
#include <node/context.h>
10+
#include <test/util/setup_common.h>
11+
#include <util/translation.h>
12+
#include <validationinterface.h>
13+
#include <wallet/context.h>
14+
#include <wallet/wallet.h>
15+
#include <wallet/walletutil.h>
16+
#include <wallet/test/util.h>
17+
18+
namespace wallet {
19+
static void WalletIsMine(benchmark::Bench& bench, bool legacy_wallet, int num_combo = 0)
20+
{
21+
const auto test_setup = MakeNoLogFileContext<TestingSetup>();
22+
23+
WalletContext context;
24+
context.args = &test_setup->m_args;
25+
context.chain = test_setup->m_node.chain.get();
26+
27+
// Setup the wallet
28+
// Loading the wallet will also create it
29+
uint64_t create_flags = 0;
30+
if (!legacy_wallet) {
31+
create_flags = WALLET_FLAG_DESCRIPTORS;
32+
}
33+
auto database = CreateMockableWalletDatabase();
34+
auto wallet = TestLoadWallet(std::move(database), context, create_flags);
35+
36+
// For a descriptor wallet, fill with num_combo combo descriptors with random keys
37+
// This benchmarks a non-HD wallet migrated to descriptors
38+
if (!legacy_wallet && num_combo > 0) {
39+
LOCK(wallet->cs_wallet);
40+
for (int i = 0; i < num_combo; ++i) {
41+
CKey key;
42+
key.MakeNewKey(/*fCompressed=*/true);
43+
FlatSigningProvider keys;
44+
std::string error;
45+
std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);
46+
WalletDescriptor w_desc(std::move(desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);
47+
auto spkm = wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", /*internal=*/false);
48+
assert(spkm);
49+
}
50+
}
51+
52+
const CScript script = GetScriptForDestination(DecodeDestination(ADDRESS_BCRT1_UNSPENDABLE));
53+
54+
bench.run([&] {
55+
LOCK(wallet->cs_wallet);
56+
isminetype mine = wallet->IsMine(script);
57+
assert(mine == ISMINE_NO);
58+
});
59+
60+
TestUnloadWallet(std::move(wallet));
61+
}
62+
63+
#ifdef USE_BDB
64+
static void WalletIsMineLegacy(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/true); }
65+
BENCHMARK(WalletIsMineLegacy, benchmark::PriorityLevel::LOW);
66+
#endif
67+
68+
#ifdef USE_SQLITE
69+
static void WalletIsMineDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/false); }
70+
static void WalletIsMineMigratedDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/false, /*num_combo=*/2000); }
71+
BENCHMARK(WalletIsMineDescriptors, benchmark::PriorityLevel::LOW);
72+
BENCHMARK(WalletIsMineMigratedDescriptors, benchmark::PriorityLevel::LOW);
73+
#endif
74+
} // namespace wallet

src/wallet/scriptpubkeyman.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,9 @@ bool LegacyScriptPubKeyMan::TopUp(unsigned int kpSize)
12881288
}
12891289
if (!batch.TxnCommit()) throw std::runtime_error(strprintf("Error during keypool top up. Cannot commit changes for wallet %s", m_storage.GetDisplayName()));
12901290
NotifyCanGetAddressesChanged();
1291+
// Note: Unlike with DescriptorSPKM, LegacySPKM does not need to call
1292+
// m_storage.TopUpCallback() as we do not know what new scripts the LegacySPKM is
1293+
// watching for. CWallet's scriptPubKey cache is not used for LegacySPKMs.
12911294
return true;
12921295
}
12931296

@@ -2152,6 +2155,7 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
21522155
bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int size)
21532156
{
21542157
LOCK(cs_desc_man);
2158+
std::set<CScript> new_spks;
21552159
unsigned int target_size;
21562160
if (size > 0) {
21572161
target_size = size;
@@ -2182,6 +2186,7 @@ bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int siz
21822186
if (!m_wallet_descriptor.descriptor->Expand(i, provider, scripts_temp, out_keys, &temp_cache)) return false;
21832187
}
21842188
// Add all of the scriptPubKeys to the scriptPubKey set
2189+
new_spks.insert(scripts_temp.begin(), scripts_temp.end());
21852190
for (const CScript& script : scripts_temp) {
21862191
m_map_script_pub_keys[script] = i;
21872192
}
@@ -2207,6 +2212,7 @@ bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int siz
22072212
// By this point, the cache size should be the size of the entire range
22082213
assert(m_wallet_descriptor.range_end - 1 == m_max_cached_index);
22092214

2215+
m_storage.TopUpCallback(new_spks, this);
22102216
NotifyCanGetAddressesChanged();
22112217
return true;
22122218
}
@@ -2620,6 +2626,7 @@ uint256 DescriptorScriptPubKeyMan::GetID() const
26202626
void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)
26212627
{
26222628
LOCK(cs_desc_man);
2629+
std::set<CScript> new_spks;
26232630
m_wallet_descriptor.cache = cache;
26242631
for (int32_t i = m_wallet_descriptor.range_start; i < m_wallet_descriptor.range_end; ++i) {
26252632
FlatSigningProvider out_keys;
@@ -2628,6 +2635,7 @@ void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)
26282635
throw std::runtime_error("Error: Unable to expand wallet descriptor from cache");
26292636
}
26302637
// Add all of the scriptPubKeys to the scriptPubKey set
2638+
new_spks.insert(scripts_temp.begin(), scripts_temp.end());
26312639
for (const CScript& script : scripts_temp) {
26322640
if (m_map_script_pub_keys.count(script) != 0) {
26332641
throw std::runtime_error(strprintf("Error: Already loaded script at index %d as being at index %d", i, m_map_script_pub_keys[script]));
@@ -2645,6 +2653,8 @@ void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)
26452653
}
26462654
m_max_cached_index++;
26472655
}
2656+
// Make sure the wallet knows about our new spks
2657+
m_storage.TopUpCallback(new_spks, this);
26482658
}
26492659

26502660
bool DescriptorScriptPubKeyMan::AddKey(const CKeyID& key_id, const CKey& key)

src/wallet/scriptpubkeyman.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct bilingual_str;
3131

3232
namespace wallet {
3333
struct MigrationData;
34+
class ScriptPubKeyMan;
3435

3536
// Wallet storage things that ScriptPubKeyMans need in order to be able to store things to the wallet database.
3637
// It provides access to things that are part of the entire wallet and not specific to a ScriptPubKeyMan such as
@@ -51,6 +52,8 @@ class WalletStorage
5152
virtual bool WithEncryptionKey(std::function<bool (const CKeyingMaterial&)> cb) const = 0;
5253
virtual bool HasEncryptionKeys() const = 0;
5354
virtual bool IsLocked() const = 0;
55+
//! Callback function for after TopUp completes containining any scripts that were added by a SPKMan
56+
virtual void TopUpCallback(const std::set<CScript>&, ScriptPubKeyMan*) = 0;
5457
};
5558

5659
//! Constant representing an unknown spkm creation time

src/wallet/wallet.cpp

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,11 +1571,22 @@ isminetype CWallet::IsMine(const CTxDestination& dest) const
15711571
isminetype CWallet::IsMine(const CScript& script) const
15721572
{
15731573
AssertLockHeld(cs_wallet);
1574-
isminetype result = ISMINE_NO;
1575-
for (const auto& spk_man_pair : m_spk_managers) {
1576-
result = std::max(result, spk_man_pair.second->IsMine(script));
1574+
1575+
// Search the cache so that IsMine is called only on the relevant SPKMs instead of on everything in m_spk_managers
1576+
const auto& it = m_cached_spks.find(script);
1577+
if (it != m_cached_spks.end()) {
1578+
isminetype res = ISMINE_NO;
1579+
for (const auto& spkm : it->second) {
1580+
res = std::max(res, spkm->IsMine(script));
1581+
}
1582+
Assume(res == ISMINE_SPENDABLE);
1583+
return res;
15771584
}
1578-
return result;
1585+
1586+
// Legacy wallet
1587+
if (IsLegacy()) return GetLegacyScriptPubKeyMan()->IsMine(script);
1588+
1589+
return ISMINE_NO;
15791590
}
15801591

15811592
bool CWallet::IsMine(const CTransaction& tx) const
@@ -3474,12 +3485,18 @@ ScriptPubKeyMan* CWallet::GetScriptPubKeyMan(const OutputType& type, bool intern
34743485
std::set<ScriptPubKeyMan*> CWallet::GetScriptPubKeyMans(const CScript& script) const
34753486
{
34763487
std::set<ScriptPubKeyMan*> spk_mans;
3477-
SignatureData sigdata;
3478-
for (const auto& spk_man_pair : m_spk_managers) {
3479-
if (spk_man_pair.second->CanProvide(script, sigdata)) {
3480-
spk_mans.insert(spk_man_pair.second.get());
3481-
}
3488+
3489+
// Search the cache for relevant SPKMs instead of iterating m_spk_managers
3490+
const auto& it = m_cached_spks.find(script);
3491+
if (it != m_cached_spks.end()) {
3492+
spk_mans.insert(it->second.begin(), it->second.end());
34823493
}
3494+
SignatureData sigdata;
3495+
Assume(std::all_of(spk_mans.begin(), spk_mans.end(), [&script, &sigdata](ScriptPubKeyMan* spkm) { return spkm->CanProvide(script, sigdata); }));
3496+
3497+
// Legacy wallet
3498+
if (IsLegacy() && GetLegacyScriptPubKeyMan()->CanProvide(script, sigdata)) spk_mans.insert(GetLegacyScriptPubKeyMan());
3499+
34833500
return spk_mans;
34843501
}
34853502

@@ -3499,11 +3516,17 @@ std::unique_ptr<SigningProvider> CWallet::GetSolvingProvider(const CScript& scri
34993516

35003517
std::unique_ptr<SigningProvider> CWallet::GetSolvingProvider(const CScript& script, SignatureData& sigdata) const
35013518
{
3502-
for (const auto& spk_man_pair : m_spk_managers) {
3503-
if (spk_man_pair.second->CanProvide(script, sigdata)) {
3504-
return spk_man_pair.second->GetSolvingProvider(script);
3505-
}
3519+
// Search the cache for relevant SPKMs instead of iterating m_spk_managers
3520+
const auto& it = m_cached_spks.find(script);
3521+
if (it != m_cached_spks.end()) {
3522+
// All spkms for a given script must already be able to make a SigningProvider for the script, so just return the first one.
3523+
Assume(it->second.at(0)->CanProvide(script, sigdata));
3524+
return it->second.at(0)->GetSolvingProvider(script);
35063525
}
3526+
3527+
// Legacy wallet
3528+
if (IsLegacy() && GetLegacyScriptPubKeyMan()->CanProvide(script, sigdata)) return GetLegacyScriptPubKeyMan()->GetSolvingProvider(script);
3529+
35073530
return nullptr;
35083531
}
35093532

@@ -3582,15 +3605,16 @@ void CWallet::ConnectScriptPubKeyManNotifiers()
35823605
}
35833606
}
35843607

3585-
void CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc)
3608+
DescriptorScriptPubKeyMan& CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc)
35863609
{
3610+
DescriptorScriptPubKeyMan* spk_manager;
35873611
if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
3588-
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, desc, m_keypool_size));
3589-
AddScriptPubKeyMan(id, std::move(spk_manager));
3612+
spk_manager = new ExternalSignerScriptPubKeyMan(*this, desc, m_keypool_size);
35903613
} else {
3591-
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size));
3592-
AddScriptPubKeyMan(id, std::move(spk_manager));
3614+
spk_manager = new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size);
35933615
}
3616+
AddScriptPubKeyMan(id, std::unique_ptr<ScriptPubKeyMan>(spk_manager));
3617+
return *spk_manager;
35943618
}
35953619

35963620
void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
@@ -3945,6 +3969,8 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
39453969
if (ExtractDestination(script, dest)) not_migrated_dests.emplace(dest);
39463970
}
39473971

3972+
Assume(!m_cached_spks.empty());
3973+
39483974
for (auto& desc_spkm : data.desc_spkms) {
39493975
if (m_spk_managers.count(desc_spkm->GetID()) > 0) {
39503976
error = _("Error: Duplicate descriptors created during migration. Your wallet may be corrupted.");
@@ -4428,4 +4454,17 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
44284454
}
44294455
return res;
44304456
}
4457+
4458+
void CWallet::CacheNewScriptPubKeys(const std::set<CScript>& spks, ScriptPubKeyMan* spkm)
4459+
{
4460+
for (const auto& script : spks) {
4461+
m_cached_spks[script].push_back(spkm);
4462+
}
4463+
}
4464+
4465+
void CWallet::TopUpCallback(const std::set<CScript>& spks, ScriptPubKeyMan* spkm)
4466+
{
4467+
// Update scriptPubKey cache
4468+
CacheNewScriptPubKeys(spks, spkm);
4469+
}
44314470
} // namespace wallet

src/wallet/wallet.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
422422
// Same as 'AddActiveScriptPubKeyMan' but designed for use within a batch transaction context
423423
void AddActiveScriptPubKeyManWithDb(WalletBatch& batch, uint256 id, OutputType type, bool internal);
424424

425+
//! Cache of descriptor ScriptPubKeys used for IsMine. Maps ScriptPubKey to set of spkms
426+
std::unordered_map<CScript, std::vector<ScriptPubKeyMan*>, SaltedSipHasher> m_cached_spks;
427+
425428
/**
426429
* Catch wallet up to current chain, scanning new blocks, updating the best
427430
* block locator and m_last_block_processed, and registering for
@@ -994,7 +997,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
994997
void ConnectScriptPubKeyManNotifiers();
995998

996999
//! Instantiate a descriptor ScriptPubKeyMan from the WalletDescriptor and load it
997-
void LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc);
1000+
DescriptorScriptPubKeyMan& LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc);
9981001

9991002
//! Adds the active ScriptPubKeyMan for the specified type and internal. Writes it to the wallet file
10001003
//! @param[in] id The unique id for the ScriptPubKeyMan
@@ -1045,6 +1048,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10451048

10461049
//! Whether the (external) signer performs R-value signature grinding
10471050
bool CanGrindR() const;
1051+
1052+
//! Add scriptPubKeys for this ScriptPubKeyMan into the scriptPubKey cache
1053+
void CacheNewScriptPubKeys(const std::set<CScript>& spks, ScriptPubKeyMan* spkm);
1054+
1055+
void TopUpCallback(const std::set<CScript>& spks, ScriptPubKeyMan* spkm) override;
10481056
};
10491057

10501058
/**

src/wallet/walletdb.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,10 @@ static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& bat
804804
strErr = strprintf("%s\nDetails: %s", strErr, e.what());
805805
return DBErrors::UNKNOWN_DESCRIPTOR;
806806
}
807-
pwallet->LoadDescriptorScriptPubKeyMan(id, desc);
807+
DescriptorScriptPubKeyMan& spkm = pwallet->LoadDescriptorScriptPubKeyMan(id, desc);
808808

809809
// Prior to doing anything with this spkm, verify ID compatibility
810-
if (id != pwallet->GetDescriptorScriptPubKeyMan(desc)->GetID()) {
810+
if (id != spkm.GetID()) {
811811
strErr = "The descriptor ID calculated by the wallet differs from the one in DB";
812812
return DBErrors::CORRUPT;
813813
}

0 commit comments

Comments
 (0)