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

Commit c7e5f2f

Browse files
committed
Integrate LruCache into transaction queue
Use LruCache to manage dropped transactions
1 parent eb8969b commit c7e5f2f

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

libethereum/TransactionQueue.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ using namespace std;
1111
using namespace dev;
1212
using namespace dev::eth;
1313

14+
namespace
15+
{
1416
constexpr size_t c_maxVerificationQueueSize = 8192;
1517
constexpr size_t c_maxDroppedTransactionCount = 1024;
18+
} // namespace
1619

17-
TransactionQueue::TransactionQueue(unsigned _limit, unsigned _futureLimit):
18-
m_current(PriorityCompare { *this }),
19-
m_limit(_limit),
20-
m_futureLimit(_futureLimit)
20+
TransactionQueue::TransactionQueue(unsigned _limit, unsigned _futureLimit)
21+
: m_dropped{c_maxDroppedTransactionCount},
22+
m_current{PriorityCompare{*this}},
23+
m_limit{_limit},
24+
m_futureLimit{_futureLimit}
2125
{
2226
unsigned verifierThreads = std::max(thread::hardware_concurrency(), 3U) - 2U;
2327
for (unsigned i = 0; i < verifierThreads; ++i)
@@ -54,7 +58,7 @@ ImportResult TransactionQueue::check_WITH_LOCK(h256 const& _h, IfDropped _ik)
5458
if (m_known.count(_h))
5559
return ImportResult::AlreadyKnown;
5660

57-
if (m_dropped.count(_h) && _ik == IfDropped::Ignore)
61+
if (m_dropped.contains(_h) && _ik == IfDropped::Ignore)
5862
return ImportResult::AlreadyInChain;
5963

6064
return ImportResult::Success;
@@ -312,15 +316,7 @@ void TransactionQueue::drop(h256 const& _txHash)
312316
return;
313317

314318
UpgradeGuard ul(l);
315-
if (m_dropped.size() == c_maxDroppedTransactionCount)
316-
{
317-
LOG(m_loggerDetail) << "Dropped transaction list is at capacity ("
318-
<< c_maxDroppedTransactionCount
319-
<< "), removing dropped transaction from list (txhash: "
320-
<< *m_dropped.begin() << ")";
321-
m_dropped.erase(m_dropped.begin());
322-
}
323-
m_dropped.insert(_txHash);
319+
m_dropped.insert(_txHash, true /* placeholder value */);
324320
remove_WITH_LOCK(_txHash);
325321
}
326322

libethereum/TransactionQueue.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121

2222
#pragma once
2323

24-
#include <functional>
25-
#include <condition_variable>
26-
#include <thread>
27-
#include <deque>
24+
#include "Transaction.h"
2825
#include <libdevcore/Common.h>
2926
#include <libdevcore/Guards.h>
3027
#include <libdevcore/Log.h>
28+
#include <libdevcore/LruCache.h>
3129
#include <libethcore/Common.h>
32-
#include "Transaction.h"
30+
#include <condition_variable>
31+
#include <deque>
32+
#include <functional>
33+
#include <thread>
3334

3435
namespace dev
3536
{
@@ -189,7 +190,11 @@ class TransactionQueue
189190
h256Hash m_known; ///< Headers of transactions in both sets.
190191

191192
std::unordered_map<h256, std::function<void(ImportResult)>> m_callbacks; ///< Called once.
192-
h256Hash m_dropped; ///< Transactions that have previously been dropped
193+
194+
///< Transactions that have previously been dropped. We technically only need to store the tx
195+
///< hash, but we also store bool as a placeholder value so that we can use an LRU cache to cap
196+
///< the number of transaction hashes stored.
197+
LruCache<h256, bool> m_dropped;
193198

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

0 commit comments

Comments
 (0)