Skip to content

Commit

Permalink
Merge pull request #2310 from rsksmart/feature/include_stable_minGasP…
Browse files Browse the repository at this point in the history
…rice_provider

Configurable Fiat-Based Stable MinGasPrice
  • Loading branch information
Vovchyk authored Sep 19, 2024
2 parents 0994382 + eede703 commit 7dd58c6
Show file tree
Hide file tree
Showing 37 changed files with 1,852 additions and 49 deletions.
22 changes: 17 additions & 5 deletions rskj-core/src/main/java/co/rsk/RskContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import co.rsk.cli.CliArgs;
import co.rsk.cli.RskCli;
import co.rsk.config.*;
import co.rsk.config.mining.StableMinGasPriceSystemConfig;
import co.rsk.core.*;
import co.rsk.core.bc.*;
import co.rsk.crypto.Keccak256;
Expand All @@ -40,6 +41,8 @@
import co.rsk.metrics.HashRateCalculatorMining;
import co.rsk.metrics.HashRateCalculatorNonMining;
import co.rsk.mine.*;
import co.rsk.mine.gas.provider.MinGasPriceProvider;
import co.rsk.mine.gas.provider.MinGasPriceProviderFactory;
import co.rsk.net.*;
import co.rsk.net.discovery.KnownPeersHandler;
import co.rsk.net.discovery.PeerExplorer;
Expand Down Expand Up @@ -253,6 +256,7 @@ public class RskContext implements NodeContext, NodeBootstrapper {
private TxQuotaChecker txQuotaChecker;
private GasPriceTracker gasPriceTracker;
private BlockChainFlusher blockChainFlusher;
private MinGasPriceProvider minGasPriceProvider;
private final Map<String, DbKind> dbPathToDbKindMap = new HashMap<>();

private volatile boolean closed;
Expand Down Expand Up @@ -1842,7 +1846,7 @@ private BlockToMineBuilder getBlockToMineBuilder() {
getMinerClock(),
getBlockFactory(),
getBlockExecutor(),
new MinimumGasPriceCalculator(Coin.valueOf(getMiningConfig().getMinGasPriceTarget())),
new MinimumGasPriceCalculator(getMinGasPriceProvider()),
new MinerUtils(),
getBlockTxSignatureCache()
);
Expand All @@ -1851,6 +1855,16 @@ private BlockToMineBuilder getBlockToMineBuilder() {
return blockToMineBuilder;
}

private MinGasPriceProvider getMinGasPriceProvider() {
if (minGasPriceProvider == null) {
long minGasPrice = getRskSystemProperties().minerMinGasPrice();
StableMinGasPriceSystemConfig stableGasPriceSystemConfig = getRskSystemProperties().getStableGasPriceSystemConfig();
minGasPriceProvider = MinGasPriceProviderFactory.create(minGasPrice, stableGasPriceSystemConfig, this::getEthModule);
}
logger.debug("MinGasPriceProvider type: {}", minGasPriceProvider.getType().name());
return minGasPriceProvider;
}

private BlockNodeInformation getBlockNodeInformation() {
if (blockNodeInformation == null) {
blockNodeInformation = new BlockNodeInformation();
Expand Down Expand Up @@ -2142,7 +2156,6 @@ private MiningConfig getMiningConfig() {
rskSystemProperties.coinbaseAddress(),
rskSystemProperties.minerMinFeesNotifyInDollars(),
rskSystemProperties.minerGasUnitInDollars(),
rskSystemProperties.minerMinGasPrice(),
rskSystemProperties.getNetworkConstants().getUncleListLimit(),
rskSystemProperties.getNetworkConstants().getUncleGenerationLimit(),
new GasLimitConfig(
Expand All @@ -2151,8 +2164,7 @@ private MiningConfig getMiningConfig() {
rskSystemProperties.getForceTargetGasLimit()
),
rskSystemProperties.isMinerServerFixedClock(),
rskSystemProperties.workSubmissionRateLimitInMills()
);
rskSystemProperties.workSubmissionRateLimitInMills());
}

return miningConfig;
Expand Down Expand Up @@ -2207,4 +2219,4 @@ private void checkIfNotClosed() {
throw new IllegalStateException("RSK Context is closed and cannot be in use anymore");
}
}
}
}
8 changes: 1 addition & 7 deletions rskj-core/src/main/java/co/rsk/config/MiningConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ public class MiningConfig {
private final RskAddress coinbaseAddress;
private final double minFeesNotifyInDollars;
private final double minerGasUnitInDollars;
private final long minGasPriceTarget;
private final int uncleListLimit;
private final int uncleGenerationLimit;
private final GasLimitConfig gasLimit;
private final boolean isFixedClock;
private final long workSubmissionRateLimitInMills;

public MiningConfig(RskAddress coinbaseAddress, double minFeesNotifyInDollars, double minerGasUnitInDollars,
long minGasPriceTarget, int uncleListLimit, int uncleGenerationLimit, GasLimitConfig gasLimit,
int uncleListLimit, int uncleGenerationLimit, GasLimitConfig gasLimit,
boolean isFixedClock, long workSubmissionRateLimitInMills) {
this.coinbaseAddress = coinbaseAddress;
this.minFeesNotifyInDollars = minFeesNotifyInDollars;
this.minerGasUnitInDollars = minerGasUnitInDollars;
this.minGasPriceTarget= minGasPriceTarget;
this.uncleListLimit = uncleListLimit;
this.uncleGenerationLimit = uncleGenerationLimit;
this.gasLimit = gasLimit;
Expand All @@ -62,10 +60,6 @@ public double getGasUnitInDollars() {
return minerGasUnitInDollars;
}

public long getMinGasPriceTarget() {
return minGasPriceTarget;
}

public int getUncleListLimit() {
return uncleListLimit;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of RskJ
* Copyright (C) 2024 RSK Labs Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package co.rsk.config.mining;

import com.typesafe.config.Config;

public class EthCallMinGasPriceSystemConfig {
private static final String FROM_PROPERTY = "from";
private static final String TO_PROPERTY = "to";
private static final String DATA_PROPERTY = "data";
private final String address;
private final String from;
private final String data;

public EthCallMinGasPriceSystemConfig(Config config) {
address = config.getString(TO_PROPERTY);
from = config.getString(FROM_PROPERTY);
data = config.getString(DATA_PROPERTY);
}

public String getAddress() {
return address;
}

public String getFrom() {
return from;
}

public String getData() {
return data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of RskJ
* Copyright (C) 2024 RSK Labs Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.config.mining;

import com.typesafe.config.Config;

import java.time.Duration;
import java.util.Objects;

public class HttpGetStableMinGasSystemConfig {
private static final String URL_PROPERTY = "url";
private static final String JSON_PATH = "jsonPath";
private static final String TIMEOUT_PROPERTY = "timeout";

private final String url;
private final String jsonPath;
private final Duration timeout;

public HttpGetStableMinGasSystemConfig(Config config) {
this.url = Objects.requireNonNull(config.getString(URL_PROPERTY));
this.jsonPath = Objects.requireNonNull(config.getString(JSON_PATH));
this.timeout = Objects.requireNonNull(config.getDuration(TIMEOUT_PROPERTY));
}

public String getUrl() {
return url;
}

public String getJsonPath() {
return jsonPath;
}

public Duration getTimeout() {
return timeout;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This file is part of RskJ
* Copyright (C) 2024 RSK Labs Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.config.mining;

import co.rsk.mine.gas.provider.MinGasPriceProviderType;
import com.typesafe.config.Config;

import java.time.Duration;

public class StableMinGasPriceSystemConfig {
public static final String STABLE_GAS_PRICE_CONFIG_PATH_PROPERTY = "miner.stableGasPrice";

private static final String ENABLED_PROPERTY = "enabled";
private static final String REFRESH_RATE_PROPERTY = "refreshRate";
private static final String MIN_STABLE_GAS_PRICE_PROPERTY = "minStableGasPrice";
private static final String METHOD_PROPERTY = "source.method";
private static final String PARAMS_PROPERTY = "source.params";

private final Duration refreshRate;
private final Long minStableGasPrice;
private final boolean enabled;
private final MinGasPriceProviderType method;
private final Config config;

public StableMinGasPriceSystemConfig(Config config) {
this.enabled = config.hasPath(ENABLED_PROPERTY) && config.getBoolean(ENABLED_PROPERTY);
this.refreshRate = this.enabled && config.hasPath(REFRESH_RATE_PROPERTY) ? config.getDuration(REFRESH_RATE_PROPERTY) : Duration.ZERO;
this.minStableGasPrice = this.enabled && config.hasPath(MIN_STABLE_GAS_PRICE_PROPERTY) ? config.getLong(MIN_STABLE_GAS_PRICE_PROPERTY) : 0;
this.method = this.enabled ? config.getEnum(MinGasPriceProviderType.class, METHOD_PROPERTY) : MinGasPriceProviderType.FIXED;
this.config = config;
}

public Duration getRefreshRate() {
return refreshRate;
}

public Long getMinStableGasPrice() {
return minStableGasPrice;
}

public boolean isEnabled() {
return enabled;
}

public HttpGetStableMinGasSystemConfig getHttpGetConfig() {
return new HttpGetStableMinGasSystemConfig(config.getConfig(PARAMS_PROPERTY));
}

public EthCallMinGasPriceSystemConfig getEthCallConfig() {
return new EthCallMinGasPriceSystemConfig(config.getConfig(PARAMS_PROPERTY));
}

public MinGasPriceProviderType getMethod() {
return method;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@
package co.rsk.mine;

import co.rsk.core.Coin;
import co.rsk.mine.gas.provider.MinGasPriceProvider;

/**
* This is the implementation of RSKIP-09
* Created by mario on 22/12/16.
*/
public class MinimumGasPriceCalculator {

private final Coin targetMGP;
private MinGasPriceProvider minGasPriceProvider;

public MinimumGasPriceCalculator(Coin targetMGP) {
this.targetMGP = targetMGP;
public MinimumGasPriceCalculator(MinGasPriceProvider minGasPriceProvider) {
this.minGasPriceProvider = minGasPriceProvider;
}

public Coin calculate(Coin previousMGP) {
BlockGasPriceRange priceRange = new BlockGasPriceRange(previousMGP);
Coin targetMGP = minGasPriceProvider.getMinGasPriceAsCoin();
if (priceRange.inRange(targetMGP)) {
return targetMGP;
}
Expand All @@ -44,4 +46,5 @@ public Coin calculate(Coin previousMGP) {

return priceRange.getLowerLimit();
}

}
Loading

0 comments on commit 7dd58c6

Please sign in to comment.