Skip to content

Commit

Permalink
evmtool was not respecting the --genesis option (hyperledger#7518)
Browse files Browse the repository at this point in the history
* EVMTool should respect --genesis option

Update the code so that the genesis file option will be respected when
set. Also, default --fork options should set a rational base fee.

Signed-off-by: Danno Ferrin <danno@numisight.com>

---------

Signed-off-by: Danno Ferrin <danno@numisight.com>
  • Loading branch information
shemnon authored Aug 26, 2024
1 parent d87650b commit a851507
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## [Unreleased]

### Fixed
- **DebugMetrics**: Fixed a `ClassCastException` occurring in `DebugMetrics` when handling nested metric structures. Previously, `Double` values within these structures were incorrectly cast to `Map` objects, leading to errors. This update allows for proper handling of both direct values and nested structures at the same level. Issue# [#7383]
- **DebugMetrics**: Fixed a `ClassCastException` occurring in `DebugMetrics` when handling nested metric structures. Previously, `Double` values within these structures were incorrectly cast to `Map` objects, leading to errors. This update allows for proper handling of both direct values and nested structures at the same level. Issue# [#7383](https://github.com/hyperledger/besu/pull/7383)
- `evmtool` was not respecting the `--genesis` setting, resulting in unexpected trace results. [#7433](https://github.com/hyperledger/besu/pull/7433)

### Tests
- Added a comprehensive test case to reproduce the bug and verify the fix for the `ClassCastException` in `DebugMetrics`. This ensures that complex, dynamically nested metric structures can be handled without errors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
package org.hyperledger.besu.datatypes;

import java.util.Comparator;
import java.util.stream.Stream;

/** Description and metadata for a hard fork */
public interface HardforkId {

Expand Down Expand Up @@ -112,6 +115,19 @@ public boolean finalized() {
public String description() {
return description;
}

/**
* The most recent finalized mainnet hardfork Besu supports. This will change across versions
* and will be updated after mainnet activations.
*
* @return the most recently activated mainnet spec.
*/
public static MainnetHardforkId mostRecent() {
return Stream.of(MainnetHardforkId.values())
.filter(MainnetHardforkId::finalized)
.max(Comparator.naturalOrder())
.orElseThrow();
}
}

/** List of all Ethereum Classic hard forks. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,23 @@ public void writeTo(final RLPOutput out) {
out.writeBytes(extraData);
out.writeBytes(mixHashOrPrevRandao);
out.writeLong(nonce);
if (baseFee != null) {
do {
if (baseFee == null) break;
out.writeUInt256Scalar(baseFee);
}
if (withdrawalsRoot != null) {

if (withdrawalsRoot == null) break;
out.writeBytes(withdrawalsRoot);
}
if (excessBlobGas != null && blobGasUsed != null) {

if (excessBlobGas == null || blobGasUsed == null) break;
out.writeLongScalar(blobGasUsed);
out.writeUInt64Scalar(excessBlobGas);
}
if (parentBeaconBlockRoot != null) {

if (parentBeaconBlockRoot == null) break;
out.writeBytes(parentBeaconBlockRoot);
}
if (requestsRoot != null) {

if (requestsRoot == null) break;
out.writeBytes(requestsRoot);
}
} while (false);
out.endList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,14 @@ public void run() {
.mixHash(Hash.ZERO)
.nonce(0)
.blockHeaderFunctions(new MainnetBlockHeaderFunctions())
.baseFee(component.getBlockchain().getChainHeadHeader().getBaseFee().orElse(null))
.baseFee(
component
.getBlockchain()
.getChainHeadHeader()
.getBaseFee()
.or(() -> genesisFileModule.providesGenesisConfigFile().getBaseFeePerGas())
.orElse(
protocolSpec.getFeeMarket().implementsBaseFee() ? Wei.of(0xa) : null))
.buildBlockHeader();

Address contractAddress =
Expand Down Expand Up @@ -519,13 +526,12 @@ public void run() {
lastTime = stopwatch.elapsed().toNanos();
}
if (lastLoop) {
if (messageFrame.getExceptionalHaltReason().isPresent()) {
out.println(messageFrame.getExceptionalHaltReason().get());
}
if (messageFrame.getRevertReason().isPresent()) {
out.println(
new String(messageFrame.getRevertReason().get().toArrayUnsafe(), UTF_8));
}
messageFrame
.getExceptionalHaltReason()
.ifPresent(haltReason -> out.println(haltReason));
messageFrame
.getRevertReason()
.ifPresent(bytes -> out.println(new String(bytes.toArrayUnsafe(), UTF_8)));
}
}
}
Expand Down Expand Up @@ -572,7 +578,7 @@ public static void dumpWorldState(final MutableWorldState worldState, final Prin
out.println("{");
worldState
.streamAccounts(Bytes32.ZERO, Integer.MAX_VALUE)
.sorted(Comparator.comparing(o -> o.getAddress().get().toHexString()))
.sorted(Comparator.comparing(o -> o.getAddress().orElse(Address.ZERO).toHexString()))
.forEach(
a -> {
var account = worldState.get(a.getAddress().get());
Expand All @@ -585,7 +591,7 @@ public static void dumpWorldState(final MutableWorldState worldState, final Prin
.map(
e ->
Map.entry(
e.getKey().get(),
e.getKey().orElse(UInt256.ZERO),
account.getStorageValue(UInt256.fromBytes(e.getKey().get()))))
.filter(e -> !e.getValue().isZero())
.sorted(Map.Entry.comparingByKey())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.cli.config.NetworkName;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.datatypes.HardforkId.MainnetHardforkId;
import org.hyperledger.besu.ethereum.chain.GenesisState;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeaderFunctions;
Expand All @@ -28,6 +29,7 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Locale;
import java.util.Optional;
import javax.inject.Named;
import javax.inject.Singleton;
Expand Down Expand Up @@ -116,7 +118,7 @@ static GenesisFileModule createGenesisModule() {
final JsonObject config = new JsonObject();
genesis.put("config", config);
config.put("chainId", 1337);
config.put("londonBlock", 0);
config.put(MainnetHardforkId.mostRecent().toString().toLowerCase(Locale.ROOT) + "Time", 0);
genesis.put("baseFeePerGas", "0x3b9aca00");
genesis.put("gasLimit", "0x2540be400");
genesis.put("difficulty", "0x0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters;
import org.hyperledger.besu.evm.EvmSpecVersion;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;

Expand Down Expand Up @@ -71,13 +70,12 @@ ProtocolSchedule provideProtocolSchedule(
}
}

var schedules = createSchedules(configOptions.getChainId().orElse(BigInteger.valueOf(1337)));
var schedule =
schedules.get(
fork.orElse(EvmSpecVersion.defaultVersion().getName())
.toLowerCase(Locale.getDefault()));
if (schedule != null) {
return schedule.get();
if (fork.isPresent()) {
var schedules = createSchedules(configOptions.getChainId().orElse(BigInteger.valueOf(1337)));
var schedule = schedules.get(fork.get().toLowerCase(Locale.getDefault()));
if (schedule != null) {
return schedule.get();
}
}

return MainnetProtocolSchedule.fromConfig(
Expand Down

0 comments on commit a851507

Please sign in to comment.