Skip to content

Commit 3a4f8bc

Browse files
committed
bench: add benchmark for wallet 'AvailableCoins' function.
1 parent 48174c0 commit 3a4f8bc

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
@@ -132,11 +132,51 @@ static void WalletCreateTx(benchmark::Bench& bench, const OutputType output_type
132132
});
133133
}
134134

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

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

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

0 commit comments

Comments
 (0)