diff --git a/src/Makefile.am b/src/Makefile.am index e0a118991..5b4ec878d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -105,7 +105,6 @@ BITCOIN_CORE_H = \ hash.h \ httprpc.h \ httpserver.h \ - indirectmap.h \ init.h \ key.h \ keystore.h \ diff --git a/src/indirectmap.h b/src/indirectmap.h deleted file mode 100644 index 28e1e8ded..000000000 --- a/src/indirectmap.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef BITCOIN_INDIRECTMAP_H -#define BITCOIN_INDIRECTMAP_H - -template -struct DereferencingComparator { bool operator()(const T a, const T b) const { return *a < *b; } }; - -/* Map whose keys are pointers, but are compared by their dereferenced values. - * - * Differs from a plain std::map > in - * that methods that take a key for comparison take a K rather than taking a K* - * (taking a K* would be confusing, since it's the value rather than the address - * of the object for comparison that matters due to the dereferencing comparator). - * - * Objects pointed to by keys must not be modified in any way that changes the - * result of DereferencingComparator. - */ -template -class indirectmap { -private: - typedef std::map > base; - base m; -public: - typedef typename base::iterator iterator; - typedef typename base::const_iterator const_iterator; - typedef typename base::size_type size_type; - typedef typename base::value_type value_type; - - // passthrough (pointer interface) - std::pair insert(const value_type& value) { return m.insert(value); } - - // pass address (value interface) - iterator find(const K& key) { return m.find(&key); } - const_iterator find(const K& key) const { return m.find(&key); } - iterator lower_bound(const K& key) { return m.lower_bound(&key); } - const_iterator lower_bound(const K& key) const { return m.lower_bound(&key); } - size_type erase(const K& key) { return m.erase(&key); } - size_type count(const K& key) const { return m.count(&key); } - - // passthrough - bool empty() const { return m.empty(); } - size_type size() const { return m.size(); } - size_type max_size() const { return m.max_size(); } - void clear() { m.clear(); } - iterator begin() { return m.begin(); } - iterator end() { return m.end(); } - const_iterator begin() const { return m.begin(); } - const_iterator end() const { return m.end(); } - const_iterator cbegin() const { return m.cbegin(); } - const_iterator cend() const { return m.cend(); } -}; - -#endif // BITCOIN_INDIRECTMAP_H diff --git a/src/main.cpp b/src/main.cpp index 8bafdfb6a..a05aba09a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1119,8 +1119,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState &state, const C { COutPoint outpoint = txin.prevout; // A respend is a tx that conflicts with a member of the pool - auto itConflicting = pool.mapNextTx.find(txin.prevout); - if (itConflicting != pool.mapNextTx.end()) + if (pool.mapNextTx.count(txin.prevout)) { fRespend = true; // Relay only one tx per respent outpoint, but not if tx is equivalent to pool member @@ -1130,7 +1129,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState &state, const C break; } /* - const CTransaction *ptxConflicting = itConflicting->second; + const CTransaction *ptxConflicting = pool.mapNextTx[txin.prevout].ptx; if (!setConflicts.count(ptxConflicting->GetHash())) { // Allow opt-out of transaction replacement by setting diff --git a/src/memusage.h b/src/memusage.h index 9c98e5c2c..49760e64c 100644 --- a/src/memusage.h +++ b/src/memusage.h @@ -5,8 +5,6 @@ #ifndef BITCOIN_MEMUSAGE_H #define BITCOIN_MEMUSAGE_H -#include "indirectmap.h" - #include #include @@ -108,20 +106,6 @@ static inline size_t IncrementalDynamicUsage(const std::map& m) return MallocUsage(sizeof(stl_tree_node >)); } -// indirectmap has underlying map with pointer as key - -template -static inline size_t DynamicUsage(const indirectmap& m) -{ - return MallocUsage(sizeof(stl_tree_node >)) * m.size(); -} - -template -static inline size_t IncrementalDynamicUsage(const indirectmap& m) -{ - return MallocUsage(sizeof(stl_tree_node >)); -} - // Boost data structures template diff --git a/src/txmempool.cpp b/src/txmempool.cpp index be8b2c206..9e3b21778 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -147,11 +147,11 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector &vHashes if (it == mapTx.end()) { continue; } - auto iter = mapNextTx.lower_bound(COutPoint(hash, 0)); + std::map::iterator iter = mapNextTx.lower_bound(COutPoint(hash, 0)); // First calculate the children, and update setMemPoolChildren to // include them, and update their setMemPoolParents to include this tx. - for (; iter != mapNextTx.end() && iter->first->hash == hash; ++iter) { - const uint256 &childHash = iter->second->GetHash(); + for (; iter != mapNextTx.end() && iter->first.hash == hash; ++iter) { + const uint256 &childHash = iter->second.ptx->GetHash(); txiter childIter = mapTx.find(childHash); assert(childIter != mapTx.end()); // We can skip updating entries we've encountered before or that @@ -365,11 +365,11 @@ void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins) { LOCK(cs); - auto it = mapNextTx.lower_bound(COutPoint(hashTx, 0)); + std::map::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0)); // iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx - while (it != mapNextTx.end() && it->first->hash == hashTx) { - coins.Spend(it->first->n); // and remove those outputs from coins + while (it != mapNextTx.end() && it->first.hash == hashTx) { + coins.Spend(it->first.n); // and remove those outputs from coins it++; } } @@ -414,7 +414,7 @@ bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, const CTransaction& tx = newit->GetTx(); std::set setParentTransactions; for (unsigned int i = 0; i < tx.vin.size(); i++) { - mapNextTx.insert(std::make_pair(&tx.vin[i].prevout, &tx)); + mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i); setParentTransactions.insert(tx.vin[i].prevout.hash); } // Don't bother worrying about child transactions of this one. @@ -641,10 +641,10 @@ void CTxMemPool::removeRecursive(const CTransaction &origTx, std::list::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i)); if (it == mapNextTx.end()) continue; - txiter nextit = mapTx.find(it->second->GetHash()); + txiter nextit = mapTx.find(it->second.ptx->GetHash()); assert(nextit != mapTx.end()); txToRemove.insert(nextit); } @@ -701,9 +701,9 @@ void CTxMemPool::removeConflicts(const CTransaction &tx, std::list // Remove transactions which depend on inputs of tx, recursively LOCK(cs); BOOST_FOREACH(const CTxIn &txin, tx.vin) { - auto it = mapNextTx.find(txin.prevout); + std::map::iterator it = mapNextTx.find(txin.prevout); if (it != mapNextTx.end()) { - const CTransaction &txConflict = *it->second; + const CTransaction &txConflict = *it->second.ptx; if (txConflict != tx) { removeRecursive(txConflict, removed); @@ -811,10 +811,10 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const assert(coins && coins->IsAvailable(txin.prevout.n)); } // Check whether its inputs are marked in mapNextTx. - auto it3 = mapNextTx.find(txin.prevout); + std::map::const_iterator it3 = mapNextTx.find(txin.prevout); assert(it3 != mapNextTx.end()); - assert(it3->first == &txin.prevout); - assert(it3->second == &tx); + assert(it3->second.ptx == &tx); + assert(it3->second.n == i); i++; } assert(setParentCheck == GetMemPoolParents(it)); @@ -841,10 +841,10 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const // Check children against mapNextTx CTxMemPool::setEntries setChildrenCheck; - auto iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetHash(), 0)); + std::map::const_iterator iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetHash(), 0)); int64_t childSizes = 0; - for (; iter != mapNextTx.end() && iter->first->hash == it->GetTx().GetHash(); ++iter) { - txiter childit = mapTx.find(iter->second->GetHash()); + for (; iter != mapNextTx.end() && iter->first.hash == it->GetTx().GetHash(); ++iter) { + txiter childit = mapTx.find(iter->second.ptx->GetHash()); assert(childit != mapTx.end()); // mapNextTx points to in-mempool transactions if (setChildrenCheck.insert(childit).second) { childSizes += childit->GetTxSize(); @@ -878,12 +878,14 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const stepsSinceLastRemove = 0; } } - for (auto it = mapNextTx.cbegin(); it != mapNextTx.cend(); it++) { - uint256 hash = it->second->GetHash(); + for (std::map::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) { + uint256 hash = it->second.ptx->GetHash(); indexed_transaction_set::const_iterator it2 = mapTx.find(hash); const CTransaction& tx = it2->GetTx(); assert(it2 != mapTx.end()); - assert(&tx == it->second); + assert(&tx == it->second.ptx); + assert(tx.vin.size() > it->second.n); + assert(it->first == it->second.ptx->vin[it->second.n].prevout); } assert(totalTxSize == checkTotal); @@ -1172,8 +1174,8 @@ void CTxMemPool::TrimToSize(size_t sizelimit, std::vector* pvNoSpendsRe BOOST_FOREACH(const CTxIn& txin, tx.vin) { if (exists(txin.prevout.hash)) continue; - auto it = mapNextTx.lower_bound(COutPoint(txin.prevout.hash, 0)); - if (it == mapNextTx.end() || it->first->hash != txin.prevout.hash) + std::map::iterator it = mapNextTx.lower_bound(COutPoint(txin.prevout.hash, 0)); + if (it == mapNextTx.end() || it->first.hash != txin.prevout.hash) pvNoSpendsRemaining->push_back(txin.prevout.hash); } } diff --git a/src/txmempool.h b/src/txmempool.h index a59cea1e2..864a0c97e 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -13,7 +13,6 @@ #include "spentindex.h" #include "amount.h" #include "coins.h" -#include "indirectmap.h" #include "primitives/transaction.h" #include "sync.h" @@ -309,6 +308,20 @@ struct ancestor_score {}; class CBlockPolicyEstimator; +/** An inpoint - a combination of a transaction and an index n into its vin */ +class CInPoint +{ +public: + const CTransaction* ptx; + uint32_t n; + + CInPoint() { SetNull(); } + CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; } + void SetNull() { ptx = NULL; n = (uint32_t) -1; } + bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); } + size_t DynamicMemoryUsage() const { return 0; } +}; + /** * CTxMemPool stores valid-according-to-the-current-best-chain * transactions that may be included in the next block. @@ -479,7 +492,7 @@ private: void UpdateChild(txiter entry, txiter child, bool add); public: - indirectmap mapNextTx; + std::map mapNextTx; std::map > mapDeltas; /** Create a new CTxMemPool.