2019-12-30 06:39:22 -03:00
|
|
|
// Copyright (c) 2012-2019 The Bitcoin Core developers
|
2016-10-02 18:38:48 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <bench/bench.h>
|
2017-05-30 15:55:17 -04:00
|
|
|
#include <interfaces/chain.h>
|
2019-09-17 19:28:03 -03:00
|
|
|
#include <node/context.h>
|
2018-03-06 16:24:15 -03:00
|
|
|
#include <wallet/coinselection.h>
|
2018-11-27 18:53:49 -03:00
|
|
|
#include <wallet/wallet.h>
|
2016-10-02 18:38:48 -03:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
2018-11-27 18:53:49 -03:00
|
|
|
static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<std::unique_ptr<CWalletTx>>& wtxs)
|
2016-10-02 18:38:48 -03:00
|
|
|
{
|
|
|
|
static int nextLockTime = 0;
|
|
|
|
CMutableTransaction tx;
|
|
|
|
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
|
2018-11-27 18:53:49 -03:00
|
|
|
tx.vout.resize(1);
|
|
|
|
tx.vout[0].nValue = nValue;
|
|
|
|
wtxs.push_back(MakeUnique<CWalletTx>(&wallet, MakeTransactionRef(std::move(tx))));
|
2016-10-02 18:38:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2019-09-17 19:28:03 -03:00
|
|
|
NodeContext node;
|
|
|
|
auto chain = interfaces::MakeChain(node);
|
2020-06-17 12:30:37 -04:00
|
|
|
CWallet wallet(chain.get(), WalletLocation(), CreateDummyWalletDatabase());
|
2019-10-07 15:11:34 -03:00
|
|
|
wallet.SetupLegacyScriptPubKeyMan();
|
2018-11-27 18:53:49 -03:00
|
|
|
std::vector<std::unique_ptr<CWalletTx>> wtxs;
|
2016-10-02 18:38:48 -03:00
|
|
|
LOCK(wallet.cs_wallet);
|
|
|
|
|
2018-06-28 06:48:27 -04:00
|
|
|
// Add coins.
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
2018-11-27 18:53:49 -03:00
|
|
|
addCoin(1000 * COIN, wallet, wtxs);
|
|
|
|
}
|
|
|
|
addCoin(3 * COIN, wallet, wtxs);
|
|
|
|
|
|
|
|
// Create groups
|
|
|
|
std::vector<OutputGroup> groups;
|
|
|
|
for (const auto& wtx : wtxs) {
|
|
|
|
COutput output(wtx.get(), 0 /* iIn */, 6 * 24 /* nDepthIn */, true /* spendable */, true /* solvable */, true /* safe */);
|
|
|
|
groups.emplace_back(output.GetInputCoin(), 6, false, 0, 0);
|
2018-06-28 06:48:27 -04:00
|
|
|
}
|
2016-10-02 18:38:48 -03:00
|
|
|
|
2018-06-28 06:48:27 -04:00
|
|
|
const CoinEligibilityFilter filter_standard(1, 6, 0);
|
|
|
|
const CoinSelectionParams coin_selection_params(true, 34, 148, CFeeRate(0), 0);
|
|
|
|
while (state.KeepRunning()) {
|
2017-04-07 06:38:33 -03:00
|
|
|
std::set<CInputCoin> setCoinsRet;
|
2016-10-02 18:38:48 -03:00
|
|
|
CAmount nValueRet;
|
2018-03-05 18:42:49 -03:00
|
|
|
bool bnb_used;
|
2018-07-18 22:45:26 -04:00
|
|
|
bool success = wallet.SelectCoinsMinConf(1003 * COIN, filter_standard, groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used);
|
2016-10-02 18:38:48 -03:00
|
|
|
assert(success);
|
|
|
|
assert(nValueRet == 1003 * COIN);
|
|
|
|
assert(setCoinsRet.size() == 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:24:15 -03:00
|
|
|
typedef std::set<CInputCoin> CoinSet;
|
2019-09-17 19:28:03 -03:00
|
|
|
static NodeContext testNode;
|
|
|
|
static auto testChain = interfaces::MakeChain(testNode);
|
2020-06-17 12:30:37 -04:00
|
|
|
static CWallet testWallet(testChain.get(), WalletLocation(), CreateDummyWalletDatabase());
|
2018-07-18 22:45:26 -04:00
|
|
|
std::vector<std::unique_ptr<CWalletTx>> wtxn;
|
2018-03-06 16:24:15 -03:00
|
|
|
|
|
|
|
// Copied from src/wallet/test/coinselector_tests.cpp
|
2018-07-18 22:45:26 -04:00
|
|
|
static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>& set)
|
2018-03-06 16:24:15 -03:00
|
|
|
{
|
|
|
|
CMutableTransaction tx;
|
|
|
|
tx.vout.resize(nInput + 1);
|
|
|
|
tx.vout[nInput].nValue = nValue;
|
2018-09-21 06:03:21 -03:00
|
|
|
std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
|
2018-07-18 22:45:26 -04:00
|
|
|
set.emplace_back(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0);
|
|
|
|
wtxn.emplace_back(std::move(wtx));
|
2018-03-06 16:24:15 -03:00
|
|
|
}
|
|
|
|
// Copied from src/wallet/test/coinselector_tests.cpp
|
2018-07-18 22:45:26 -04:00
|
|
|
static CAmount make_hard_case(int utxos, std::vector<OutputGroup>& utxo_pool)
|
2018-03-06 16:24:15 -03:00
|
|
|
{
|
|
|
|
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
|
2019-10-07 15:11:34 -03:00
|
|
|
testWallet.SetupLegacyScriptPubKeyMan();
|
2018-07-18 22:45:26 -04:00
|
|
|
std::vector<OutputGroup> utxo_pool;
|
2018-03-06 16:24:15 -03:00
|
|
|
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 11:48:02 -03:00
|
|
|
BENCHMARK(CoinSelection, 650);
|
2018-03-06 16:24:15 -03:00
|
|
|
BENCHMARK(BnBExhaustion, 650);
|