Skip to content

Commit

Permalink
Ensure fixed and variable cost profitability parameters are updated a…
Browse files Browse the repository at this point in the history
…tomically

Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed May 22, 2024
1 parent ea322d3 commit 03b5427
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

Expand All @@ -27,11 +26,31 @@
@Getter
@ToString
public class LineaProfitabilityConfiguration {
@Setter private volatile long fixedCostKWei;
@Setter private volatile long variableCostKWei;
double minMargin;
double estimateGasMinMargin;
double txPoolMinMargin;
boolean txPoolCheckApiEnabled;
boolean txPoolCheckP2pEnabled;
private long fixedCostKWei;
private long variableCostKWei;
private double minMargin;
private double estimateGasMinMargin;
private double txPoolMinMargin;
private boolean txPoolCheckApiEnabled;
private boolean txPoolCheckP2pEnabled;

/**
* These 2 parameters must be atomically updated
*
* @param fixedCostKWei fixed cost in KWei
* @param variableCostKWei variable cost in KWei
*/
public synchronized void updateFixedAndVariableCost(
final long fixedCostKWei, final long variableCostKWei) {
this.fixedCostKWei = fixedCostKWei;
this.variableCostKWei = variableCostKWei;
}

public synchronized long fixedCostKWei() {
return fixedCostKWei;
}

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

import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.config.LineaProfitabilityConfiguration;
import org.apache.commons.lang3.mutable.MutableLong;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt32;
import org.hyperledger.besu.datatypes.Wei;
Expand Down Expand Up @@ -69,16 +70,20 @@ static Long toLong(final Bytes fieldBytes) {

@SuppressWarnings("rawtypes")
private class Version1Parser implements ExtraDataParser {

private final LineaProfitabilityConfiguration profitabilityConf;
private final FieldConsumer[] fieldsSequence;
private final MutableLong currFixedCostKWei = new MutableLong();
private final MutableLong currVariableCostKWei = new MutableLong();

public Version1Parser(final LineaProfitabilityConfiguration profitabilityConf) {
this.profitabilityConf = profitabilityConf;

final FieldConsumer fixedGasCostField =
new FieldConsumer<>(
"fixedGasCost", 4, ExtraDataParser::toLong, profitabilityConf::fixedCostKWei);
"fixedGasCost", 4, ExtraDataParser::toLong, currFixedCostKWei::setValue);
final FieldConsumer variableGasCostField =
new FieldConsumer<>(
"variableGasCost", 4, ExtraDataParser::toLong, profitabilityConf::variableCostKWei);
"variableGasCost", 4, ExtraDataParser::toLong, currVariableCostKWei::setValue);
final FieldConsumer minGasPriceField =
new FieldConsumer<>("minGasPrice", 4, ExtraDataParser::toLong, this::updateMinGasPrice);

Expand All @@ -90,13 +95,16 @@ public boolean canParse(final Bytes rawExtraData) {
return rawExtraData.get(0) == (byte) 1;
}

public void parse(final Bytes extraData) {
public synchronized void parse(final Bytes extraData) {
log.info("Parsing extra data version 1: {}", extraData.toHexString());
int startIndex = 0;
for (final FieldConsumer fieldConsumer : fieldsSequence) {
fieldConsumer.accept(extraData.slice(startIndex, fieldConsumer.length));
startIndex += fieldConsumer.length;
}

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

void updateMinGasPrice(final Long minGasPriceKWei) {
Expand Down

0 comments on commit 03b5427

Please sign in to comment.