Skip to content

Commit

Permalink
bench: Have AvailableCoins benchmark include a lot of unrelated utxos
Browse files Browse the repository at this point in the history
One of the main issues with AvailableCoins is its performance when txs
have unrelated outputs, so update the benchmark to check the performance
of that.
  • Loading branch information
achow101 committed Sep 7, 2023
1 parent bfe5f61 commit 5150a95
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/bench/wallet_create_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,17 @@ void generateFakeBlock(const CChainParams& params,
coinbase_tx.vin[0].prevout.SetNull();
coinbase_tx.vout.resize(2);
coinbase_tx.vout[0].scriptPubKey = coinbase_out_script;
coinbase_tx.vout[0].nValue = 49 * COIN;
coinbase_tx.vout[0].nValue = 48 * COIN;
coinbase_tx.vin[0].scriptSig = CScript() << ++tip.tip_height << OP_0;
coinbase_tx.vout[1].scriptPubKey = coinbase_out_script; // extra output
coinbase_tx.vout[1].nValue = 1 * COIN;

// Fill the coinbase with outputs that don't belong to the wallet in order to benchmark
// AvailableCoins' behavior with unnecessary TXOs
for (int i = 0; i < 50; ++i) {
coinbase_tx.vout.push_back(CTxOut(1 * COIN / 50, CScript(OP_TRUE)));
}

block.vtx = {MakeTransactionRef(std::move(coinbase_tx))};

block.nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
Expand Down Expand Up @@ -105,14 +112,14 @@ static void WalletCreateTx(benchmark::Bench& bench, const OutputType output_type

// Check available balance
auto bal = WITH_LOCK(wallet.cs_wallet, return wallet::AvailableCoins(wallet).GetTotalAmount()); // Cache
assert(bal == 50 * COIN * (chain_size - COINBASE_MATURITY));
assert(bal == 49 * COIN * (chain_size - COINBASE_MATURITY));

wallet::CCoinControl coin_control;
coin_control.m_allow_other_inputs = allow_other_inputs;

CAmount target = 0;
if (preset_inputs) {
// Select inputs, each has 49 BTC
// Select inputs, each has 48 BTC
wallet::CoinFilterParams filter_coins;
filter_coins.max_count = preset_inputs->num_of_internal_inputs;
const auto& res = WITH_LOCK(wallet.cs_wallet,
Expand Down Expand Up @@ -144,8 +151,8 @@ static void AvailableCoins(benchmark::Bench& bench, const std::vector<OutputType
{
LOCK(wallet.cs_wallet);
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
wallet.SetupDescriptorScriptPubKeyMans();
if (wallet.LoadWallet() != DBErrors::LOAD_OK) assert(false);
wallet.SetupDescriptorScriptPubKeyMans();
}

// Generate destinations
Expand All @@ -166,7 +173,7 @@ static void AvailableCoins(benchmark::Bench& bench, const std::vector<OutputType

// Check available balance
auto bal = WITH_LOCK(wallet.cs_wallet, return wallet::AvailableCoins(wallet).GetTotalAmount()); // Cache
assert(bal == 50 * COIN * (chain_size - COINBASE_MATURITY));
assert(bal == 49 * COIN * (chain_size - COINBASE_MATURITY));

bench.epochIterations(2).run([&] {
LOCK(wallet.cs_wallet);
Expand All @@ -185,4 +192,4 @@ static void WalletAvailableCoins(benchmark::Bench& bench) { AvailableCoins(bench

BENCHMARK(WalletCreateTxUseOnlyPresetInputs, benchmark::PriorityLevel::LOW)
BENCHMARK(WalletCreateTxUsePresetInputsAndCoinSelection, benchmark::PriorityLevel::LOW)
BENCHMARK(WalletAvailableCoins, benchmark::PriorityLevel::LOW);
BENCHMARK(WalletAvailableCoins, benchmark::PriorityLevel::LOW);

0 comments on commit 5150a95

Please sign in to comment.