2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2012-2017 The Bitcoin Core developers
|
2016-10-02 23:38:48 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <bench/bench.h>
|
|
|
|
#include <wallet/wallet.h>
|
2018-03-06 20:24:15 +01:00
|
|
|
#include <wallet/coinselection.h>
|
2016-10-02 23:38:48 +02:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
2016-12-05 08:03:53 +01:00
|
|
|
static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<COutput>& vCoins)
|
2016-10-02 23:38:48 +02:00
|
|
|
{
|
|
|
|
int nInput = 0;
|
|
|
|
|
|
|
|
static int nextLockTime = 0;
|
|
|
|
CMutableTransaction tx;
|
|
|
|
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
|
|
|
|
tx.vout.resize(nInput + 1);
|
|
|
|
tx.vout[nInput].nValue = nValue;
|
2016-11-12 01:54:51 +01:00
|
|
|
CWalletTx* wtx = new CWalletTx(&wallet, MakeTransactionRef(std::move(tx)));
|
2016-10-02 23:38:48 +02:00
|
|
|
|
|
|
|
int nAge = 6 * 24;
|
2017-02-23 17:20:16 +01:00
|
|
|
COutput output(wtx, nInput, nAge, true /* spendable */, true /* solvable */, true /* safe */);
|
2016-10-02 23:38:48 +02:00
|
|
|
vCoins.push_back(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple benchmark for wallet coin selection. Note that it maybe be necessary
|
|
|
|
// to build up more complicated scenarios in order to get meaningful
|
|
|
|
// measurements of performance. From laanwj, "Wallet coin selection is probably
|
|
|
|
// the hardest, as you need a wider selection of scenarios, just testing the
|
|
|
|
// same one over and over isn't too useful. Generating random isn't useful
|
|
|
|
// either for measurements."
|
|
|
|
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
|
|
|
|
static void CoinSelection(benchmark::State& state)
|
|
|
|
{
|
2017-12-08 12:39:22 +01:00
|
|
|
const CWallet wallet("dummy", WalletDatabase::CreateDummy());
|
2016-12-05 08:03:53 +01:00
|
|
|
std::vector<COutput> vCoins;
|
2016-10-02 23:38:48 +02:00
|
|
|
LOCK(wallet.cs_wallet);
|
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
// Add coins.
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
addCoin(1000 * COIN, wallet, vCoins);
|
|
|
|
addCoin(3 * COIN, wallet, vCoins);
|
|
|
|
|
2017-04-07 11:38:33 +02:00
|
|
|
std::set<CInputCoin> setCoinsRet;
|
2016-10-02 23:38:48 +02:00
|
|
|
CAmount nValueRet;
|
2018-03-05 22:42:49 +01:00
|
|
|
bool bnb_used;
|
2018-03-10 05:11:20 +01:00
|
|
|
CoinEligibilityFilter filter_standard(1, 6, 0);
|
2018-03-05 22:42:49 +01:00
|
|
|
CoinSelectionParams coin_selection_params(false, 34, 148, CFeeRate(0), 0);
|
|
|
|
bool success = wallet.SelectCoinsMinConf(1003 * COIN, filter_standard, vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)
|
|
|
|
|| wallet.SelectCoinsMinConf(1003 * COIN, filter_standard, vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used);
|
2016-10-02 23:38:48 +02:00
|
|
|
assert(success);
|
|
|
|
assert(nValueRet == 1003 * COIN);
|
|
|
|
assert(setCoinsRet.size() == 2);
|
2018-02-17 20:29:56 +01:00
|
|
|
|
|
|
|
// Empty wallet.
|
|
|
|
for (COutput& output : vCoins) {
|
|
|
|
delete output.tx;
|
|
|
|
}
|
|
|
|
vCoins.clear();
|
2016-10-02 23:38:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 20:24:15 +01:00
|
|
|
typedef std::set<CInputCoin> CoinSet;
|
|
|
|
|
|
|
|
// Copied from src/wallet/test/coinselector_tests.cpp
|
|
|
|
static void add_coin(const CAmount& nValue, int nInput, std::vector<CInputCoin>& set)
|
|
|
|
{
|
|
|
|
CMutableTransaction tx;
|
|
|
|
tx.vout.resize(nInput + 1);
|
|
|
|
tx.vout[nInput].nValue = nValue;
|
|
|
|
set.emplace_back(MakeTransactionRef(tx), nInput);
|
|
|
|
}
|
|
|
|
// Copied from src/wallet/test/coinselector_tests.cpp
|
|
|
|
static CAmount make_hard_case(int utxos, std::vector<CInputCoin>& utxo_pool)
|
|
|
|
{
|
|
|
|
utxo_pool.clear();
|
|
|
|
CAmount target = 0;
|
|
|
|
for (int i = 0; i < utxos; ++i) {
|
|
|
|
target += (CAmount)1 << (utxos+i);
|
|
|
|
add_coin((CAmount)1 << (utxos+i), 2*i, utxo_pool);
|
|
|
|
add_coin(((CAmount)1 << (utxos+i)) + ((CAmount)1 << (utxos-1-i)), 2*i + 1, utxo_pool);
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BnBExhaustion(benchmark::State& state)
|
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
std::vector<CInputCoin> utxo_pool;
|
|
|
|
CoinSet selection;
|
|
|
|
CAmount value_ret = 0;
|
|
|
|
CAmount not_input_fees = 0;
|
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
// Benchmark
|
|
|
|
CAmount target = make_hard_case(17, utxo_pool);
|
|
|
|
SelectCoinsBnB(utxo_pool, target, 0, selection, value_ret, not_input_fees); // Should exhaust
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
utxo_pool.clear();
|
|
|
|
selection.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:48:02 +02:00
|
|
|
BENCHMARK(CoinSelection, 650);
|
2018-03-06 20:24:15 +01:00
|
|
|
BENCHMARK(BnBExhaustion, 650);
|