From ead31633ee1d8276a9e89ea441daa57538b47c6f Mon Sep 17 00:00:00 2001 From: Daniel Lehrner Date: Tue, 21 Jan 2025 10:15:24 +0100 Subject: [PATCH 1/3] add missing fields to genesis file when command line option is set Signed-off-by: Daniel Lehrner --- .../org/hyperledger/besu/cli/BesuCommand.java | 33 ++++++++++- .../besu/cli/options/GenesisCLIOptions.java | 57 +++++++++++++++++++ .../besu/config/GenesisCLIConfiguration.java | 22 +++++++ .../besu/config/JsonGenesisConfigOptions.java | 11 +++- 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 besu/src/main/java/org/hyperledger/besu/cli/options/GenesisCLIOptions.java create mode 100644 config/src/main/java/org/hyperledger/besu/config/GenesisCLIConfiguration.java diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 8ee35458b2f..dfb151eca96 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -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; @@ -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; @@ -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(); @@ -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); @@ -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 gethGenesisConfigOverrides = + gethGenesisConfigOverrides(genesisConfig); + + return genesisConfig.withOverrides(gethGenesisConfigOverrides); + } + + private Map gethGenesisConfigOverrides(final GenesisConfig genesisConfig) { + final Map 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(); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/options/GenesisCLIOptions.java b/besu/src/main/java/org/hyperledger/besu/cli/options/GenesisCLIOptions.java new file mode 100644 index 00000000000..a61fb1a3315 --- /dev/null +++ b/besu/src/main/java/org/hyperledger/besu/cli/options/GenesisCLIOptions.java @@ -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 { + 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 getCLIOptions() { + return CommandLineUtils.getCLIOptions(this, new GenesisCLIOptions()); + } +} diff --git a/config/src/main/java/org/hyperledger/besu/config/GenesisCLIConfiguration.java b/config/src/main/java/org/hyperledger/besu/config/GenesisCLIConfiguration.java new file mode 100644 index 00000000000..a62f97aec83 --- /dev/null +++ b/config/src/main/java/org/hyperledger/besu/config/GenesisCLIConfiguration.java @@ -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) {} diff --git a/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java b/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java index 9239d4977e8..49742898184 100644 --- a/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java +++ b/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java @@ -129,7 +129,7 @@ public String getConsensusEngine() { @Override public boolean isEthHash() { - return configRoot.has(ETHASH_CONFIG_KEY); + return getOptionalString(ETHASH_CONFIG_KEY).isPresent(); } @Override @@ -551,6 +551,15 @@ public Map asMap() { return builder.build(); } + private Optional getOptionalString(final String key) { + if (configOverrides.containsKey(key)) { + final String value = configOverrides.get(key); + return value == null || value.isEmpty() ? Optional.empty() : Optional.of(value); + } + + return JsonUtil.getString(configRoot, key); + } + private OptionalLong getOptionalLong(final String key) { if (configOverrides.containsKey(key)) { final String value = configOverrides.get(key); From edada1bc38d9e4e30612af20433bcbcd17382ed6 Mon Sep 17 00:00:00 2001 From: Daniel Lehrner Date: Tue, 21 Jan 2025 11:36:35 +0100 Subject: [PATCH 2/3] fix ethhash key lookup Signed-off-by: Daniel Lehrner --- .../besu/config/JsonGenesisConfigOptions.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java b/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java index 49742898184..eb7e3cdf6d0 100644 --- a/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java +++ b/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java @@ -129,7 +129,7 @@ public String getConsensusEngine() { @Override public boolean isEthHash() { - return getOptionalString(ETHASH_CONFIG_KEY).isPresent(); + return isKeyPresent(ETHASH_CONFIG_KEY); } @Override @@ -551,13 +551,12 @@ public Map asMap() { return builder.build(); } - private Optional getOptionalString(final String key) { + private boolean isKeyPresent(final String key) { if (configOverrides.containsKey(key)) { - final String value = configOverrides.get(key); - return value == null || value.isEmpty() ? Optional.empty() : Optional.of(value); + return true; } - return JsonUtil.getString(configRoot, key); + return JsonUtil.getObjectNode(configRoot, key).isPresent(); } private OptionalLong getOptionalLong(final String key) { From 5099b9eb0c3a0f93ac3f594567f89d01c66fe434 Mon Sep 17 00:00:00 2001 From: Daniel Lehrner Date: Tue, 21 Jan 2025 12:45:41 +0100 Subject: [PATCH 3/3] BackwardSyncAlgSpec: increase timeout to pass test in CI Signed-off-by: Daniel Lehrner --- .../ethereum/eth/sync/backwardsync/BackwardSyncAlgSpec.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncAlgSpec.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncAlgSpec.java index bd5a9e7ec9c..c6e2979520c 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncAlgSpec.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncAlgSpec.java @@ -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);