ccoins_caching.cpp raw
1 // Copyright (c) 2016-present 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 <consensus/amount.h>
8 #include <consensus/validation.h>
9 #include <key.h>
10 #include <policy/policy.h>
11 #include <primitives/transaction.h>
12 #include <script/script.h>
13 #include <script/signingprovider.h>
14 #include <test/util/transaction_utils.h>
15 #include <util/check.h>
16
17 #include <span>
18 #include <vector>
19
20 // Microbenchmark for simple accesses to a CCoinsViewCache database. Note from
21 // laanwj, "replicating the actual usage patterns of the client is hard though,
22 // many times micro-benchmarks of the database showed completely different
23 // characteristics than e.g. reindex timings. But that's not a requirement of
24 // every benchmark."
25 // (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
26 static void CCoinsCaching(benchmark::Bench& bench)
27 {
28 ECC_Context ecc_context{};
29
30 FillableSigningProvider keystore;
31 CCoinsViewCache coins{&CoinsViewEmpty::Get()};
32 std::vector<CMutableTransaction> dummyTransactions =
33 SetupDummyInputs(keystore, coins, {11 * COIN, 50 * COIN, 21 * COIN, 22 * COIN});
34
35 CMutableTransaction t1;
36 t1.vin.resize(3);
37 t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
38 t1.vin[0].prevout.n = 1;
39 t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
40 t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
41 t1.vin[1].prevout.n = 0;
42 t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
43 t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
44 t1.vin[2].prevout.n = 1;
45 t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
46 t1.vout.resize(2);
47 t1.vout[0].nValue = 90 * COIN;
48 t1.vout[0].scriptPubKey << OP_1;
49
50 // Benchmark.
51 const CTransaction tx_1(t1);
52 bench.run([&] {
53 assert(ValidateInputsStandardness(tx_1, coins).IsValid());
54 });
55 }
56
57 BENCHMARK(CCoinsCaching);
58