Bitcoin Core  0.21.1
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules
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(/* use_bnb= */ true, /* change_output_size= */ 34,
54  /* change_spend_size= */ 148, /* effective_feerate= */ CFeeRate(0),
55  /* long_term_feerate= */ CFeeRate(0), /* discard_feerate= */ CFeeRate(0),
56  /* tx_no_inputs_size= */ 0);
57  bench.run([&] {
58  std::set<CInputCoin> setCoinsRet;
59  CAmount nValueRet;
60  bool bnb_used;
61  bool success = wallet.SelectCoinsMinConf(1003 * COIN, filter_standard, groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used);
62  assert(success);
63  assert(nValueRet == 1003 * COIN);
64  assert(setCoinsRet.size() == 2);
65  });
66 }
67 
68 typedef std::set<CInputCoin> CoinSet;
70 static auto testChain = interfaces::MakeChain(testNode);
72 std::vector<std::unique_ptr<CWalletTx>> wtxn;
73 
74 // Copied from src/wallet/test/coinselector_tests.cpp
75 static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>& set)
76 {
78  tx.vout.resize(nInput + 1);
79  tx.vout[nInput].nValue = nValue;
80  std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
81  set.emplace_back(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0);
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 
void SetupLegacyScriptPubKeyMan()
Make a LegacyScriptPubKeyMan and set it for all types, internal, and external.
Definition: wallet.cpp:4381
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
CoinSelectionParams coin_selection_params(false, 0, 0, CFeeRate(0), CFeeRate(0), CFeeRate(0), 0)
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
#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:1067
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
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:633
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's benchmarking facility.
Definition: nanobench.h:583
static void BnBExhaustion(benchmark::Bench &bench)
BENCHMARK(CoinSelection)
static CWallet testWallet(testChain.get(),"", CreateDummyWalletDatabase())