Skip to content

Commit

Permalink
Prefer using wei instead of kwei
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed May 24, 2024
1 parent b2da6e6 commit ec207ea
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.Response;
import org.web3j.protocol.http.HttpService;

public class EstimateGasTest extends LineaPluginTestBase {
protected static final int FIXED_GAS_COST_KWEI = 1_200_000;
protected static final int VARIABLE_GAS_COST_KWEI = 90_000;
protected static final int FIXED_GAS_COST_WEI = 0;
protected static final int VARIABLE_GAS_COST_WEI = 1_000_000_000;
protected static final double MIN_MARGIN = 1.0;
protected static final double ESTIMATE_GAS_MIN_MARGIN = 1.0;
protected static final Wei MIN_GAS_PRICE = Wei.of(1_000_000_000);
Expand All @@ -64,8 +63,8 @@ public List<String> getTestCliOptions() {

protected TestCommandLineOptionsBuilder getTestCommandLineOptionsBuilder() {
return new TestCommandLineOptionsBuilder()
.set("--plugin-linea-fixed-gas-cost-kwei=", String.valueOf(FIXED_GAS_COST_KWEI))
.set("--plugin-linea-variable-gas-cost-kwei=", String.valueOf(VARIABLE_GAS_COST_KWEI))
.set("--plugin-linea-fixed-gas-cost-wei=", String.valueOf(FIXED_GAS_COST_WEI))
.set("--plugin-linea-variable-gas-cost-wei=", String.valueOf(VARIABLE_GAS_COST_WEI))
.set("--plugin-linea-min-margin=", String.valueOf(MIN_MARGIN))
.set("--plugin-linea-estimate-gas-min-margin=", String.valueOf(ESTIMATE_GAS_MIN_MARGIN))
.set("--plugin-linea-max-tx-gas-limit=", String.valueOf(MAX_TRANSACTION_GAS_LIMIT));
Expand All @@ -80,8 +79,8 @@ public void setMinGasPrice() {
public void createDefaultConfigurations() {
profitabilityConf =
LineaProfitabilityCliOptions.create().toDomainObject().toBuilder()
.fixedCostKWei(FIXED_GAS_COST_KWEI)
.variableCostKWei(VARIABLE_GAS_COST_KWEI)
.fixedCostWei(FIXED_GAS_COST_WEI)
.variableCostWei(VARIABLE_GAS_COST_WEI)
.minMargin(MIN_MARGIN)
.estimateGasMinMargin(ESTIMATE_GAS_MIN_MARGIN)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package net.consensys.linea.bl;

import static net.consensys.linea.config.LineaProfitabilityConfiguration.WEI_IN_KWEI;

import java.math.BigDecimal;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -41,25 +39,24 @@ public Wei profitablePriorityFeePerGas(
final Wei minGasPriceWei) {
final int compressedTxSize = getCompressedTxSize(transaction);

final long variableCostKWei =
final long variableCostWei =
profitabilityConf.extraDataPricingEnabled()
? profitabilityConf.variableCostKWei()
: minGasPriceWei.divide(WEI_IN_KWEI).toLong();
? profitabilityConf.variableCostWei()
: minGasPriceWei.toLong();

final var profitAtKWei =
minMargin * (variableCostKWei * compressedTxSize / gas + profitabilityConf.fixedCostKWei());
final var profitAt =
minMargin * (variableCostWei * compressedTxSize / gas + profitabilityConf.fixedCostWei());

final var profitAtWei =
Wei.ofNumber(BigDecimal.valueOf(profitAtKWei).toBigInteger()).multiply(WEI_IN_KWEI);
final var profitAtWei = Wei.ofNumber(BigDecimal.valueOf(profitAt).toBigInteger());

log.atDebug()
.setMessage(
"Estimated profitable priorityFeePerGas: {}; minMargin={}, fixedCostKWei={}, "
+ "variableCostKWei={}, gas={}, txSize={}, compressedTxSize={}")
"Estimated profitable priorityFeePerGas: {}; minMargin={}, fixedCostWei={}, "
+ "variableCostWei={}, gas={}, txSize={}, compressedTxSize={}")
.addArgument(profitAtWei::toHumanReadableString)
.addArgument(minMargin)
.addArgument(profitabilityConf.fixedCostKWei())
.addArgument(variableCostKWei)
.addArgument(profitabilityConf.fixedCostWei())
.addArgument(variableCostWei)
.addArgument(gas)
.addArgument(transaction::getSize)
.addArgument(compressedTxSize)
Expand Down Expand Up @@ -121,7 +118,7 @@ private void log(

leb.setMessage(
"Context {}. Transaction {} has a margin of {}, minMargin={}, effectiveGasPrice={},"
+ " profitableGasPrice={}, fixedCostKWei={}, variableCostKWei={}, "
+ " profitableGasPrice={}, fixedCostWei={}, variableCostWei={}, "
+ " gasUsed={}")
.addArgument(context)
.addArgument(transaction::getHash)
Expand All @@ -132,12 +129,12 @@ private void log(
.addArgument(minMargin)
.addArgument(effectiveGasPrice::toHumanReadableString)
.addArgument(profitableGasPrice::toHumanReadableString)
.addArgument(profitabilityConf.fixedCostKWei())
.addArgument(profitabilityConf.fixedCostWei())
.addArgument(
() ->
profitabilityConf.extraDataPricingEnabled()
? profitabilityConf.variableCostKWei()
: minGasPriceWei.divide(WEI_IN_KWEI).toLong())
? profitabilityConf.variableCostWei()
: minGasPriceWei.toLong())
.addArgument(gasUsed)
.log();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

/** The Linea profitability calculator CLI options. */
public class LineaProfitabilityCliOptions {
public static final String FIXED_GAS_COST_KWEI = "--plugin-linea-fixed-gas-cost-kwei";
public static final long DEFAULT_FIXED_GAS_COST_KWEI = 0;
public static final String FIXED_GAS_COST_WEI = "--plugin-linea-fixed-gas-cost-wei";
public static final long DEFAULT_FIXED_GAS_COST_WEI = 0;

public static final String VARIABLE_GAS_COST_KWEI = "--plugin-linea-variable-gas-cost-kwei";
public static final long DEFAULT_VARIABLE_GAS_COST_KWEI = 1_000_000;
public static final String VARIABLE_GAS_COST_WEI = "--plugin-linea-variable-gas-cost-wei";
public static final long DEFAULT_VARIABLE_GAS_COST_WEI = 1_000_000_000;

public static final String MIN_MARGIN = "--plugin-linea-min-margin";
public static final BigDecimal DEFAULT_MIN_MARGIN = BigDecimal.ONE;
Expand All @@ -52,19 +52,19 @@ public class LineaProfitabilityCliOptions {

@Positive
@CommandLine.Option(
names = {FIXED_GAS_COST_KWEI},
names = {FIXED_GAS_COST_WEI},
hidden = true,
paramLabel = "<INTEGER>",
description = "Fixed gas cost in KWei (default: ${DEFAULT-VALUE})")
private long fixedGasCostKwei = DEFAULT_FIXED_GAS_COST_KWEI;
description = "Fixed gas cost in Wei (default: ${DEFAULT-VALUE})")
private long fixedGasCostWei = DEFAULT_FIXED_GAS_COST_WEI;

@Positive
@CommandLine.Option(
names = {VARIABLE_GAS_COST_KWEI},
names = {VARIABLE_GAS_COST_WEI},
hidden = true,
paramLabel = "<INTEGER>",
description = "Variable gas cost in KWei (default: ${DEFAULT-VALUE})")
private long variableGasCostKwei = DEFAULT_VARIABLE_GAS_COST_KWEI;
description = "Variable gas cost in Wei (default: ${DEFAULT-VALUE})")
private long variableGasCostWei = DEFAULT_VARIABLE_GAS_COST_WEI;

@Positive
@CommandLine.Option(
Expand Down Expand Up @@ -139,8 +139,8 @@ public static LineaProfitabilityCliOptions create() {
public static LineaProfitabilityCliOptions fromConfig(
final LineaProfitabilityConfiguration config) {
final LineaProfitabilityCliOptions options = create();
options.fixedGasCostKwei = config.fixedCostKWei();
options.variableGasCostKwei = config.variableCostKWei();
options.fixedGasCostWei = config.fixedCostWei();
options.variableGasCostWei = config.variableCostWei();
options.minMargin = BigDecimal.valueOf(config.minMargin());
options.estimageGasMinMargin = BigDecimal.valueOf(config.estimateGasMinMargin());
options.txPoolMinMargin = BigDecimal.valueOf(config.txPoolMinMargin());
Expand All @@ -157,8 +157,8 @@ public static LineaProfitabilityCliOptions fromConfig(
*/
public LineaProfitabilityConfiguration toDomainObject() {
return LineaProfitabilityConfiguration.builder()
.fixedCostKWei(fixedGasCostKwei)
.variableCostKWei(variableGasCostKwei)
.fixedCostWei(fixedGasCostWei)
.variableCostWei(variableGasCostWei)
.minMargin(minMargin.doubleValue())
.estimateGasMinMargin(estimageGasMinMargin.doubleValue())
.txPoolMinMargin(txPoolMinMargin.doubleValue())
Expand All @@ -171,8 +171,8 @@ public LineaProfitabilityConfiguration toDomainObject() {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add(FIXED_GAS_COST_KWEI, fixedGasCostKwei)
.add(VARIABLE_GAS_COST_KWEI, variableGasCostKwei)
.add(FIXED_GAS_COST_WEI, fixedGasCostWei)
.add(VARIABLE_GAS_COST_WEI, variableGasCostWei)
.add(MIN_MARGIN, minMargin)
.add(ESTIMATE_GAS_MIN_MARGIN, estimageGasMinMargin)
.add(TX_POOL_MIN_MARGIN, txPoolMinMargin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
@Getter
@ToString
public class LineaProfitabilityConfiguration {
public static final int WEI_IN_KWEI = 1_000;
/** It is safe to keep this as long, since it will store value <= max_int * 1000 */
private long fixedCostWei;
/** It is safe to keep this as long, since it will store value <= max_int * 1000 */
private long variableCostWei;

private long fixedCostKWei;
private long variableCostKWei;
private double minMargin;
private double estimateGasMinMargin;
private double txPoolMinMargin;
Expand All @@ -40,20 +41,20 @@ public class LineaProfitabilityConfiguration {
/**
* These 2 parameters must be atomically updated
*
* @param fixedCostKWei fixed cost in KWei
* @param variableCostKWei variable cost in KWei
* @param fixedCostWei fixed cost in Wei
* @param variableCostWei variable cost in Wei
*/
public synchronized void updateFixedAndVariableCost(
final long fixedCostKWei, final long variableCostKWei) {
this.fixedCostKWei = fixedCostKWei;
this.variableCostKWei = variableCostKWei;
final long fixedCostWei, final long variableCostWei) {
this.fixedCostWei = fixedCostWei;
this.variableCostWei = variableCostWei;
}

public synchronized long fixedCostKWei() {
return fixedCostKWei;
public synchronized long fixedCostWei() {
return fixedCostWei;
}

public synchronized long variableCostKWei() {
return variableCostKWei;
public synchronized long variableCostWei() {
return variableCostWei;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

package net.consensys.linea.extradata;

import static net.consensys.linea.config.LineaProfitabilityConfiguration.WEI_IN_KWEI;

import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -83,6 +81,7 @@ static Long toLong(final Bytes fieldBytes) {

@SuppressWarnings("rawtypes")
private class Version1Parser implements ExtraDataParser {
private static final int WEI_IN_KWEI = 1_000;
private final LineaProfitabilityConfiguration profitabilityConf;
private final FieldConsumer[] fieldsSequence;
private final MutableLong currFixedCostKWei = new MutableLong();
Expand Down Expand Up @@ -117,7 +116,8 @@ public synchronized void parse(final Bytes extraData) {
}

profitabilityConf.updateFixedAndVariableCost(
currFixedCostKWei.longValue(), currVariableCostKWei.longValue());
currFixedCostKWei.longValue() * WEI_IN_KWEI,
currVariableCostKWei.longValue() * WEI_IN_KWEI);
}

void updateMinGasPrice(final Long minGasPriceKWei) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import org.junit.jupiter.api.Test;

public class ProfitableTransactionSelectorTest {
private static final int FIXED_GAS_COST_KWEI = 0;
private static final int VARIABLE_GAS_COST_KWEI = 1_000_000;
private static final int FIXED_GAS_COST_WEI = 0;
private static final int VARIABLE_GAS_COST_WEI = 1_000_000_000;
private static final double MIN_MARGIN = 1.0;
private static final int UNPROFITABLE_CACHE_SIZE = 2;
private static final int UNPROFITABLE_RETRY_LIMIT = 1;
Expand All @@ -54,8 +54,8 @@ public class ProfitableTransactionSelectorTest {
private final LineaProfitabilityConfiguration profitabilityConf =
LineaProfitabilityCliOptions.create().toDomainObject().toBuilder()
.minMargin(MIN_MARGIN)
.fixedCostKWei(FIXED_GAS_COST_KWEI)
.variableCostKWei(VARIABLE_GAS_COST_KWEI)
.fixedCostWei(FIXED_GAS_COST_WEI)
.variableCostWei(VARIABLE_GAS_COST_WEI)
.build();
private TestableProfitableTransactionSelector transactionSelector;

Expand Down

0 comments on commit ec207ea

Please sign in to comment.