From dde99ca891809bd6daeb156d32c1a66faeefbe1c Mon Sep 17 00:00:00 2001 From: Alex Morcos Date: Mon, 26 Oct 2015 11:08:46 -0400 Subject: [PATCH] Store the total sig op count of a tx. Store sum of legacy and P2SH sig op counts. This is calculated in AcceptToMemory pool and storing it saves redoing the expensive calculation in block template creation. (cherry picked from commit c49d5bc9e6c97c47c0bd78604b2c393a7e4af097) --- src/gtest/test_mempool.cpp | 2 +- src/main.cpp | 2 +- src/test/test_bitcoin.cpp | 8 +++++--- src/test/test_bitcoin.h | 4 +++- src/txmempool.cpp | 4 ++-- src/txmempool.h | 5 ++++- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/gtest/test_mempool.cpp b/src/gtest/test_mempool.cpp index ced90a2f6..19067b378 100644 --- a/src/gtest/test_mempool.cpp +++ b/src/gtest/test_mempool.cpp @@ -94,7 +94,7 @@ TEST(Mempool, PriorityStatsDoNotCrash) { unsigned int nHeight = 92045; double dPriority = view.GetPriority(tx, nHeight); - CTxMemPoolEntry entry(tx, nFees, nTime, dPriority, nHeight, true, false, SPROUT_BRANCH_ID); + CTxMemPoolEntry entry(tx, nFees, nTime, dPriority, nHeight, true, false, 0, SPROUT_BRANCH_ID); // Check it does not crash (ie. the death test fails) EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(testPool.addUnchecked(tx.GetHash(), entry), ""), ""); diff --git a/src/main.cpp b/src/main.cpp index a592c4fca..e0694c744 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1886,7 +1886,7 @@ bool AcceptToMemoryPool( // For v1-v4 transactions, we don't yet know if the transaction commits // to consensusBranchId, but if the entry gets added to the mempool, then // it has passed ContextualCheckInputs and therefore this is correct. - CTxMemPoolEntry entry(tx, nFees, GetTime(), dPriority, chainActive.Height(), pool.HasNoInputsOf(tx), fSpendsCoinbase, consensusBranchId); + CTxMemPoolEntry entry(tx, nFees, GetTime(), dPriority, chainActive.Height(), pool.HasNoInputsOf(tx), fSpendsCoinbase, nSigOps, consensusBranchId); unsigned int nSize = entry.GetTxSize(); // Before zcashd 4.2.0, we had a condition here to always accept a tx if it contained diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index 088e730cf..301aa93b1 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -208,9 +208,11 @@ TestChain100Setup::~TestChain100Setup() CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(CMutableTransaction &tx, CTxMemPool *pool) { - return CTxMemPoolEntry(tx, nFee, nTime, dPriority, nHeight, - pool ? pool->HasNoInputsOf(tx) : hadNoDependencies, - spendsCoinbase, nBranchId); + CTransaction txn(tx); + bool hasNoDependencies = pool ? pool->HasNoInputsOf(tx) : hadNoDependencies; + + return CTxMemPoolEntry(txn, nFee, nTime, dPriority, nHeight, + hasNoDependencies, spendsCoinbase, sigOpCount, nBranchId); } void Shutdown(void* parg) diff --git a/src/test/test_bitcoin.h b/src/test/test_bitcoin.h index 8b236a84a..02cb77c03 100644 --- a/src/test/test_bitcoin.h +++ b/src/test/test_bitcoin.h @@ -75,11 +75,12 @@ struct TestMemPoolEntryHelper unsigned int nHeight; bool hadNoDependencies; bool spendsCoinbase; + unsigned int sigOpCount; uint32_t nBranchId; TestMemPoolEntryHelper() : nFee(0), nTime(0), dPriority(0.0), nHeight(1), - hadNoDependencies(false), spendsCoinbase(false), + hadNoDependencies(false), spendsCoinbase(false), sigOpCount(1), nBranchId(SPROUT_BRANCH_ID) { } CTxMemPoolEntry FromTx(CMutableTransaction &tx, CTxMemPool *pool = NULL); @@ -91,6 +92,7 @@ struct TestMemPoolEntryHelper TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; } TestMemPoolEntryHelper &HadNoDependencies(bool _hnd) { hadNoDependencies = _hnd; return *this; } TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; } + TestMemPoolEntryHelper &SigOps(unsigned int _sigops) { sigOpCount = _sigops; return *this; } TestMemPoolEntryHelper &BranchId(uint32_t _branchId) { nBranchId = _branchId; return *this; } }; diff --git a/src/txmempool.cpp b/src/txmempool.cpp index e970756cf..b801476ec 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -31,10 +31,10 @@ CTxMemPoolEntry::CTxMemPoolEntry(): CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee, int64_t _nTime, double _dPriority, unsigned int _nHeight, bool poolHasNoInputsOf, - bool _spendsCoinbase, uint32_t _nBranchId): + bool _spendsCoinbase, unsigned int _sigOps, uint32_t _nBranchId): tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight), hadNoDependencies(poolHasNoInputsOf), - spendsCoinbase(_spendsCoinbase), nBranchId(_nBranchId) + spendsCoinbase(_spendsCoinbase), sigOpCount(_sigOps), nBranchId(_nBranchId) { nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); nModSize = tx.CalculateModifiedSize(nTxSize); diff --git a/src/txmempool.h b/src/txmempool.h index 602f70fca..38ab7acb6 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -56,12 +56,14 @@ private: unsigned int nHeight; //!< Chain height when entering the mempool bool hadNoDependencies; //!< Not dependent on any other txs when it entered the mempool bool spendsCoinbase; //!< keep track of transactions that spend a coinbase + unsigned int sigOpCount; //!< Legacy sig ops plus P2SH sig op count uint32_t nBranchId; //!< Branch ID this transaction is known to commit to, cached for efficiency public: CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee, int64_t _nTime, double _dPriority, unsigned int _nHeight, - bool poolHasNoInputsOf, bool spendsCoinbase, uint32_t nBranchId); + bool poolHasNoInputsOf, bool spendsCoinbase, + unsigned int nSigOps, uint32_t nBranchId); CTxMemPoolEntry(); CTxMemPoolEntry(const CTxMemPoolEntry& other); @@ -73,6 +75,7 @@ public: int64_t GetTime() const { return nTime; } unsigned int GetHeight() const { return nHeight; } bool WasClearAtEntry() const { return hadNoDependencies; } + unsigned int GetSigOpCount() const { return sigOpCount; } size_t DynamicMemoryUsage() const { return nUsageSize; } bool GetSpendsCoinbase() const { return spendsCoinbase; }