Skip to content

Commit edf38e8

Browse files
committed
Fixes for pre-timestamp and pre-blob forks
Signed-off-by: Simon Dudley <simon.dudley@consensys.net>
1 parent 43714ec commit edf38e8

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

  • ethereum

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthConfig.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods;
1616

1717
import org.hyperledger.besu.config.GenesisConfigOptions;
18+
import org.hyperledger.besu.datatypes.HardforkId;
1819
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
1920
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
2021
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
@@ -51,6 +52,8 @@ public class EthConfig implements JsonRpcMethod {
5152
private final BlockchainQueries blockchain;
5253
private final ProtocolSchedule protocolSchedule;
5354
private final ForkIdManager forkIdManager;
55+
private final Long firstTimestampMilestone;
56+
private final Long firstBlobsMilestone;
5457

5558
public EthConfig(
5659
final BlockchainQueries blockchain,
@@ -64,6 +67,10 @@ public EthConfig(
6467
blockchain.getBlockchain(),
6568
genesisConfigOptions.getForkBlockNumbers(),
6669
genesisConfigOptions.getForkBlockTimestamps());
70+
firstTimestampMilestone =
71+
protocolSchedule.milestoneFor(HardforkId.MainnetHardforkId.SHANGHAI).orElse(0L);
72+
firstBlobsMilestone =
73+
protocolSchedule.milestoneFor(HardforkId.MainnetHardforkId.CANCUN).orElse(0L);
6774
}
6875

6976
@Override
@@ -120,8 +127,12 @@ private boolean showAllForks(final JsonRpcRequestContext requestContext) {
120127
}
121128
}
122129

123-
private String getForkIdAsHexString(final long currentTime) {
124-
return forkIdManager.getForkIdByTimestamp(currentTime).getHash().toHexString();
130+
private String getForkIdAsHexString(final long milestone) {
131+
if (milestone >= firstTimestampMilestone) {
132+
return forkIdManager.getForkIdByTimestamp(milestone).getHash().toHexString();
133+
} else {
134+
return forkIdManager.getForkIdByBlockNumber(milestone).getHash().toHexString();
135+
}
125136
}
126137

127138
void generateConfig(final ObjectNode result, final ScheduledProtocolSpec scheduledSpec) {
@@ -133,13 +144,19 @@ void generateConfig(final ObjectNode result, final ProtocolSpec spec) {
133144
}
134145

135146
void generateConfig(final ObjectNode result, final Hardfork forkId, final ProtocolSpec spec) {
136-
result.put("activationTime", forkId.milestone());
147+
if (forkId.milestone() < firstTimestampMilestone) {
148+
result.put("activationBlock", forkId.milestone());
149+
} else {
150+
result.put("activationTime", forkId.milestone());
151+
}
137152

138-
ObjectNode blobs = result.putObject("blobSchedule");
139-
blobs.put(
140-
"baseFeeUpdateFraction", spec.getFeeMarket().getBaseFeeUpdateFraction().longValueExact());
141-
blobs.put("max", spec.getGasLimitCalculator().currentBlobGasLimit() / (128 * 1024));
142-
blobs.put("target", spec.getGasLimitCalculator().getTargetBlobGasPerBlock() / (128 * 1024));
153+
if (forkId.milestone() >= firstBlobsMilestone) {
154+
ObjectNode blobs = result.putObject("blobSchedule");
155+
blobs.put(
156+
"baseFeeUpdateFraction", spec.getFeeMarket().getBaseFeeUpdateFraction().longValueExact());
157+
blobs.put("max", spec.getGasLimitCalculator().currentBlobGasLimit() / (128 * 1024));
158+
blobs.put("target", spec.getGasLimitCalculator().getTargetBlobGasPerBlock() / (128 * 1024));
159+
}
143160

144161
result.put(
145162
"chainId", protocolSchedule.getChainId().map(c -> "0x" + c.toString(16)).orElse(null));

ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthConfigTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ void ethConfigForMainnet() {
6565
for (JsonNode forkObj : result.get("all")) {
6666
assertThat(forkObj.get("chainId").asText()).isEqualTo("0x" + BigInteger.ONE.toString(16));
6767
}
68+
System.out.println(result);
6869
}
6970
}

ethereum/core/src/main/java/org/hyperledger/besu/ethereum/forkid/ForkIdManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,13 @@ public ForkId getForkIdByTimestamp(final long timestamp) {
240240
}
241241
return allForkIds.isEmpty() ? new ForkId(genesisHashCrc, 0) : allForkIds.getLast();
242242
}
243+
244+
public ForkId getForkIdByBlockNumber(final long blockNumber) {
245+
for (final ForkId forkId : blockNumbersForkIds) {
246+
if (blockNumber < forkId.getNext()) {
247+
return forkId;
248+
}
249+
}
250+
return allForkIds.isEmpty() ? new ForkId(genesisHashCrc, 0) : allForkIds.getLast();
251+
}
243252
}

0 commit comments

Comments
 (0)