Bitcoin Core  0.20.99
P2P Digital Currency
coin_selection.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 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(MakeUnique<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 {
32  NodeContext node;
33  auto chain = interfaces::MakeChain(node);
34  CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
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 groups
46  std::vector<OutputGroup> groups;
47  for (const auto& wtx : wtxs) {
48  COutput output(wtx.get(), 0 /* iIn */, 6 * 24 /* nDepthIn */, true /* spendable */, true /* solvable */, true /* safe */);
49  groups.emplace_back(output.GetInputCoin(), 6, false, 0, 0);
50  }
51 
53  const CoinSelectionParams coin_selection_params(true, 34, 148, CFeeRate(0), 0);
54  bench.run([&] {
55  std::set<CInputCoin> setCoinsRet;
56  CAmount nValueRet;
57  bool bnb_used;
58  bool success = wallet.SelectCoinsMinConf(1003 * COIN, filter_standard, groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used);
59  assert(success);
60  assert(nValueRet == 1003 * COIN);
61  assert(setCoinsRet.size() == 2);
62  });
63 }
64 
65 typedef std::set<CInputCoin> CoinSet;
67 static auto testChain = interfaces::MakeChain(testNode);
69 std::vector<std::unique_ptr<CWalletTx>> wtxn;
70 
71 // Copied from src/wallet/test/coinselector_tests.cpp
72 static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>& set)
73 {
75  tx.vout.resize(nInput + 1);
76  tx.vout[nInput].nValue = nValue;
77  std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
78  set.emplace_back(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0);
79  wtxn.emplace_back(std::move(wtx));
80 }
81 // Copied from src/wallet/test/coinselector_tests.cpp
82 static CAmount make_hard_case(int utxos, std::vector<OutputGroup>& utxo_pool)
83 {
84  utxo_pool.clear();
85  CAmount target = 0;
86  for (int i = 0; i < utxos; ++i) {
87  target += (CAmount)1 << (utxos+i);
88  add_coin((CAmount)1 << (utxos+i), 2*i, utxo_pool);
89  add_coin(((CAmount)1 << (utxos+i)) + ((CAmount)1 << (utxos-1-i)), 2*i + 1, utxo_pool);
90  }
91  return target;
92 }
93 
94 static void BnBExhaustion(benchmark::Bench& bench)
95 {
96  // Setup
98  std::vector<OutputGroup> utxo_pool;
99  CoinSet selection;
100  CAmount value_ret = 0;
101  CAmount not_input_fees = 0;
102 
103  bench.run([&] {
104  // Benchmark
105  CAmount target = make_hard_case(17, utxo_pool);
106  SelectCoinsBnB(utxo_pool, target, 0, selection, value_ret, not_input_fees); // Should exhaust
107 
108  // Cleanup
109  utxo_pool.clear();
110  selection.clear();
111  });
112 }
113 
void SetupLegacyScriptPubKeyMan()
Make a LegacyScriptPubKeyMan and set it for all types, internal, and external.
Definition: wallet.cpp:4400
std::set< CInputCoin > CoinSet
static const CAmount COIN
Definition: amount.h:14
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)
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:396
CoinEligibilityFilter filter_standard(1, 6, 0)
std::vector< std::unique_ptr< CWalletTx > > wtxn
std::unique_ptr< Chain > MakeChain(NodeContext &node)
Return implementation of Chain interface.
Definition: chain.cpp:415
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
NodeContext struct containing references to chain state and connection state.
Definition: context.h:36
static CWallet testWallet(testChain.get(), "", CreateDummyWalletDatabase())
#define LOCK(cs)
Definition: sync.h:230
static CAmount make_hard_case(int utxos, std::vector< OutputGroup > &utxo_pool)
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1059
std::vector< CTxOut > vout
Definition: transaction.h:356
static void add_coin(const CAmount &nValue, int nInput, std::vector< OutputGroup > &set)
static void addCoin(const CAmount &nValue, const CWallet &wallet, std::vector< std::unique_ptr< CWalletTx >> &wtxs)
static auto testChain
static NodeContext testNode
CoinSelectionParams coin_selection_params(false, 0, 0, CFeeRate(0), 0)
static void CoinSelection(benchmark::Bench &bench)
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:622
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
A mutable version of CTransaction.
Definition: transaction.h:353
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:583
static void BnBExhaustion(benchmark::Bench &bench)
BENCHMARK(CoinSelection)