Bitcoin ABC  0.24.7
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 <interfaces/chain.h>
8 #include <node/context.h>
9 #include <wallet/coinselection.h>
10 #include <wallet/wallet.h>
11 
12 #include <memory>
13 #include <set>
14 
15 static void addCoin(const Amount nValue, const CWallet &wallet,
16  std::vector<std::unique_ptr<CWalletTx>> &wtxs) {
17  static int nextLockTime = 0;
19  // so all transactions get different hashes
20  tx.nLockTime = nextLockTime++;
21  tx.vout.resize(1);
22  tx.vout[0].nValue = nValue;
23  wtxs.push_back(std::make_unique<CWalletTx>(
24  &wallet, MakeTransactionRef(std::move(tx))));
25 }
26 
27 // Simple benchmark for wallet coin selection. Note that it maybe be necessary
28 // to build up more complicated scenarios in order to get meaningful
29 // measurements of performance. From laanwj, "Wallet coin selection is probably
30 // the hardest, as you need a wider selection of scenarios, just testing the
31 // same one over and over isn't too useful. Generating random isn't useful
32 // either for measurements."
33 // (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
34 static void CoinSelection(benchmark::Bench &bench) {
36 
37  NodeContext node;
38  auto chain = interfaces::MakeChain(node, Params());
39  CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
41  std::vector<std::unique_ptr<CWalletTx>> wtxs;
42  LOCK(wallet.cs_wallet);
43 
44  // Add coins.
45  for (int i = 0; i < 1000; ++i) {
46  addCoin(1000 * COIN, wallet, wtxs);
47  }
48  addCoin(3 * COIN, wallet, wtxs);
49 
50  // Create groups
51  std::vector<OutputGroup> groups;
52  for (const auto &wtx : wtxs) {
53  COutput output(wtx.get(), 0 /* iIn */, 6 * 24 /* nDepthIn */,
54  true /* spendable */, true /* solvable */,
55  true /* safe */);
56  groups.emplace_back(output.GetInputCoin(), 6, false, 0, 0);
57  }
58 
61  true, 34, 148, CFeeRate(Amount::zero()), 0);
62  bench.run([&] {
63  std::set<CInputCoin> setCoinsRet;
64  Amount nValueRet;
65  bool bnb_used;
66  bool success = wallet.SelectCoinsMinConf(
67  1003 * COIN, filter_standard, groups, setCoinsRet, nValueRet,
68  coin_selection_params, bnb_used);
69  assert(success);
70  assert(nValueRet == 1003 * COIN);
71  assert(setCoinsRet.size() == 2);
72  });
73 }
74 
75 typedef std::set<CInputCoin> CoinSet;
76 std::vector<std::unique_ptr<CWalletTx>> wtxn;
77 
78 // Copied from src/wallet/test/coinselector_tests.cpp
79 static void add_coin(const CWallet &wallet, const Amount nValue, int nInput,
80  std::vector<OutputGroup> &set) {
82  tx.vout.resize(nInput + 1);
83  tx.vout[nInput].nValue = nValue;
84  auto wtx =
85  std::make_unique<CWalletTx>(&wallet, MakeTransactionRef(std::move(tx)));
86  set.emplace_back(
87  COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true,
88  0, 0);
89  wtxn.emplace_back(std::move(wtx));
90 }
91 
92 // Copied from src/wallet/test/coinselector_tests.cpp
93 static Amount make_hard_case(const CWallet &wallet, int utxos,
94  std::vector<OutputGroup> &utxo_pool) {
95  utxo_pool.clear();
96  Amount target = Amount::zero();
97  for (int i = 0; i < utxos; ++i) {
98  const Amount base = (int64_t(1) << (utxos + i)) * SATOSHI;
99  target += base;
100  add_coin(wallet, base, 2 * i, utxo_pool);
101  add_coin(wallet, base + (int64_t(1) << (utxos - 1 - i)) * SATOSHI,
102  2 * i + 1, utxo_pool);
103  }
104  return target;
105 }
106 
107 static void BnBExhaustion(benchmark::Bench &bench) {
109 
110  NodeContext node;
111  auto chain = interfaces::MakeChain(node, Params());
112  CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
113 
114  LOCK(wallet.cs_wallet);
115 
116  // Setup
118  std::vector<OutputGroup> utxo_pool;
119  CoinSet selection;
120  Amount value_ret = Amount::zero();
121  Amount not_input_fees = Amount::zero();
122 
123  bench.run([&] {
124  // Benchmark
125  Amount target = make_hard_case(wallet, 17, utxo_pool);
126  // Should exhaust
127  SelectCoinsBnB(utxo_pool, target, Amount::zero(), selection, value_ret,
128  not_input_fees);
129 
130  // Cleanup
131  utxo_pool.clear();
132  selection.clear();
133  });
134 }
135 
MakeTransactionRef
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:320
CreateDummyWalletDatabase
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1164
wallet.h
CoinSelection
static void CoinSelection(benchmark::Bench &bench)
Definition: coin_selection.cpp:34
BENCHMARK
BENCHMARK(CoinSelection)
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
CoinEligibilityFilter
Definition: coinselection.h:64
chainparams.h
chain.h
context.h
CWallet::SelectCoinsMinConf
bool SelectCoinsMinConf(const Amount nTargetValue, const CoinEligibilityFilter &eligibility_filter, std::vector< OutputGroup > groups, std::set< CInputCoin > &setCoinsRet, Amount &nValueRet, const CoinSelectionParams &coin_selection_params, bool &bnb_used) const
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
Definition: wallet.cpp:2666
CFeeRate
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
COIN
static constexpr Amount COIN
Definition: amount.h:154
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
COutput::GetInputCoin
CInputCoin GetInputCoin() const
Definition: wallet.h:648
SATOSHI
static constexpr Amount SATOSHI
Definition: amount.h:153
coinselection.h
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:23
CoinSelectionParams
Definition: wallet.h:653
coin_selection_params
CoinSelectionParams coin_selection_params(false, 0, 0, CFeeRate(Amount::zero()), 0)
make_hard_case
static Amount make_hard_case(const CWallet &wallet, int utxos, std::vector< OutputGroup > &utxo_pool)
Definition: coin_selection.cpp:93
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
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
Definition: chainparams.cpp:530
Amount
Definition: amount.h:19
bench.h
CWallet::SetupLegacyScriptPubKeyMan
void SetupLegacyScriptPubKeyMan()
Make a LegacyScriptPubKeyMan and set it for all types, internal, and external.
Definition: wallet.cpp:4891
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:281
LOCK
#define LOCK(cs)
Definition: sync.h:241
CWallet
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:678
CMutableTransaction::nLockTime
uint32_t nLockTime
Definition: transaction.h:283
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:508
CWallet::cs_wallet
RecursiveMutex cs_wallet
Definition: wallet.h:819
interfaces::MakeChain
std::unique_ptr< Chain > MakeChain(NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: chain.cpp:491
CoinSet
std::set< CInputCoin > CoinSet
Definition: coin_selection.cpp:75
add_coin
static void add_coin(const CWallet &wallet, const Amount nValue, int nInput, std::vector< OutputGroup > &set)
Definition: coin_selection.cpp:79
filter_standard
CoinEligibilityFilter filter_standard(1, 6, 0)
NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:36
COutput
Definition: wallet.h:597
wtxn
std::vector< std::unique_ptr< CWalletTx > > wtxn
Definition: coin_selection.cpp:76
BnBExhaustion
static void BnBExhaustion(benchmark::Bench &bench)
Definition: coin_selection.cpp:107
SelectCoinsBnB
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)
Definition: coinselection.cpp:72
addCoin
static void addCoin(const Amount nValue, const CWallet &wallet, std::vector< std::unique_ptr< CWalletTx >> &wtxs)
Definition: coin_selection.cpp:15
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:278