Next generation supression code.

This commit is contained in:
JoelKatz
2012-10-31 17:32:26 -07:00
parent 8e4b11b668
commit bcff9fad17
3 changed files with 85 additions and 29 deletions

View File

@@ -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); }

View File

@@ -1,39 +1,68 @@
#include "Suppression.h"
#include <boost/foreach.hpp>
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<uint256, Suppression>::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<uint160> >::iterator
it = mSuppressionTimes.begin(), end = mSuppressionTimes.end();
while (it != end)
// See if any supressions need to be expired
std::map< time_t, std::list<uint256> >::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;
}

View File

@@ -1,14 +1,36 @@
#ifndef __SUPPRESSION__
#define __SUPPRESSION__
#include <set>
#include <map>
#include <list>
#include <boost/unordered_map.hpp>
#include <boost/thread/mutex.hpp>
#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<uint64> mPeers;
public:
Suppression() : mFlags(0) { ; }
const std::set<uint64>& 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<uint160, time_t> mSuppressionMap;
boost::unordered_map<uint256, Suppression> mSuppressionMap;
// Stores all expiration times and the hashes indexed for them
boost::unordered_map< time_t, std::list<uint160> > mSuppressionTimes;
std::map< time_t, std::list<uint256> > 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