Bitcoin ABC  0.22.13
P2P Digital Currency
ccoins_caching.cpp
Go to the documentation of this file.
1 // Copyright (c) 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 <coins.h>
7 #include <policy/policy.h>
9 #include <test/util/transaction_utils.h>
10 
11 #include <vector>
12 
13 // Microbenchmark for simple accesses to a CCoinsViewCache database. Note from
14 // laanwj, "replicating the actual usage patterns of the client is hard though,
15 // many times micro-benchmarks of the database showed completely different
16 // characteristics than e.g. reindex timings. But that's not a requirement of
17 // every benchmark."
18 // (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
19 static void CCoinsCaching(benchmark::State &state) {
20  const ECCVerifyHandle verify_handle;
21  ECC_Start();
22 
23  FillableSigningProvider keystore;
24  CCoinsView coinsDummy;
25  CCoinsViewCache coins(&coinsDummy);
26  std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(
27  keystore, coins, {{11 * COIN, 50 * COIN, 21 * COIN, 22 * COIN}});
28 
30  t1.vin.resize(3);
31  t1.vin[0].prevout = COutPoint(dummyTransactions[0].GetId(), 1);
32  t1.vin[0].scriptSig << std::vector<uint8_t>(65, 0);
33  t1.vin[1].prevout = COutPoint(dummyTransactions[1].GetId(), 0);
34  t1.vin[1].scriptSig << std::vector<uint8_t>(65, 0)
35  << std::vector<uint8_t>(33, 4);
36  t1.vin[2].prevout = COutPoint(dummyTransactions[1].GetId(), 1);
37  t1.vin[2].scriptSig << std::vector<uint8_t>(65, 0)
38  << std::vector<uint8_t>(33, 4);
39  t1.vout.resize(2);
40  t1.vout[0].nValue = 90 * COIN;
41  t1.vout[0].scriptPubKey << OP_1;
42 
43  // Benchmark.
44  while (state.KeepRunning()) {
45  CTransaction t(t1);
46  bool success =
48  assert(success);
49  Amount value = coins.GetValueIn(t);
50  assert(value == (50 + 21 + 22) * COIN);
51  }
52  ECC_Stop();
53 }
54 
55 BENCHMARK(CCoinsCaching, 170 * 1000);
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:438
Amount 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:290
Definition: amount.h:17
bool KeepRunning()
Definition: bench.h:71
static void CCoinsCaching(benchmark::State &state)
std::vector< CTxIn > vin
Definition: transaction.h:299
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:223
static constexpr Amount COIN
Definition: amount.h:153
Abstract view on the open txout dataset.
Definition: coins.h:175
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:455
Fillable signing provider that keeps keys in an address->secret map.
Definition: script.h:55
BENCHMARK(CCoinsCaching, 170 *1000)
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
A mutable version of CTransaction.
Definition: transaction.h:297
static constexpr uint32_t STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
Definition: policy.h:91
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:231
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs, uint32_t flags)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:141