Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Integrate LruCache into transaction queue
Browse files Browse the repository at this point in the history
Use LruCache to manage dropped transactions
  • Loading branch information
halfalicious committed Jul 30, 2019
1 parent eb8969b commit c7e5f2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
24 changes: 10 additions & 14 deletions libethereum/TransactionQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ using namespace std;
using namespace dev;
using namespace dev::eth;

namespace
{
constexpr size_t c_maxVerificationQueueSize = 8192;
constexpr size_t c_maxDroppedTransactionCount = 1024;
} // namespace

TransactionQueue::TransactionQueue(unsigned _limit, unsigned _futureLimit):
m_current(PriorityCompare { *this }),
m_limit(_limit),
m_futureLimit(_futureLimit)
TransactionQueue::TransactionQueue(unsigned _limit, unsigned _futureLimit)
: m_dropped{c_maxDroppedTransactionCount},
m_current{PriorityCompare{*this}},
m_limit{_limit},
m_futureLimit{_futureLimit}
{
unsigned verifierThreads = std::max(thread::hardware_concurrency(), 3U) - 2U;
for (unsigned i = 0; i < verifierThreads; ++i)
Expand Down Expand Up @@ -54,7 +58,7 @@ ImportResult TransactionQueue::check_WITH_LOCK(h256 const& _h, IfDropped _ik)
if (m_known.count(_h))
return ImportResult::AlreadyKnown;

if (m_dropped.count(_h) && _ik == IfDropped::Ignore)
if (m_dropped.contains(_h) && _ik == IfDropped::Ignore)
return ImportResult::AlreadyInChain;

return ImportResult::Success;
Expand Down Expand Up @@ -312,15 +316,7 @@ void TransactionQueue::drop(h256 const& _txHash)
return;

UpgradeGuard ul(l);
if (m_dropped.size() == c_maxDroppedTransactionCount)
{
LOG(m_loggerDetail) << "Dropped transaction list is at capacity ("
<< c_maxDroppedTransactionCount
<< "), removing dropped transaction from list (txhash: "
<< *m_dropped.begin() << ")";
m_dropped.erase(m_dropped.begin());
}
m_dropped.insert(_txHash);
m_dropped.insert(_txHash, true /* placeholder value */);
remove_WITH_LOCK(_txHash);
}

Expand Down
17 changes: 11 additions & 6 deletions libethereum/TransactionQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@

#pragma once

#include <functional>
#include <condition_variable>
#include <thread>
#include <deque>
#include "Transaction.h"
#include <libdevcore/Common.h>
#include <libdevcore/Guards.h>
#include <libdevcore/Log.h>
#include <libdevcore/LruCache.h>
#include <libethcore/Common.h>
#include "Transaction.h"
#include <condition_variable>
#include <deque>
#include <functional>
#include <thread>

namespace dev
{
Expand Down Expand Up @@ -189,7 +190,11 @@ class TransactionQueue
h256Hash m_known; ///< Headers of transactions in both sets.

std::unordered_map<h256, std::function<void(ImportResult)>> m_callbacks; ///< Called once.
h256Hash m_dropped; ///< Transactions that have previously been dropped

///< Transactions that have previously been dropped. We technically only need to store the tx
///< hash, but we also store bool as a placeholder value so that we can use an LRU cache to cap
///< the number of transaction hashes stored.
LruCache<h256, bool> m_dropped;

PriorityQueue m_current;
std::unordered_map<h256, PriorityQueue::iterator> m_currentByHash; ///< Transaction hash to set ref
Expand Down

0 comments on commit c7e5f2f

Please sign in to comment.