Bitcoin Core  0.19.99
P2P Digital Currency
ccoins_caching.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-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 <coins.h>
7 #include <policy/policy.h>
9 
10 #include <vector>
11 
12 // FIXME: Dedup with SetupDummyInputs in test/transaction_tests.cpp.
13 //
14 // Helper: create two dummy transactions, each with
15 // two outputs. The first has 11 and 50 COIN outputs
16 // paid to a TX_PUBKEY, the second 21 and 22 COIN outputs
17 // paid to a TX_PUBKEYHASH.
18 //
19 static std::vector<CMutableTransaction>
21 {
22  std::vector<CMutableTransaction> dummyTransactions;
23  dummyTransactions.resize(2);
24 
25  // Add some keys to the keystore:
26  CKey key[4];
27  for (int i = 0; i < 4; i++) {
28  key[i].MakeNewKey(i % 2);
29  keystoreRet.AddKey(key[i]);
30  }
31 
32  // Create some dummy input transactions
33  dummyTransactions[0].vout.resize(2);
34  dummyTransactions[0].vout[0].nValue = 11 * COIN;
35  dummyTransactions[0].vout[0].scriptPubKey << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
36  dummyTransactions[0].vout[1].nValue = 50 * COIN;
37  dummyTransactions[0].vout[1].scriptPubKey << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
38  AddCoins(coinsRet, CTransaction(dummyTransactions[0]), 0);
39 
40  dummyTransactions[1].vout.resize(2);
41  dummyTransactions[1].vout[0].nValue = 21 * COIN;
42  dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(PKHash(key[2].GetPubKey()));
43  dummyTransactions[1].vout[1].nValue = 22 * COIN;
44  dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(PKHash(key[3].GetPubKey()));
45  AddCoins(coinsRet, CTransaction(dummyTransactions[1]), 0);
46 
47  return dummyTransactions;
48 }
49 
50 // Microbenchmark for simple accesses to a CCoinsViewCache database. Note from
51 // laanwj, "replicating the actual usage patterns of the client is hard though,
52 // many times micro-benchmarks of the database showed completely different
53 // characteristics than e.g. reindex timings. But that's not a requirement of
54 // every benchmark."
55 // (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
56 static void CCoinsCaching(benchmark::State& state)
57 {
58  FillableSigningProvider keystore;
59  CCoinsView coinsDummy;
60  CCoinsViewCache coins(&coinsDummy);
61  std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
62 
64  t1.vin.resize(3);
65  t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
66  t1.vin[0].prevout.n = 1;
67  t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
68  t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
69  t1.vin[1].prevout.n = 0;
70  t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
71  t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
72  t1.vin[2].prevout.n = 1;
73  t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
74  t1.vout.resize(2);
75  t1.vout[0].nValue = 90 * COIN;
76  t1.vout[0].scriptPubKey << OP_1;
77 
78  // Benchmark.
79  const CTransaction tx_1(t1);
80  while (state.KeepRunning()) {
81  bool success = AreInputsStandard(tx_1, coins);
82  assert(success);
83  CAmount value = coins.GetValueIn(tx_1);
84  assert(value == (50 + 21 + 22) * COIN);
85  }
86 }
87 
88 BENCHMARK(CCoinsCaching, 170 * 1000);
std::vector< CTxIn > vin
Definition: transaction.h:368
static const CAmount COIN
Definition: amount.h:14
bool KeepRunning()
Definition: bench.h:72
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check)
Utility function to add all of a transaction&#39;s outputs to a cache.
Definition: coins.cpp:89
static void CCoinsCaching(benchmark::State &state)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:156
static std::vector< CMutableTransaction > SetupDummyInputs(FillableSigningProvider &keystoreRet, CCoinsViewCache &coinsRet)
Abstract view on the open txout dataset.
Definition: coins.h:154
Fillable signing provider that keeps keys in an address->secret map.
Definition: script.h:64
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:157
BENCHMARK(CCoinsCaching, 170 *1000)
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:289
std::vector< CTxOut > vout
Definition: transaction.h:369
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition: sign.cpp:52
CAmount GetValueIn(const CTransaction &tx) const
Amount of bitcoins coming in to a transaction Note that lightweight clients may not know anything bes...
Definition: coins.cpp:225
A mutable version of CTransaction.
Definition: transaction.h:366
An encapsulated private key.
Definition: key.h:27
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:210
virtual bool AddKey(const CKey &key)
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:48