Skip to content

Commit 0800895

Browse files
committed
bench: add benchmark for SignTransaction
1 parent b940619 commit 0800895

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Makefile.bench.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ bench_bench_bitcoin_SOURCES = \
5252
bench/rollingbloom.cpp \
5353
bench/rpc_blockchain.cpp \
5454
bench/rpc_mempool.cpp \
55+
bench/sign_transaction.cpp \
5556
bench/streams_findbyte.cpp \
5657
bench/strencodings.cpp \
5758
bench/util_time.cpp \

src/bench/sign_transaction.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <bench/bench.h>
6+
#include <addresstype.h>
7+
#include <coins.h>
8+
#include <key.h>
9+
#include <primitives/transaction.h>
10+
#include <pubkey.h>
11+
#include <script/interpreter.h>
12+
#include <script/script.h>
13+
#include <script/sign.h>
14+
#include <uint256.h>
15+
#include <util/translation.h>
16+
17+
enum class InputType {
18+
P2WPKH, // segwitv0, witness-pubkey-hash (ECDSA signature)
19+
P2TR, // segwitv1, taproot key-path spend (Schnorr signature)
20+
};
21+
22+
static void SignTransactionSingleInput(benchmark::Bench& bench, InputType input_type)
23+
{
24+
ECC_Context ecc_context{};
25+
26+
FlatSigningProvider keystore;
27+
std::vector<CScript> prev_spks;
28+
29+
// Create a bunch of keys / UTXOs to avoid signing with the same key repeatedly
30+
for (int i = 0; i < 32; i++) {
31+
CKey privkey = GenerateRandomKey();
32+
CPubKey pubkey = privkey.GetPubKey();
33+
CKeyID key_id = pubkey.GetID();
34+
keystore.keys.emplace(key_id, privkey);
35+
keystore.pubkeys.emplace(key_id, pubkey);
36+
37+
// Create specified locking script type
38+
CScript prev_spk;
39+
switch (input_type) {
40+
case InputType::P2WPKH: prev_spk = GetScriptForDestination(WitnessV0KeyHash(pubkey)); break;
41+
case InputType::P2TR: prev_spk = GetScriptForDestination(WitnessV1Taproot(XOnlyPubKey{pubkey})); break;
42+
default: assert(false);
43+
}
44+
prev_spks.push_back(prev_spk);
45+
}
46+
47+
// Simple 1-input tx with artificial outpoint
48+
// (note that for the purpose of signing with SIGHASH_ALL we don't need any outputs)
49+
COutPoint prevout{/*hashIn=*/Txid::FromUint256(uint256::ONE), /*nIn=*/1337};
50+
CMutableTransaction unsigned_tx;
51+
unsigned_tx.vin.emplace_back(prevout);
52+
53+
// Benchmark.
54+
int iter = 0;
55+
bench.minEpochIterations(100).run([&] {
56+
CMutableTransaction tx{unsigned_tx};
57+
std::map<COutPoint, Coin> coins;
58+
CScript prev_spk = prev_spks[(iter++) % prev_spks.size()];
59+
coins[prevout] = Coin(CTxOut(10000, prev_spk), /*nHeightIn=*/100, /*fCoinBaseIn=*/false);
60+
std::map<int, bilingual_str> input_errors;
61+
bool complete = SignTransaction(tx, &keystore, coins, SIGHASH_ALL, input_errors);
62+
assert(complete);
63+
});
64+
}
65+
66+
static void SignTransactionECDSA(benchmark::Bench& bench) { SignTransactionSingleInput(bench, InputType::P2WPKH); }
67+
static void SignTransactionSchnorr(benchmark::Bench& bench) { SignTransactionSingleInput(bench, InputType::P2TR); }
68+
69+
BENCHMARK(SignTransactionECDSA, benchmark::PriorityLevel::HIGH);
70+
BENCHMARK(SignTransactionSchnorr, benchmark::PriorityLevel::HIGH);

0 commit comments

Comments
 (0)