]>
Commit | Line | Data |
---|---|---|
6962bb3d TH |
1 | #include <unistd.h> |
2 | #include <boost/filesystem.hpp> | |
f7478de6 | 3 | #include <boost/thread.hpp> |
6962bb3d TH |
4 | #include "coins.h" |
5 | #include "util.h" | |
6 | #include "init.h" | |
7 | #include "primitives/transaction.h" | |
f5edc37f | 8 | #include "base58.h" |
bf8def97 TH |
9 | #include "crypto/equihash.h" |
10 | #include "chainparams.h" | |
f5edc37f JG |
11 | #include "consensus/validation.h" |
12 | #include "main.h" | |
13 | #include "miner.h" | |
a1cd1a27 | 14 | #include "pow.h" |
f5edc37f | 15 | #include "script/sign.h" |
722b0117 TH |
16 | #include "sodium.h" |
17 | #include "streams.h" | |
f5edc37f | 18 | #include "wallet/wallet.h" |
6962bb3d TH |
19 | |
20 | #include "zcbenchmarks.h" | |
21 | ||
2dc35992 SB |
22 | #include "zcash/Zcash.h" |
23 | #include "zcash/IncrementalMerkleTree.hpp" | |
24 | ||
25 | using namespace libzcash; | |
26 | ||
6962bb3d TH |
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 | |
393b2b78 SB |
54 | boost::filesystem::path pk_path = ZC_GetParamsDir() / "z9-proving.key"; |
55 | boost::filesystem::path vk_path = ZC_GetParamsDir() / "z9-verifying.key"; | |
6962bb3d TH |
56 | |
57 | timer_start(); | |
2dc35992 SB |
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; | |
6962bb3d TH |
70 | } |
71 | ||
72 | double benchmark_create_joinsplit() | |
73 | { | |
21406393 | 74 | uint256 pubKeyHash; |
6962bb3d | 75 | |
6962bb3d | 76 | /* Get the anchor of an empty commitment tree. */ |
5961dcb6 | 77 | uint256 anchor = ZCIncrementalMerkleTree().root(); |
6962bb3d TH |
78 | |
79 | timer_start(); | |
22de1602 SB |
80 | JSDescription jsdesc(*pzcashParams, |
81 | pubKeyHash, | |
82 | anchor, | |
83 | {JSInput(), JSInput()}, | |
84 | {JSOutput(), JSOutput()}, | |
85 | 0, | |
86 | 0); | |
6962bb3d | 87 | double ret = timer_stop(); |
21406393 | 88 | |
22de1602 | 89 | assert(jsdesc.Verify(*pzcashParams, pubKeyHash)); |
6962bb3d TH |
90 | return ret; |
91 | } | |
bf8def97 | 92 | |
a8c68ffe | 93 | double benchmark_verify_joinsplit(const JSDescription &joinsplit) |
a1cd1a27 TH |
94 | { |
95 | timer_start(); | |
21406393 | 96 | uint256 pubKeyHash; |
2dc35992 | 97 | joinsplit.Verify(*pzcashParams, pubKeyHash); |
a1cd1a27 TH |
98 | return timer_stop(); |
99 | } | |
100 | ||
f7478de6 | 101 | double benchmark_solve_equihash(bool time) |
bf8def97 | 102 | { |
722b0117 TH |
103 | CBlock pblock; |
104 | CEquihashInput I{pblock}; | |
105 | CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); | |
106 | ss << I; | |
107 | ||
e9574728 JG |
108 | unsigned int n = Params(CBaseChainParams::MAIN).EquihashN(); |
109 | unsigned int k = Params(CBaseChainParams::MAIN).EquihashK(); | |
bf8def97 | 110 | crypto_generichash_blake2b_state eh_state; |
e9574728 | 111 | EhInitialiseState(n, k, eh_state); |
722b0117 TH |
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 | ||
f7478de6 JG |
120 | if (time) |
121 | timer_start(); | |
e9574728 | 122 | std::set<std::vector<unsigned int>> solns; |
51eb5273 | 123 | EhOptimisedSolveUncancellable(n, k, eh_state, |
5be6abbf | 124 | [](std::vector<unsigned char> soln) { return false; }); |
f7478de6 JG |
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(); | |
bf8def97 TH |
138 | return timer_stop(); |
139 | } | |
d44feea4 | 140 | |
a1cd1a27 | 141 | double benchmark_verify_equihash() |
d44feea4 | 142 | { |
a1cd1a27 TH |
143 | CChainParams params = Params(CBaseChainParams::MAIN); |
144 | CBlock genesis = Params(CBaseChainParams::MAIN).GenesisBlock(); | |
145 | CBlockHeader genesis_header = genesis.GetBlockHeader(); | |
d44feea4 | 146 | timer_start(); |
a1cd1a27 | 147 | CheckEquihashSolution(&genesis_header, params); |
d44feea4 TH |
148 | return timer_stop(); |
149 | } | |
a1cd1a27 | 150 | |
9c45b501 | 151 | double benchmark_large_tx() |
f5edc37f | 152 | { |
9c45b501 | 153 | // Number of inputs in the spending transaction that we will simulate |
000383c1 | 154 | const size_t NUM_INPUTS = 11100; |
9c45b501 SB |
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; | |
10d2c57c | 174 | auto input_hash = orig_tx.GetTxid(); |
9c45b501 SB |
175 | // Add NUM_INPUTS inputs |
176 | for (size_t i = 0; i < NUM_INPUTS; i++) { | |
177 | spending_tx.vin.emplace_back(input_hash, 0); | |
f5edc37f JG |
178 | } |
179 | ||
9c45b501 SB |
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); | |
f5edc37f | 183 | } |
f5edc37f | 184 | |
9c45b501 SB |
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; | |
f5edc37f | 190 | |
9c45b501 SB |
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); | |
f5edc37f JG |
194 | } |
195 | ||
822b84b6 S |
196 | // Spending tx has all its inputs signed and does not need to be mutated anymore |
197 | CTransaction final_spending_tx(spending_tx); | |
198 | ||
9c45b501 | 199 | // Benchmark signature verification costs: |
f5edc37f | 200 | timer_start(); |
9c45b501 SB |
201 | for (size_t i = 0; i < NUM_INPUTS; i++) { |
202 | ScriptError serror = SCRIPT_ERR_OK; | |
75c2f268 | 203 | assert(VerifyScript(final_spending_tx.vin[i].scriptSig, |
9c45b501 SB |
204 | prevPubKey, |
205 | STANDARD_SCRIPT_VERIFY_FLAGS, | |
822b84b6 | 206 | TransactionSignatureChecker(&final_spending_tx, i), |
9c45b501 SB |
207 | &serror)); |
208 | } | |
209 | return timer_stop(); | |
f5edc37f JG |
210 | } |
211 |