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