Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

coin_selection.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2015 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 <wallet/wallet.h>
7 
8 #include <set>
9 
10 static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<COutput>& vCoins)
11 {
12  int nInput = 0;
13 
14  static int nextLockTime = 0;
16  tx.nLockTime = nextLockTime++; // so all transactions get different hashes
17  tx.vout.resize(nInput + 1);
18  tx.vout[nInput].nValue = nValue;
19  CWalletTx* wtx = new CWalletTx(&wallet, MakeTransactionRef(std::move(tx)));
20 
21  int nAge = 6 * 24;
22  COutput output(wtx, nInput, nAge, true /* spendable */, true /* solvable */, true /* safe */);
23  vCoins.push_back(output);
24 }
25 
26 // Simple benchmark for wallet coin selection. Note that it maybe be necessary
27 // to build up more complicated scenarios in order to get meaningful
28 // measurements of performance. From laanwj, "Wallet coin selection is probably
29 // the hardest, as you need a wider selection of scenarios, just testing the
30 // same one over and over isn't too useful. Generating random isn't useful
31 // either for measurements."
32 // (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
33 static void CoinSelection(benchmark::State& state)
34 {
35  const CWallet wallet("dummy", WalletDatabase::CreateDummy());
36  std::vector<COutput> vCoins;
37  LOCK(wallet.cs_wallet);
38 
39  while (state.KeepRunning()) {
40  // Empty wallet.
41  for (COutput output : vCoins)
42  delete output.tx;
43  vCoins.clear();
44 
45  // Add coins.
46  for (int i = 0; i < 1000; i++)
47  addCoin(1000 * COIN, wallet, vCoins);
48  addCoin(3 * COIN, wallet, vCoins);
49 
50  std::set<CInputCoin> setCoinsRet;
51  CAmount nValueRet;
52  bool success = wallet.SelectCoinsMinConf(1003 * COIN, 1, 6, 0, vCoins, setCoinsRet, nValueRet);
53  assert(success);
54  assert(nValueRet == 1003 * COIN);
55  assert(setCoinsRet.size() == 2);
56  }
57 }
58 
static std::unique_ptr< BerkeleyDatabase > CreateDummy()
Return object for accessing dummy database with no read/write capabilities.
Definition: db.h:123
bool SelectCoinsMinConf(const CAmount &nTargetValue, int nConfMine, int nConfTheirs, uint64_t nMaxAncestors, std::vector< COutput > vCoins, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet, CoinType nCoinType=CoinType::ALL_COINS) const
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
Definition: wallet.cpp:3025
CCriticalSection cs_wallet
Definition: wallet.h:836
static const CAmount COIN
Definition: amount.h:14
bool KeepRunning()
Definition: bench.cpp:39
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:346
#define LOCK(cs)
Definition: sync.h:178
std::vector< CTxOut > vout
Definition: transaction.h:294
static void addCoin(const CAmount &nValue, const CWallet &wallet, std::vector< COutput > &vCoins)
static std::vector< COutput > vCoins
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:280
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:715
A mutable version of CTransaction.
Definition: transaction.h:291
static void CoinSelection(benchmark::State &state)
BENCHMARK(CoinSelection)
Released under the MIT license