Bitcoin ABC 0.32.4
P2P Digital Currency
coin_selection.cpp
Go to the documentation of this file.
1// Copyright (c) 2012-2016 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <bench/bench.h>
6#include <chainparams.h>
7#include <consensus/amount.h>
8#include <interfaces/chain.h>
9#include <node/context.h>
10#include <util/chaintype.h>
12#include <wallet/spend.h>
13#include <wallet/wallet.h>
14
15#include <memory>
16#include <set>
17
19
20static void addCoin(const Amount nValue, const CWallet &wallet,
21 std::vector<std::unique_ptr<CWalletTx>> &wtxs) {
22 static int nextLockTime = 0;
24 // so all transactions get different hashes
25 tx.nLockTime = nextLockTime++;
26 tx.vout.resize(1);
27 tx.vout[0].nValue = nValue;
28 wtxs.push_back(
29 std::make_unique<CWalletTx>(MakeTransactionRef(std::move(tx))));
30}
31
32// Simple benchmark for wallet coin selection. Note that it maybe be necessary
33// to build up more complicated scenarios in order to get meaningful
34// measurements of performance. From laanwj, "Wallet coin selection is probably
35// the hardest, as you need a wider selection of scenarios, just testing the
36// same one over and over isn't too useful. Generating random isn't useful
37// either for measurements."
38// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
39static void CoinSelection(benchmark::Bench &bench) {
41
43 auto chain = interfaces::MakeChain(node, Params());
44 CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
45 wallet.SetupLegacyScriptPubKeyMan();
46 std::vector<std::unique_ptr<CWalletTx>> wtxs;
47 LOCK(wallet.cs_wallet);
48
49 // Add coins.
50 for (int i = 0; i < 1000; ++i) {
51 addCoin(1000 * COIN, wallet, wtxs);
52 }
53 addCoin(3 * COIN, wallet, wtxs);
54
55 // Create coins
56 std::vector<COutput> coins;
57 coins.reserve(wtxs.size());
58 for (const auto &wtx : wtxs) {
59 coins.emplace_back(wallet, *wtx, 0 /* iIn */, 6 * 24 /* nDepthIn */,
60 true /* spendable */, true /* solvable */,
61 true /* safe */);
62 }
63
66 true, 34, 148, CFeeRate(Amount::zero()), 0, false);
67 bench.run([&] {
68 std::set<CInputCoin> setCoinsRet;
69 Amount nValueRet;
70 bool bnb_used;
71 bool success = SelectCoinsMinConf(wallet, 1003 * COIN, filter_standard,
72 coins, setCoinsRet, nValueRet,
73 coin_selection_params, bnb_used);
74 assert(success);
75 assert(nValueRet == 1003 * COIN);
76 assert(setCoinsRet.size() == 2);
77 });
78}
79
80typedef std::set<CInputCoin> CoinSet;
81std::vector<std::unique_ptr<CWalletTx>> wtxn;
82
83// Copied from src/wallet/test/coinselector_tests.cpp
84static void add_coin(const CWallet &wallet, const Amount nValue, int nInput,
85 std::vector<OutputGroup> &set) {
87 tx.vout.resize(nInput + 1);
88 tx.vout[nInput].nValue = nValue;
89 auto wtx = std::make_unique<CWalletTx>(MakeTransactionRef(std::move(tx)));
90 set.emplace_back();
91 set.back().Insert(
92 COutput(wallet, *wtx, nInput, 0, true, true, true).GetInputCoin(), 0,
93 true, false);
94 wtxn.emplace_back(std::move(wtx));
95}
96
97// Copied from src/wallet/test/coinselector_tests.cpp
98static Amount make_hard_case(const CWallet &wallet, int utxos,
99 std::vector<OutputGroup> &utxo_pool) {
100 utxo_pool.clear();
101 Amount target = Amount::zero();
102 for (int i = 0; i < utxos; ++i) {
103 const Amount base = (int64_t(1) << (utxos + i)) * SATOSHI;
104 target += base;
105 add_coin(wallet, base, 2 * i, utxo_pool);
106 add_coin(wallet, base + (int64_t(1) << (utxos - 1 - i)) * SATOSHI,
107 2 * i + 1, utxo_pool);
108 }
109 return target;
110}
111
112static void BnBExhaustion(benchmark::Bench &bench) {
114
116 auto chain = interfaces::MakeChain(node, Params());
117 CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
118
119 LOCK(wallet.cs_wallet);
120
121 // Setup
122 wallet.SetupLegacyScriptPubKeyMan();
123 std::vector<OutputGroup> utxo_pool;
124 CoinSet selection;
125 Amount value_ret = Amount::zero();
126 Amount not_input_fees = Amount::zero();
127
128 bench.run([&] {
129 // Benchmark
130 Amount target = make_hard_case(wallet, 17, utxo_pool);
131 // Should exhaust
132 SelectCoinsBnB(utxo_pool, target, Amount::zero(), selection, value_ret,
133 not_input_fees);
134
135 // Cleanup
136 utxo_pool.clear();
137 selection.clear();
138 });
139}
140
static constexpr Amount SATOSHI
Definition: amount.h:143
static constexpr Amount COIN
Definition: amount.h:144
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given BIP70 chain name.
Definition: chainparams.cpp:50
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
A mutable version of CTransaction.
Definition: transaction.h:274
std::vector< CTxOut > vout
Definition: transaction.h:277
Definition: spend.h:22
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:269
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
static void CoinSelection(benchmark::Bench &bench)
static void addCoin(const Amount nValue, const CWallet &wallet, std::vector< std::unique_ptr< CWalletTx > > &wtxs)
static void add_coin(const CWallet &wallet, const Amount nValue, int nInput, std::vector< OutputGroup > &set)
static void BnBExhaustion(benchmark::Bench &bench)
std::vector< std::unique_ptr< CWalletTx > > wtxn
std::set< CInputCoin > CoinSet
static Amount make_hard_case(const CWallet &wallet, int utxos, std::vector< OutputGroup > &utxo_pool)
BENCHMARK(CoinSelection)
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const Amount &target_value, const Amount &cost_of_change, std::set< CInputCoin > &out_set, Amount &value_ret, const Amount not_input_fees)
This is the Branch and Bound Coin Selection algorithm designed by Murch.
CoinSelectionParams coin_selection_params(false, 0, 0, CFeeRate(Amount::zero()), 0, false)
CoinEligibilityFilter filter_standard(1, 6)
std::unique_ptr< Chain > MakeChain(node::NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: interfaces.cpp:832
Definition: init.h:31
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:316
bool SelectCoinsMinConf(const CWallet &wallet, const Amount nTargetValue, const CoinEligibilityFilter &eligibility_filter, std::vector< COutput > coins, std::set< CInputCoin > &setCoinsRet, Amount &nValueRet, const CoinSelectionParams &coin_selection_params, bool &bnb_used)
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
Definition: spend.cpp:420
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
NodeContext struct containing references to chain state and connection state.
Definition: context.h:48
#define LOCK(cs)
Definition: sync.h:306
assert(!tx.IsCoinBase())
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1168