]> Git Repo - VerusCoin.git/blob - src/zcbenchmarks.cpp
Make `validatelargetx` test more accurate, reduce block size limit to 1MB for now.
[VerusCoin.git] / src / zcbenchmarks.cpp
1 #include <unistd.h>
2 #include <boost/filesystem.hpp>
3 #include "coins.h"
4 #include "util.h"
5 #include "init.h"
6 #include "primitives/transaction.h"
7 #include "base58.h"
8 #include "crypto/equihash.h"
9 #include "chainparams.h"
10 #include "consensus/validation.h"
11 #include "main.h"
12 #include "miner.h"
13 #include "pow.h"
14 #include "script/sign.h"
15 #include "sodium.h"
16 #include "streams.h"
17 #include "wallet/wallet.h"
18
19 #include "zcbenchmarks.h"
20
21 #include "zcash/Zcash.h"
22 #include "zcash/IncrementalMerkleTree.hpp"
23
24 using namespace libzcash;
25
26 struct timeval tv_start;
27
28 void timer_start()
29 {
30     gettimeofday(&tv_start, 0);
31 }
32
33 double timer_stop()
34 {
35     double elapsed;
36     struct timeval tv_end;
37     gettimeofday(&tv_end, 0);
38     elapsed = double(tv_end.tv_sec-tv_start.tv_sec) +
39         (tv_end.tv_usec-tv_start.tv_usec)/double(1000000);
40     return elapsed;
41 }
42
43 double benchmark_sleep()
44 {
45     timer_start();
46     sleep(1);
47     return timer_stop();
48 }
49
50 double benchmark_parameter_loading()
51 {
52     // FIXME: this is duplicated with the actual loading code
53     boost::filesystem::path pk_path = ZC_GetParamsDir() / "z5-proving.key";
54     boost::filesystem::path vk_path = ZC_GetParamsDir() / "z5-verifying.key";
55
56     timer_start();
57
58     auto newParams = ZCJoinSplit::Unopened();
59
60     newParams->loadVerifyingKey(vk_path.string());
61     newParams->setProvingKeyPath(pk_path.string());
62     newParams->loadProvingKey();
63
64     double ret = timer_stop();
65
66     delete newParams;
67
68     return ret;
69 }
70
71 double benchmark_create_joinsplit()
72 {
73     uint256 pubKeyHash;
74
75     /* Get the anchor of an empty commitment tree. */
76     uint256 anchor = ZCIncrementalMerkleTree().root();
77
78     timer_start();
79     CPourTx pourtx(*pzcashParams,
80                    pubKeyHash,
81                    anchor,
82                    {JSInput(), JSInput()},
83                    {JSOutput(), JSOutput()},
84                    0,
85                    0);
86     double ret = timer_stop();
87
88     assert(pourtx.Verify(*pzcashParams, pubKeyHash));
89     return ret;
90 }
91
92 double benchmark_verify_joinsplit(const CPourTx &joinsplit)
93 {
94     timer_start();
95     uint256 pubKeyHash;
96     joinsplit.Verify(*pzcashParams, pubKeyHash);
97     return timer_stop();
98 }
99
100 double benchmark_solve_equihash()
101 {
102     CBlock pblock;
103     CEquihashInput I{pblock};
104     CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
105     ss << I;
106
107     unsigned int n = Params(CBaseChainParams::MAIN).EquihashN();
108     unsigned int k = Params(CBaseChainParams::MAIN).EquihashK();
109     crypto_generichash_blake2b_state eh_state;
110     EhInitialiseState(n, k, eh_state);
111     crypto_generichash_blake2b_update(&eh_state, (unsigned char*)&ss[0], ss.size());
112
113     uint256 nonce;
114     randombytes_buf(nonce.begin(), 32);
115     crypto_generichash_blake2b_update(&eh_state,
116                                     nonce.begin(),
117                                     nonce.size());
118
119     timer_start();
120     std::set<std::vector<unsigned int>> solns;
121     EhOptimisedSolve(n, k, eh_state, solns);
122     return timer_stop();
123 }
124
125 double benchmark_verify_equihash()
126 {
127     CChainParams params = Params(CBaseChainParams::MAIN);
128     CBlock genesis = Params(CBaseChainParams::MAIN).GenesisBlock();
129     CBlockHeader genesis_header = genesis.GetBlockHeader();
130     timer_start();
131     CheckEquihashSolution(&genesis_header, params);
132     return timer_stop();
133 }
134
135 double benchmark_large_tx()
136 {
137     // Number of inputs in the spending transaction that we will simulate
138     const size_t NUM_INPUTS = 5550;
139
140     // Create priv/pub key
141     CKey priv;
142     priv.MakeNewKey(false);
143     auto pub = priv.GetPubKey();
144     CBasicKeyStore tempKeystore;
145     tempKeystore.AddKey(priv);
146
147     // The "original" transaction that the spending transaction will spend
148     // from.
149     CMutableTransaction m_orig_tx;
150     m_orig_tx.vout.resize(1);
151     m_orig_tx.vout[0].nValue = 1000000;
152     CScript prevPubKey = GetScriptForDestination(pub.GetID());
153     m_orig_tx.vout[0].scriptPubKey = prevPubKey;
154
155     auto orig_tx = CTransaction(m_orig_tx);
156
157     CMutableTransaction spending_tx;
158     auto input_hash = orig_tx.GetHash();
159     // Add NUM_INPUTS inputs
160     for (size_t i = 0; i < NUM_INPUTS; i++) {
161         spending_tx.vin.emplace_back(input_hash, 0);
162     }
163
164     // Sign for all the inputs
165     for (size_t i = 0; i < NUM_INPUTS; i++) {
166         SignSignature(tempKeystore, prevPubKey, spending_tx, i, SIGHASH_ALL);
167     }
168
169     // Serialize:
170     {
171         CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
172         ss << spending_tx;
173         //std::cout << "SIZE OF SPENDING TX: " << ss.size() << std::endl;
174
175         auto error = MAX_BLOCK_SIZE / 20; // 5% error
176         assert(ss.size() < MAX_BLOCK_SIZE + error);
177         assert(ss.size() > MAX_BLOCK_SIZE - error);
178     }
179
180     // Benchmark signature verification costs:
181     timer_start();
182     for (size_t i = 0; i < NUM_INPUTS; i++) {
183         ScriptError serror = SCRIPT_ERR_OK;
184         assert(VerifyScript(spending_tx.vin[i].scriptSig,
185                             prevPubKey,
186                             STANDARD_SCRIPT_VERIFY_FLAGS,
187                             MutableTransactionSignatureChecker(&spending_tx, i),
188                             &serror));
189     }
190     return timer_stop();
191 }
192
This page took 0.033746 seconds and 4 git commands to generate.