|
|
|
@ -17,8 +17,8 @@ package org.hyperledger.besu.ethereum.eth.transactions; |
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
import static org.mockito.Mockito.verifyNoInteractions; |
|
|
|
|
import static org.mockito.Mockito.verifyNoMoreInteractions; |
|
|
|
|
import static org.mockito.Mockito.verifyZeroInteractions; |
|
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
|
|
|
|
|
|
import org.hyperledger.besu.crypto.KeyPair; |
|
|
|
@ -124,30 +124,15 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void shouldDropOldestTransactionWhenLimitExceeded() { |
|
|
|
|
final Transaction oldestTransaction = |
|
|
|
|
new TransactionTestFixture() |
|
|
|
|
.value(Wei.of(1L)) |
|
|
|
|
.nonce(0L) |
|
|
|
|
.createTransaction(SIGNATURE_ALGORITHM.get().generateKeyPair()); |
|
|
|
|
final Transaction oldestTransaction = createTransaction(0); |
|
|
|
|
transactions.addRemoteTransaction(oldestTransaction); |
|
|
|
|
for (int i = 1; i < MAX_TRANSACTIONS; i++) { |
|
|
|
|
final Transaction newerTransaction = |
|
|
|
|
new TransactionTestFixture() |
|
|
|
|
.value(Wei.of(1L)) |
|
|
|
|
.nonce(0L) |
|
|
|
|
.createTransaction(SIGNATURE_ALGORITHM.get().generateKeyPair()); |
|
|
|
|
transactions.addRemoteTransaction(newerTransaction); |
|
|
|
|
transactions.addRemoteTransaction(createTransaction(i)); |
|
|
|
|
} |
|
|
|
|
assertThat(transactions.size()).isEqualTo(MAX_TRANSACTIONS); |
|
|
|
|
assertThat(metricsSystem.getCounterValue(REMOVED_COUNTER, REMOTE, DROPPED)).isZero(); |
|
|
|
|
|
|
|
|
|
final Transaction lastTransaction = |
|
|
|
|
new TransactionTestFixture() |
|
|
|
|
.value(Wei.of(1L)) |
|
|
|
|
.nonce(MAX_TRANSACTIONS + 1) |
|
|
|
|
.createTransaction(SIGNATURE_ALGORITHM.get().generateKeyPair()); |
|
|
|
|
|
|
|
|
|
transactions.addRemoteTransaction(lastTransaction); |
|
|
|
|
transactions.addRemoteTransaction(createTransaction(MAX_TRANSACTIONS + 1)); |
|
|
|
|
assertThat(transactions.size()).isEqualTo(MAX_TRANSACTIONS); |
|
|
|
|
assertTransactionNotPending(oldestTransaction); |
|
|
|
|
assertThat(metricsSystem.getCounterValue(REMOVED_COUNTER, REMOTE, DROPPED)).isEqualTo(1); |
|
|
|
@ -170,11 +155,6 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void shouldPrioritizeLocalTransaction() { |
|
|
|
|
|
|
|
|
|
transactions.subscribeDroppedTransactions( |
|
|
|
|
transaction -> |
|
|
|
|
assertThat(transactions.getLocalTransactions().contains(transaction)).isFalse()); |
|
|
|
|
|
|
|
|
|
final Transaction localTransaction = createTransaction(0); |
|
|
|
|
transactions.addLocalTransaction(localTransaction); |
|
|
|
|
|
|
|
|
@ -187,14 +167,9 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void shouldPrioritizeGasPriceThenTimeAddedToPool() { |
|
|
|
|
transactions.subscribeDroppedTransactions( |
|
|
|
|
transaction -> assertThat(transaction.getGasPrice().get().toLong()).isLessThan(100)); |
|
|
|
|
final List<Transaction> lowGasPriceTransactions = |
|
|
|
|
IntStream.range(0, MAX_TRANSACTIONS) |
|
|
|
|
.mapToObj( |
|
|
|
|
i -> |
|
|
|
|
transactionWithNonceSenderAndGasPrice( |
|
|
|
|
i + 1, SIGNATURE_ALGORITHM.get().generateKeyPair(), 10 + i)) |
|
|
|
|
.mapToObj(i -> transactionWithNonceSenderAndGasPrice(i + 1, KEYS1, 10)) |
|
|
|
|
.collect(Collectors.toUnmodifiableList()); |
|
|
|
|
|
|
|
|
|
// Fill the pool
|
|
|
|
@ -202,8 +177,7 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
|
|
|
|
|
// This should kick the oldest tx with the low gas price out, namely the first one we added
|
|
|
|
|
final Transaction highGasPriceTransaction = |
|
|
|
|
transactionWithNonceSenderAndGasPrice( |
|
|
|
|
MAX_TRANSACTIONS + 10, SIGNATURE_ALGORITHM.get().generateKeyPair(), 100); |
|
|
|
|
transactionWithNonceSenderAndGasPrice(MAX_TRANSACTIONS + 1, KEYS1, 100); |
|
|
|
|
transactions.addRemoteTransaction(highGasPriceTransaction); |
|
|
|
|
assertThat(transactions.size()).isEqualTo(MAX_TRANSACTIONS); |
|
|
|
|
|
|
|
|
@ -214,12 +188,14 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void shouldStartDroppingLocalTransactionsWhenPoolIsFullOfLocalTransactions() { |
|
|
|
|
transactions.subscribeDroppedTransactions(this::assertTransactionNotPending); |
|
|
|
|
final Transaction firstLocalTransaction = createTransaction(0); |
|
|
|
|
transactions.addLocalTransaction(firstLocalTransaction); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i <= MAX_TRANSACTIONS; i++) { |
|
|
|
|
for (int i = 1; i <= MAX_TRANSACTIONS; i++) { |
|
|
|
|
transactions.addLocalTransaction(createTransaction(i)); |
|
|
|
|
} |
|
|
|
|
assertThat(transactions.size()).isEqualTo(MAX_TRANSACTIONS); |
|
|
|
|
assertTransactionNotPending(firstLocalTransaction); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -233,7 +209,6 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void shouldNotNotifyListenerAfterUnsubscribe() { |
|
|
|
|
|
|
|
|
|
final long id = transactions.subscribePendingTransactions(listener); |
|
|
|
|
|
|
|
|
|
transactions.addRemoteTransaction(transaction1); |
|
|
|
@ -241,8 +216,10 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
verify(listener).onTransactionAdded(transaction1); |
|
|
|
|
|
|
|
|
|
transactions.unsubscribePendingTransactions(id); |
|
|
|
|
verifyNoMoreInteractions(listener); |
|
|
|
|
|
|
|
|
|
transactions.addRemoteTransaction(transaction2); |
|
|
|
|
|
|
|
|
|
verifyZeroInteractions(listener); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -302,7 +279,7 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
|
|
|
|
|
transactions.transactionAddedToBlock(transaction1); |
|
|
|
|
|
|
|
|
|
verifyNoInteractions(droppedListener); |
|
|
|
|
verifyZeroInteractions(droppedListener); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -498,7 +475,7 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
assertTransactionNotPending(transaction1b); |
|
|
|
|
assertTransactionPending(transaction1); |
|
|
|
|
assertThat(transactions.size()).isEqualTo(1); |
|
|
|
|
verifyNoInteractions(listener); |
|
|
|
|
verifyZeroInteractions(listener); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -706,18 +683,18 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
@Test |
|
|
|
|
public void assertThatCorrectNonceIsReturned() { |
|
|
|
|
assertThat(transactions.getNextNonceForSender(transaction1.getSender())).isEmpty(); |
|
|
|
|
addLocalTransactions(1, 2, 4); |
|
|
|
|
addLocalTransactions(1, 2, 4, 5); |
|
|
|
|
assertThat(transactions.getNextNonceForSender(transaction1.getSender())) |
|
|
|
|
.isPresent() |
|
|
|
|
.hasValue(3); |
|
|
|
|
addLocalTransactions(3); |
|
|
|
|
assertThat(transactions.getNextNonceForSender(transaction1.getSender())) |
|
|
|
|
.isPresent() |
|
|
|
|
.hasValue(5); |
|
|
|
|
addLocalTransactions(5); |
|
|
|
|
.hasValue(6); |
|
|
|
|
addLocalTransactions(6, 10); |
|
|
|
|
assertThat(transactions.getNextNonceForSender(transaction1.getSender())) |
|
|
|
|
.isPresent() |
|
|
|
|
.hasValue(6); |
|
|
|
|
.hasValue(7); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -751,60 +728,4 @@ public class BaseFeePendingTransactionsTest { |
|
|
|
|
when(blockHeader.getBaseFee()).thenReturn(Optional.empty()); |
|
|
|
|
return blockHeader; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void shouldIgnoreFutureNoncedTxs() { |
|
|
|
|
|
|
|
|
|
// create maxtx transactions with valid addresses/nonces
|
|
|
|
|
// all addresses should be unique, chained txs will be checked in another test.
|
|
|
|
|
// TODO: how do we test around reorgs? do we?
|
|
|
|
|
List<Transaction> toValidate = new ArrayList<>((int) transactions.maxSize()); |
|
|
|
|
for (int entries = 1; entries <= transactions.maxSize(); entries++) { |
|
|
|
|
KeyPair kp = SIGNATURE_ALGORITHM.get().generateKeyPair(); |
|
|
|
|
Address a = Util.publicKeyToAddress(kp.getPublicKey()); |
|
|
|
|
Transaction t = |
|
|
|
|
new TransactionTestFixture() |
|
|
|
|
.sender(a) |
|
|
|
|
.value(Wei.of(2)) |
|
|
|
|
.maxPriorityFeePerGas(Optional.of(Wei.of(2L))) |
|
|
|
|
.nonce(entries) |
|
|
|
|
.createTransaction(kp); |
|
|
|
|
transactions.addRemoteTransaction(t); |
|
|
|
|
toValidate.add(t); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// create maxtx transaction with nonces in the future, could be any volume though since pool
|
|
|
|
|
// already full
|
|
|
|
|
List<Transaction> attackTxs = new ArrayList<>(); |
|
|
|
|
KeyPair attackerKp = SIGNATURE_ALGORITHM.get().generateKeyPair(); |
|
|
|
|
Address attackerA = Util.publicKeyToAddress(attackerKp.getPublicKey()); |
|
|
|
|
|
|
|
|
|
for (int entries = 10; |
|
|
|
|
entries < transactions.maxSize() + 10; |
|
|
|
|
entries++) { // badguy nonces are 2 digits
|
|
|
|
|
Transaction t = |
|
|
|
|
new TransactionTestFixture() |
|
|
|
|
.sender(attackerA) |
|
|
|
|
.value(Wei.of(2)) |
|
|
|
|
.nonce(entries) |
|
|
|
|
.maxPriorityFeePerGas(Optional.of(Wei.of(2L))) |
|
|
|
|
.createTransaction(attackerKp); |
|
|
|
|
attackTxs.add(t); |
|
|
|
|
transactions.addRemoteTransaction(t); // all but the last one of these should be dropped
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// assert txpool contains 1st attack
|
|
|
|
|
|
|
|
|
|
assertThat(transactions.getTransactionByHash(attackTxs.get(0).getHash())).isNotEmpty(); |
|
|
|
|
// assert txpool does not contain rest of attack
|
|
|
|
|
attackTxs.stream() |
|
|
|
|
.skip(1L) |
|
|
|
|
.forEach(t -> assertThat(transactions.getTransactionByHash(t.getHash())).isEmpty()); |
|
|
|
|
// assert that only 1 of the valid batch was purged
|
|
|
|
|
long droppedValidCount = |
|
|
|
|
toValidate.stream() |
|
|
|
|
.filter(t -> transactions.getTransactionByHash(t.getHash()).isEmpty()) |
|
|
|
|
.count(); |
|
|
|
|
assertThat(droppedValidCount).isEqualTo(1L); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|