Skip to content

Commit

Permalink
Merge branch 'main' into reuse-bonsai-historical-block-limit
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Dudley <[email protected]>
  • Loading branch information
siladu committed Jan 23, 2024
2 parents 508c91b + 3fc3fb1 commit 2a8b47a
Show file tree
Hide file tree
Showing 32 changed files with 947 additions and 360 deletions.
12 changes: 12 additions & 0 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.hyperledger.besu.chainimport.RlpBlockImporter;
import org.hyperledger.besu.cli.config.EthNetworkConfig;
import org.hyperledger.besu.cli.config.NetworkName;
import org.hyperledger.besu.cli.config.ProfileName;
import org.hyperledger.besu.cli.converter.MetricCategoryConverter;
import org.hyperledger.besu.cli.converter.PercentageConverter;
import org.hyperledger.besu.cli.custom.CorsAllowedOriginsProperty;
Expand Down Expand Up @@ -536,6 +537,13 @@ private InetAddress autoDiscoverDefaultIP() {
+ " (default: ${DEFAULT-VALUE})")
private final NetworkName network = null;

@Option(
names = {PROFILE_OPTION_NAME},
paramLabel = PROFILE_FORMAT_HELP,
description =
"Overwrite default settings. Possible values are ${COMPLETION-CANDIDATES}. (default: none)")
private final ProfileName profile = null;

@Option(
names = {"--nat-method"},
description =
Expand Down Expand Up @@ -3508,6 +3516,10 @@ private String generateConfigurationOverview() {
builder.setNetwork(network.normalize());
}

if (profile != null) {
builder.setProfile(profile.toString());
}

builder.setHasCustomGenesis(genesisFile != null);
if (genesisFile != null) {
builder.setCustomGenesis(genesisFile.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ConfigurationOverviewBuilder {

private String network;
private BigInteger networkId;
private String profile;
private boolean hasCustomGenesis;
private String customGenesisFileName;
private String dataStorage;
Expand Down Expand Up @@ -88,6 +89,17 @@ public ConfigurationOverviewBuilder setNetworkId(final BigInteger networkId) {
return this;
}

/**
* Sets profile.
*
* @param profile the profile
* @return the profile
*/
public ConfigurationOverviewBuilder setProfile(final String profile) {
this.profile = profile;
return this;
}

/**
* Sets whether a custom genesis has been specified.
*
Expand Down Expand Up @@ -290,6 +302,10 @@ public String build() {
lines.add("Network Id: " + networkId);
}

if (profile != null) {
lines.add("Profile: " + profile);
}

if (dataStorage != null) {
lines.add("Data storage: " + dataStorage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public interface DefaultCommandValues {
String MANDATORY_MODE_FORMAT_HELP = "<MODE>";
/** The constant MANDATORY_NETWORK_FORMAT_HELP. */
String MANDATORY_NETWORK_FORMAT_HELP = "<NETWORK>";
/** The constant PROFILE_OPTION_NAME. */
String PROFILE_OPTION_NAME = "--profile";
/** The constant PROFILE_FORMAT_HELP. */
String PROFILE_FORMAT_HELP = "<PROFILE>";
/** The constant MANDATORY_NODE_ID_FORMAT_HELP. */
String MANDATORY_NODE_ID_FORMAT_HELP = "<NODEID>";
/** The constant PERMISSIONING_CONFIG_LOCATION. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.cli.config;

/** Enum for profile names. Each profile corresponds to a configuration file. */
public enum ProfileName {
/** The 'DEV' profile. Corresponds to the 'profiles/dev.toml' configuration file. */
DEV("profiles/dev.toml");

private final String configFile;

/**
* Constructs a new ProfileName.
*
* @param configFile the configuration file corresponding to the profile
*/
ProfileName(final String configFile) {
this.configFile = configFile;
}

/**
* Gets the configuration file corresponding to the profile.
*
* @return the configuration file
*/
public String getConfigFile() {
return configFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.hyperledger.besu.cli.BesuCommand;
import org.hyperledger.besu.cli.DefaultCommandValues;
import org.hyperledger.besu.cli.util.TomlConfigFileDefaultProvider;
import org.hyperledger.besu.cli.util.TomlConfigurationDefaultProvider;
import org.hyperledger.besu.cli.util.VersionProvider;

import java.io.PrintWriter;
Expand Down Expand Up @@ -69,7 +69,8 @@ public ValidateConfigSubCommand(final CommandLine commandLine, final PrintWriter
public void run() {
checkNotNull(parentCommand);
try {
new TomlConfigFileDefaultProvider(commandLine, dataPath.toFile()).loadConfigurationFromFile();
TomlConfigurationDefaultProvider.fromFile(commandLine, dataPath.toFile())
.loadConfigurationFromFile();
} catch (Exception e) {
this.out.println(e);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.cli.util;

import java.util.Map;
import java.util.Optional;

import picocli.CommandLine;

/**
* Abstract class for finding configuration resources. This class provides a common structure for
* classes that need to find configuration resources based on command line options and environment
* variables.
*
* @param <T> the type of configuration resource this finder will return
*/
public abstract class AbstractConfigurationFinder<T> {

/**
* Returns the name of the configuration option.
*
* @return the name of the configuration option
*/
protected abstract String getConfigOptionName();

/**
* Returns the name of the environment variable for the configuration.
*
* @return the name of the environment variable for the configuration
*/
protected abstract String getConfigEnvName();

/**
* Finds the configuration resource based on command line options and environment variables.
*
* @param environment the environment variables
* @param parseResult the command line parse result
* @return an Optional containing the configuration resource, or an empty Optional if no
* configuration resource was found
*/
public Optional<T> findConfiguration(
final Map<String, String> environment, final CommandLine.ParseResult parseResult) {
final CommandLine commandLine = parseResult.commandSpec().commandLine();
if (isConfigSpecifiedInBothSources(environment, parseResult)) {
throwExceptionForBothSourcesSpecified(environment, parseResult, commandLine);
}
if (parseResult.hasMatchedOption(getConfigOptionName())) {
return getFromOption(parseResult, commandLine);
}
if (environment.containsKey(getConfigEnvName())) {
return getFromEnvironment(environment, commandLine);
}
return Optional.empty();
}

/**
* Gets the configuration resource from the command line option.
*
* @param parseResult the command line parse result
* @param commandLine the command line
* @return an Optional containing the configuration resource, or an empty Optional if the
* configuration resource was not specified in the command line option
*/
protected abstract Optional<T> getFromOption(
final CommandLine.ParseResult parseResult, final CommandLine commandLine);

/**
* Gets the configuration resource from the environment variable.
*
* @param environment the environment variables
* @param commandLine the command line
* @return an Optional containing the configuration resource, or an empty Optional if the
* configuration resource was not specified in the environment variable
*/
protected abstract Optional<T> getFromEnvironment(
final Map<String, String> environment, final CommandLine commandLine);

/**
* Checks if the configuration resource is specified in both command line options and environment
* variables.
*
* @param environment the environment variables
* @param parseResult the command line parse result
* @return true if the configuration resource is specified in both places, false otherwise
*/
public boolean isConfigSpecifiedInBothSources(
final Map<String, String> environment, final CommandLine.ParseResult parseResult) {
return parseResult.hasMatchedOption(getConfigOptionName())
&& environment.containsKey(getConfigEnvName());
}

/**
* Throws an exception if the configuration resource is specified in both command line options and
* environment variables.
*
* @param environment the environment variables
* @param parseResult the command line parse result
* @param commandLine the command line
*/
public void throwExceptionForBothSourcesSpecified(
final Map<String, String> environment,
final CommandLine.ParseResult parseResult,
final CommandLine commandLine) {
throw new CommandLine.ParameterException(
commandLine,
String.format(
"Both %s=%s and %s %s specified. Please specify only one.",
getConfigEnvName(),
getConfigOptionName(),
environment.get(getConfigEnvName()),
parseResult.matchedOption(getConfigOptionName()).stringValues()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package org.hyperledger.besu.cli.util;

import static java.util.Arrays.asList;

import java.util.List;

import picocli.CommandLine.IDefaultValueProvider;
Expand All @@ -34,8 +32,8 @@ public class CascadingDefaultProvider implements IDefaultValueProvider {
*
* @param defaultValueProviders List of default value providers
*/
public CascadingDefaultProvider(final IDefaultValueProvider... defaultValueProviders) {
this.defaultValueProviders = asList(defaultValueProviders);
public CascadingDefaultProvider(final List<IDefaultValueProvider> defaultValueProviders) {
this.defaultValueProviders = defaultValueProviders;
}

@Override
Expand Down
100 changes: 100 additions & 0 deletions besu/src/main/java/org/hyperledger/besu/cli/util/ConfigFileFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.cli.util;

import static org.hyperledger.besu.cli.DefaultCommandValues.CONFIG_FILE_OPTION_NAME;

import java.io.File;
import java.util.Map;
import java.util.Optional;

import picocli.CommandLine;

/**
* Class for finding configuration files. This class extends the AbstractConfigurationFinder and
* provides methods for finding configuration files based on command line options and environment
* variables.
*/
public class ConfigFileFinder extends AbstractConfigurationFinder<File> {
private static final String CONFIG_FILE_ENV_NAME = "BESU_CONFIG_FILE";

/**
* Returns the name of the configuration option.
*
* @return the name of the configuration option
*/
@Override
protected String getConfigOptionName() {
return CONFIG_FILE_OPTION_NAME;
}

/**
* Returns the name of the environment variable for the configuration.
*
* @return the name of the environment variable for the configuration
*/
@Override
protected String getConfigEnvName() {
return CONFIG_FILE_ENV_NAME;
}

/**
* Gets the configuration file from the command line option.
*
* @param parseResult the command line parse result
* @param commandLine the command line
* @return an Optional containing the configuration file, or an empty Optional if the
* configuration file was not specified in the command line option
*/
@Override
public Optional<File> getFromOption(
final CommandLine.ParseResult parseResult, final CommandLine commandLine) {
final CommandLine.Model.OptionSpec configFileOption =
parseResult.matchedOption(CONFIG_FILE_OPTION_NAME);
try {
File file = configFileOption.getter().get();
if (!file.exists()) {
throw new CommandLine.ParameterException(
commandLine,
String.format("Unable to read TOML configuration, file not found: %s", file));
}
return Optional.of(file);
} catch (final Exception e) {
throw new CommandLine.ParameterException(commandLine, e.getMessage(), e);
}
}

/**
* Gets the configuration file from the environment variable.
*
* @param environment the environment variables
* @param commandLine the command line
* @return an Optional containing the configuration file, or an empty Optional if the
* configuration file was not specified in the environment variable
*/
@Override
public Optional<File> getFromEnvironment(
final Map<String, String> environment, final CommandLine commandLine) {
final File toml = new File(environment.get(CONFIG_FILE_ENV_NAME));
if (!toml.exists()) {
throw new CommandLine.ParameterException(
commandLine,
String.format(
"TOML file %s specified in environment variable %s not found",
CONFIG_FILE_ENV_NAME, environment.get(CONFIG_FILE_ENV_NAME)));
}
return Optional.of(toml);
}
}
Loading

0 comments on commit 2a8b47a

Please sign in to comment.