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.
5 #include <bench/bench.h>
7 #include <policy/policy.h>
8 #include <wallet/crypter.h>
12 // FIXME: Dedup with SetupDummyInputs in test/transaction_tests.cpp.
14 // Helper: create two dummy transactions, each with
15 // two outputs. The first has 11 and 50 CENT outputs
16 // paid to a TX_PUBKEY, the second 21 and 22 CENT outputs
17 // paid to a TX_PUBKEYHASH.
19 static std::vector
<CMutableTransaction
>
20 SetupDummyInputs(CBasicKeyStore
& keystoreRet
, CCoinsViewCache
& coinsRet
)
22 std::vector
<CMutableTransaction
> dummyTransactions
;
23 dummyTransactions
.resize(2);
25 // Add some keys to the keystore:
27 for (int i
= 0; i
< 4; i
++) {
28 key
[i
].MakeNewKey(i
% 2);
29 keystoreRet
.AddKey(key
[i
]);
32 // Create some dummy input transactions
33 dummyTransactions
[0].vout
.resize(2);
34 dummyTransactions
[0].vout
[0].nValue
= 11 * CENT
;
35 dummyTransactions
[0].vout
[0].scriptPubKey
<< ToByteVector(key
[0].GetPubKey()) << OP_CHECKSIG
;
36 dummyTransactions
[0].vout
[1].nValue
= 50 * CENT
;
37 dummyTransactions
[0].vout
[1].scriptPubKey
<< ToByteVector(key
[1].GetPubKey()) << OP_CHECKSIG
;
38 AddCoins(coinsRet
, dummyTransactions
[0], 0);
40 dummyTransactions
[1].vout
.resize(2);
41 dummyTransactions
[1].vout
[0].nValue
= 21 * CENT
;
42 dummyTransactions
[1].vout
[0].scriptPubKey
= GetScriptForDestination(key
[2].GetPubKey().GetID());
43 dummyTransactions
[1].vout
[1].nValue
= 22 * CENT
;
44 dummyTransactions
[1].vout
[1].scriptPubKey
= GetScriptForDestination(key
[3].GetPubKey().GetID());
45 AddCoins(coinsRet
, dummyTransactions
[1], 0);
47 return dummyTransactions
;
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
55 // (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
56 static void CCoinsCaching(benchmark::State
& state
)
58 CBasicKeyStore keystore
;
59 CCoinsView coinsDummy
;
60 CCoinsViewCache
coins(&coinsDummy
);
61 std::vector
<CMutableTransaction
> dummyTransactions
= SetupDummyInputs(keystore
, coins
);
63 CMutableTransaction t1
;
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);
75 t1
.vout
[0].nValue
= 90 * CENT
;
76 t1
.vout
[0].scriptPubKey
<< OP_1
;
79 while (state
.KeepRunning()) {
80 bool success
= AreInputsStandard(t1
, coins
);
82 CAmount value
= coins
.GetValueIn(t1
);
83 assert(value
== (50 + 21 + 22) * CENT
);
87 BENCHMARK(CCoinsCaching
);