From 03b54275a7b9a344ef4c42c423b53c108bb39d5d Mon Sep 17 00:00:00 2001 From: Fabio Di Fabio Date: Wed, 22 May 2024 19:41:50 +0300 Subject: [PATCH] Ensure fixed and variable cost profitability parameters are updated atomically Signed-off-by: Fabio Di Fabio --- .../LineaProfitabilityConfiguration.java | 35 ++++++++++++++----- .../extradata/LineaExtraDataHandler.java | 16 ++++++--- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/config/LineaProfitabilityConfiguration.java b/arithmetization/src/main/java/net/consensys/linea/config/LineaProfitabilityConfiguration.java index 9157a3b8..75dc498f 100644 --- a/arithmetization/src/main/java/net/consensys/linea/config/LineaProfitabilityConfiguration.java +++ b/arithmetization/src/main/java/net/consensys/linea/config/LineaProfitabilityConfiguration.java @@ -17,7 +17,6 @@ import lombok.Builder; import lombok.Getter; -import lombok.Setter; import lombok.ToString; import lombok.experimental.Accessors; @@ -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; + } } diff --git a/arithmetization/src/main/java/net/consensys/linea/extradata/LineaExtraDataHandler.java b/arithmetization/src/main/java/net/consensys/linea/extradata/LineaExtraDataHandler.java index 63abb5c4..3ab6a33f 100644 --- a/arithmetization/src/main/java/net/consensys/linea/extradata/LineaExtraDataHandler.java +++ b/arithmetization/src/main/java/net/consensys/linea/extradata/LineaExtraDataHandler.java @@ -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; @@ -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); @@ -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) {