Skip to content

Commit

Permalink
Move miner options tests
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 Oct 11, 2023
1 parent 7684d7e commit 78741da
Show file tree
Hide file tree
Showing 6 changed files with 751 additions and 431 deletions.
93 changes: 46 additions & 47 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.hyperledger.besu.cli.options.stable.DataStorageOptions;
import org.hyperledger.besu.cli.options.stable.EthstatsOptions;
import org.hyperledger.besu.cli.options.stable.LoggingLevelOption;
import org.hyperledger.besu.cli.options.stable.MiningOptions;
import org.hyperledger.besu.cli.options.stable.NodePrivateKeyFileOption;
import org.hyperledger.besu.cli.options.stable.P2PTLSConfigOptions;
import org.hyperledger.besu.cli.options.stable.TransactionPoolOptions;
Expand All @@ -65,7 +66,6 @@
import org.hyperledger.besu.cli.options.unstable.EvmOptions;
import org.hyperledger.besu.cli.options.unstable.IpcOptions;
import org.hyperledger.besu.cli.options.unstable.MetricsCLIOptions;
import org.hyperledger.besu.cli.options.unstable.MiningOptions;
import org.hyperledger.besu.cli.options.unstable.NatOptions;
import org.hyperledger.besu.cli.options.unstable.NativeLibraryOptions;
import org.hyperledger.besu.cli.options.unstable.NetworkingOptions;
Expand Down Expand Up @@ -287,7 +287,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
unstableTransactionPoolOptions =
org.hyperledger.besu.cli.options.unstable.TransactionPoolOptions.create();
private final DnsOptions unstableDnsOptions = DnsOptions.create();
private final MiningOptions unstableMiningOptions = MiningOptions.create();
// private final MiningOptions unstableMiningOptions = MiningOptions.create();
private final NatOptions unstableNatOptions = NatOptions.create();
private final NativeLibraryOptions unstableNativeLibraryOptions = NativeLibraryOptions.create();
private final RPCOptions unstableRPCOptions = RPCOptions.create();
Expand All @@ -308,8 +308,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
stableTransactionPoolOptions = TransactionPoolOptions.create();

@CommandLine.ArgGroup(validate = false, heading = "@|bold Miner Options|@%n")
final org.hyperledger.besu.cli.options.stable.MiningOptions stableMiningOptions =
org.hyperledger.besu.cli.options.stable.MiningOptions.create();
final MiningOptions miningOptions = MiningOptions.create();

private final RunnerBuilder runnerBuilder;
private final BesuController.Builder controllerBuilderFactory;
Expand Down Expand Up @@ -1489,7 +1488,7 @@ public void run() {

// Need to create vertx after cmdline has been parsed, such that metricsSystem is configurable
vertx = createVertx(createVertxOptions(metricsSystem.get()));
miningParameters = buildMininingParameters();
miningParameters = buildMiningParameters();

validateOptions();
configure();
Expand Down Expand Up @@ -1587,7 +1586,7 @@ private void handleUnstableOptions() {
.put("Privacy Plugin Configuration", unstablePrivacyPluginOptions)
.put("Synchronizer", unstableSynchronizerOptions)
.put("TransactionPool", unstableTransactionPoolOptions)
.put("Mining", unstableMiningOptions)
// .put("Mining", unstableMiningOptions)
.put("Native Library", unstableNativeLibraryOptions)
.put("EVM Options", unstableEvmOptions)
.put("IPC Options", unstableIpcOptions)
Expand Down Expand Up @@ -1872,9 +1871,9 @@ private void validateRequiredOptions() {
});
}

@SuppressWarnings("ConstantConditions")
// @SuppressWarnings("ConstantConditions")
private void validateMiningParams() {
stableMiningOptions.validate(
miningOptions.validate(
commandLine, logger, isMergeEnabled(), getActualGenesisConfigOptions().isEthHash());
// if (Boolean.TRUE.equals(minerOptionGroup.isMiningEnabled)
// && minerOptionGroup.coinbase == null) {
Expand All @@ -1892,19 +1891,19 @@ private void validateMiningParams() {
// (remove --miner-stratum-enabled) "
// + "or specify mining is enabled (--miner-enabled)");
// }
if (unstableMiningOptions.getPosBlockCreationMaxTime() <= 0
|| unstableMiningOptions.getPosBlockCreationMaxTime()
> MiningParameters.DEFAULT_POS_BLOCK_CREATION_MAX_TIME) {
throw new ParameterException(
this.commandLine, "--Xpos-block-creation-max-time must be positive and ≤ 12000");
}

if (unstableMiningOptions.getPosBlockCreationRepetitionMinDuration() <= 0
|| unstableMiningOptions.getPosBlockCreationRepetitionMinDuration() > 2000) {
throw new ParameterException(
this.commandLine,
"--Xpos-block-creation-repetition-min-duration must be positive and ≤ 2000");
}
// if (unstableMiningOptions.getPosBlockCreationMaxTime() <= 0
// || unstableMiningOptions.getPosBlockCreationMaxTime()
// > MiningParameters.DEFAULT_POS_BLOCK_CREATION_MAX_TIME) {
// throw new ParameterException(
// this.commandLine, "--Xpos-block-creation-max-time must be positive and ≤ 12000");
// }
//
// if (unstableMiningOptions.getPosBlockCreationRepetitionMinDuration() <= 0
// || unstableMiningOptions.getPosBlockCreationRepetitionMinDuration() > 2000) {
// throw new ParameterException(
// this.commandLine,
// "--Xpos-block-creation-repetition-min-duration must be positive and ≤ 2000");
// }
}

/**
Expand Down Expand Up @@ -2077,30 +2076,30 @@ private void issueOptionWarnings() {
"--p2p-port",
"--remote-connections-max-percentage"));

// Check that block producer options work
if (!isMergeEnabled() && getActualGenesisConfigOptions().isEthHash()) {
CommandLineUtils.checkOptionDependencies(
logger,
commandLine,
"--miner-enabled",
!miningParameters.isMiningEnabled(),
asList(
"--miner-coinbase",
"--min-gas-price",
"--min-block-occupancy-ratio",
"--miner-extra-data"));

// Check that mining options are able to work
CommandLineUtils.checkOptionDependencies(
logger,
commandLine,
"--miner-enabled",
!miningParameters.isMiningEnabled(),
asList(
"--miner-stratum-enabled",
"--Xminer-remote-sealers-limit",
"--Xminer-remote-sealers-hashrate-ttl"));
}
// // Check that block producer options work
// if (!isMergeEnabled() && getActualGenesisConfigOptions().isEthHash()) {
// CommandLineUtils.checkOptionDependencies(
// logger,
// commandLine,
// "--miner-enabled",
// !miningParameters.isMiningEnabled(),
// asList(
// "--miner-coinbase",
// "--min-gas-price",
// "--min-block-occupancy-ratio",
// "--miner-extra-data"));
//
// // Check that mining options are able to work
// CommandLineUtils.checkOptionDependencies(
// logger,
// commandLine,
// "--miner-enabled",
// !miningParameters.isMiningEnabled(),
// asList(
// "--miner-stratum-enabled",
// "--Xminer-remote-sealers-limit",
// "--Xminer-remote-sealers-hashrate-ttl"));
// }

CommandLineUtils.failIfOptionDoesntMeetRequirement(
commandLine,
Expand Down Expand Up @@ -2959,8 +2958,8 @@ private TransactionPoolConfiguration buildTransactionPoolConfiguration() {
.build();
}

private MiningParameters buildMininingParameters() {
return stableMiningOptions.toDomainObject();
private MiningParameters buildMiningParameters() {
return miningOptions.toDomainObject();
}

private boolean isPruningEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
package org.hyperledger.besu.cli.options.stable;

import static java.util.Arrays.asList;
import static org.hyperledger.besu.ethereum.core.MiningParameters.DEFAULT_MAX_OMMERS_DEPTH;
import static org.hyperledger.besu.ethereum.core.MiningParameters.DEFAULT_POS_BLOCK_CREATION_MAX_TIME;
import static org.hyperledger.besu.ethereum.core.MiningParameters.DEFAULT_POS_BLOCK_CREATION_REPETITION_MIN_DURATION;
import static org.hyperledger.besu.ethereum.core.MiningParameters.DEFAULT_POW_JOB_TTL;
import static org.hyperledger.besu.ethereum.core.MiningParameters.DEFAULT_REMOTE_SEALERS_LIMIT;
import static org.hyperledger.besu.ethereum.core.MiningParameters.DEFAULT_REMOTE_SEALERS_TTL;
import static org.hyperledger.besu.ethereum.core.MiningParameters.Dynamic.DEFAULT_EXTRA_DATA;
import static org.hyperledger.besu.ethereum.core.MiningParameters.Dynamic.DEFAULT_MIN_BLOCK_OCCUPANCY_RATIO;
import static org.hyperledger.besu.ethereum.core.MiningParameters.Dynamic.DEFAULT_MIN_TRANSACTION_GAS_PRICE;
Expand All @@ -32,19 +38,20 @@
import org.slf4j.Logger;
import picocli.CommandLine;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParameterException;

/** The Mining CLI options. */
public class MiningOptions implements CLIOptions<MiningParameters> {

@Option(
names = {"--miner-enabled"},
description = "Set if node will perform mining (default: ${DEFAULT-VALUE})")
private final Boolean isMiningEnabled = false;
private Boolean isMiningEnabled = false;

@Option(
names = {"--miner-stratum-enabled"},
description = "Set if node will perform Stratum mining (default: ${DEFAULT-VALUE})")
private final Boolean iStratumMiningEnabled = false;
private Boolean iStratumMiningEnabled = false;

@SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings.
@Option(
Expand All @@ -55,45 +62,103 @@ public class MiningOptions implements CLIOptions<MiningParameters> {
@Option(
names = {"--miner-stratum-port"},
description = "Stratum port binding (default: ${DEFAULT-VALUE})")
private final Integer stratumPort = 8008;
private Integer stratumPort = 8008;

@Option(
names = {"--miner-coinbase"},
description =
"Account to which mining rewards are paid. You must specify a valid coinbase if "
+ "mining is enabled using --miner-enabled option",
arity = "1")
private final Address coinbase = null;
private Address coinbase = null;

@Option(
names = {"--miner-extra-data"},
description =
"A hex string representing the (32) bytes to be included in the extra data "
+ "field of a mined block (default: ${DEFAULT-VALUE})",
arity = "1")
private final Bytes extraData = DEFAULT_EXTRA_DATA;
private Bytes extraData = DEFAULT_EXTRA_DATA;

@Option(
names = {"--min-block-occupancy-ratio"},
description = "Minimum occupancy ratio for a mined block (default: ${DEFAULT-VALUE})",
arity = "1")
private final Double minBlockOccupancyRatio = DEFAULT_MIN_BLOCK_OCCUPANCY_RATIO;
private Double minBlockOccupancyRatio = DEFAULT_MIN_BLOCK_OCCUPANCY_RATIO;

@Option(
names = {"--min-gas-price"},
description =
"Minimum price (in Wei) offered by a transaction for it to be included in a mined "
+ "block (default: ${DEFAULT-VALUE})",
arity = "1")
private final Wei minTransactionGasPrice = DEFAULT_MIN_TRANSACTION_GAS_PRICE;
private Wei minTransactionGasPrice = DEFAULT_MIN_TRANSACTION_GAS_PRICE;

@Option(
names = {"--target-gas-limit"},
description =
"Sets target gas limit per block. If set, each block's gas limit will approach this setting over time if the current gas limit is different.")
private final Long targetGasLimit = null;
private Long targetGasLimit = null;

private MiningOptions() {}
@CommandLine.ArgGroup(
validate = false)
private final Unstable unstableOptions = new Unstable();

static class Unstable {
@CommandLine.Option(
hidden = true,
names = {"--Xminer-remote-sealers-limit"},
description =
"Limits the number of remote sealers that can submit their hashrates (default: ${DEFAULT-VALUE})")
private Integer remoteSealersLimit = DEFAULT_REMOTE_SEALERS_LIMIT;

@CommandLine.Option(
hidden = true,
names = {"--Xminer-remote-sealers-hashrate-ttl"},
description =
"Specifies the lifetime of each entry in the cache. An entry will be automatically deleted if no update has been received before the deadline (default: ${DEFAULT-VALUE} minutes)")
private Long remoteSealersTimeToLive = DEFAULT_REMOTE_SEALERS_TTL;

@CommandLine.Option(
hidden = true,
names = {"--Xminer-pow-job-ttl"},
description =
"Specifies the time PoW jobs are kept in cache and will accept a solution from miners (default: ${DEFAULT-VALUE} milliseconds)")
private Long powJobTimeToLive = DEFAULT_POW_JOB_TTL;

@CommandLine.Option(
hidden = true,
names = {"--Xmax-ommers-depth"},
description =
"Specifies the depth of ommer blocks to accept when receiving solutions (default: ${DEFAULT-VALUE})")
private Integer maxOmmersDepth = DEFAULT_MAX_OMMERS_DEPTH;

@CommandLine.Option(
hidden = true,
names = {"--Xminer-stratum-extranonce"},
description = "Extranonce for Stratum network miners (default: ${DEFAULT-VALUE})")
private String stratumExtranonce = "080c";

@CommandLine.Option(
hidden = true,
names = {"--Xpos-block-creation-max-time"},
description =
"Specifies the maximum time, in milliseconds, a PoS block creation jobs is allowed to run. Must be positive and ≤ 12000 (default: ${DEFAULT-VALUE} milliseconds)")
private Long posBlockCreationMaxTime = DEFAULT_POS_BLOCK_CREATION_MAX_TIME;

@CommandLine.Option(
hidden = true,
names = {"--Xpos-block-creation-repetition-min-duration"},
description =
"If a PoS block creation repetition takes less than this duration, in milliseconds,"
+ " then it waits before next repetition. Must be positive and ≤ 2000 (default: ${DEFAULT-VALUE} milliseconds)")
private Long posBlockCreationRepetitionMinDuration =
DEFAULT_POS_BLOCK_CREATION_REPETITION_MIN_DURATION;
}

private MiningOptions() {

}

/**
* Create mining options.
Expand All @@ -119,13 +184,13 @@ public void validate(
final boolean isMergeEnabled,
final boolean isEthHash) {
if (Boolean.TRUE.equals(isMiningEnabled) && coinbase == null) {
throw new CommandLine.ParameterException(
throw new ParameterException(
commandLine,
"Unable to mine without a valid coinbase. Either disable mining (remove --miner-enabled) "
+ "or specify the beneficiary of mining (via --miner-coinbase <Address>)");
}
if (Boolean.FALSE.equals(isMiningEnabled) && Boolean.TRUE.equals(iStratumMiningEnabled)) {
throw new CommandLine.ParameterException(
throw new ParameterException(
commandLine,
"Unable to mine with Stratum if mining is disabled. Either disable Stratum mining (remove --miner-stratum-enabled) "
+ "or specify mining is enabled (--miner-enabled)");
Expand Down Expand Up @@ -155,6 +220,42 @@ public void validate(
"--Xminer-remote-sealers-limit",
"--Xminer-remote-sealers-hashrate-ttl"));
}

if (unstableOptions.posBlockCreationMaxTime <= 0
|| unstableOptions.posBlockCreationMaxTime
> MiningParameters.DEFAULT_POS_BLOCK_CREATION_MAX_TIME) {
throw new ParameterException(
commandLine, "--Xpos-block-creation-max-time must be positive and ≤ 12000");
}

if (unstableOptions.posBlockCreationRepetitionMinDuration <= 0
|| unstableOptions.posBlockCreationRepetitionMinDuration > 2000) {
throw new ParameterException(
commandLine,
"--Xpos-block-creation-repetition-min-duration must be positive and ≤ 2000");
}
}

static MiningOptions fromConfig(final MiningParameters miningParameters) {
final MiningOptions miningOptions = MiningOptions.create();
miningOptions.isMiningEnabled = miningParameters.isMiningEnabled();
miningOptions.iStratumMiningEnabled = miningParameters.isStratumMiningEnabled();
miningOptions.stratumNetworkInterface = miningParameters.getStratumNetworkInterface();
miningOptions.stratumPort = miningParameters.getStratumPort();
miningOptions.extraData = miningParameters.getDynamic().getExtraData();
miningOptions.minTransactionGasPrice = miningParameters.getDynamic().getMinTransactionGasPrice();
miningOptions.minBlockOccupancyRatio = miningParameters.getDynamic().getMinBlockOccupancyRatio();
miningOptions.unstableOptions.remoteSealersLimit = miningParameters.getRemoteSealersLimit();
miningOptions.unstableOptions.remoteSealersTimeToLive = miningParameters.getRemoteSealersTimeToLive();
miningOptions.unstableOptions.powJobTimeToLive = miningParameters.getPowJobTimeToLive();
miningOptions.unstableOptions.maxOmmersDepth = miningParameters.getMaxOmmerDepth();
miningOptions.unstableOptions.stratumExtranonce = miningParameters.getStratumExtranonce();
miningOptions.unstableOptions.posBlockCreationMaxTime = miningParameters.getPosBlockCreationMaxTime();
miningOptions.unstableOptions.posBlockCreationRepetitionMinDuration = miningParameters.getPosBlockCreationRepetitionMinDuration();

miningParameters.getCoinbase().ifPresent(coinbase -> miningOptions.coinbase = coinbase);
miningParameters.getDynamic().getTargetGasLimit().ifPresent(tgl -> miningOptions.targetGasLimit = tgl);
return miningOptions;
}

@Override
Expand All @@ -164,7 +265,14 @@ public MiningParameters toDomainObject() {
.isMiningEnabled(isMiningEnabled)
.isStratumMiningEnabled(iStratumMiningEnabled)
.stratumNetworkInterface(stratumNetworkInterface)
.stratumPort(stratumPort);
.stratumPort(stratumPort)
.remoteSealersLimit(unstableOptions.remoteSealersLimit)
.remoteSealersTimeToLive(unstableOptions.remoteSealersTimeToLive)
.powJobTimeToLive(unstableOptions.powJobTimeToLive)
.maxOmmerDepth(unstableOptions.maxOmmersDepth)
.stratumExtranonce(unstableOptions.stratumExtranonce)
.posBlockCreationMaxTime(unstableOptions.posBlockCreationMaxTime)
.posBlockCreationRepetitionMinDuration(unstableOptions.posBlockCreationRepetitionMinDuration);

if (coinbase != null) {
miningParametersBuilder.coinbase(coinbase);
Expand Down
Loading

0 comments on commit 78741da

Please sign in to comment.