Fabcoin Core  0.16.2
P2P Digital Currency
ccoins_caching.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2017 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>
8 #include <wallet/crypter.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 CENT outputs
16 // paid to a TX_PUBKEY, the second 21 and 22 CENT outputs
17 // paid to a TX_PUBKEYHASH.
18 //
19 static std::vector<CMutableTransaction>
20 SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet)
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 * 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);
39 
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);
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/blockchaingate/fabcoin/issues/7883#issuecomment-224807484)
56 static void CCoinsCaching(benchmark::State& state)
57 {
58  CBasicKeyStore 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 * CENT;
76  t1.vout[0].scriptPubKey << OP_1;
77 
78  // Benchmark.
79  while (state.KeepRunning()) {
80  bool success = AreInputsStandard(t1, coins);
81  assert(success);
82  CAmount value = coins.GetValueIn(t1);
83  assert(value == (50 + 21 + 22) * CENT);
84  }
85 }
86 
87 BENCHMARK(CCoinsCaching, 170 * 1000);
std::vector< CTxIn > vin
Definition: transaction.h:392
bool KeepRunning()
Definition: bench.h:70
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:90
assert(len-trim+(2 *lenIndices)<=WIDTH)
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check for standard transaction types.
Definition: policy.cpp:164
Abstract view on the open txout dataset.
Definition: coins.h:145
Definition: script.h:58
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:126
#define t1
BENCHMARK(CCoinsCaching, 170 *1000)
CScript GetScriptForDestination(const CTxDestination &dest)
Definition: standard.cpp:370
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:12
std::vector< CTxOut > vout
Definition: transaction.h:393
A mutable version of CTransaction.
Definition: transaction.h:390
An encapsulated private key.
Definition: key.h:35
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:201
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:54
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:42