Skip to content
This repository was archived by the owner on Jul 1, 2025. It is now read-only.

Commit 08a42c5

Browse files
authored
Merge pull request #2 from thinkAfCod/main
handle genesis state root hash
2 parents 19577c7 + ca19317 commit 08a42c5

File tree

15 files changed

+15442
-33
lines changed

15 files changed

+15442
-33
lines changed

besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static org.hyperledger.besu.cli.DefaultCommandValues.getDefaultBesuDataPath;
2323
import static org.hyperledger.besu.cli.config.NetworkName.EPHEMERY;
2424
import static org.hyperledger.besu.cli.config.NetworkName.MAINNET;
25+
import static org.hyperledger.besu.cli.config.NetworkName.OP_MAINNET;
26+
import static org.hyperledger.besu.cli.config.NetworkName.OP_SEPOLIA;
2527
import static org.hyperledger.besu.cli.util.CommandLineUtils.DEPENDENCY_WARNING_MSG;
2628
import static org.hyperledger.besu.cli.util.CommandLineUtils.isOptionSet;
2729
import static org.hyperledger.besu.controller.BesuController.DATABASE_PATH;
@@ -88,6 +90,7 @@
8890
import org.hyperledger.besu.config.GenesisConfigFile;
8991
import org.hyperledger.besu.config.GenesisConfigOptions;
9092
import org.hyperledger.besu.config.MergeConfiguration;
93+
import org.hyperledger.besu.config.OpGenesisConfigFile;
9194
import org.hyperledger.besu.controller.BesuController;
9295
import org.hyperledger.besu.controller.BesuControllerBuilder;
9396
import org.hyperledger.besu.crypto.Blake2bfMessageDigest;
@@ -1597,13 +1600,17 @@ private void validateChainDataPruningParams() {
15971600

15981601
private GenesisConfigFile readGenesisConfigFile() {
15991602
GenesisConfigFile effectiveGenesisFile;
1600-
effectiveGenesisFile =
1601-
network.equals(EPHEMERY)
1602-
? EphemeryGenesisUpdater.updateGenesis(genesisConfigOverrides)
1603-
: genesisFile != null
1604-
? GenesisConfigFile.fromSource(genesisConfigSource(genesisFile))
1605-
: GenesisConfigFile.fromResource(
1606-
Optional.ofNullable(network).orElse(MAINNET).getGenesisFile());
1603+
if (EPHEMERY.equals(network)) {
1604+
effectiveGenesisFile = EphemeryGenesisUpdater.updateGenesis(genesisConfigOverrides);
1605+
} else if (genesisFile != null) {
1606+
effectiveGenesisFile = GenesisConfigFile.fromSource(genesisConfigSource(genesisFile));
1607+
} else if (OP_SEPOLIA.equals(network) || OP_MAINNET.equals(network)) {
1608+
effectiveGenesisFile = OpGenesisConfigFile.fromResource(network.getGenesisFile());
1609+
} else {
1610+
effectiveGenesisFile =
1611+
GenesisConfigFile.fromResource(
1612+
Optional.ofNullable(network).orElse(MAINNET).getGenesisFile());
1613+
}
16071614
return effectiveGenesisFile.withOverrides(genesisConfigOverrides);
16081615
}
16091616

besu/src/main/java/org/hyperledger/besu/cli/config/NetworkName.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ public enum NetworkName {
4646
/** Classic network name. */
4747
CLASSIC("/classic.json", BigInteger.valueOf(1)),
4848
/** Mordor network name. */
49-
MORDOR("/mordor.json", BigInteger.valueOf(7));
49+
MORDOR("/mordor.json", BigInteger.valueOf(7)),
50+
51+
/** Optimism Mainnet network name. */
52+
OP_MAINNET("/optimism-mainnet.json", BigInteger.valueOf(10L)),
53+
/** Optimism sepolia network name. */
54+
OP_SEPOLIA("/optimism-sepolia.json", BigInteger.valueOf(11155420L));
5055

5156
private final String genesisFile;
5257
private final BigInteger networkId;

besu/src/main/java/org/hyperledger/besu/controller/BesuController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.hyperledger.besu.cli.config.EthNetworkConfig;
1818
import org.hyperledger.besu.config.GenesisConfigFile;
1919
import org.hyperledger.besu.config.GenesisConfigOptions;
20+
import org.hyperledger.besu.config.OpGenesisConfigFile;
2021
import org.hyperledger.besu.config.PowAlgorithm;
2122
import org.hyperledger.besu.config.QbftConfigOptions;
2223
import org.hyperledger.besu.cryptoservices.NodeKey;
@@ -365,6 +366,9 @@ public BesuControllerBuilder fromGenesisFile(
365366
builder = new QbftBesuControllerBuilder();
366367
} else if (configOptions.isClique()) {
367368
builder = new CliqueBesuControllerBuilder();
369+
} else if (genesisConfigFile instanceof OpGenesisConfigFile) {
370+
// todo Temporary modification, will later use OptimismBesuControllerBuilder
371+
builder = new MainnetBesuControllerBuilder();
368372
} else {
369373
throw new IllegalArgumentException("Unknown consensus mechanism defined");
370374
}

besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hyperledger.besu.config.CheckpointConfigOptions;
2121
import org.hyperledger.besu.config.GenesisConfigFile;
2222
import org.hyperledger.besu.config.GenesisConfigOptions;
23+
import org.hyperledger.besu.config.OpGenesisConfigFile;
2324
import org.hyperledger.besu.consensus.merge.MergeContext;
2425
import org.hyperledger.besu.consensus.merge.UnverifiedForkchoiceSupplier;
2526
import org.hyperledger.besu.consensus.qbft.BFTPivotSelectorFromPeers;
@@ -40,6 +41,7 @@
4041
import org.hyperledger.besu.ethereum.chain.DefaultBlockchain;
4142
import org.hyperledger.besu.ethereum.chain.GenesisState;
4243
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
44+
import org.hyperledger.besu.ethereum.chain.OptimismGenesisState;
4345
import org.hyperledger.besu.ethereum.chain.VariablesStorage;
4446
import org.hyperledger.besu.ethereum.core.BlockHeader;
4547
import org.hyperledger.besu.ethereum.core.Difficulty;
@@ -840,9 +842,16 @@ private GenesisState getGenesisState(
840842
genesisStateRoot ->
841843
GenesisState.fromStorage(genesisStateRoot, genesisConfigFile, protocolSchedule))
842844
.orElseGet(
843-
() ->
844-
GenesisState.fromConfig(
845-
dataStorageConfiguration, genesisConfigFile, protocolSchedule));
845+
() -> {
846+
if (genesisConfigFile instanceof OpGenesisConfigFile opGenesisConfigFile) {
847+
// todo Temporary modification
848+
return OptimismGenesisState.fromConfig(
849+
dataStorageConfiguration, opGenesisConfigFile, protocolSchedule);
850+
} else {
851+
return GenesisState.fromConfig(
852+
dataStorageConfiguration, genesisConfigFile, protocolSchedule);
853+
}
854+
});
846855
}
847856

848857
private TrieLogPruner createTrieLogPruner(

config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ public class GenesisConfigFile {
3838
/** The constant BASEFEE_AT_GENESIS_DEFAULT_VALUE. */
3939
public static final Wei BASEFEE_AT_GENESIS_DEFAULT_VALUE = Wei.of(1_000_000_000L);
4040

41-
private final GenesisReader loader;
42-
private final ObjectNode genesisRoot;
43-
private Map<String, String> overrides;
41+
final GenesisReader loader;
4442

45-
private GenesisConfigFile(final GenesisReader loader) {
43+
/** genesis root node */
44+
protected final ObjectNode genesisRoot;
45+
46+
/** map of genesis overrides */
47+
protected Map<String, String> overrides;
48+
49+
GenesisConfigFile(final GenesisReader loader) {
4650
this.loader = loader;
4751
this.genesisRoot = loader.getRoot();
4852
}

config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,23 @@ public class JsonGenesisConfigOptions implements GenesisConfigOptions {
5555
private static final String CONSOLIDATION_REQUEST_CONTRACT_ADDRESS_KEY =
5656
"consolidationrequestcontractaddress";
5757

58-
private final ObjectNode configRoot;
59-
private final Map<String, String> configOverrides = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
60-
private final TransitionsConfigOptions transitions;
58+
/** root node. */
59+
protected final ObjectNode configRoot;
60+
61+
/** genesis config override map. */
62+
protected final Map<String, String> configOverrides =
63+
new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
64+
65+
/** transitions config options. */
66+
protected final TransitionsConfigOptions transitions;
6167

6268
/**
6369
* From json object json genesis config options.
6470
*
6571
* @param configRoot the config root
6672
* @return the json genesis config options
6773
*/
68-
public static JsonGenesisConfigOptions fromJsonObject(final ObjectNode configRoot) {
74+
public static JsonOptimismGenesisConfigOptions fromJsonObject(final ObjectNode configRoot) {
6975
return fromJsonObjectWithOverrides(configRoot, emptyMap());
7076
}
7177

@@ -76,11 +82,12 @@ public static JsonGenesisConfigOptions fromJsonObject(final ObjectNode configRoo
7682
* @param configOverrides the config overrides
7783
* @return the json genesis config options
7884
*/
79-
static JsonGenesisConfigOptions fromJsonObjectWithOverrides(
85+
static JsonOptimismGenesisConfigOptions fromJsonObjectWithOverrides(
8086
final ObjectNode configRoot, final Map<String, String> configOverrides) {
8187
final TransitionsConfigOptions transitionsConfigOptions;
8288
transitionsConfigOptions = loadTransitionsFrom(configRoot);
83-
return new JsonGenesisConfigOptions(configRoot, configOverrides, transitionsConfigOptions);
89+
return new JsonOptimismGenesisConfigOptions(
90+
configRoot, configOverrides, transitionsConfigOptions);
8491
}
8592

8693
private static TransitionsConfigOptions loadTransitionsFrom(final ObjectNode parentNode) {
@@ -540,7 +547,13 @@ public Map<String, Object> asMap() {
540547
return builder.build();
541548
}
542549

543-
private OptionalLong getOptionalLong(final String key) {
550+
/**
551+
* Gets optional long value from config, deferring to overrides if they are present.
552+
*
553+
* @param key string to lookup the value for.
554+
* @return OptionalLong value, empty if not present.
555+
*/
556+
protected OptionalLong getOptionalLong(final String key) {
544557
if (configOverrides.containsKey(key)) {
545558
final String value = configOverrides.get(key);
546559
return value == null || value.isEmpty()
@@ -551,7 +564,13 @@ private OptionalLong getOptionalLong(final String key) {
551564
}
552565
}
553566

554-
private OptionalInt getOptionalInt(final String key) {
567+
/**
568+
* Gets an optional int value from config, by key name.
569+
*
570+
* @param key string to fetch the value by.
571+
* @return OptionalInt value.
572+
*/
573+
protected OptionalInt getOptionalInt(final String key) {
555574
if (configOverrides.containsKey(key)) {
556575
final String value = configOverrides.get(key);
557576
return value == null || value.isEmpty()
@@ -562,7 +581,13 @@ private OptionalInt getOptionalInt(final String key) {
562581
}
563582
}
564583

565-
private Optional<BigInteger> getOptionalBigInteger(final String key) {
584+
/**
585+
* Gets an optional BigInteger value from config, by key name.
586+
*
587+
* @param key string key to fetch the value by.
588+
* @return Optional BigInteger value.
589+
*/
590+
protected Optional<BigInteger> getOptionalBigInteger(final String key) {
566591
if (configOverrides.containsKey(key)) {
567592
final String value = configOverrides.get(key);
568593
return value == null || value.isEmpty()
@@ -573,7 +598,13 @@ private Optional<BigInteger> getOptionalBigInteger(final String key) {
573598
}
574599
}
575600

576-
private Optional<Boolean> getOptionalBoolean(final String key) {
601+
/**
602+
* Gets an optional boolean value from config, by key name.
603+
*
604+
* @param key string to fetch the value by.
605+
* @return Optional bolean value.
606+
*/
607+
protected Optional<Boolean> getOptionalBoolean(final String key) {
577608
if (configOverrides.containsKey(key)) {
578609
final String value = configOverrides.get(key);
579610
return value == null || value.isEmpty()
@@ -584,7 +615,13 @@ private Optional<Boolean> getOptionalBoolean(final String key) {
584615
}
585616
}
586617

587-
private Optional<Hash> getOptionalHash(final String key) {
618+
/**
619+
* Gets an optional Hash value from config, by key name.
620+
*
621+
* @param key string to fetch the value by.
622+
* @return Optional Hash value.
623+
*/
624+
protected Optional<Hash> getOptionalHash(final String key) {
588625
if (configOverrides.containsKey(key)) {
589626
final String overrideHash = configOverrides.get(key);
590627
return Optional.of(Hash.fromHexString(overrideHash));
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright contributors to Besu.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
package org.hyperledger.besu.config;
16+
17+
import java.util.Map;
18+
import java.util.OptionalLong;
19+
20+
import com.fasterxml.jackson.databind.node.ObjectNode;
21+
import com.google.common.collect.ImmutableMap;
22+
23+
/** Json implementation of Optimism genesis config options. */
24+
public class JsonOptimismConfigOptions implements OptimismConfigOptions {
25+
26+
/** Default Json Optimism config options */
27+
public static final JsonOptimismConfigOptions DEFAULT =
28+
new JsonOptimismConfigOptions(JsonUtil.createEmptyObjectNode());
29+
30+
private static final String EIP1559_ELASTICITY = "eip1559elasticity";
31+
32+
private static final String EIP1559_DENOMINATOR = "eip1559denominator";
33+
34+
private static final String EIP1559_DENOMINATOR_CANYON = "eip1559denominatorcanyon";
35+
36+
private final ObjectNode optimismConfigRoot;
37+
38+
/**
39+
* Instantiates a new optimism options.
40+
*
41+
* @param optimismConfigRoot the optimism config root
42+
*/
43+
public JsonOptimismConfigOptions(final ObjectNode optimismConfigRoot) {
44+
this.optimismConfigRoot = optimismConfigRoot;
45+
}
46+
47+
/**
48+
* Gets EIP1559 elasticity.
49+
*
50+
* @return the EIP1559 elasticity
51+
*/
52+
@Override
53+
public OptionalLong getEIP1559Elasticity() {
54+
OptionalLong eip1559elasticity = JsonUtil.getLong(optimismConfigRoot, EIP1559_ELASTICITY);
55+
return eip1559elasticity.isEmpty() ? OptionalLong.of(6L) : eip1559elasticity;
56+
}
57+
58+
/**
59+
* Gets EIP1559 denominator.
60+
*
61+
* @return the EIP1559 denominator
62+
*/
63+
@Override
64+
public OptionalLong getEIP1559Denominator() {
65+
OptionalLong eip1559Denominator = JsonUtil.getLong(optimismConfigRoot, EIP1559_DENOMINATOR);
66+
return eip1559Denominator.isEmpty() ? OptionalLong.of(50L) : eip1559Denominator;
67+
}
68+
69+
/**
70+
* Gets EIP1559 denominatorCanyon.
71+
*
72+
* @return the EIP1559 denominatorCanyon
73+
*/
74+
@Override
75+
public OptionalLong getEIP1559DenominatorCanyon() {
76+
OptionalLong eip1559DenominatorCanyon =
77+
JsonUtil.getLong(optimismConfigRoot, EIP1559_DENOMINATOR_CANYON);
78+
return eip1559DenominatorCanyon.isEmpty() ? OptionalLong.of(250L) : eip1559DenominatorCanyon;
79+
}
80+
81+
@Override
82+
public Map<String, Object> asMap() {
83+
final ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
84+
if (optimismConfigRoot.has(EIP1559_ELASTICITY)) {
85+
builder.put(EIP1559_ELASTICITY, getEIP1559Elasticity());
86+
}
87+
if (optimismConfigRoot.has(EIP1559_DENOMINATOR)) {
88+
builder.put(EIP1559_DENOMINATOR, getEIP1559Denominator());
89+
}
90+
if (optimismConfigRoot.has(EIP1559_DENOMINATOR_CANYON)) {
91+
builder.put(EIP1559_DENOMINATOR_CANYON, getEIP1559DenominatorCanyon());
92+
}
93+
return builder.build();
94+
}
95+
}

0 commit comments

Comments
 (0)