Microbenchmark for verification of a basic P2WPKH script. Can be easily modified to measure performance of other types of scripts.
| 52 | // Microbenchmark for verification of a basic P2WPKH script. Can be easily |
| 53 | // modified to measure performance of other types of scripts. |
| 54 | static void VerifyScriptBench(benchmark::State& state) |
| 55 | { |
| 56 | const int flags = SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH; |
| 57 | const int witnessversion = 0; |
| 58 | |
| 59 | // Keypair. |
| 60 | CKey key; |
| 61 | static const std::array<unsigned char, 32> vchKey = { |
| 62 | { |
| 63 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 |
| 64 | } |
| 65 | }; |
| 66 | key.Set(vchKey.begin(), vchKey.end(), false); |
| 67 | CPubKey pubkey = key.GetPubKey(); |
| 68 | uint160 pubkeyHash; |
| 69 | CHash160().Write(pubkey.begin(), pubkey.size()).Finalize(pubkeyHash.begin()); |
| 70 | |
| 71 | // Script. |
| 72 | CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash); |
| 73 | CScript scriptSig; |
| 74 | CScript witScriptPubkey = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkeyHash) << OP_EQUALVERIFY << OP_CHECKSIG; |
| 75 | const CMutableTransaction& txCredit = BuildCreditingTransaction(scriptPubKey); |
| 76 | CMutableTransaction txSpend = BuildSpendingTransaction(scriptSig, txCredit); |
| 77 | CScriptWitness& witness = txSpend.vin[0].scriptWitness; |
| 78 | witness.stack.emplace_back(); |
| 79 | key.Sign(SignatureHash(witScriptPubkey, txSpend, 0, SIGHASH_ALL, txCredit.vout[0].nValue, SigVersion::WITNESS_V0, false), witness.stack.back()); |
| 80 | witness.stack.back().push_back(static_cast<unsigned char>(SIGHASH_ALL)); |
| 81 | witness.stack.push_back(ToByteVector(pubkey)); |
| 82 | |
| 83 | // Benchmark. |
| 84 | while (state.KeepRunning()) { |
| 85 | ScriptError err; |
| 86 | bool success = VerifyScript( |
| 87 | txSpend.vin[0].scriptSig, |
| 88 | txCredit.vout[0].scriptPubKey, |
| 89 | &txSpend.vin[0].scriptWitness, |
| 90 | flags, |
| 91 | MutableTransactionSignatureChecker(&txSpend, 0, txCredit.vout[0].nValue), |
| 92 | &err); |
| 93 | assert(err == SCRIPT_ERR_OK); |
| 94 | assert(success); |
| 95 | |
| 96 | #if defined(HAVE_CONSENSUS_LIB) |
| 97 | CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); |
| 98 | stream << txSpend; |
| 99 | int csuccess = bitcoinconsensus_verify_script_with_amount( |
| 100 | txCredit.vout[0].scriptPubKey.data(), |
| 101 | txCredit.vout[0].scriptPubKey.size(), |
| 102 | txCredit.vout[0].nValue, |
| 103 | (const unsigned char*)stream.data(), stream.size(), 0, flags, nullptr); |
| 104 | assert(csuccess == 1); |
| 105 | #endif |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | BENCHMARK(VerifyScriptBench, 6300); |
nothing calls this directly
no test coverage detected