Raven Core  3.0.0
P2P Digital Currency
coin_selection.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "bench.h"
7 #include "wallet/wallet.h"
8 
9 #include <set>
10 
11 static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<COutput>& vCoins)
12 {
13  int nInput = 0;
14 
15  static int nextLockTime = 0;
17  tx.nLockTime = nextLockTime++; // so all transactions get different hashes
18  tx.vout.resize(nInput + 1);
19  tx.vout[nInput].nValue = nValue;
20  CWalletTx* wtx = new CWalletTx(&wallet, MakeTransactionRef(std::move(tx)));
21 
22  int nAge = 6 * 24;
23  COutput output(wtx, nInput, nAge, true /* spendable */, true /* solvable */, true /* safe */);
24  vCoins.push_back(output);
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/RavenProject/Ravencoin/issues/7883#issuecomment-224807484)
34 static void CoinSelection(benchmark::State& state)
35 {
36  const CWallet wallet;
37  std::vector<COutput> vCoins;
38  LOCK(wallet.cs_wallet);
39 
40  while (state.KeepRunning()) {
41  // Empty wallet.
42  for (COutput output : vCoins)
43  delete output.tx;
44  vCoins.clear();
45 
46  // Add coins.
47  for (int i = 0; i < 1000; i++)
48  addCoin(1000 * COIN, wallet, vCoins);
49  addCoin(3 * COIN, wallet, vCoins);
50 
51  std::set<CInputCoin> setCoinsRet;
52  CAmount nValueRet;
53  bool success = wallet.SelectCoinsMinConf(1003 * COIN, 1, 6, 0, vCoins, setCoinsRet, nValueRet);
54  assert(success);
55  assert(nValueRet == 1003 * COIN);
56  assert(setCoinsRet.size() == 2);
57  }
58 }
59 
60 BENCHMARK(CoinSelection);
bool SelectCoinsMinConf(const CAmount &nTargetValue, int nConfMine, int nConfTheirs, uint64_t nMaxAncestors, std::vector< COutput > vCoins, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet) const
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
Definition: wallet.cpp:2580
CCriticalSection cs_wallet
Definition: wallet.h:751
bool KeepRunning()
Definition: bench.cpp:44
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
#define LOCK(cs)
Definition: sync.h:176
std::vector< CTxOut > vout
Definition: transaction.h:392
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:285
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:673
A mutable version of CTransaction.
Definition: transaction.h:389
BENCHMARK(CoinSelection)