Skip to content

Commit 139ba2b

Browse files
committed
Merge bitcoin/bitcoin#25234: bench: add benchmark for wallet 'AvailableCoins' function.
3a4f8bc bench: add benchmark for wallet 'AvailableCoins' function. (furszy) Pull request description: #### Rationale `AvailableCoins` is part of several important flows for the wallet; from RPC commands that create transactions like `fundrawtransaction`, `send`, `walletcreatefundedpsbt`, get the available balance, list the available coins with `listunspent` etc. to GUI connected processes that perform the same or similar actions: tx creation, available balance calculation, present the spendable coins in the coin control dialog. As we are improving this process in #24699, #25005 and there are more structural changes coming on the way. This benchmark aims to ensure us that, at least, there are no regressions (obviously performance improvements are great but, at least for me, this heads into the direction of having a base metric to compare future structural changes). #### Implementation Notes There are 5 new benchmarks, one per wallet supported output type (LEGACY, P2SH_SEGWIT, BECH32, BECH32M), plus a multi-output-type wallet benchmark which contains outputs from all the descriptor types. The test, by default, fills-up the wallet with 1k transactions, 2k outputs. Mainly to not consume much time if the user just want to verify that no substantial regressions were introduced. But, my expectation for those who are focused on this process is to use a much higher number locally to really note the differences across commits. ACKs for top commit: achow101: ACK 3a4f8bc hernanmarino: ACK 3a4f8bc aureleoules: ACK 3a4f8bc Tree-SHA512: d0bb4c165f1efa181b47cb31561e6217eff9135bcd1b6761a7292f9018e456d13d18a1b886c2e2268d35c52f9e1fd8e0f252972424e5c5f00c280620b79c5a1b
2 parents bf3b589 + 3a4f8bc commit 139ba2b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/bench/wallet_create_tx.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,51 @@ static void WalletCreateTx(benchmark::Bench& bench, const OutputType output_type
133133
});
134134
}
135135

136+
static void AvailableCoins(benchmark::Bench& bench, const std::vector<OutputType>& output_type)
137+
{
138+
const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
139+
CWallet wallet{test_setup->m_node.chain.get(), "", gArgs, CreateMockWalletDatabase()};
140+
{
141+
LOCK(wallet.cs_wallet);
142+
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
143+
wallet.SetupDescriptorScriptPubKeyMans();
144+
if (wallet.LoadWallet() != DBErrors::LOAD_OK) assert(false);
145+
}
146+
147+
// Generate destinations
148+
std::vector<CScript> dest_wallet;
149+
for (auto type : output_type) {
150+
dest_wallet.emplace_back(GetScriptForDestination(getNewDestination(wallet, type)));
151+
}
152+
153+
// Generate chain; each coinbase will have two outputs to fill-up the wallet
154+
const auto& params = Params();
155+
unsigned int chain_size = 1000;
156+
for (unsigned int i = 0; i < chain_size / dest_wallet.size(); ++i) {
157+
for (const auto& dest : dest_wallet) {
158+
generateFakeBlock(params, test_setup->m_node, wallet, dest);
159+
}
160+
}
161+
162+
// Check available balance
163+
auto bal = wallet::GetAvailableBalance(wallet); // Cache
164+
assert(bal == 50 * COIN * (chain_size - COINBASE_MATURITY));
165+
166+
bench.epochIterations(2).run([&] {
167+
LOCK(wallet.cs_wallet);
168+
const auto& res = wallet::AvailableCoins(wallet);
169+
assert(res.All().size() == (chain_size - COINBASE_MATURITY) * 2);
170+
});
171+
}
172+
136173
static void WalletCreateTxUseOnlyPresetInputs(benchmark::Bench& bench) { WalletCreateTx(bench, OutputType::BECH32, /*allow_other_inputs=*/false,
137174
{{/*num_of_internal_inputs=*/4}}); }
138175

139176
static void WalletCreateTxUsePresetInputsAndCoinSelection(benchmark::Bench& bench) { WalletCreateTx(bench, OutputType::BECH32, /*allow_other_inputs=*/true,
140177
{{/*num_of_internal_inputs=*/4}}); }
141178

179+
static void WalletAvailableCoins(benchmark::Bench& bench) { AvailableCoins(bench, {OutputType::BECH32M}); }
180+
142181
BENCHMARK(WalletCreateTxUseOnlyPresetInputs, benchmark::PriorityLevel::LOW)
143182
BENCHMARK(WalletCreateTxUsePresetInputsAndCoinSelection, benchmark::PriorityLevel::LOW)
183+
BENCHMARK(WalletAvailableCoins, benchmark::PriorityLevel::LOW);

0 commit comments

Comments
 (0)