Skip to content

Commit

Permalink
Add the possibility to adjust the tx size in the profitability formula
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 Jan 22, 2024
1 parent b223a55 commit e055c98
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public class LineaTransactionSelectorCliOptions {
public static final int DEFAULT_VERIFICATION_CAPACITY = 90_000;
public static final int DEFAULT_GAS_PRICE_RATIO = 15;
public static final BigDecimal DEFAULT_MIN_MARGIN = BigDecimal.ONE;
public static final int DEFAULT_ADJUST_TX_SIZE = -45;
private static final String MAX_BLOCK_CALLDATA_SIZE = "--plugin-linea-max-block-calldata-size";
private static final String MODULE_LIMIT_FILE_PATH = "--plugin-linea-module-limit-file-path";
private static final String MAX_GAS_PER_BLOCK = "--plugin-linea-max-block-gas";
private static final String VERIFICATION_GAS_COST = "--plugin-linea-verification-gas-cost";
private static final String VERIFICATION_CAPACITY = "--plugin-linea-verification-capacity";
private static final String GAS_PRICE_RATIO = "--plugin-linea-gas-price-ratio";
private static final String MIN_MARGIN = "--plugin-linea-min-margin";
private static final String ADJUST_TX_SIZE = "--plugin-adjust-tx-size";

@Positive
@CommandLine.Option(
Expand Down Expand Up @@ -94,6 +96,15 @@ public class LineaTransactionSelectorCliOptions {
description = "Minimum margin of a transaction to be selected (default: ${DEFAULT-VALUE})")
private BigDecimal minMargin = DEFAULT_MIN_MARGIN;

@Positive
@CommandLine.Option(
names = {ADJUST_TX_SIZE},
hidden = true,
paramLabel = "<INTEGER>",
description =
"Adjust transaction size for profitability calculation (default: ${DEFAULT-VALUE})")
private int adjustTxSize = DEFAULT_ADJUST_TX_SIZE;

private LineaTransactionSelectorCliOptions() {}

/**
Expand Down Expand Up @@ -121,6 +132,7 @@ public static LineaTransactionSelectorCliOptions fromConfig(
options.verificationCapacity = config.getVerificationCapacity();
options.gasPriceRatio = config.getGasPriceRatio();
options.minMargin = BigDecimal.valueOf(config.getMinMargin());
options.adjustTxSize = config.getAdjustTxSize();
return options;
}

Expand All @@ -138,6 +150,7 @@ public LineaTransactionSelectorConfiguration toDomainObject() {
.verificationCapacity(verificationCapacity)
.gasPriceRatio(gasPriceRatio)
.minMargin(minMargin.doubleValue())
.adjustTxSize(adjustTxSize)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class LineaTransactionSelectorConfiguration {
private final int verificationCapacity;
private final int gasPriceRatio;
private final double minMargin;
private final int adjustTxSize;

private LineaTransactionSelectorConfiguration(
final int maxBlockCallDataSize,
Expand All @@ -32,14 +33,16 @@ private LineaTransactionSelectorConfiguration(
final int verificationGasCost,
final int verificationCapacity,
final int gasPriceRatio,
final double minMargin) {
final double minMargin,
final int adjustTxSize) {
this.maxBlockCallDataSize = maxBlockCallDataSize;
this.moduleLimitsFilePath = moduleLimitsFilePath;
this.maxGasPerBlock = maxGasPerBlock;
this.verificationGasCost = verificationGasCost;
this.verificationCapacity = verificationCapacity;
this.gasPriceRatio = gasPriceRatio;
this.minMargin = minMargin;
this.adjustTxSize = adjustTxSize;
}

public int maxBlockCallDataSize() {
Expand Down Expand Up @@ -70,6 +73,10 @@ public double getMinMargin() {
return minMargin;
}

public int getAdjustTxSize() {
return adjustTxSize;
}

public static class Builder {
private int maxBlockCallDataSize;
private String moduleLimitsFilePath;
Expand All @@ -78,6 +85,7 @@ public static class Builder {
private int verificationCapacity;
private int gasPriceRatio;
private double minMargin;
private int adjustTxSize;

public Builder maxBlockCallDataSize(final int maxBlockCallDataSize) {
this.maxBlockCallDataSize = maxBlockCallDataSize;
Expand Down Expand Up @@ -114,6 +122,11 @@ public Builder minMargin(final double minMargin) {
return this;
}

public Builder adjustTxSize(final int adjustTxSize) {
this.adjustTxSize = adjustTxSize;
return this;
}

public LineaTransactionSelectorConfiguration build() {
return new LineaTransactionSelectorConfiguration(
maxBlockCallDataSize,
Expand All @@ -122,7 +135,8 @@ public LineaTransactionSelectorConfiguration build() {
verificationGasCost,
verificationCapacity,
gasPriceRatio,
minMargin);
minMargin,
adjustTxSize);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ private List<PluginTransactionSelector> createTransactionSelectors(
lineaConfiguration.getVerificationGasCost(),
lineaConfiguration.getVerificationCapacity(),
lineaConfiguration.getGasPriceRatio(),
lineaConfiguration.getMinMargin()),
lineaConfiguration.getMinMargin(),
lineaConfiguration.getAdjustTxSize()),
traceLineLimitTransactionSelector);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ProfitableTransactionSelector implements PluginTransactionSelector
private final int verificationCapacity;
private final int gasPriceRatio;
private final double minMargin;
private final int adjustTxSize;

@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
Expand All @@ -50,7 +51,7 @@ public TransactionSelectionResult evaluateTransactionPostProcessing(

final double minGasPrice = evaluationContext.getMinGasPrice().getAsBigInteger().doubleValue();
final double l1GasPrice = minGasPrice * gasPriceRatio;
final int serializedSize = transaction.getSize();
final int serializedSize = Math.max(0, transaction.getSize() + adjustTxSize);
final double verificationGasCostSlice =
(((double) serializedSize) / verificationCapacity) * verificationGasCost;
final double cost = l1GasPrice * verificationGasCostSlice;
Expand All @@ -65,7 +66,8 @@ public TransactionSelectionResult evaluateTransactionPostProcessing(
gasUsed,
minGasPrice,
l1GasPrice,
serializedSize);
serializedSize,
adjustTxSize);

if (margin < minMargin) {
log(
Expand All @@ -76,7 +78,8 @@ public TransactionSelectionResult evaluateTransactionPostProcessing(
gasUsed,
minGasPrice,
l1GasPrice,
serializedSize);
serializedSize,
adjustTxSize);
return TX_UNPROFITABLE;
}
}
Expand All @@ -97,11 +100,12 @@ private void log(
final long gasUsed,
final double minGasPrice,
final double l1GasPrice,
final int serializedSize) {
final int serializedSize,
final int adjustTxSize) {
leb.setMessage(
"Transaction {} has a margin of {}, minMargin={}, verificationCapacity={}, "
+ "verificationGasCost={}, gasPriceRatio={}, effectiveGasPrice={}, gasUsed={}, minGasPrice={}, "
+ "l1GasPrice={}, serializedSize={}")
+ "l1GasPrice={}, serializedSize={}, adjustTxSize={}")
.addArgument(transaction::getHash)
.addArgument(margin)
.addArgument(minMargin)
Expand All @@ -113,6 +117,7 @@ private void log(
.addArgument(minGasPrice)
.addArgument(l1GasPrice)
.addArgument(serializedSize)
.addArgument(adjustTxSize)
.log();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ public class ProfitableTransactionSelectorTest {
private static final int VERIFICATION_CAPACITY = 90_000;
private static final int GAS_PRICE_RATIO = 15;
private static final double MIN_MARGIN = 1.0;
private static final int ADJUST_TX_SIZE = -45;

private PluginTransactionSelector transactionSelector;

@BeforeEach
public void initialize() {
transactionSelector =
new ProfitableTransactionSelector(
VERIFICATION_GAS_COST, VERIFICATION_CAPACITY, GAS_PRICE_RATIO, MIN_MARGIN);
VERIFICATION_GAS_COST,
VERIFICATION_CAPACITY,
GAS_PRICE_RATIO,
MIN_MARGIN,
ADJUST_TX_SIZE);
}

@Test
Expand All @@ -55,6 +60,16 @@ public void shouldSelectWhenProfitable() {
SELECTED);
}

@Test
public void shouldSelectWhenProfitableWithAdjustedSize() {
var mockTransactionProcessingResult = mockTransactionProcessingResult(21000);
verifyTransactionSelection(
transactionSelector,
mockEvaluationContext(false, 150, Wei.of(1_100_000_000), Wei.of(1_000_000_000)),
mockTransactionProcessingResult,
SELECTED);
}

@Test
public void shouldNotSelectWhenUnprofitable() {
var mockTransactionProcessingResult = mockTransactionProcessingResult(21000);
Expand Down

0 comments on commit e055c98

Please sign in to comment.