Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Geth genesis file via overrides #8144

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.hyperledger.besu.cli.config.NetworkName.MAINNET;
import static org.hyperledger.besu.cli.util.CommandLineUtils.DEPENDENCY_WARNING_MSG;
import static org.hyperledger.besu.cli.util.CommandLineUtils.isOptionSet;
import static org.hyperledger.besu.config.GenesisConfig.BASEFEE_AT_GENESIS_DEFAULT_VALUE;
import static org.hyperledger.besu.controller.BesuController.DATABASE_PATH;
import static org.hyperledger.besu.ethereum.api.jsonrpc.authentication.EngineAuthService.EPHEMERAL_JWT_FILE;
import static org.hyperledger.besu.nat.kubernetes.KubernetesNatManager.DEFAULT_BESU_SERVICE_NAME_FILTER;
Expand All @@ -48,6 +49,7 @@
import org.hyperledger.besu.cli.options.EthProtocolOptions;
import org.hyperledger.besu.cli.options.EthstatsOptions;
import org.hyperledger.besu.cli.options.EvmOptions;
import org.hyperledger.besu.cli.options.GenesisCLIOptions;
import org.hyperledger.besu.cli.options.GraphQlOptions;
import org.hyperledger.besu.cli.options.InProcessRpcOptions;
import org.hyperledger.besu.cli.options.IpcOptions;
Expand Down Expand Up @@ -303,6 +305,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
private final EvmOptions unstableEvmOptions = EvmOptions.create();
private final IpcOptions unstableIpcOptions = IpcOptions.create();
private final ChainPruningOptions unstableChainPruningOptions = ChainPruningOptions.create();
private final GenesisCLIOptions unstableGenesisCLIOptions = GenesisCLIOptions.create();

// stable CLI options
final DataStorageOptions dataStorageOptions = DataStorageOptions.create();
Expand Down Expand Up @@ -1164,6 +1167,7 @@ private void handleUnstableOptions() {
.put("EVM Options", unstableEvmOptions)
.put("IPC Options", unstableIpcOptions)
.put("Chain Data Pruning Options", unstableChainPruningOptions)
.put("Genesis File Options", unstableGenesisCLIOptions)
.build();

UnstableOptionsSubCommand.createUnstableOptions(commandLine, unstableOptions);
Expand Down Expand Up @@ -1604,12 +1608,39 @@ private GenesisConfig readGenesisConfig() {
network.equals(EPHEMERY)
? EphemeryGenesisUpdater.updateGenesis(genesisConfigOverrides)
: genesisFile != null
? GenesisConfig.fromSource(genesisConfigSource(genesisFile))
? getGenesisConfigFromSource()
: GenesisConfig.fromResource(
Optional.ofNullable(network).orElse(MAINNET).getGenesisFile());
return effectiveGenesisFile.withOverrides(genesisConfigOverrides);
}

private GenesisConfig getGenesisConfigFromSource() {
final GenesisConfig genesisConfig = GenesisConfig.fromSource(genesisConfigSource(genesisFile));

if (!unstableGenesisCLIOptions.toDomainObject().gethGenesisFileSupport()) {
return genesisConfig;
}

final Map<String, String> gethGenesisConfigOverrides =
gethGenesisConfigOverrides(genesisConfig);

return genesisConfig.withOverrides(gethGenesisConfigOverrides);
}

private Map<String, String> gethGenesisConfigOverrides(final GenesisConfig genesisConfig) {
final Map<String, String> overrides = new HashMap<>();

if (!genesisConfig.getConfigOptions().isEthHash()) {
overrides.put("ethash", "");
}

if (genesisConfig.getBaseFeePerGas().isEmpty()) {
overrides.put("baseFeePerGas", BASEFEE_AT_GENESIS_DEFAULT_VALUE.toHexString());
}

return overrides;
}

private GenesisConfigOptions readGenesisConfigOptions() {
try {
return genesisConfigSupplier.get().getConfigOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* 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.options;

import org.hyperledger.besu.cli.util.CommandLineUtils;
import org.hyperledger.besu.config.GenesisCLIConfiguration;

import java.util.List;

import picocli.CommandLine;

/** Command line options for configuring the genesis file. */
public class GenesisCLIOptions implements CLIOptions<GenesisCLIConfiguration> {
private static final String GETH_GENESIS_FILE_SUPPORT = "--Xgeth-genesis-file-support";

private GenesisCLIOptions() {
// empty constructor
}

@CommandLine.Option(
hidden = true,
names = {GETH_GENESIS_FILE_SUPPORT},
description =
"Enable the support of using Geth style genesis files (default: ${DEFAULT-VALUE})")
private final Boolean gethGenesisFileSupport = Boolean.FALSE;

/**
* Create GenesisCLIOptions instance
*
* @return new GenesisCLIOptions object
*/
public static GenesisCLIOptions create() {
return new GenesisCLIOptions();
}

@Override
public GenesisCLIConfiguration toDomainObject() {
return new GenesisCLIConfiguration(gethGenesisFileSupport);
}

@Override
public List<String> getCLIOptions() {
return CommandLineUtils.getCLIOptions(this, new GenesisCLIOptions());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* 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.config;

/**
* Configuration for the genesis file.
*
* @param gethGenesisFileSupport Whether to support Geth genesis file format.
*/
public record GenesisCLIConfiguration(boolean gethGenesisFileSupport) {}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public String getConsensusEngine() {

@Override
public boolean isEthHash() {
return configRoot.has(ETHASH_CONFIG_KEY);
return isKeyPresent(ETHASH_CONFIG_KEY);
}

@Override
Expand Down Expand Up @@ -551,6 +551,14 @@ public Map<String, Object> asMap() {
return builder.build();
}

private boolean isKeyPresent(final String key) {
if (configOverrides.containsKey(key)) {
return true;
}

return JsonUtil.getObjectNode(configRoot, key).isPresent();
}

private OptionalLong getOptionalLong(final String key) {
if (configOverrides.containsKey(key)) {
final String value = configOverrides.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void shouldAwokeWhenConditionReachedAndReady() throws Exception {

completionCaptor.getValue().onInitialSyncCompleted();

voidCompletableFuture.get(800, TimeUnit.MILLISECONDS);
voidCompletableFuture.get(5, TimeUnit.SECONDS);
assertThat(voidCompletableFuture).isCompleted();

verify(context.getSyncState()).unsubscribeTTDReached(88L);
Expand Down
Loading