diff --git a/src/Makefile.bench.include b/src/Makefile.bench.include index 309c507e0..e63987574 100644 --- a/src/Makefile.bench.include +++ b/src/Makefile.bench.include @@ -16,7 +16,6 @@ bench_bench_bitcoin_SOURCES = \ bench/crypto_hash.cpp \ bench/ccoins_caching.cpp \ bench/mempool_eviction.cpp \ - bench/verify_script.cpp \ bench/base58.cpp bench_bench_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(EVENT_CLFAGS) $(EVENT_PTHREADS_CFLAGS) -I$(builddir)/bench/ diff --git a/src/bench/mempool_eviction.cpp b/src/bench/mempool_eviction.cpp index 0ae69c75f..eaf7dfe8f 100644 --- a/src/bench/mempool_eviction.cpp +++ b/src/bench/mempool_eviction.cpp @@ -108,7 +108,7 @@ static void MempoolEviction(benchmark::State& state) AddTx(tx6, 1100LL, pool); AddTx(tx7, 9000LL, pool); pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); - pool.TrimToSize(GetVirtualTransactionSize(tx1)); + // pool.TrimToSize(tx1.GetTotalSize()) } } diff --git a/src/bench/verify_script.cpp b/src/bench/verify_script.cpp deleted file mode 100644 index dc3940cdb..000000000 --- a/src/bench/verify_script.cpp +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) 2016 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 "bench.h" -#include "key.h" -#if defined(HAVE_CONSENSUS_LIB) -#include "script/bitcoinconsensus.h" -#endif -#include "script/script.h" -#include "script/sign.h" -#include "streams.h" - -// 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.wit.vtxinwit.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 -// modified to measure performance of other types of scripts. -static void VerifyScriptBench(benchmark::State& state) -{ - const int flags = SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH; - const int witnessversion = 0; - - // Keypair. - CKey key; - const unsigned char vchKey[32] = {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}; - key.Set(vchKey, vchKey + 32, false); - CPubKey pubkey = key.GetPubKey(); - uint160 pubkeyHash; - CHash160().Write(pubkey.begin(), pubkey.size()).Finalize(pubkeyHash.begin()); - - // Script. - CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash); - CScript scriptSig; - CScript witScriptPubkey = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkeyHash) << OP_EQUALVERIFY << OP_CHECKSIG; - CTransaction txCredit = BuildCreditingTransaction(scriptPubKey); - CMutableTransaction txSpend = BuildSpendingTransaction(scriptSig, txCredit); - CScriptWitness& witness = txSpend.wit.vtxinwit[0].scriptWitness; - witness.stack.emplace_back(); - key.Sign(SignatureHash(witScriptPubkey, txSpend, 0, SIGHASH_ALL, txCredit.vout[0].nValue, SIGVERSION_WITNESS_V0), witness.stack.back(), 0); - witness.stack.back().push_back(static_cast(SIGHASH_ALL)); - witness.stack.push_back(ToByteVector(pubkey)); - - // Benchmark. - while (state.KeepRunning()) { - ScriptError err; - bool success = VerifyScript( - txSpend.vin[0].scriptSig, - txCredit.vout[0].scriptPubKey, - &txSpend.wit.vtxinwit[0].scriptWitness, - flags, - MutableTransactionSignatureChecker(&txSpend, 0, txCredit.vout[0].nValue), - &err); - assert(err == SCRIPT_ERR_OK); - assert(success); - -#if defined(HAVE_CONSENSUS_LIB) - CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); - stream << txSpend; - int csuccess = bitcoinconsensus_verify_script_with_amount( - begin_ptr(txCredit.vout[0].scriptPubKey), - txCredit.vout[0].scriptPubKey.size(), - txCredit.vout[0].nValue, - (const unsigned char*)&stream[0], stream.size(), 0, flags, nullptr); - assert(csuccess == 1); -#endif - } -} - -BENCHMARK(VerifyScriptBench); diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index 1cd8f3055..d5f2a0a47 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -116,6 +116,11 @@ CAmount CTransaction::GetValueOut() const return nValueOut; } +unsigned int CTransaction::GetTotalSize() const +{ + return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION); +} + double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const { nTxSize = CalculateModifiedSize(nTxSize); @@ -148,7 +153,7 @@ std::string CTransaction::ToString() const str += strprintf("CTransaction(hash=%s, ver=%d, nTime=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n", GetHash().ToString().substr(0,10), nVersion, - nTime, + nTime, vin.size(), vout.size(), nLockTime); diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index 7735f0335..64692a182 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -286,17 +286,23 @@ public: // Compute modified tx size for priority calculation (optionally given tx size) unsigned int CalculateModifiedSize(unsigned int nTxSize=0) const; - bool IsCoinStake() const - { - // the coin stake transaction is marked with the first output empty - return (vin.size() > 0 && (!vin[0].prevout.IsNull()) && vout.size() >= 2 && vout[0].IsEmpty()); - } + /** + * Get the total transaction size in bytes. + * @return Total transaction size in bytes + */ + unsigned int GetTotalSize() const; bool IsCoinBase() const { return (vin.size() == 1 && vin[0].prevout.IsNull()); } + bool IsCoinStake() const + { + // the coin stake transaction is marked with the first output empty + return (vin.size() > 0 && (!vin[0].prevout.IsNull()) && vout.size() >= 2 && vout[0].IsEmpty()); + } + friend bool operator==(const CTransaction& a, const CTransaction& b) { return a.hash == b.hash; diff --git a/src/test/multisig_tests.cpp b/src/test/multisig_tests.cpp index c023a0cd5..b7cf93751 100644 --- a/src/test/multisig_tests.cpp +++ b/src/test/multisig_tests.cpp @@ -29,7 +29,7 @@ BOOST_FIXTURE_TEST_SUITE(multisig_tests, BasicTestingSetup) CScript sign_multisig(CScript scriptPubKey, vector keys, CTransaction transaction, int whichIn) { - uint256 hash = SignatureHash(scriptPubKey, transaction, whichIn, SIGHASH_ALL); + uint256 hash = SignatureHash(scriptPubKey, transaction, whichIn, SIGHASH_ALL, 0); CScript result; result << OP_0; // CHECKMULTISIG bug workaround diff --git a/src/test/txvalidationcache_tests.cpp b/src/test/txvalidationcache_tests.cpp index 237b26329..49538314e 100644 --- a/src/test/txvalidationcache_tests.cpp +++ b/src/test/txvalidationcache_tests.cpp @@ -48,7 +48,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup) // Sign: std::vector vchSig; - uint256 hash = SignatureHash(scriptPubKey, spends[i], 0, SIGHASH_ALL); + uint256 hash = SignatureHash(scriptPubKey, spends[i], 0, SIGHASH_ALL, 0); BOOST_CHECK(coinbaseKey.Sign(hash, vchSig)); vchSig.push_back((unsigned char)SIGHASH_ALL); spends[i].vin[0].scriptSig << vchSig;