Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
shouldEvictMultipleOldTransactions
Browse files Browse the repository at this point in the history
  • Loading branch information
smatthewenglish committed Apr 20, 2019
1 parent ec20c2d commit f114ed9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package tech.pegasys.pantheon.ethereum.eth.transactions;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

import tech.pegasys.pantheon.ethereum.core.AccountTransactionOrder;
import tech.pegasys.pantheon.ethereum.core.Address;
Expand Down Expand Up @@ -104,13 +105,16 @@ public PendingTransactions(
timerUtil.setPeriodic(transactionEvictionIntervalMs, this::evictOldTransactions);
}

private void evictOldTransactions() {
private boolean filterStream(TransactionInfo transaction) {
final long now = System.currentTimeMillis();
for (Map.Entry<Hash, TransactionInfo> transaction : pendingTransactions.entrySet()) {
final long then = transaction.getValue().getAddedToPoolAt().getEpochSecond();
if (now - then > transactionEvictionIntervalMs) {
removeTransaction(transaction.getValue().getTransaction());
}
return now - transaction.getAddedToPoolAt().getEpochSecond() > transactionEvictionIntervalMs;
}

private void evictOldTransactions() {
synchronized (pendingTransactions) {
final List<TransactionInfo> transactionsToRemove =
prioritizedTransactions.stream().filter(this::filterStream).collect(toList());
transactionsToRemove.forEach(transaction -> removeTransaction(transaction.getTransaction()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ private Transaction createTransaction(final int transactionNumber) {
}

@Test
public void shouldEvictOldTransactions() {
public void shouldEvictSingleOldTransaction() {
final long TRANSACTION_EVICTION_INTERVAL_MS = TimeUnit.SECONDS.toMillis(1);
final PendingTransactions transactions =
new PendingTransactions(
Expand All @@ -442,4 +442,27 @@ public void shouldEvictOldTransactions() {
}
assertThat(transactions.size()).isEqualTo(0);
}

@Test
public void shouldEvictMultipleOldTransactions() {
final long TRANSACTION_EVICTION_INTERVAL_MS = TimeUnit.SECONDS.toMillis(1);
final PendingTransactions transactions =
new PendingTransactions(
timerUtil,
TRANSACTION_EVICTION_INTERVAL_MS,
MAX_TRANSACTIONS,
TestClock.fixed(),
metricsSystem);

transactions.addRemoteTransaction(transaction1);
assertThat(transactions.size()).isEqualTo(1);
transactions.addRemoteTransaction(transaction2);
assertThat(transactions.size()).isEqualTo(2);

try {
TimeUnit.SECONDS.sleep(2);
} catch (Exception ignored) {
}
assertThat(transactions.size()).isEqualTo(0);
}
}

0 comments on commit f114ed9

Please sign in to comment.