mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-05-22 10:02:34 +02:00
refactor: test/bench: dedup Build{Crediting,Spending}Transaction()
prototypes used in src/test/script_tests.cpp: - CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey, int nValue = 0); - CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CScriptWitness& scriptWitness, const CTransaction& txCredit); prototypes used in bench/verify_script.cpp: - CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey); - CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CMutableTransaction& txCredit); The more generic versions from the script tests are moved into a new file pair transaction_utils.cpp/h and the calls are adapted accordingly in the verify_script benchmark (passing the nValue of 1 explicitely for BuildCreditingTransaction(), passing empty scriptWitness explicitely and converting txCredit parameter to CTransaction in BuildSpendingTransaction()).
This commit is contained in:
parent
ec3ed5a448
commit
a0fc076476
@ -13,6 +13,7 @@
|
|||||||
<ClCompile Include="..\..\src\test\*_properties.cpp" />
|
<ClCompile Include="..\..\src\test\*_properties.cpp" />
|
||||||
<ClCompile Include="..\..\src\test\gen\*_gen.cpp" />
|
<ClCompile Include="..\..\src\test\gen\*_gen.cpp" />
|
||||||
<ClCompile Include="..\..\src\wallet\test\*_tests.cpp" />
|
<ClCompile Include="..\..\src\wallet\test\*_tests.cpp" />
|
||||||
|
<ClCompile Include="..\..\src\test\lib\*.cpp" />
|
||||||
<ClCompile Include="..\..\src\test\setup_common.cpp" />
|
<ClCompile Include="..\..\src\test\setup_common.cpp" />
|
||||||
<ClCompile Include="..\..\src\test\main.cpp" />
|
<ClCompile Include="..\..\src\test\main.cpp" />
|
||||||
<ClCompile Include="..\..\src\wallet\test\*_fixture.cpp" />
|
<ClCompile Include="..\..\src\wallet\test\*_fixture.cpp" />
|
||||||
|
@ -39,6 +39,8 @@ bench_bench_bitcoin_SOURCES = \
|
|||||||
bench/lockedpool.cpp \
|
bench/lockedpool.cpp \
|
||||||
bench/poly1305.cpp \
|
bench/poly1305.cpp \
|
||||||
bench/prevector.cpp \
|
bench/prevector.cpp \
|
||||||
|
test/lib/transaction_utils.h \
|
||||||
|
test/lib/transaction_utils.cpp \
|
||||||
test/setup_common.h \
|
test/setup_common.h \
|
||||||
test/setup_common.cpp \
|
test/setup_common.cpp \
|
||||||
test/util.h \
|
test/util.h \
|
||||||
|
@ -53,6 +53,8 @@ RAW_TEST_FILES =
|
|||||||
GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h)
|
GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h)
|
||||||
|
|
||||||
BITCOIN_TEST_SUITE = \
|
BITCOIN_TEST_SUITE = \
|
||||||
|
test/lib/transaction_utils.h \
|
||||||
|
test/lib/transaction_utils.cpp \
|
||||||
test/main.cpp \
|
test/main.cpp \
|
||||||
test/setup_common.h \
|
test/setup_common.h \
|
||||||
test/setup_common.cpp
|
test/setup_common.cpp
|
||||||
|
@ -10,44 +10,10 @@
|
|||||||
#include <script/script.h>
|
#include <script/script.h>
|
||||||
#include <script/standard.h>
|
#include <script/standard.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
|
#include <test/lib/transaction_utils.h>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
// FIXME: Dedup with BuildCreditingTransaction in test/script_tests.cpp.
|
|
||||||
static CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey)
|
|
||||||
{
|
|
||||||
CMutableTransaction txCredit;
|
|
||||||
txCredit.nVersion = 1;
|
|
||||||
txCredit.nLockTime = 0;
|
|
||||||
txCredit.vin.resize(1);
|
|
||||||
txCredit.vout.resize(1);
|
|
||||||
txCredit.vin[0].prevout.SetNull();
|
|
||||||
txCredit.vin[0].scriptSig = CScript() << CScriptNum(0) << CScriptNum(0);
|
|
||||||
txCredit.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
|
|
||||||
txCredit.vout[0].scriptPubKey = scriptPubKey;
|
|
||||||
txCredit.vout[0].nValue = 1;
|
|
||||||
|
|
||||||
return txCredit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: Dedup with BuildSpendingTransaction in test/script_tests.cpp.
|
|
||||||
static CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CMutableTransaction& txCredit)
|
|
||||||
{
|
|
||||||
CMutableTransaction txSpend;
|
|
||||||
txSpend.nVersion = 1;
|
|
||||||
txSpend.nLockTime = 0;
|
|
||||||
txSpend.vin.resize(1);
|
|
||||||
txSpend.vout.resize(1);
|
|
||||||
txSpend.vin[0].prevout.hash = txCredit.GetHash();
|
|
||||||
txSpend.vin[0].prevout.n = 0;
|
|
||||||
txSpend.vin[0].scriptSig = scriptSig;
|
|
||||||
txSpend.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
|
|
||||||
txSpend.vout[0].scriptPubKey = CScript();
|
|
||||||
txSpend.vout[0].nValue = txCredit.vout[0].nValue;
|
|
||||||
|
|
||||||
return txSpend;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Microbenchmark for verification of a basic P2WPKH script. Can be easily
|
// Microbenchmark for verification of a basic P2WPKH script. Can be easily
|
||||||
// modified to measure performance of other types of scripts.
|
// modified to measure performance of other types of scripts.
|
||||||
static void VerifyScriptBench(benchmark::State& state)
|
static void VerifyScriptBench(benchmark::State& state)
|
||||||
@ -71,8 +37,8 @@ static void VerifyScriptBench(benchmark::State& state)
|
|||||||
CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);
|
CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);
|
||||||
CScript scriptSig;
|
CScript scriptSig;
|
||||||
CScript witScriptPubkey = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkeyHash) << OP_EQUALVERIFY << OP_CHECKSIG;
|
CScript witScriptPubkey = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkeyHash) << OP_EQUALVERIFY << OP_CHECKSIG;
|
||||||
const CMutableTransaction& txCredit = BuildCreditingTransaction(scriptPubKey);
|
const CMutableTransaction& txCredit = BuildCreditingTransaction(scriptPubKey, 1);
|
||||||
CMutableTransaction txSpend = BuildSpendingTransaction(scriptSig, txCredit);
|
CMutableTransaction txSpend = BuildSpendingTransaction(scriptSig, CScriptWitness(), CTransaction(txCredit));
|
||||||
CScriptWitness& witness = txSpend.vin[0].scriptWitness;
|
CScriptWitness& witness = txSpend.vin[0].scriptWitness;
|
||||||
witness.stack.emplace_back();
|
witness.stack.emplace_back();
|
||||||
key.Sign(SignatureHash(witScriptPubkey, txSpend, 0, SIGHASH_ALL, txCredit.vout[0].nValue, SigVersion::WITNESS_V0), witness.stack.back());
|
key.Sign(SignatureHash(witScriptPubkey, txSpend, 0, SIGHASH_ALL, txCredit.vout[0].nValue, SigVersion::WITNESS_V0), witness.stack.back());
|
||||||
|
39
src/test/lib/transaction_utils.cpp
Normal file
39
src/test/lib/transaction_utils.cpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
// Copyright (c) 2019 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <test/lib/transaction_utils.h>
|
||||||
|
|
||||||
|
CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey, int nValue)
|
||||||
|
{
|
||||||
|
CMutableTransaction txCredit;
|
||||||
|
txCredit.nVersion = 1;
|
||||||
|
txCredit.nLockTime = 0;
|
||||||
|
txCredit.vin.resize(1);
|
||||||
|
txCredit.vout.resize(1);
|
||||||
|
txCredit.vin[0].prevout.SetNull();
|
||||||
|
txCredit.vin[0].scriptSig = CScript() << CScriptNum(0) << CScriptNum(0);
|
||||||
|
txCredit.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
|
||||||
|
txCredit.vout[0].scriptPubKey = scriptPubKey;
|
||||||
|
txCredit.vout[0].nValue = nValue;
|
||||||
|
|
||||||
|
return txCredit;
|
||||||
|
}
|
||||||
|
|
||||||
|
CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CScriptWitness& scriptWitness, const CTransaction& txCredit)
|
||||||
|
{
|
||||||
|
CMutableTransaction txSpend;
|
||||||
|
txSpend.nVersion = 1;
|
||||||
|
txSpend.nLockTime = 0;
|
||||||
|
txSpend.vin.resize(1);
|
||||||
|
txSpend.vout.resize(1);
|
||||||
|
txSpend.vin[0].scriptWitness = scriptWitness;
|
||||||
|
txSpend.vin[0].prevout.hash = txCredit.GetHash();
|
||||||
|
txSpend.vin[0].prevout.n = 0;
|
||||||
|
txSpend.vin[0].scriptSig = scriptSig;
|
||||||
|
txSpend.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
|
||||||
|
txSpend.vout[0].scriptPubKey = CScript();
|
||||||
|
txSpend.vout[0].nValue = txCredit.vout[0].nValue;
|
||||||
|
|
||||||
|
return txSpend;
|
||||||
|
}
|
19
src/test/lib/transaction_utils.h
Normal file
19
src/test/lib/transaction_utils.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// Copyright (c) 2019 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_TEST_LIB_TRANSACTION_UTILS_H
|
||||||
|
#define BITCOIN_TEST_LIB_TRANSACTION_UTILS_H
|
||||||
|
|
||||||
|
#include <primitives/transaction.h>
|
||||||
|
|
||||||
|
// create crediting transaction
|
||||||
|
// [1 coinbase input => 1 output with given scriptPubkey and value]
|
||||||
|
CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey, int nValue = 0);
|
||||||
|
|
||||||
|
// create spending transaction
|
||||||
|
// [1 input with referenced transaction outpoint, scriptSig, scriptWitness =>
|
||||||
|
// 1 output with empty scriptPubKey, full value of referenced transaction]
|
||||||
|
CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CScriptWitness& scriptWitness, const CTransaction& txCredit);
|
||||||
|
|
||||||
|
#endif // BITCOIN_TEST_LIB_TRANSACTION_UTILS_H
|
@ -12,6 +12,7 @@
|
|||||||
#include <script/signingprovider.h>
|
#include <script/signingprovider.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
|
#include <test/lib/transaction_utils.h>
|
||||||
#include <test/setup_common.h>
|
#include <test/setup_common.h>
|
||||||
#include <rpc/util.h>
|
#include <rpc/util.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
@ -121,40 +122,6 @@ static ScriptError_t ParseScriptError(const std::string &name)
|
|||||||
|
|
||||||
BOOST_FIXTURE_TEST_SUITE(script_tests, BasicTestingSetup)
|
BOOST_FIXTURE_TEST_SUITE(script_tests, BasicTestingSetup)
|
||||||
|
|
||||||
CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey, int nValue = 0)
|
|
||||||
{
|
|
||||||
CMutableTransaction txCredit;
|
|
||||||
txCredit.nVersion = 1;
|
|
||||||
txCredit.nLockTime = 0;
|
|
||||||
txCredit.vin.resize(1);
|
|
||||||
txCredit.vout.resize(1);
|
|
||||||
txCredit.vin[0].prevout.SetNull();
|
|
||||||
txCredit.vin[0].scriptSig = CScript() << CScriptNum(0) << CScriptNum(0);
|
|
||||||
txCredit.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
|
|
||||||
txCredit.vout[0].scriptPubKey = scriptPubKey;
|
|
||||||
txCredit.vout[0].nValue = nValue;
|
|
||||||
|
|
||||||
return txCredit;
|
|
||||||
}
|
|
||||||
|
|
||||||
CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CScriptWitness& scriptWitness, const CTransaction& txCredit)
|
|
||||||
{
|
|
||||||
CMutableTransaction txSpend;
|
|
||||||
txSpend.nVersion = 1;
|
|
||||||
txSpend.nLockTime = 0;
|
|
||||||
txSpend.vin.resize(1);
|
|
||||||
txSpend.vout.resize(1);
|
|
||||||
txSpend.vin[0].scriptWitness = scriptWitness;
|
|
||||||
txSpend.vin[0].prevout.hash = txCredit.GetHash();
|
|
||||||
txSpend.vin[0].prevout.n = 0;
|
|
||||||
txSpend.vin[0].scriptSig = scriptSig;
|
|
||||||
txSpend.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
|
|
||||||
txSpend.vout[0].scriptPubKey = CScript();
|
|
||||||
txSpend.vout[0].nValue = txCredit.vout[0].nValue;
|
|
||||||
|
|
||||||
return txSpend;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& scriptWitness, int flags, const std::string& message, int scriptError, CAmount nValue = 0)
|
void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& scriptWitness, int flags, const std::string& message, int scriptError, CAmount nValue = 0)
|
||||||
{
|
{
|
||||||
bool expect = (scriptError == SCRIPT_ERR_OK);
|
bool expect = (scriptError == SCRIPT_ERR_OK);
|
||||||
|
Loading…
Reference in New Issue
Block a user