From db386f055bbca14f8ce84b765efdfee693e9c627 Mon Sep 17 00:00:00 2001 From: Fabio Di Fabio Date: Thu, 23 May 2024 19:43:47 +0300 Subject: [PATCH] Selecting variable cost depending on the extra data pricing enabled or not Signed-off-by: Fabio Di Fabio --- .../acc/test/rpc/linea/EstimateGasTest.java | 5 ++- .../TransactionProfitabilityCalculator.java | 43 +++++++++++++------ .../linea/rpc/linea/LineaEstimateGas.java | 2 +- .../validators/ProfitabilityValidator.java | 3 +- .../ProfitableTransactionSelector.java | 6 ++- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/acceptance-tests/src/test/java/linea/plugin/acc/test/rpc/linea/EstimateGasTest.java b/acceptance-tests/src/test/java/linea/plugin/acc/test/rpc/linea/EstimateGasTest.java index 527bd3a4..fdacc9ec 100644 --- a/acceptance-tests/src/test/java/linea/plugin/acc/test/rpc/linea/EstimateGasTest.java +++ b/acceptance-tests/src/test/java/linea/plugin/acc/test/rpc/linea/EstimateGasTest.java @@ -158,7 +158,7 @@ protected void assertIsProfitable( final var profitablePriorityFee = profitabilityCalculator.profitablePriorityFeePerGas( - tx, profitabilityConf.txPoolMinMargin(), estimatedGasLimit); + tx, profitabilityConf.txPoolMinMargin(), estimatedGasLimit, minGasPrice); assertThat(profitablePriorityFee.greaterThan(minGasPrice)).isTrue(); @@ -168,7 +168,8 @@ protected void assertIsProfitable( tx, profitabilityConf.txPoolMinMargin(), estimatedMaxGasPrice, - estimatedGasLimit)) + estimatedGasLimit, + minGasPrice)) .isTrue(); } diff --git a/arithmetization/src/main/java/net/consensys/linea/bl/TransactionProfitabilityCalculator.java b/arithmetization/src/main/java/net/consensys/linea/bl/TransactionProfitabilityCalculator.java index 2768f71d..ec888b11 100644 --- a/arithmetization/src/main/java/net/consensys/linea/bl/TransactionProfitabilityCalculator.java +++ b/arithmetization/src/main/java/net/consensys/linea/bl/TransactionProfitabilityCalculator.java @@ -15,7 +15,6 @@ package net.consensys.linea.bl; import java.math.BigDecimal; -import java.math.BigInteger; import lombok.extern.slf4j.Slf4j; import net.consensys.linea.compress.LibCompress; @@ -26,7 +25,7 @@ @Slf4j public class TransactionProfitabilityCalculator { - private static final BigInteger TO_WEI_MULTIPLIER = BigInteger.valueOf(1000); + private static final long TO_WEI_MULTIPLIER = 1_000; private final LineaProfitabilityConfiguration profitabilityConf; public TransactionProfitabilityCalculator( @@ -35,16 +34,22 @@ public TransactionProfitabilityCalculator( } public Wei profitablePriorityFeePerGas( - final Transaction transaction, final double minMargin, final long gas) { + final Transaction transaction, + final double minMargin, + final long gas, + final Wei minGasPrice) { final int compressedTxSize = getCompressedTxSize(transaction); + final long variableCostKWei = + profitabilityConf.extraDataPricingEnabled() + ? profitabilityConf.variableCostKWei() + : minGasPrice.divide(TO_WEI_MULTIPLIER).toLong(); + final var profitAtKWei = - minMargin - * (profitabilityConf.variableCostKWei() * compressedTxSize / gas - + profitabilityConf.fixedCostKWei()); + minMargin * (variableCostKWei * compressedTxSize / gas + profitabilityConf.fixedCostKWei()); final var profitAtWei = - Wei.ofNumber(BigDecimal.valueOf(profitAtKWei).toBigInteger().multiply(TO_WEI_MULTIPLIER)); + Wei.ofNumber(BigDecimal.valueOf(profitAtKWei).toBigInteger()).multiply(TO_WEI_MULTIPLIER); log.atDebug() .setMessage( @@ -53,7 +58,7 @@ public Wei profitablePriorityFeePerGas( .addArgument(profitAtWei::toHumanReadableString) .addArgument(minMargin) .addArgument(profitabilityConf.fixedCostKWei()) - .addArgument(profitabilityConf.variableCostKWei()) + .addArgument(variableCostKWei) .addArgument(gas) .addArgument(transaction::getSize) .addArgument(compressedTxSize) @@ -67,9 +72,11 @@ public boolean isProfitable( final Transaction transaction, final double minMargin, final Wei effectiveGasPrice, - final long gas) { + final long gas, + final Wei minGasPrice) { - final Wei profitablePriorityFee = profitablePriorityFeePerGas(transaction, minMargin, gas); + final Wei profitablePriorityFee = + profitablePriorityFeePerGas(transaction, minMargin, gas, minGasPrice); if (effectiveGasPrice.lessThan(profitablePriorityFee)) { log( @@ -79,7 +86,8 @@ public boolean isProfitable( minMargin, effectiveGasPrice, profitablePriorityFee, - gas); + gas, + minGasPrice); return false; } @@ -90,7 +98,8 @@ public boolean isProfitable( minMargin, effectiveGasPrice, profitablePriorityFee, - gas); + gas, + minGasPrice); return true; } @@ -106,7 +115,9 @@ private void log( final double minMargin, final Wei effectiveGasPrice, final Wei profitableGasPrice, - final long gasUsed) { + final long gasUsed, + final Wei minGasPrice) { + leb.setMessage( "Context {}. Transaction {} has a margin of {}, minMargin={}, effectiveGasPrice={}," + " profitableGasPrice={}, fixedCostKWei={}, variableCostKWei={}, " @@ -121,7 +132,11 @@ private void log( .addArgument(effectiveGasPrice::toHumanReadableString) .addArgument(profitableGasPrice::toHumanReadableString) .addArgument(profitabilityConf.fixedCostKWei()) - .addArgument(profitabilityConf.variableCostKWei()) + .addArgument( + () -> + profitabilityConf.extraDataPricingEnabled() + ? profitabilityConf.variableCostKWei() + : minGasPrice.divide(TO_WEI_MULTIPLIER).toLong()) .addArgument(gasUsed) .log(); } diff --git a/arithmetization/src/main/java/net/consensys/linea/rpc/linea/LineaEstimateGas.java b/arithmetization/src/main/java/net/consensys/linea/rpc/linea/LineaEstimateGas.java index 183df4ea..b43872f9 100644 --- a/arithmetization/src/main/java/net/consensys/linea/rpc/linea/LineaEstimateGas.java +++ b/arithmetization/src/main/java/net/consensys/linea/rpc/linea/LineaEstimateGas.java @@ -188,7 +188,7 @@ private Wei getEstimatedPriorityFee( final Wei profitablePriorityFee = txProfitabilityCalculator.profitablePriorityFeePerGas( - transaction, profitabilityConf.estimateGasMinMargin(), estimatedGasUsed); + transaction, profitabilityConf.estimateGasMinMargin(), estimatedGasUsed, minGasPrice); if (profitablePriorityFee.greaterOrEqualThan(priorityFeeLowerBound)) { return profitablePriorityFee; diff --git a/arithmetization/src/main/java/net/consensys/linea/sequencer/txpoolvalidation/validators/ProfitabilityValidator.java b/arithmetization/src/main/java/net/consensys/linea/sequencer/txpoolvalidation/validators/ProfitabilityValidator.java index 0acf3975..12be4142 100644 --- a/arithmetization/src/main/java/net/consensys/linea/sequencer/txpoolvalidation/validators/ProfitabilityValidator.java +++ b/arithmetization/src/main/java/net/consensys/linea/sequencer/txpoolvalidation/validators/ProfitabilityValidator.java @@ -56,7 +56,8 @@ public Optional validateTransaction( transaction, profitabilityConf.txPoolMinMargin(), calculateUpfrontGasPrice(transaction), - transaction.getGasLimit()) + transaction.getGasLimit(), + besuConfiguration.getMinGasPrice()) ? Optional.empty() : Optional.of("Gas price too low"); } diff --git a/arithmetization/src/main/java/net/consensys/linea/sequencer/txselection/selectors/ProfitableTransactionSelector.java b/arithmetization/src/main/java/net/consensys/linea/sequencer/txselection/selectors/ProfitableTransactionSelector.java index 08c33e87..5c5c53bd 100644 --- a/arithmetization/src/main/java/net/consensys/linea/sequencer/txselection/selectors/ProfitableTransactionSelector.java +++ b/arithmetization/src/main/java/net/consensys/linea/sequencer/txselection/selectors/ProfitableTransactionSelector.java @@ -81,7 +81,8 @@ public TransactionSelectionResult evaluateTransactionPreProcessing( transaction, profitabilityConf.minMargin(), evaluationContext.getTransactionGasPrice(), - gasLimit)) { + gasLimit, + minGasPrice)) { return TX_UNPROFITABLE_UPFRONT; } @@ -133,7 +134,8 @@ public TransactionSelectionResult evaluateTransactionPostProcessing( transaction, profitabilityConf.minMargin(), evaluationContext.getTransactionGasPrice(), - gasUsed)) { + gasUsed, + evaluationContext.getMinGasPrice())) { rememberUnprofitable(transaction); return TX_UNPROFITABLE; }