From 2206ff3be6882bc08ce9acc97fc2f1c14a4c7009 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 4 Jun 2012 06:01:11 -0700 Subject: [PATCH] Get rid of hash_SMN and instead extend boost::hash. This makes the TaggedCache code cleaner. --- src/LedgerConsensus.h | 12 ++++++------ src/NetworkOPs.cpp | 2 +- src/Peer.cpp | 4 +++- src/SHAMap.cpp | 6 +++--- src/SHAMap.h | 14 ++++---------- src/TaggedCache.h | 23 ++++++++++++----------- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/LedgerConsensus.h b/src/LedgerConsensus.h index e2b69e8fd2..9541f926c3 100644 --- a/src/LedgerConsensus.h +++ b/src/LedgerConsensus.h @@ -47,7 +47,7 @@ protected: int mYays, mNays; bool mOurPosition; std::vector transaction; - boost::unordered_map mVotes; + boost::unordered_map mVotes; public: typedef boost::shared_ptr pointer; @@ -85,17 +85,17 @@ protected: LedgerProposal::pointer mOurPosition; // Convergence tracking, trusted peers indexed by hash of public key - boost::unordered_map mPeerPositions; + boost::unordered_map mPeerPositions; // Transaction Sets, indexed by hash of transaction tree - boost::unordered_map mComplete; - boost::unordered_map mAcquiring; + boost::unordered_map mComplete; + boost::unordered_map mAcquiring; // Peer sets - boost::unordered_map >, hash_SMN> mPeerData; + boost::unordered_map > > mPeerData; // Disputed transactions - boost::unordered_map mDisputes; + boost::unordered_map mDisputes; // final accept logic static void Saccept(boost::shared_ptr This, SHAMap::pointer txSet); diff --git a/src/NetworkOPs.cpp b/src/NetworkOPs.cpp index 64cdc2ca7d..33838890bd 100644 --- a/src/NetworkOPs.cpp +++ b/src/NetworkOPs.cpp @@ -285,7 +285,7 @@ void NetworkOPs::checkState(const boost::system::error_code& result) // Do we have sufficient validations for our last closed ledger? Or do sufficient nodes // agree? And do we have no better ledger available? // If so, we are either tracking or full. - boost::unordered_map ledgers; + boost::unordered_map ledgers; for (std::vector::iterator it = peerList.begin(), end = peerList.end(); it != end; ++it) { diff --git a/src/Peer.cpp b/src/Peer.cpp index 02b975cae5..893db52ab0 100644 --- a/src/Peer.cpp +++ b/src/Peer.cpp @@ -726,6 +726,7 @@ void Peer::recvGetLedger(newcoin::TMGetLedger& packet) { SHAMap::pointer map; newcoin::TMLedgerData reply; + bool fatLeaves = true; if (packet.itype() == newcoin::liTS_CANDIDATE) { // Request is for a transaction candidate set @@ -748,6 +749,7 @@ void Peer::recvGetLedger(newcoin::TMGetLedger& packet) reply.set_ledgerseq(0); reply.set_ledgerhash(txHash.begin(), txHash.size()); reply.set_type(newcoin::liTS_CANDIDATE); + fatLeaves = false; // We'll already have most transactions } else { // Figure out what ledger they want @@ -828,7 +830,7 @@ void Peer::recvGetLedger(newcoin::TMGetLedger& packet) } std::vector nodeIDs; std::list< std::vector > rawNodes; - if(map->getNodeFat(mn, nodeIDs, rawNodes)) + if(map->getNodeFat(mn, nodeIDs, rawNodes, fatLeaves)) { std::vector::iterator nodeIDIterator; std::list< std::vector >::iterator rawNodeIterator; diff --git a/src/SHAMap.cpp b/src/SHAMap.cpp index 31ef55c436..709a732b1c 100644 --- a/src/SHAMap.cpp +++ b/src/SHAMap.cpp @@ -15,14 +15,14 @@ #include "SHAMap.h" #include "Application.h" -std::size_t hash_SMN::operator() (const SHAMapNode& mn) const +std::size_t hash_value(const SHAMapNode& mn) { return mn.getDepth() ^ *reinterpret_cast(mn.getNodeID().begin()) ^ *reinterpret_cast(theApp->getNonce256().begin()); } -std::size_t hash_SMN::operator() (const uint256& u) const +std::size_t hash_value(const uint256& u) { return *reinterpret_cast(u.begin()) ^ *reinterpret_cast(theApp->getNonce256().begin()); @@ -671,7 +671,7 @@ void SHAMap::dump(bool hash) std::cerr << " MAP Contains" << std::endl; boost::recursive_mutex::scoped_lock sl(mLock); - for(boost::unordered_map::iterator it = mTNByID.begin(); + for(boost::unordered_map::iterator it = mTNByID.begin(); it != mTNByID.end(); ++it) { std::cerr << it->second->getString() << std::endl; diff --git a/src/SHAMap.h b/src/SHAMap.h index d5c79586d6..329ce65f6e 100644 --- a/src/SHAMap.h +++ b/src/SHAMap.h @@ -76,14 +76,8 @@ public: SHAMapNode(const void *ptr, int len); }; -class hash_SMN -{ - -public: - std::size_t operator() (const SHAMapNode& mn) const; - - std::size_t operator() (const uint256& u) const; -}; +extern std::size_t hash_value(const SHAMapNode& mn); +extern std::size_t hash_value(const uint256& u); class SHAMapItem { // an item stored in a SHAMap @@ -234,7 +228,7 @@ public: private: uint32 mSeq; mutable boost::recursive_mutex mLock; - boost::unordered_map mTNByID; + boost::unordered_map mTNByID; boost::shared_ptr > mDirtyNodes; @@ -301,7 +295,7 @@ public: // comparison/sync functions void getMissingNodes(std::vector& nodeIDs, std::vector& hashes, int max); bool getNodeFat(const SHAMapNode& node, std::vector& nodeIDs, - std::list >& rawNode); + std::list >& rawNode, bool fatLeaves); bool addRootNode(const uint256& hash, const std::vector& rootNode); bool addRootNode(const std::vector& rootNode); bool addKnownNode(const SHAMapNode& nodeID, const std::vector& rawNode); diff --git a/src/TaggedCache.h b/src/TaggedCache.h index f5c501d0a9..6f79e172c5 100644 --- a/src/TaggedCache.h +++ b/src/TaggedCache.h @@ -4,6 +4,7 @@ #include #include +#include #include // This class implemented a cache and a map. The cache keeps objects alive @@ -25,10 +26,10 @@ protected: mutable boost::recursive_mutex mLock; int mTargetSize, mTargetAge; - std::map mCache; // Hold strong reference to recent objects + boost::unordered_map mCache; // Hold strong reference to recent objects time_t mLastSweep; - std::map mMap; // Track stored objects + boost::unordered_map mMap; // Track stored objects public: TaggedCache(int size, int age) : mTargetSize(size), mTargetAge(age), mLastSweep(time(NULL)) { ; } @@ -81,7 +82,7 @@ template void TaggedCache::sweep time_t target = now - mTargetAge; // Pass 1, remove old objects from cache - typename std::map::iterator cit = mCache.begin(); + typename boost::unordered_map::iterator cit = mCache.begin(); while (cit != mCache.end()) { if (cit->second->second.first < target) @@ -90,7 +91,7 @@ template void TaggedCache::sweep } // Pass 2, remove dead objects from map - typename std::map::iterator mit = mMap.begin(); + typename boost::unordered_map::iterator mit = mMap.begin(); while (mit != mMap.end()) { if (mit->second->expired()) @@ -104,7 +105,7 @@ template bool TaggedCache::touch boost::recursive_mutex::scoped_lock sl(mLock); // Is the object in the map? - typename std::map::iterator mit = mMap.find(key); + typename boost::unordered_map::iterator mit = mMap.find(key); if (mit == mMap.end()) return false; if (mit->second->expired()) { // in map, but expired @@ -113,7 +114,7 @@ template bool TaggedCache::touch } // Is the object in the cache? - typename std::map::iterator cit = mCache.find(key); + typename boost::unordered_map::iterator cit = mCache.find(key); if (cit != mCache.end()) { // in both map and cache cit->second->first = time(NULL); @@ -129,7 +130,7 @@ template bool TaggedCache::del(c { // Remove from cache, map unaffected boost::recursive_mutex::scoped_lock sl(mLock); - typename std::map::iterator cit = mCache.find(key); + typename boost::unordered_map::iterator cit = mCache.find(key); if (cit == mCache.end()) return false; mCache.erase(cit); return true; @@ -141,7 +142,7 @@ bool TaggedCache::canonicalize(const key_type& key, boost::shared // Return values: true=we had the data already boost::recursive_mutex::scoped_lock sl(mLock); - typename std::map::iterator mit = mMap.find(key); + typename boost::unordered_map::iterator mit = mMap.find(key); if (mit == mMap.end()) { // not in map mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data))); @@ -159,7 +160,7 @@ bool TaggedCache::canonicalize(const key_type& key, boost::shared assert(!!data); // Valid in map, is it in cache? - typename std::map::iterator cit = mCache.find(key); + typename boost::unordered_map::iterator cit = mCache.find(key); if (cit != mCache.end()) cit->second.first = time(NULL); // Yes, refesh else // no, add to cache @@ -174,7 +175,7 @@ boost::shared_ptr TaggedCache::fetch(const key_type& key) boost::recursive_mutex::scoped_lock sl(mLock); // Is it in the map? - typename std::map::iterator mit = mMap.find(key); + typename boost::unordered_map::iterator mit = mMap.find(key); if (mit == mMap.end()) return data_ptr(); // No, we're done if (mit->second.expired()) { // in map, but expired @@ -185,7 +186,7 @@ boost::shared_ptr TaggedCache::fetch(const key_type& key) boost::shared_ptr data = mit->second.lock(); // Valid in map, is it in the cache? - typename std::map::iterator cit = mCache.find(key); + typename boost::unordered_map::iterator cit = mCache.find(key); if (cit != mCache.end()) cit->second.first = time(NULL); // Yes, refresh else // No, add to cache