Skip to content

Commit

Permalink
Selecting variable cost depending on the extra data pricing enabled o…
Browse files Browse the repository at this point in the history
…r not

Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed May 23, 2024
1 parent fad3b5a commit db386f0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -168,7 +168,8 @@ protected void assertIsProfitable(
tx,
profitabilityConf.txPoolMinMargin(),
estimatedMaxGasPrice,
estimatedGasLimit))
estimatedGasLimit,
minGasPrice))
.isTrue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -79,7 +86,8 @@ public boolean isProfitable(
minMargin,
effectiveGasPrice,
profitablePriorityFee,
gas);
gas,
minGasPrice);
return false;
}

Expand All @@ -90,7 +98,8 @@ public boolean isProfitable(
minMargin,
effectiveGasPrice,
profitablePriorityFee,
gas);
gas,
minGasPrice);
return true;
}

Expand All @@ -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={}, "
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public Optional<String> validateTransaction(
transaction,
profitabilityConf.txPoolMinMargin(),
calculateUpfrontGasPrice(transaction),
transaction.getGasLimit())
transaction.getGasLimit(),
besuConfiguration.getMinGasPrice())
? Optional.empty()
: Optional.of("Gas price too low");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public TransactionSelectionResult evaluateTransactionPreProcessing(
transaction,
profitabilityConf.minMargin(),
evaluationContext.getTransactionGasPrice(),
gasLimit)) {
gasLimit,
minGasPrice)) {
return TX_UNPROFITABLE_UPFRONT;
}

Expand Down Expand Up @@ -133,7 +134,8 @@ public TransactionSelectionResult evaluateTransactionPostProcessing(
transaction,
profitabilityConf.minMargin(),
evaluationContext.getTransactionGasPrice(),
gasUsed)) {
gasUsed,
evaluationContext.getMinGasPrice())) {
rememberUnprofitable(transaction);
return TX_UNPROFITABLE;
}
Expand Down

0 comments on commit db386f0

Please sign in to comment.