diff --git a/src/Application.h b/src/Application.h index 819340832..288f505bc 100644 --- a/src/Application.h +++ b/src/Application.h @@ -96,7 +96,6 @@ public: ValidationCollection& getValidations() { return mValidations; } JobQueue& getJobQueue() { return mJobQueue; } bool isNew(const uint256& s) { return mSuppressions.addSuppression(s); } - bool isNew(const uint160& s) { return mSuppressions.addSuppression(s); } bool running() { return mTxnDB != NULL; } bool getSystemTimeOffset(int& offset) { return mSNTPClient.getOffset(offset); } diff --git a/src/Suppression.cpp b/src/Suppression.cpp index 4b7cad571..2576a2aed 100644 --- a/src/Suppression.cpp +++ b/src/Suppression.cpp @@ -1,39 +1,68 @@ + #include "Suppression.h" #include -bool SuppressionTable::addSuppression(const uint160& suppression) -{ - boost::mutex::scoped_lock sl(mSuppressionMutex); +DECLARE_INSTANCE(Suppression); - if (mSuppressionMap.find(suppression) != mSuppressionMap.end()) - return false; +Suppression& SuppressionTable::findCreateEntry(const uint256& index, bool& created) +{ + boost::unordered_map::iterator fit = mSuppressionMap.find(index); + + if (fit != mSuppressionMap.end()) + { + created = false; + return fit->second; + } + created = true; time_t now = time(NULL); time_t expireTime = now - mHoldTime; - boost::unordered_map< time_t, std::list >::iterator - it = mSuppressionTimes.begin(), end = mSuppressionTimes.end(); - while (it != end) + // See if any supressions need to be expired + std::map< time_t, std::list >::iterator it = mSuppressionTimes.begin(); + if ((it != mSuppressionTimes.end()) && (it->first <= expireTime)) { - if (it->first <= expireTime) - { - BOOST_FOREACH(const uint160& lit, it->second) - mSuppressionMap.erase(lit); - it = mSuppressionTimes.erase(it); - } - else ++it; + BOOST_FOREACH(const uint256& lit, it->second) + mSuppressionMap.erase(lit); + mSuppressionTimes.erase(it); } - mSuppressionMap[suppression] = now; - mSuppressionTimes[now].push_back(suppression); - - return true; + mSuppressionTimes[now].push_back(index); + return mSuppressionMap.insert(std::make_pair(index, Suppression())).first->second; } -bool SuppressionTable::addSuppression(const uint256& suppression) +bool SuppressionTable::addSuppression(const uint256& index) { - uint160 u; - memcpy(u.begin(), suppression.begin() + (suppression.size() - u.size()), u.size()); - return addSuppression(u); + boost::mutex::scoped_lock sl(mSuppressionMutex); + + bool created; + findCreateEntry(index, created); + return created; +} + +Suppression SuppressionTable::getEntry(const uint256& index) +{ + boost::mutex::scoped_lock sl(mSuppressionMutex); + + bool created; + return findCreateEntry(index, created); +} + +bool SuppressionTable::addSuppressionPeer(const uint256& index, uint64 peer) +{ + boost::mutex::scoped_lock sl(mSuppressionMutex); + + bool created; + findCreateEntry(index, created).addPeer(peer); + return created; +} + +bool SuppressionTable::addSuppressionFlags(const uint256& index, int flag) +{ + boost::mutex::scoped_lock sl(mSuppressionMutex); + + bool created; + findCreateEntry(index, created).setFlag(flag); + return created; } diff --git a/src/Suppression.h b/src/Suppression.h index 608a57d73..18abb3a98 100644 --- a/src/Suppression.h +++ b/src/Suppression.h @@ -1,14 +1,36 @@ #ifndef __SUPPRESSION__ #define __SUPPRESSION__ +#include +#include #include #include #include #include "uint256.h" +#include "types.h" +#include "InstanceCounter.h" -extern std::size_t hash_value(const uint160& u); +DEFINE_INSTANCE(Suppression); + +class Suppression : private IS_INSTANCE(Suppression) +{ +protected: + int mFlags; + std::set mPeers; + +public: + Suppression() : mFlags(0) { ; } + + const std::set& peekPeers() { return mPeers; } + void addPeer(uint64 peer) { mPeers.insert(peer); } + bool hasPeer(uint64 peer) { return mPeers.count(peer) > 0; } + + bool hasFlag(int f) { return (mFlags & f) != 0; } + void setFlag(int f) { mFlags |= f; } + void clearFlag(int f) { mFlags &= ~f; } +}; class SuppressionTable { @@ -17,18 +39,24 @@ protected: boost::mutex mSuppressionMutex; // Stores all suppressed hashes and their expiration time - boost::unordered_map mSuppressionMap; + boost::unordered_map mSuppressionMap; // Stores all expiration times and the hashes indexed for them - boost::unordered_map< time_t, std::list > mSuppressionTimes; + std::map< time_t, std::list > mSuppressionTimes; int mHoldTime; + Suppression& findCreateEntry(const uint256&, bool& created); + public: SuppressionTable(int holdTime = 120) : mHoldTime(holdTime) { ; } - bool addSuppression(const uint256& suppression); - bool addSuppression(const uint160& suppression); + bool addSuppression(const uint256& index); + + bool addSuppressionPeer(const uint256& index, uint64 peer); + bool addSuppressionFlags(const uint256& index, int flag); + + Suppression getEntry(const uint256&); }; #endif