Skip to content

Commit 55819c5

Browse files
refactor: move target gas limit to network definition (#9502)
Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
1 parent 05b6c1a commit 55819c5

File tree

7 files changed

+176
-58
lines changed

7 files changed

+176
-58
lines changed

acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ProcessBesuNodeRunner.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ private List<String> commandlineArgs(final BesuNode node, final Path dataDir) {
326326
node.getMiningParameters().getMinTransactionGasPrice().intValue()));
327327
});
328328

329+
if (node.getMiningParameters().getTargetGasLimit().isPresent()) {
330+
params.add("--target-gas-limit");
331+
params.add(Long.toString(node.getMiningParameters().getTargetGasLimit().getAsLong()));
332+
}
333+
329334
if (!node.isP2pEnabled()) {
330335
params.add("--p2p-enabled");
331336
params.add("false");

acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ public BesuNode createExecutionEngineGenesisNode(final String name, final String
645645
MiningConfiguration.newDefault()
646646
.setCoinbase(AddressHelpers.ofValue(1))
647647
.setExtraData(Bytes.EMPTY)
648-
.setMiningEnabled(true))
648+
.setMiningEnabled(true)
649+
.setTargetGasLimit(60_000_000L)) // make tests agnostic to gas limit changes
649650
.jsonRpcEnabled()
650651
.jsonRpcTxPool()
651652
.engineRpcEnabled(true)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,8 +2716,8 @@ private String generateConfigurationOverview() {
27162716
if (miningParametersSupplier.get().getTargetGasLimit().isPresent()) {
27172717
builder.setTargetGasLimit(miningParametersSupplier.get().getTargetGasLimit().getAsLong());
27182718
} else {
2719-
builder.setTargetGasLimit(
2720-
MergeCoordinator.getDefaultGasLimitByChainId(Optional.of(ethNetworkConfig.networkId())));
2719+
MergeCoordinator.getDefaultGasLimitByChainId(genesisConfigOptionsSupplier.get().getChainId())
2720+
.ifPresent(builder::setTargetGasLimit);
27212721
}
27222722

27232723
builder

app/src/test/java/org/hyperledger/besu/cli/config/EthNetworkConfigTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,13 @@ public void testBuilderWithNetworkId() {
105105
assertThat(config.bootNodes()).isNotEmpty();
106106
assertThat(config.networkId()).isEqualTo(BigInteger.valueOf(42));
107107
}
108+
109+
@Test
110+
public void testNetworkDefinitionChainIdsMatchGenesis() {
111+
for (NetworkDefinition network : NetworkDefinition.values()) {
112+
EthNetworkConfig config = EthNetworkConfig.getNetworkConfig(network);
113+
assertThat(config.genesisConfig().getConfigOptions().getChainId().orElseThrow())
114+
.isEqualTo(network.getChainId());
115+
}
116+
}
108117
}

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

Lines changed: 147 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,71 +21,153 @@
2121
/** The enum Network name. */
2222
public enum NetworkDefinition {
2323
/** Mainnet network name. */
24-
MAINNET("/mainnet.json", BigInteger.valueOf(1), true, true),
24+
MAINNET(
25+
"/mainnet.json",
26+
1, // chain id
27+
1, // network id
28+
true, // can snap sync
29+
true, // native required
30+
60_000_000L), // target gas limit
2531
/** Sepolia network name. */
26-
SEPOLIA("/sepolia.json", BigInteger.valueOf(11155111), true, true),
32+
SEPOLIA(
33+
"/sepolia.json",
34+
11155111, // chain id
35+
11155111, // network id
36+
true, // can snap sync
37+
true, // native required
38+
60_000_000L), // target gas limit
2739
/** Holešky network name. */
28-
HOLESKY("/holesky.json", BigInteger.valueOf(17000), true, true),
40+
HOLESKY(
41+
"/holesky.json",
42+
17000, // chain id
43+
17000, // network id
44+
true, // can snap sync
45+
true, // native required
46+
60_000_000L, // target gas limit
47+
"November 2025"),
2948
/** Hoodi network name. */
30-
HOODI("/hoodi.json", BigInteger.valueOf(560048), true, true),
49+
HOODI(
50+
"/hoodi.json",
51+
560048, // chain id
52+
560048, // network id
53+
true, // can snap sync
54+
true, // native required
55+
60_000_000L), // target gas limit
3156
/**
3257
* EPHEMERY network name. The actual networkId used is calculated based on this default value and
3358
* the current time. <a href="https://ephemery.dev/">Ephemery developer info</a>
3459
*/
35-
EPHEMERY("/ephemery.json", BigInteger.valueOf(39438135), true, true),
60+
EPHEMERY(
61+
"/ephemery.json",
62+
39438135, // chain id
63+
39438135, // network id
64+
true, // can snap sync
65+
true, // native required
66+
60_000_000L), // target gas limit
3667
/**
3768
* Linea mainnet network name <a
3869
* href="https://docs.linea.build/get-started/how-to/run-a-node/besu">Linea Besu developer
3970
* info</a>
4071
*/
41-
LINEA_MAINNET("/linea-mainnet.json", BigInteger.valueOf(59144), true, true),
72+
LINEA_MAINNET(
73+
"/linea-mainnet.json",
74+
59144, // chain id
75+
59144, // network id
76+
true, // can snap sync
77+
true, // native required
78+
60_000_000L), // target gas limit
4279
/** Linea sepolia network name */
43-
LINEA_SEPOLIA("/linea-sepolia.json", BigInteger.valueOf(59141), true, true),
80+
LINEA_SEPOLIA(
81+
"/linea-sepolia.json",
82+
59141, // chain id
83+
59141, // network id
84+
true, // can snap sync
85+
true, // native required
86+
60_000_000L), // target gas limit
4487
/** LUKSO mainnet network name. */
45-
LUKSO("/lukso.json", BigInteger.valueOf(42)),
88+
LUKSO(
89+
"/lukso.json",
90+
42, // chain id
91+
42, // network id
92+
true, // can snap sync
93+
false, // native required
94+
60_000_000L), // target gas limit
4695
/** Dev network name. */
47-
DEV("/dev.json", BigInteger.valueOf(2018), false),
96+
DEV(
97+
"/dev.json",
98+
1337, // chain id
99+
2018, // network id
100+
false, // can snap sync
101+
false, // native required
102+
60_000_000L), // target gas limit
48103
/** Future EIPs network name. */
49-
FUTURE_EIPS("/future.json", BigInteger.valueOf(2022), false),
104+
FUTURE_EIPS(
105+
"/future.json",
106+
2022, // chain id
107+
2022, // network id
108+
false, // can snap sync
109+
false, // native required
110+
60_000_000L), // target gas limit
50111
/** Experimental EIPs network name. */
51-
EXPERIMENTAL_EIPS("/experimental.json", BigInteger.valueOf(2023), false),
112+
EXPERIMENTAL_EIPS(
113+
"/experimental.json",
114+
2023, // chain id
115+
2023, // network id
116+
false, // can snap sync
117+
false, // native required
118+
60_000_000L), // target gas limit
52119
/** Classic network name. */
53-
CLASSIC("/classic.json", BigInteger.valueOf(1)),
120+
CLASSIC(
121+
"/classic.json",
122+
61, // chain id
123+
1, // network id
124+
true, // can snap sync
125+
false, // native required
126+
60_000_000L, // target gas limit
127+
"November 2025"),
54128
/** Mordor network name. */
55-
MORDOR("/mordor.json", BigInteger.valueOf(7));
129+
MORDOR(
130+
"/mordor.json",
131+
63, // chain id
132+
7, // network id
133+
true, // can snap sync
134+
false, // native required
135+
60_000_000L, // target gas limit
136+
"November 2025");
56137

57138
private final String genesisFile;
58-
private final BigInteger networkId;
139+
private final long chainId;
140+
private final long networkId;
59141
private final boolean canSnapSync;
60-
private String deprecationDate;
142+
private final String deprecationDate;
61143
private final boolean nativeRequired;
62-
63-
static {
64-
HOLESKY.deprecationDate = "November 2025";
65-
CLASSIC.deprecationDate = "November 2025";
66-
MORDOR.deprecationDate = "November 2025";
67-
}
68-
69-
NetworkDefinition(final String genesisFile, final BigInteger networkId) {
70-
this(genesisFile, networkId, true);
71-
}
144+
private final long targetGasLimit;
72145

73146
NetworkDefinition(
74-
final String genesisFile, final BigInteger networkId, final boolean canSnapSync) {
75-
this(genesisFile, networkId, canSnapSync, false);
147+
final String genesisFile,
148+
final long chainId,
149+
final long networkId,
150+
final boolean canSnapSync,
151+
final boolean nativeRequired,
152+
final long targetGasLimit) {
153+
this(genesisFile, chainId, networkId, canSnapSync, nativeRequired, targetGasLimit, null);
76154
}
77155

78156
NetworkDefinition(
79157
final String genesisFile,
80-
final BigInteger networkId,
158+
final long chainId,
159+
final long networkId,
81160
final boolean canSnapSync,
82-
final boolean nativeRequired) {
161+
final boolean nativeRequired,
162+
final long targetGasLimit,
163+
final String deprecationDate) {
83164
this.genesisFile = genesisFile;
165+
this.chainId = chainId;
84166
this.networkId = networkId;
85167
this.canSnapSync = canSnapSync;
86-
// no deprecations planned
87-
this.deprecationDate = null;
88168
this.nativeRequired = nativeRequired;
169+
this.targetGasLimit = targetGasLimit;
170+
this.deprecationDate = deprecationDate;
89171
}
90172

91173
/**
@@ -97,13 +179,22 @@ public String getGenesisFile() {
97179
return genesisFile;
98180
}
99181

182+
/**
183+
* Gets chain id.
184+
*
185+
* @return the chain id
186+
*/
187+
public BigInteger getChainId() {
188+
return BigInteger.valueOf(chainId);
189+
}
190+
100191
/**
101192
* Gets network id.
102193
*
103194
* @return the network id
104195
*/
105196
public BigInteger getNetworkId() {
106-
return networkId;
197+
return BigInteger.valueOf(networkId);
107198
}
108199

109200
/**
@@ -156,4 +247,28 @@ public Optional<String> getDeprecationDate() {
156247
public boolean hasNativeRequirements() {
157248
return nativeRequired;
158249
}
250+
251+
/**
252+
* Gets target gas limit.
253+
*
254+
* @return the target gas limit
255+
*/
256+
public long getTargetGasLimit() {
257+
return targetGasLimit;
258+
}
259+
260+
/**
261+
* From chain id.
262+
*
263+
* @param chainId the chain id
264+
* @return the optional
265+
*/
266+
public static Optional<NetworkDefinition> fromChainId(final BigInteger chainId) {
267+
for (final NetworkDefinition network : NetworkDefinition.values()) {
268+
if (network.getChainId().equals(chainId)) {
269+
return Optional.of(network);
270+
}
271+
}
272+
return Optional.empty();
273+
}
159274
}

consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinator.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator.ForkchoiceResult.Status.INVALID;
1919
import static org.hyperledger.besu.ethereum.trie.pathbased.common.provider.WorldStateQueryParams.withBlockHeaderAndUpdateNodeHead;
2020

21+
import org.hyperledger.besu.config.NetworkDefinition;
2122
import org.hyperledger.besu.consensus.merge.MergeContext;
2223
import org.hyperledger.besu.consensus.merge.PayloadWrapper;
2324
import org.hyperledger.besu.datatypes.Address;
@@ -83,21 +84,6 @@ public class MergeCoordinator implements MergeMiningCoordinator, BadChainListene
8384
*/
8485
private static final double TRY_FILL_BLOCK = 1.0;
8586

86-
// if you change these gas limits, also update the tests in MergeCoordinatorTest
87-
private static final long DEFAULT_TARGET_GAS_LIMIT = 60_000_000L;
88-
// testnets might have higher gas limits than mainnet and are incrementally updated
89-
private static final long DEFAULT_TARGET_GAS_LIMIT_TESTNET = 60_000_000L;
90-
// next target gas limit TBD
91-
// private static final long NEXT_STEP_TARGET_GAS_LIMIT_TESTNET = 60_000_000L;
92-
93-
private static final Map<BigInteger, Long> TESTNET_CHAIN_IDS =
94-
Map.of(
95-
BigInteger.valueOf(11155111), DEFAULT_TARGET_GAS_LIMIT_TESTNET, // Sepolia
96-
BigInteger.valueOf(17000), DEFAULT_TARGET_GAS_LIMIT_TESTNET, // Holesky
97-
BigInteger.valueOf(560048), DEFAULT_TARGET_GAS_LIMIT_TESTNET, // Hoodi
98-
BigInteger.valueOf(39438135), DEFAULT_TARGET_GAS_LIMIT_TESTNET // Ephemery
99-
);
100-
10187
/** The Mining parameters. */
10288
protected final MiningConfiguration miningConfiguration;
10389

@@ -186,7 +172,7 @@ public MergeCoordinator(
186172
miningParams.setCoinbase(Address.ZERO);
187173
}
188174
if (miningParams.getTargetGasLimit().isEmpty()) {
189-
miningParams.setTargetGasLimit(getDefaultGasLimit(protocolSchedule));
175+
getDefaultGasLimit(protocolSchedule).ifPresent(miningParams::setTargetGasLimit);
190176
}
191177
miningParams.setMinBlockOccupancyRatio(TRY_FILL_BLOCK);
192178
this.miningConfiguration = miningParams;
@@ -974,11 +960,11 @@ boolean isBlockCreationCancelled(final PayloadIdentifier payloadId) {
974960
return job.cancelled.get();
975961
}
976962

977-
private long getDefaultGasLimit(final ProtocolSchedule protocolSchedule) {
963+
private Optional<Long> getDefaultGasLimit(final ProtocolSchedule protocolSchedule) {
978964
return protocolSchedule
979965
.getChainId()
980-
.map(TESTNET_CHAIN_IDS::get)
981-
.orElse(DEFAULT_TARGET_GAS_LIMIT);
966+
.flatMap(NetworkDefinition::fromChainId)
967+
.map(NetworkDefinition::getTargetGasLimit);
982968
}
983969

984970
/**
@@ -987,8 +973,10 @@ private long getDefaultGasLimit(final ProtocolSchedule protocolSchedule) {
987973
* @param chainId the chain id
988974
* @return default gas limit by chain id
989975
*/
990-
public static long getDefaultGasLimitByChainId(final Optional<BigInteger> chainId) {
991-
return chainId.map(TESTNET_CHAIN_IDS::get).orElse(DEFAULT_TARGET_GAS_LIMIT);
976+
public static Optional<Long> getDefaultGasLimitByChainId(final Optional<BigInteger> chainId) {
977+
return chainId
978+
.flatMap(NetworkDefinition::fromChainId)
979+
.map(NetworkDefinition::getTargetGasLimit);
992980
}
993981

994982
private static class BlockCreationTask {

consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinatorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,14 +1018,14 @@ public void shouldSetCorrectTargetGasLimit(final ArgumentsAccessor argumentsAcce
10181018
@Test
10191019
public void shouldReturnExpectedTargetGasLimitForMainnet() {
10201020
final long targetGasLimitMainnet =
1021-
MergeCoordinator.getDefaultGasLimitByChainId(Optional.of(CHAIN_ID_MAINNET));
1021+
MergeCoordinator.getDefaultGasLimitByChainId(Optional.of(CHAIN_ID_MAINNET)).orElseThrow();
10221022
assertThat(targetGasLimitMainnet).isEqualTo(DEFAULT_TARGET_GAS_LIMIT);
10231023
}
10241024

10251025
@Test
10261026
public void shouldReturnExpectedTargetGasLimitForTestnet() {
10271027
final long targetGasLimitMainnet =
1028-
MergeCoordinator.getDefaultGasLimitByChainId(Optional.of(CHAIN_ID_HOODI));
1028+
MergeCoordinator.getDefaultGasLimitByChainId(Optional.of(CHAIN_ID_HOODI)).orElseThrow();
10291029
assertThat(targetGasLimitMainnet).isEqualTo(DEFAULT_TARGET_GAS_LIMIT_TESTNET);
10301030
}
10311031

0 commit comments

Comments
 (0)