From 3a4f8bc24271d05765e9bf1e26558a28ab2e6b81 Mon Sep 17 00:00:00 2001 From: furszy Date: Mon, 14 Nov 2022 10:55:10 -0300 Subject: [PATCH] bench: add benchmark for wallet 'AvailableCoins' function. --- src/bench/wallet_create_tx.cpp | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/bench/wallet_create_tx.cpp b/src/bench/wallet_create_tx.cpp index 207b22c5845..e33b1509a66 100644 --- a/src/bench/wallet_create_tx.cpp +++ b/src/bench/wallet_create_tx.cpp @@ -132,11 +132,51 @@ static void WalletCreateTx(benchmark::Bench& bench, const OutputType output_type }); } +static void AvailableCoins(benchmark::Bench& bench, const std::vector& output_type) +{ + const auto test_setup = MakeNoLogFileContext(); + CWallet wallet{test_setup->m_node.chain.get(), "", gArgs, CreateMockWalletDatabase()}; + { + LOCK(wallet.cs_wallet); + wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS); + wallet.SetupDescriptorScriptPubKeyMans(); + if (wallet.LoadWallet() != DBErrors::LOAD_OK) assert(false); + } + + // Generate destinations + std::vector dest_wallet; + for (auto type : output_type) { + dest_wallet.emplace_back(GetScriptForDestination(getNewDestination(wallet, type))); + } + + // Generate chain; each coinbase will have two outputs to fill-up the wallet + const auto& params = Params(); + unsigned int chain_size = 1000; + for (unsigned int i = 0; i < chain_size / dest_wallet.size(); ++i) { + for (const auto& dest : dest_wallet) { + generateFakeBlock(params, test_setup->m_node, wallet, dest); + } + } + + // Check available balance + auto bal = wallet::GetAvailableBalance(wallet); // Cache + assert(bal == 50 * COIN * (chain_size - COINBASE_MATURITY)); + + bench.epochIterations(2).run([&] { + LOCK(wallet.cs_wallet); + const auto& res = wallet::AvailableCoins(wallet); + assert(res.All().size() == (chain_size - COINBASE_MATURITY) * 2); + }); +} + static void WalletCreateTxUseOnlyPresetInputs(benchmark::Bench& bench) { WalletCreateTx(bench, OutputType::BECH32, /*allow_other_inputs=*/false, {{/*num_of_internal_inputs=*/4}}); } static void WalletCreateTxUsePresetInputsAndCoinSelection(benchmark::Bench& bench) { WalletCreateTx(bench, OutputType::BECH32, /*allow_other_inputs=*/true, {{/*num_of_internal_inputs=*/4}}); } +static void WalletAvailableCoins(benchmark::Bench& bench) { AvailableCoins(bench, {OutputType::BECH32M}); } + BENCHMARK(WalletCreateTxUseOnlyPresetInputs, benchmark::PriorityLevel::LOW) BENCHMARK(WalletCreateTxUsePresetInputsAndCoinSelection, benchmark::PriorityLevel::LOW) +BENCHMARK(WalletAvailableCoins, benchmark::PriorityLevel::LOW); \ No newline at end of file