From 22302319f45056fce1c2a040732c7def0e09ec5e Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 18 Jun 2012 21:00:48 -0700 Subject: [PATCH] Supression table. --- src/Application.h | 4 ++++ src/Suppression.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/Suppression.h | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/Suppression.cpp create mode 100644 src/Suppression.h diff --git a/src/Application.h b/src/Application.h index 7d9ada8ea5..ec00b94e89 100644 --- a/src/Application.h +++ b/src/Application.h @@ -14,6 +14,7 @@ #include "NetworkOPs.h" #include "TaggedCache.h" #include "ValidationCollection.h" +#include "Suppression.h" #include "../database/database.h" @@ -46,6 +47,7 @@ class Application NetworkOPs mNetOps; NodeCache mNodeCache; ValidationCollection mValidations; + SuppressionTable mSuppressions; DatabaseCon *mTxnDB, *mLedgerDB, *mWalletDB, *mHashNodeDB, *mNetNodeDB; @@ -77,6 +79,8 @@ public: TransactionMaster& getMasterTransaction() { return mMasterTransaction; } NodeCache& getNodeCache() { return mNodeCache; } ValidationCollection& getValidations() { return mValidations; } + bool suppress(const uint256& s) { return mSuppressions.addSuppression(s); } + bool suppress(const uint160& s) { return mSuppressions.addSuppression(s); } DatabaseCon* getTxnDB() { return mTxnDB; } DatabaseCon* getLedgerDB() { return mLedgerDB; } diff --git a/src/Suppression.cpp b/src/Suppression.cpp new file mode 100644 index 0000000000..7ed36b5676 --- /dev/null +++ b/src/Suppression.cpp @@ -0,0 +1,37 @@ + +#include "Suppression.h" + +bool SuppressionTable::addSuppression(const uint160& suppression) +{ + boost::mutex::scoped_lock sl(mSuppressionMutex); + + if (mSuppressionMap.find(suppression) != mSuppressionMap.end()) + return false; + + time_t now = time(NULL); + + boost::unordered_map< time_t, std::list >::iterator it = mSuppressionTimes.begin(); + while (it != mSuppressionTimes.end()) + { + if ((it->first + mHoldTime) < now) + { + for (std::list::iterator lit = it->second.begin(), end = it->second.end(); + lit != end; ++lit) + mSuppressionMap.erase(*lit); + mSuppressionTimes.erase(it++); + } + else ++it; + } + + mSuppressionMap[suppression] = now; + mSuppressionTimes[now].push_back(suppression); + + return true; +} + +bool SuppressionTable::addSuppression(const uint256& suppression) +{ + uint160 u; + memcpy(u.begin(), suppression.begin() + (suppression.size() - u.size()), u.size()); + return addSuppression(u); +} diff --git a/src/Suppression.h b/src/Suppression.h new file mode 100644 index 0000000000..608a57d733 --- /dev/null +++ b/src/Suppression.h @@ -0,0 +1,34 @@ +#ifndef __SUPPRESSION__ +#define __SUPPRESSION__ + +#include + +#include +#include + +#include "uint256.h" + +extern std::size_t hash_value(const uint160& u); + +class SuppressionTable +{ +protected: + + boost::mutex mSuppressionMutex; + + // Stores all suppressed hashes and their expiration time + boost::unordered_map mSuppressionMap; + + // Stores all expiration times and the hashes indexed for them + boost::unordered_map< time_t, std::list > mSuppressionTimes; + + int mHoldTime; + +public: + SuppressionTable(int holdTime = 120) : mHoldTime(holdTime) { ; } + + bool addSuppression(const uint256& suppression); + bool addSuppression(const uint160& suppression); +}; + +#endif