From decc6d53f97416f493e7ad13727038a6da181ffc Mon Sep 17 00:00:00 2001 From: Gabriel-Trintinalia Date: Thu, 6 Jul 2023 10:46:40 +1000 Subject: [PATCH] [4844] [Hive] Fix for genesis mismatch (#5677) * fix for genesis mismatch Signed-off-by: spencer-tb * Fix tests and spotless Signed-off-by: Gabriel-Trintinalia --------- Signed-off-by: spencer-tb Signed-off-by: Gabriel-Trintinalia Co-authored-by: spencer-tb --- .../besu/config/GenesisConfigFile.java | 18 +++++++++++ .../besu/ethereum/chain/GenesisState.java | 31 ++++++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java index 73d7174303e..9b706ab1a38 100644 --- a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java +++ b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java @@ -247,6 +247,24 @@ public String getNonce() { return JsonUtil.getValueAsString(configRoot, "nonce", "0x0"); } + /** + * Gets excess data gas. + * + * @return the excess data gas + */ + public String getExcessDataGas() { + return JsonUtil.getValueAsString(configRoot, "excessdatagas", "0x0"); + } + + /** + * Gets data gas used. + * + * @return the data gas used + */ + public String getDataGasUsed() { + return JsonUtil.getValueAsString(configRoot, "datagasused", "0x0"); + } + /** * Gets coinbase. * diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/GenesisState.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/GenesisState.java index 691a8ab9ee9..09012c98159 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/GenesisState.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/GenesisState.java @@ -19,6 +19,7 @@ import org.hyperledger.besu.config.GenesisAllocation; import org.hyperledger.besu.config.GenesisConfigFile; import org.hyperledger.besu.datatypes.Address; +import org.hyperledger.besu.datatypes.DataGas; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.Wei; import org.hyperledger.besu.ethereum.core.Block; @@ -167,6 +168,8 @@ private static BlockHeader buildHeader( .blockHeaderFunctions(ScheduleBasedBlockHeaderFunctions.create(protocolSchedule)) .baseFee(genesis.getGenesisBaseFeePerGas().orElse(null)) .withdrawalsRoot(isShanghaiAtGenesis(genesis) ? Hash.EMPTY_TRIE_HASH : null) + .dataGasUsed(isCancunAtGenesis(genesis) ? parseDataGasUsed(genesis) : null) + .excessDataGas(isCancunAtGenesis(genesis) ? parseExcessDataGas(genesis) : null) .depositsRoot(isExperimentalEipsTimeAtGenesis(genesis) ? Hash.EMPTY_TRIE_HASH : null) .buildBlockHeader(); } @@ -217,12 +220,24 @@ private static long parseNonce(final GenesisConfigFile genesis) { return withNiceErrorMessage("nonce", genesis.getNonce(), GenesisState::parseUnsignedLong); } + private static long parseDataGasUsed(final GenesisConfigFile genesis) { + return withNiceErrorMessage( + "dataGasUsed", genesis.getDataGasUsed(), GenesisState::parseUnsignedLong); + } + + private static DataGas parseExcessDataGas(final GenesisConfigFile genesis) { + long excessDataGas = + withNiceErrorMessage( + "excessDataGas", genesis.getExcessDataGas(), GenesisState::parseUnsignedLong); + return DataGas.of(excessDataGas); + } + private static long parseUnsignedLong(final String value) { - String nonce = value.toLowerCase(Locale.US); - if (nonce.startsWith("0x")) { - nonce = nonce.substring(2); + String v = value.toLowerCase(Locale.US); + if (v.startsWith("0x")) { + v = v.substring(2); } - return Long.parseUnsignedLong(nonce, 16); + return Long.parseUnsignedLong(v, 16); } private static boolean isShanghaiAtGenesis(final GenesisConfigFile genesis) { @@ -233,6 +248,14 @@ private static boolean isShanghaiAtGenesis(final GenesisConfigFile genesis) { return false; } + private static boolean isCancunAtGenesis(final GenesisConfigFile genesis) { + final OptionalLong cancunTimestamp = genesis.getConfigOptions().getCancunTime(); + if (cancunTimestamp.isPresent()) { + return cancunTimestamp.getAsLong() == genesis.getTimestamp(); + } + return false; + } + private static boolean isExperimentalEipsTimeAtGenesis(final GenesisConfigFile genesis) { final OptionalLong experimentalEipsTime = genesis.getConfigOptions().getExperimentalEipsTime(); if (experimentalEipsTime.isPresent()) {