Skip to content

Commit

Permalink
[4844] [Hive] Fix for genesis mismatch (#5677)
Browse files Browse the repository at this point in the history
* fix for genesis mismatch

Signed-off-by: spencer-tb <spencer@spencertaylorbrown.uk>

* Fix tests and spotless

Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>

---------

Signed-off-by: spencer-tb <spencer@spencertaylorbrown.uk>
Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
Co-authored-by: spencer-tb <spencer@spencertaylorbrown.uk>
  • Loading branch information
2 people authored and jflo committed Jul 19, 2023
1 parent fef3660 commit decc6d5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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()) {
Expand Down

0 comments on commit decc6d5

Please sign in to comment.