Bitcoin Core  21.99.0
P2P Digital Currency
coin_selection.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 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 <interfaces/chain.h>
7 #include <node/context.h>
8 #include <wallet/coinselection.h>
9 #include <wallet/wallet.h>
10 
11 #include <set>
12 
13 static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<std::unique_ptr<CWalletTx>>& wtxs)
14 {
15  static int nextLockTime = 0;
17  tx.nLockTime = nextLockTime++; // so all transactions get different hashes
18  tx.vout.resize(1);
19  tx.vout[0].nValue = nValue;
20  wtxs.push_back(std::make_unique<CWalletTx>(&wallet, MakeTransactionRef(std::move(tx))));
21 }
22 
23 // Simple benchmark for wallet coin selection. Note that it maybe be necessary
24 // to build up more complicated scenarios in order to get meaningful
25 // measurements of performance. From laanwj, "Wallet coin selection is probably
26 // the hardest, as you need a wider selection of scenarios, just testing the
27 // same one over and over isn't too useful. Generating random isn't useful
28 // either for measurements."
29 // (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
30 static void CoinSelection(benchmark::Bench& bench)
31 {
33  auto chain = interfaces::MakeChain(node);
34  CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
35  wallet.SetupLegacyScriptPubKeyMan();
36  std::vector<std::unique_ptr<CWalletTx>> wtxs;
37  LOCK(wallet.cs_wallet);
38 
39  // Add coins.
40  for (int i = 0; i < 1000; ++i) {
41  addCoin(1000 * COIN, wallet, wtxs);
42  }
43  addCoin(3 * COIN, wallet, wtxs);
44 
45  // Create coins
46  std::vector<COutput> coins;
47  for (const auto& wtx : wtxs) {
48  coins.emplace_back(wtx.get(), 0 /* iIn */, 6 * 24 /* nDepthIn */, true /* spendable */, true /* solvable */, true /* safe */);
49  }
50 
52  const CoinSelectionParams coin_selection_params(/* use_bnb= */ true, /* change_output_size= */ 34,
53  /* change_spend_size= */ 148, /* effective_feerate= */ CFeeRate(0),
54  /* long_term_feerate= */ CFeeRate(0), /* discard_feerate= */ CFeeRate(0),
55  /* tx_no_inputs_size= */ 0, /* avoid_partial= */ false);
56  bench.run([&] {
57  std::set<CInputCoin> setCoinsRet;
58  CAmount nValueRet;
59  bool bnb_used;
60  bool success = wallet.SelectCoinsMinConf(1003 * COIN, filter_standard, coins, setCoinsRet, nValueRet, coin_selection_params, bnb_used);
61  assert(success);
62  assert(nValueRet == 1003 * COIN);
63  assert(setCoinsRet.size() == 2);
64  });
65 }
66 
67 typedef std::set<CInputCoin> CoinSet;
71 std::vector<std::unique_ptr<CWalletTx>> wtxn;
72 
73 // Copied from src/wallet/test/coinselector_tests.cpp
74 static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>& set)
75 {
77  tx.vout.resize(nInput + 1);
78  tx.vout[nInput].nValue = nValue;
79  std::unique_ptr<CWalletTx> wtx = std::make_unique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
80  set.emplace_back();
81  set.back().Insert(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0, false);
82  wtxn.emplace_back(std::move(wtx));
83 }
84 // Copied from src/wallet/test/coinselector_tests.cpp
85 static CAmount make_hard_case(int utxos, std::vector<OutputGroup>& utxo_pool)
86 {
87  utxo_pool.clear();
88  CAmount target = 0;
89  for (int i = 0; i < utxos; ++i) {
90  target += (CAmount)1 << (utxos+i);
91  add_coin((CAmount)1 << (utxos+i), 2*i, utxo_pool);
92  add_coin(((CAmount)1 << (utxos+i)) + ((CAmount)1 << (utxos-1-i)), 2*i + 1, utxo_pool);
93  }
94  return target;
95 }
96 
97 static void BnBExhaustion(benchmark::Bench& bench)
98 {
99  // Setup
101  std::vector<OutputGroup> utxo_pool;
102  CoinSet selection;
103  CAmount value_ret = 0;
104  CAmount not_input_fees = 0;
105 
106  bench.run([&] {
107  // Benchmark
108  CAmount target = make_hard_case(17, utxo_pool);
109  SelectCoinsBnB(utxo_pool, target, 0, selection, value_ret, not_input_fees); // Should exhaust
110 
111  // Cleanup
112  utxo_pool.clear();
113  selection.clear();
114  });
115 }
116 
CreateDummyWalletDatabase
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1088
wallet.h
make_hard_case
static CAmount make_hard_case(int utxos, std::vector< OutputGroup > &utxo_pool)
Definition: coin_selection.cpp:85
CoinSelection
static void CoinSelection(benchmark::Bench &bench)
Definition: coin_selection.cpp:30
BENCHMARK
BENCHMARK(CoinSelection)
interfaces::MakeChain
std::unique_ptr< Chain > MakeChain(NodeContext &node)
Return implementation of Chain interface.
Definition: interfaces.cpp:719
addCoin
static void addCoin(const CAmount &nValue, const CWallet &wallet, std::vector< std::unique_ptr< CWalletTx >> &wtxs)
Definition: coin_selection.cpp:13
add_coin
static void add_coin(const CAmount &nValue, int nInput, std::vector< OutputGroup > &set)
Definition: coin_selection.cpp:74
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
wallet
Definition: interfaces.cpp:47
testChain
static auto testChain
Definition: coin_selection.cpp:69
testWallet
static CWallet testWallet(testChain.get(), "", CreateDummyWalletDatabase())
CoinEligibilityFilter
Definition: coinselection.h:59
chain.h
SelectCoinsBnB
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &target_value, const CAmount &cost_of_change, std::set< CInputCoin > &out_set, CAmount &value_ret, CAmount not_input_fees)
Definition: coinselection.cpp:66
context.h
CFeeRate
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
COutput::GetInputCoin
CInputCoin GetInputCoin() const
Definition: wallet.h:601
testNode
static NodeContext testNode
Definition: coin_selection.cpp:68
CMutableTransaction::nLockTime
uint32_t nLockTime
Definition: transaction.h:349
coinselection.h
CoinSelectionParams
Definition: wallet.h:607
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bench.h
CWallet::SetupLegacyScriptPubKeyMan
void SetupLegacyScriptPubKeyMan()
Make a LegacyScriptPubKeyMan and set it for all types, internal, and external.
Definition: wallet.cpp:4467
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
LOCK
#define LOCK(cs)
Definition: sync.h:232
CWallet
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:638
node
Definition: interfaces.cpp:66
COIN
static const CAmount COIN
Definition: amount.h:14
coin_selection_params
CoinSelectionParams coin_selection_params(false, 0, 0, CFeeRate(0), CFeeRate(0), CFeeRate(0), 0, false)
CoinSet
std::set< CInputCoin > CoinSet
Definition: coin_selection.cpp:67
filter_standard
CoinEligibilityFilter filter_standard(1, 6, 0)
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:38
COutput
Definition: wallet.h:563
wtxn
std::vector< std::unique_ptr< CWalletTx > > wtxn
Definition: coin_selection.cpp:71
BnBExhaustion
static void BnBExhaustion(benchmark::Bench &bench)
Definition: coin_selection.cpp:97
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))