Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Oct 25, 2023
1 parent 09e46dc commit 8b73053
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.hyperledger.besu.ethereum.mainnet.ValidationResult.valid;
import static org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason.EXCEEDS_BLOCK_GAS_LIMIT;
import static org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason.GAS_PRICE_TOO_LOW;
Expand Down Expand Up @@ -91,6 +90,7 @@
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionValidator;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionValidatorFactory;
import org.hyperledger.besu.util.number.Percentage;

import java.math.BigInteger;
import java.util.Arrays;
Expand All @@ -106,6 +106,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIf;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -1033,6 +1034,157 @@ public void shouldAcceptZeroGasPrice1559TxsWhenMinGasPriceIsZeroAndLondonWithZer
addAndAssertTransactionViaApiValid(transaction, noLocalPriority);
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
public void samePriceTxReplacementWhenPriceBumpIsZeroFrontier(final boolean noLocalPriority) {
transactionPool =
createTransactionPool(b -> b.priceBump(Percentage.ZERO).noLocalPriority(noLocalPriority));
when(miningParameters.getMinTransactionGasPrice()).thenReturn(Wei.ZERO);

final Transaction transaction1a =
createBaseTransactionGasPriceMarket(0)
.gasPrice(Wei.ZERO)
.to(Optional.of(Address.ALTBN128_ADD))
.createTransaction(KEY_PAIR1);

givenTransactionIsValid(transaction1a);

transactionPool.addRemoteTransactions(List.of(transaction1a));

assertThat(transactionPool.getPendingTransactions())
.map(PendingTransaction::getTransaction)
.containsOnly(transaction1a);

final Transaction transaction1b =
createBaseTransactionGasPriceMarket(0)
.gasPrice(Wei.ZERO)
.to(Optional.of(Address.KZG_POINT_EVAL))
.createTransaction(KEY_PAIR1);

givenTransactionIsValid(transaction1b);

transactionPool.addRemoteTransactions(List.of(transaction1b));

assertThat(transactionPool.getPendingTransactions())
.map(PendingTransaction::getTransaction)
.containsOnly(transaction1b);
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
@EnabledIf("isBaseFeeMarket")
public void replaceSamePriceTxWhenPriceBumpIsZeroLondon(final boolean noLocalPriority) {
transactionPool =
createTransactionPool(b -> b.priceBump(Percentage.ZERO).noLocalPriority(noLocalPriority));
when(miningParameters.getMinTransactionGasPrice()).thenReturn(Wei.ZERO);

final Transaction transaction1a =
createBaseTransactionBaseFeeMarket(0)
.maxFeePerGas(Optional.of(Wei.ZERO))
.maxPriorityFeePerGas(Optional.of(Wei.ZERO))
.to(Optional.of(Address.ALTBN128_ADD))
.createTransaction(KEY_PAIR1);

givenTransactionIsValid(transaction1a);

transactionPool.addRemoteTransactions(List.of(transaction1a));

assertThat(transactionPool.getPendingTransactions())
.map(PendingTransaction::getTransaction)
.containsOnly(transaction1a);

final Transaction transaction1b =
createBaseTransactionBaseFeeMarket(0)
.maxFeePerGas(Optional.of(Wei.ZERO))
.maxPriorityFeePerGas(Optional.of(Wei.ZERO))
.to(Optional.of(Address.KZG_POINT_EVAL))
.createTransaction(KEY_PAIR1);

givenTransactionIsValid(transaction1b);

transactionPool.addRemoteTransactions(List.of(transaction1b));

assertThat(transactionPool.getPendingTransactions())
.map(PendingTransaction::getTransaction)
.containsOnly(transaction1b);
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
@EnabledIf("isBaseFeeMarket")
public void replaceSamePriceTxWhenPriceBumpIsZeroLondonToFrontier(final boolean noLocalPriority) {
transactionPool =
createTransactionPool(b -> b.priceBump(Percentage.ZERO).noLocalPriority(noLocalPriority));
when(miningParameters.getMinTransactionGasPrice()).thenReturn(Wei.ZERO);

final Transaction transaction1a =
createBaseTransactionBaseFeeMarket(0)
.maxFeePerGas(Optional.of(Wei.ZERO))
.maxPriorityFeePerGas(Optional.of(Wei.ZERO))
.to(Optional.of(Address.ALTBN128_ADD))
.createTransaction(KEY_PAIR1);

givenTransactionIsValid(transaction1a);

transactionPool.addRemoteTransactions(List.of(transaction1a));

assertThat(transactionPool.getPendingTransactions())
.map(PendingTransaction::getTransaction)
.containsOnly(transaction1a);

final Transaction transaction1b =
createBaseTransactionGasPriceMarket(0)
.gasPrice(Wei.ZERO)
.to(Optional.of(Address.KZG_POINT_EVAL))
.createTransaction(KEY_PAIR1);

givenTransactionIsValid(transaction1b);

transactionPool.addRemoteTransactions(List.of(transaction1b));

assertThat(transactionPool.getPendingTransactions())
.map(PendingTransaction::getTransaction)
.containsOnly(transaction1b);
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
@EnabledIf("isBaseFeeMarket")
public void replaceSamePriceTxWhenPriceBumpIsZeroFrontierToLondon(final boolean noLocalPriority) {
transactionPool =
createTransactionPool(b -> b.priceBump(Percentage.ZERO).noLocalPriority(noLocalPriority));
when(miningParameters.getMinTransactionGasPrice()).thenReturn(Wei.ZERO);

final Transaction transaction1a =
createBaseTransactionGasPriceMarket(0)
.gasPrice(Wei.ZERO)
.to(Optional.of(Address.KZG_POINT_EVAL))
.createTransaction(KEY_PAIR1);

givenTransactionIsValid(transaction1a);

transactionPool.addRemoteTransactions(List.of(transaction1a));

assertThat(transactionPool.getPendingTransactions())
.map(PendingTransaction::getTransaction)
.containsOnly(transaction1a);

final Transaction transaction1b =
createBaseTransactionBaseFeeMarket(0)
.maxFeePerGas(Optional.of(Wei.ZERO))
.maxPriorityFeePerGas(Optional.of(Wei.ZERO))
.to(Optional.of(Address.ALTBN128_ADD))
.createTransaction(KEY_PAIR1);

givenTransactionIsValid(transaction1b);

transactionPool.addRemoteTransactions(List.of(transaction1b));

assertThat(transactionPool.getPendingTransactions())
.map(PendingTransaction::getTransaction)
.containsOnly(transaction1b);
}

@Test
public void shouldAcceptBaseFeeFloorGasPriceFrontierLocalPriorityTransactionsWhenMining() {
transactionPool = createTransactionPool(b -> b.noLocalPriority(false));
Expand Down Expand Up @@ -1112,20 +1264,21 @@ public void shouldAcceptLocal1559TxsWhenMaxFeePerGasIsAtLeastEqualToMinMinGasPri
@Test
public void addRemoteTransactionsShouldAllowDuplicates() {
final Transaction transaction1 = createTransaction(1, Wei.of(7L));
final Transaction transaction2 = createTransaction(2, Wei.of(7L));
final Transaction transaction3 = createTransaction(2, Wei.of(7L));
final Transaction transaction4 = createTransaction(3, Wei.of(7L));
final Transaction transaction2a = createTransaction(2, Wei.of(7L));
final Transaction transaction2b = createTransaction(2, Wei.of(7L));
final Transaction transaction3 = createTransaction(3, Wei.of(7L));

givenTransactionIsValid(transaction1);
givenTransactionIsValid(transaction2);
givenTransactionIsValid(transaction2a);
givenTransactionIsValid(transaction2b);
givenTransactionIsValid(transaction3);
givenTransactionIsValid(transaction4);

assertThatCode(
() ->
transactionPool.addRemoteTransactions(
List.of(transaction1, transaction2, transaction3, transaction4)))
.doesNotThrowAnyException();
transactionPool.addRemoteTransactions(
List.of(transaction1, transaction2a, transaction2b, transaction3));

assertThat(transactionPool.getPendingTransactions())
.map(PendingTransaction::getTransaction)
.containsExactlyInAnyOrder(transaction1, transaction2a, transaction3);
}

private static PluginTransactionValidatorFactory getPluginTransactionValidatorFactoryReturning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import org.hyperledger.besu.ethereum.core.ExecutionContextTestFixture;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionTestFixture;
import org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction;
import org.hyperledger.besu.ethereum.eth.transactions.PendingTransactions;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.testutil.TestClock;
import org.hyperledger.besu.util.number.Fraction;

import java.time.ZoneId;
import java.util.Optional;
Expand All @@ -42,10 +40,7 @@ protected PendingTransactions createPendingTransactions(
transactionReplacementTester) {

return new BaseFeePendingTransactionsSorter(
ImmutableTransactionPoolConfiguration.builder()
.txPoolMaxSize(MAX_TRANSACTIONS)
.txPoolLimitByAccountPercentage(Fraction.fromFloat(1.0f))
.build(),
poolConfig,
TestClock.system(ZoneId.systemDefault()),
metricsSystem,
protocolContext.getBlockchain()::getChainHeadHeader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import org.hyperledger.besu.ethereum.core.ExecutionContextTestFixture;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionTestFixture;
import org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction;
import org.hyperledger.besu.ethereum.eth.transactions.PendingTransactions;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.testutil.TestClock;
import org.hyperledger.besu.util.number.Fraction;

import java.time.ZoneId;
import java.util.function.BiFunction;
Expand All @@ -41,10 +39,7 @@ protected PendingTransactions createPendingTransactions(
transactionReplacementTester) {

return new GasPricePendingTransactionsSorter(
ImmutableTransactionPoolConfiguration.builder()
.txPoolMaxSize(MAX_TRANSACTIONS)
.txPoolLimitByAccountPercentage(Fraction.fromFloat(1.0f))
.build(),
poolConfig,
TestClock.system(ZoneId.systemDefault()),
metricsSystem,
protocolContext.getBlockchain()::getChainHeadHeader);
Expand Down

0 comments on commit 8b73053

Please sign in to comment.