Raven Core  3.0.0
P2P Digital Currency
ccoins_caching.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "bench.h"
7 #include "coins.h"
8 #include "policy/policy.h"
9 #include "wallet/crypter.h"
10 
11 #include <vector>
12 
13 // FIXME: Dedup with SetupDummyInputs in test/transaction_tests.cpp.
14 //
15 // Helper: create two dummy transactions, each with
16 // two outputs. The first has 11 and 50 CENT outputs
17 // paid to a TX_PUBKEY, the second 21 and 22 CENT outputs
18 // paid to a TX_PUBKEYHASH.
19 //
20 static std::vector<CMutableTransaction>
21 SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet)
22 {
23  std::vector<CMutableTransaction> dummyTransactions;
24  dummyTransactions.resize(2);
25 
26  // Add some keys to the keystore:
27  CKey key[4];
28  for (int i = 0; i < 4; i++) {
29  key[i].MakeNewKey(i % 2);
30  keystoreRet.AddKey(key[i]);
31  }
32 
33  // Create some dummy input transactions
34  dummyTransactions[0].vout.resize(2);
35  dummyTransactions[0].vout[0].nValue = 11 * CENT;
36  dummyTransactions[0].vout[0].scriptPubKey << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
37  dummyTransactions[0].vout[1].nValue = 50 * CENT;
38  dummyTransactions[0].vout[1].scriptPubKey << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
39  AddCoins(coinsRet, dummyTransactions[0], 0, uint256());
40 
41  dummyTransactions[1].vout.resize(2);
42  dummyTransactions[1].vout[0].nValue = 21 * CENT;
43  dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(key[2].GetPubKey().GetID());
44  dummyTransactions[1].vout[1].nValue = 22 * CENT;
45  dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(key[3].GetPubKey().GetID());
46  AddCoins(coinsRet, dummyTransactions[1], 0, uint256());
47 
48  return dummyTransactions;
49 }
50 
51 // Microbenchmark for simple accesses to a CCoinsViewCache database. Note from
52 // laanwj, "replicating the actual usage patterns of the client is hard though,
53 // many times micro-benchmarks of the database showed completely different
54 // characteristics than e.g. reindex timings. But that's not a requirement of
55 // every benchmark."
56 // (https://github.com/RavenProject/Ravencoin/issues/7883#issuecomment-224807484)
57 static void CCoinsCaching(benchmark::State& state)
58 {
59  CBasicKeyStore keystore;
60  CCoinsView coinsDummy;
61  CCoinsViewCache coins(&coinsDummy);
62  std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
63 
65  t1.vin.resize(3);
66  t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
67  t1.vin[0].prevout.n = 1;
68  t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
69  t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
70  t1.vin[1].prevout.n = 0;
71  t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
72  t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
73  t1.vin[2].prevout.n = 1;
74  t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
75  t1.vout.resize(2);
76  t1.vout[0].nValue = 90 * CENT;
77  t1.vout[0].scriptPubKey << OP_1;
78 
79  // Benchmark.
80  while (state.KeepRunning()) {
81  bool success = AreInputsStandard(t1, coins);
82  assert(success);
83  CAmount value = coins.GetValueIn(t1);
84  assert(value == (50 + 21 + 22) * CENT);
85  }
86 }
87 
88 BENCHMARK(CCoinsCaching);
std::vector< CTxIn > vin
Definition: transaction.h:391
bool KeepRunning()
Definition: bench.cpp:44
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:176
BENCHMARK(CCoinsCaching)
Abstract view on the open txout dataset.
Definition: coins.h:152
Definition: script.h:61
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:127
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Raven scriptPubKey for the given CTxDestination.
Definition: standard.cpp:347
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:13
std::vector< CTxOut > vout
Definition: transaction.h:392
256-bit opaque blob.
Definition: uint256.h:123
A mutable version of CTransaction.
Definition: transaction.h:389
An encapsulated private key.
Definition: key.h:36
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:208
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, uint256 blockHash, bool check, CAssetsCache *assetsCache, std::pair< std::string, CBlockAssetUndo > *undoAssetData)
Utility function to add all of a transaction&#39;s outputs to a cache.
Definition: coins.cpp:97
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:55
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:45