Skip to content

Commit 53e4189

Browse files
committed
Add ThreadPool to spspkman
1 parent 5e78fbf commit 53e4189

File tree

6 files changed

+56
-23
lines changed

6 files changed

+56
-23
lines changed

src/bench/wallet_silentpayments_scan.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <script/sign.h>
1414
#include <script/signingprovider.h>
1515
#include <sync.h>
16+
#include <util/threadpool.h>
1617
#include <validation.h>
1718
#include <wallet/test/util.h>
1819
#include <wallet/receive.h>
@@ -26,7 +27,12 @@ static int SATS_PER_OUTPUT = 1;
2627
static int FEE_SATS = 1000;
2728
static size_t SP_RECIPIENT_GROUP_LIMIT = 1000;
2829

29-
static void WalletSPScan(benchmark::Bench& bench, size_t num_txs, size_t outputs_per_tx, std::optional<size_t> n_payments = {})
30+
static void WalletSPScan(
31+
benchmark::Bench& bench,
32+
size_t num_txs,
33+
size_t outputs_per_tx,
34+
std::optional<size_t> n_payments = {},
35+
size_t n_workers = 1)
3036
{
3137
auto testsetup = TestChain100Setup();
3238
auto& m_node = testsetup.m_node;
@@ -67,7 +73,9 @@ static void WalletSPScan(benchmark::Bench& bench, size_t num_txs, size_t outputs
6773
CBlockIndex* currentTip = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
6874
assert(nHeight == currentTip->nHeight);
6975

70-
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
76+
ThreadPool threadpool{"spbench"};
77+
CWallet wallet{m_node.chain.get(), "", CreateMockableWalletDatabase(), &threadpool};
78+
if (n_workers > 1) threadpool.Start(n_workers - 1);
7179
{
7280
LOCK(wallet.cs_wallet);
7381
LOCK(Assert(m_node.chainman)->GetMutex());
@@ -129,8 +137,14 @@ static void WalletSPScan(benchmark::Bench& bench, size_t num_txs, size_t outputs
129137

130138
assert(GetBalance(wallet).m_mine_trusted == 0);
131139
int expected_balance = SATS_PER_OUTPUT * num_sp_outputs * num_txs;
132-
133-
bench.epochs(1).epochIterations(1).unit("block").run([&] {
140+
std::string name = strprintf(
141+
"WalletSPScan_%zuTx_%zuOutputs_%zuPayments_%zuWorkers",
142+
num_txs,
143+
outputs_per_tx,
144+
n_payments ? *n_payments : outputs_per_tx,
145+
n_workers
146+
);
147+
bench.epochs(1).epochIterations(1).unit("block").run(name, [&] {
134148
WalletRescanReserver reserver(wallet);
135149
reserver.reserve();
136150
auto result = wallet.ScanForWalletTransactions(
@@ -151,14 +165,15 @@ static void WalletSPScan(benchmark::Bench& bench, size_t num_txs, size_t outputs
151165
* Max Txs with a WitnessV0KeyHash input is 8245
152166
* Max Txs with a WitnessV1Taproot input is 8986
153167
*/
154-
static void WalletSPScanMaxTxOneOutput(benchmark::Bench& bench) { WalletSPScan(bench, 8986, 1); }
155-
static void WalletSPScanOneTxMaxOutputs(benchmark::Bench& bench) { WalletSPScan(bench, 1, 23246); }
156-
static void WalletSPScanAvgTxAvgOutputs(benchmark::Bench& bench) { WalletSPScan(bench, 3200, 3); }
157-
static void WalletSPScanNoPayments(benchmark::Bench& bench) { WalletSPScan(bench, 1, 1000, 0); }
158-
static void WalletSPScanTenPayments(benchmark::Bench& bench) { WalletSPScan(bench, 1, 1000, 10); }
159-
BENCHMARK(WalletSPScanMaxTxOneOutput);
160-
BENCHMARK(WalletSPScanOneTxMaxOutputs);
161-
BENCHMARK(WalletSPScanAvgTxAvgOutputs);
162-
BENCHMARK(WalletSPScanNoPayments);
163-
BENCHMARK(WalletSPScanTenPayments);
168+
static void WalletParallelSPScan(benchmark::Bench& bench) {
169+
std::vector<size_t> threads = {1, 2, 4, 8};
170+
for (size_t n_threads: threads) {
171+
WalletSPScan(bench, 8986, 1, {}, n_threads);
172+
WalletSPScan(bench, 1, 23246, {}, n_threads);
173+
WalletSPScan(bench, 3200, 3, {}, n_threads);
174+
WalletSPScan(bench, 1, 1000, 0, n_threads);
175+
WalletSPScan(bench, 1, 1000, 10, n_threads);
176+
}
177+
}
178+
BENCHMARK(WalletParallelSPScan);
164179
} // namespace wallet

src/common/bip352.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <pubkey.h>
1111
#include <secp256k1.h>
1212
#include <span.h>
13+
#include <sync.h>
1314

1415
#include <secp256k1_silentpayments.h>
1516
#include <streams.h>
@@ -383,6 +384,7 @@ std::optional<std::vector<SilentPaymentOutput>> ScanForSilentPaymentOutputs(
383384
}
384385

385386
std::optional<std::vector<SilentPaymentOutput>> ParallelScanForSilentPaymentOutputs(
387+
ThreadPool& threadpool,
386388
const CKey& scan_key,
387389
const PrevoutsSummary& prevouts_summary,
388390
const CPubKey& recipient_spend_pubkey,
@@ -395,6 +397,10 @@ std::optional<std::vector<SilentPaymentOutput>> ParallelScanForSilentPaymentOutp
395397
found_outputs.reserve(tx_outputs.size());
396398
tx_output_objs.reserve(tx_outputs.size());
397399

400+
Mutex mutex;
401+
std::vector<std::future<void>> futures;
402+
futures.reserve(tx_outputs.size());
403+
398404
for (auto& tx_output: tx_outputs) {
399405
secp256k1_xonly_pubkey tx_output_obj;
400406
if (!secp256k1_xonly_pubkey_parse(

src/common/bip352.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <addresstype.h>
1313
#include <coins.h>
1414
#include <serialize.h>
15+
#include <util/threadpool.h>
1516

1617
#include <vector>
1718
#include <variant>
@@ -185,6 +186,7 @@ std::optional<PrevoutsSummary> GetSilentPaymentsPrevoutsSummary(const std::vecto
185186
std::optional<std::vector<SilentPaymentOutput>> ScanForSilentPaymentOutputs(const CKey& scan_key, const PrevoutsSummary& prevouts_summary, const CPubKey& spend_pubkey, const std::vector<XOnlyPubKey>& output_pub_keys, const std::map<SilentPaymentLabel, uint256>& labels);
186187

187188
std::optional<std::vector<SilentPaymentOutput>> ParallelScanForSilentPaymentOutputs(
189+
ThreadPool& m_threadpool,
188190
const CKey& scan_key,
189191
const PrevoutsSummary& prevouts_summary,
190192
const CPubKey& recipient_spend_pubkey,

src/wallet/scriptpubkeyman.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,8 +1696,15 @@ std::pair<bool, std::vector<bip352::SilentPaymentOutput>> SilentPaymentDescripto
16961696

16971697
assert(m_scan_key.IsValid());
16981698
assert(m_spend_pubkey.IsFullyValid());
1699-
auto found_outputs{bip352::ParallelScanForSilentPaymentOutputs(
1700-
m_scan_key, prevouts_summary, m_spend_pubkey, output_keys, {ChangeLabel()})};
1699+
std::optional<std::vector<bip352::SilentPaymentOutput>> found_outputs;
1700+
if (this->m_threadpool && this->m_threadpool->WorkersCount() > 0) {
1701+
found_outputs = bip352::ParallelScanForSilentPaymentOutputs(
1702+
*this->m_threadpool, m_scan_key, prevouts_summary,
1703+
m_spend_pubkey, output_keys, {ChangeLabel()});
1704+
} else {
1705+
found_outputs = bip352::ScanForSilentPaymentOutputs(
1706+
m_scan_key, prevouts_summary, m_spend_pubkey, output_keys, {ChangeLabel()});
1707+
}
17011708
if (!found_outputs.has_value()) {
17021709
return {false, {}};
17031710
}

src/wallet/scriptpubkeyman.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <script/script.h>
1919
#include <script/signingprovider.h>
2020
#include <util/result.h>
21+
#include <util/threadpool.h>
2122
#include <util/time.h>
2223
#include <wallet/crypter.h>
2324
#include <wallet/types.h>
@@ -423,6 +424,8 @@ class SilentPaymentDescriptorScriptPubKeyMan : public DescriptorScriptPubKeyMan
423424
CKey m_scan_key GUARDED_BY(cs_desc_man);
424425
CPubKey m_spend_pubkey GUARDED_BY(cs_desc_man);
425426

427+
ThreadPool* m_threadpool;
428+
426429
// Adds a tweak to m_map_spk_tweaks and writes to provided batch
427430
bool AddOutputWithDB(WalletBatch& batch, const bip352::SilentPaymentOutput& output);
428431

@@ -431,12 +434,12 @@ class SilentPaymentDescriptorScriptPubKeyMan : public DescriptorScriptPubKeyMan
431434
std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CScript& script, bool include_private = false) const override;
432435

433436
public:
434-
SilentPaymentDescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor)
435-
: DescriptorScriptPubKeyMan(storage, descriptor, 0)
437+
SilentPaymentDescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, ThreadPool* threadpool)
438+
: DescriptorScriptPubKeyMan(storage, descriptor, 0), m_threadpool(threadpool)
436439
{}
437440

438-
SilentPaymentDescriptorScriptPubKeyMan(WalletStorage& storage)
439-
: DescriptorScriptPubKeyMan(storage, 0)
441+
SilentPaymentDescriptorScriptPubKeyMan(WalletStorage& storage, ThreadPool* threadpool)
442+
: DescriptorScriptPubKeyMan(storage, 0), m_threadpool(threadpool)
440443
{}
441444

442445
util::Result<CTxDestination> GetNewDestination(const OutputType type) override;

src/wallet/wallet.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,7 +3708,7 @@ DescriptorScriptPubKeyMan& CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, Wa
37083708
if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
37093709
spk_manager = new ExternalSignerScriptPubKeyMan(*this, desc, m_keypool_size);
37103710
} else if (desc.descriptor->GetOutputType() == OutputType::SILENT_PAYMENTS) {
3711-
spk_manager = new SilentPaymentDescriptorScriptPubKeyMan(*this, desc);
3711+
spk_manager = new SilentPaymentDescriptorScriptPubKeyMan(*this, desc, this->m_thread_pool);
37123712
} else {
37133713
spk_manager = new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size);
37143714
}
@@ -3721,7 +3721,7 @@ DescriptorScriptPubKeyMan& CWallet::SetupDescriptorScriptPubKeyMan(WalletBatch&
37213721
AssertLockHeld(cs_wallet);
37223722
std::unique_ptr<DescriptorScriptPubKeyMan> spk_manager;
37233723
if (output_type == OutputType::SILENT_PAYMENTS) {
3724-
spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new SilentPaymentDescriptorScriptPubKeyMan(*this));
3724+
spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new SilentPaymentDescriptorScriptPubKeyMan(*this, this->m_thread_pool));
37253725
} else {
37263726
spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, m_keypool_size));
37273727
}
@@ -3935,7 +3935,7 @@ util::Result<std::reference_wrapper<DescriptorScriptPubKeyMan>> CWallet::AddWall
39353935
} else {
39363936
std::unique_ptr<DescriptorScriptPubKeyMan> new_spk_man;
39373937
if (desc.descriptor->GetOutputType() == OutputType::SILENT_PAYMENTS) {
3938-
new_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new SilentPaymentDescriptorScriptPubKeyMan(*this, desc));
3938+
new_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new SilentPaymentDescriptorScriptPubKeyMan(*this, desc, this->m_thread_pool));
39393939
} else {
39403940
new_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size));
39413941
}

0 commit comments

Comments
 (0)