Skip to content

Commit

Permalink
EIP-3155 Last Call Nitpicks (#7455)
Browse files Browse the repository at this point in the history
A lot of small nitpicks for standard tracing conformance
* Change evmtool run defaults to mirror go-ethereum's choices
* Add fields to run summary
* Make EOF PC zero to section
* Correct EXT*CALL min gas
* fix section depth 

Signed-off-by: Danno Ferrin <danno@numisight.com>
  • Loading branch information
shemnon authored Aug 14, 2024
1 parent dc336f4 commit 94f7c7d
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.google.common.base.Suppliers;
import org.apache.tuweni.bytes.Bytes;
import org.web3j.utils.Strings;
import picocli.CommandLine;
Expand All @@ -63,7 +65,7 @@ public class CodeValidateSubCommand implements Runnable {

@ParentCommand EvmToolCommand parentCommand;

private final EVM evm;
private final Supplier<EVM> evm;

@CommandLine.Option(
names = {"--file"},
Expand All @@ -83,12 +85,18 @@ public CodeValidateSubCommand() {

CodeValidateSubCommand(final EvmToolCommand parentCommand) {
this.parentCommand = parentCommand;
String fork = EvmSpecVersion.PRAGUE.getName();
if (parentCommand != null && parentCommand.hasFork()) {
fork = parentCommand.getFork();
}
ProtocolSpec protocolSpec = ReferenceTestProtocolSchedules.create().geSpecByName(fork);
evm = protocolSpec.getEvm();
String fork =
parentCommand != null && parentCommand.hasFork()
? parentCommand.getFork()
: EvmSpecVersion.PRAGUE.getName();

evm =
Suppliers.memoize(
() -> {
ProtocolSpec protocolSpec =
ReferenceTestProtocolSchedules.create().geSpecByName(fork);
return protocolSpec.getEvm();
});
}

@Override
Expand Down Expand Up @@ -155,12 +163,12 @@ public String considerCode(final String hexCode) {
return "";
}

EOFLayout layout = evm.parseEOF(codeBytes);
EOFLayout layout = evm.get().parseEOF(codeBytes);
if (!layout.isValid()) {
return "err: layout - " + layout.invalidReason();
}

Code code = evm.getCodeUncached(codeBytes);
Code code = evm.get().getCodeUncached(codeBytes);
if (code instanceof CodeInvalid codeInvalid) {
return "err: " + codeInvalid.getInvalidReason();
} else if (EOFContainerMode.INITCODE.equals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -152,13 +151,13 @@ void setBytes(final String optionValue) {
names = {"--sender"},
paramLabel = "<address>",
description = "Calling address for this invocation.")
private final Address sender = Address.ZERO;
private final Address sender = Address.fromHexString("0x73656e646572");

@Option(
names = {"--receiver"},
paramLabel = "<address>",
description = "Receiving address for this invocation.")
private final Address receiver = Address.ZERO;
private final Address receiver = Address.fromHexString("0x7265636569766572");

@Option(
names = {"--create"},
Expand All @@ -169,7 +168,7 @@ void setBytes(final String optionValue) {
names = {"--contract"},
paramLabel = "<address>",
description = "The address holding the contract code.")
private final Address contract = Address.ZERO;
private final Address contract = Address.fromHexString("0x7265636569766572");

@Option(
names = {"--coinbase"},
Expand Down Expand Up @@ -379,7 +378,7 @@ public void run() {
} else if (genesisFile != null) {
genesisFileModule = GenesisFileModule.createGenesisModule(genesisFile);
} else {
genesisFileModule = GenesisFileModule.createGenesisModule(NetworkName.DEV);
genesisFileModule = GenesisFileModule.createGenesisModule();
}
final EvmToolComponent component =
DaggerEvmToolComponent.builder()
Expand Down Expand Up @@ -463,18 +462,20 @@ public void run() {
BlockHeaderBuilder.create()
.parentHash(Hash.EMPTY)
.coinbase(coinbase)
.difficulty(Difficulty.ONE)
.number(1)
.gasLimit(5000)
.timestamp(Instant.now().toEpochMilli())
.difficulty(
Difficulty.fromHexString(
genesisFileModule.providesGenesisConfigFile().getDifficulty()))
.number(0)
.gasLimit(genesisFileModule.providesGenesisConfigFile().getGasLimit())
.timestamp(0)
.ommersHash(Hash.EMPTY_LIST_HASH)
.stateRoot(Hash.EMPTY_TRIE_HASH)
.transactionsRoot(Hash.EMPTY)
.receiptsRoot(Hash.EMPTY)
.logsBloom(LogsBloomFilter.empty())
.gasUsed(0)
.extraData(Bytes.EMPTY)
.mixHash(Hash.EMPTY)
.mixHash(Hash.ZERO)
.nonce(0)
.blockHeaderFunctions(new MainnetBlockHeaderFunctions())
.baseFee(component.getBlockchain().getChainHeadHeader().getBaseFee().orElse(null))
Expand Down Expand Up @@ -527,28 +528,30 @@ public void run() {
}
}
}

if (lastLoop && messageFrameStack.isEmpty()) {
final long evmGas = txGas - messageFrame.getRemainingGas();
final JsonObject resultLine = new JsonObject();
resultLine.put("gasUser", "0x" + Long.toHexString(evmGas));
if (!noTime) {
resultLine.put("timens", lastTime).put("time", lastTime / 1000);
}
resultLine
.put("gasTotal", "0x" + Long.toHexString(evmGas))
.put("output", messageFrame.getOutputData().toHexString());
out.println();
out.println(resultLine);
}
}
lastTime = stopwatch.elapsed().toNanos();
stopwatch.reset();
if (showJsonAlloc && lastLoop) {
if (lastLoop) {
initialMessageFrame.getSelfDestructs().forEach(updater::deleteAccount);
updater.clearAccountsThatAreEmpty();
updater.commit();
MutableWorldState worldState = component.getWorldState();
dumpWorldState(worldState, out);
final long evmGas = txGas - initialMessageFrame.getRemainingGas();
final JsonObject resultLine = new JsonObject();
resultLine
.put("stateRoot", worldState.rootHash().toHexString())
.put("output", initialMessageFrame.getOutputData().toHexString())
.put("gasUsed", "0x" + Long.toHexString(evmGas))
.put("pass", initialMessageFrame.getExceptionalHaltReason().isEmpty())
.put("fork", protocolSpec.getName());
if (!noTime) {
resultLine.put("timens", lastTime).put("time", lastTime / 1000);
}
out.println(resultLine);

if (showJsonAlloc) {
dumpWorldState(worldState, out);
}
}
} while (remainingIters-- > 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,7 @@ EvmConfiguration provideEvmConfiguration() {
}

/** Default constructor for the EvmToolCommandOptionsModule class. */
public EvmToolCommandOptionsModule() {}
public EvmToolCommandOptionsModule() {
// This is only here because of JavaDoc linting
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ static GenesisFileModule createGenesisModule(final File genesisFile) throws IOEx
return createGenesisModule(Files.readString(genesisFile.toPath(), Charset.defaultCharset()));
}

static GenesisFileModule createGenesisModule() {
final JsonObject genesis = new JsonObject();
final JsonObject config = new JsonObject();
genesis.put("config", config);
config.put("chainId", 1337);
config.put("londonBlock", 0);
genesis.put("baseFeePerGas", "0x3b9aca00");
genesis.put("gasLimit", "0x2540be400");
genesis.put("difficulty", "0x0");
genesis.put("mixHash", "0x0000000000000000000000000000000000000000000000000000000000000000");
genesis.put("coinbase", "0x0000000000000000000000000000000000000000");
return createGenesisModule(genesis.toString());
}

private static GenesisFileModule createGenesisModule(final String genesisConfig) {
final JsonObject genesis = new JsonObject(genesisConfig);
final JsonObject config = genesis.getJsonObject("config");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ProtocolSchedule provideProtocolSchedule(
}
}

var schedules = createSchedules();
var schedules = createSchedules(configOptions.getChainId().orElse(BigInteger.valueOf(1337)));
var schedule =
schedules.get(
fork.orElse(EvmSpecVersion.defaultVersion().getName())
Expand All @@ -89,9 +89,9 @@ ProtocolSchedule provideProtocolSchedule(
new NoOpMetricsSystem());
}

public static Map<String, Supplier<ProtocolSchedule>> createSchedules() {
public static Map<String, Supplier<ProtocolSchedule>> createSchedules(final BigInteger chainId) {
return Map.ofEntries(
Map.entry("frontier", createSchedule(new StubGenesisConfigOptions())),
Map.entry("frontier", createSchedule(new StubGenesisConfigOptions().chainId(chainId))),
Map.entry("homestead", createSchedule(new StubGenesisConfigOptions().homesteadBlock(0))),
Map.entry("eip150", createSchedule(new StubGenesisConfigOptions().eip150Block(0))),
Map.entry("eip158", createSchedule(new StubGenesisConfigOptions().eip158Block(0))),
Expand All @@ -102,43 +102,86 @@ public static Map<String, Supplier<ProtocolSchedule>> createSchedules() {
Map.entry(
"constantinoplefix", createSchedule(new StubGenesisConfigOptions().petersburgBlock(0))),
Map.entry("petersburg", createSchedule(new StubGenesisConfigOptions().petersburgBlock(0))),
Map.entry("istanbul", createSchedule(new StubGenesisConfigOptions().istanbulBlock(0))),
Map.entry(
"muirglacier", createSchedule(new StubGenesisConfigOptions().muirGlacierBlock(0))),
Map.entry("berlin", createSchedule(new StubGenesisConfigOptions().berlinBlock(0))),
"istanbul",
createSchedule(new StubGenesisConfigOptions().istanbulBlock(0).chainId(chainId))),
Map.entry(
"muirglacier",
createSchedule(new StubGenesisConfigOptions().muirGlacierBlock(0).chainId(chainId))),
Map.entry(
"berlin",
createSchedule(new StubGenesisConfigOptions().berlinBlock(0).chainId(chainId))),
Map.entry(
"london",
createSchedule(new StubGenesisConfigOptions().londonBlock(0).baseFeePerGas(0x0a))),
createSchedule(
new StubGenesisConfigOptions()
.londonBlock(0)
.baseFeePerGas(0x0a)
.chainId(chainId))),
Map.entry(
"arrowglacier", createSchedule(new StubGenesisConfigOptions().arrowGlacierBlock(0))),
"arrowglacier",
createSchedule(
new StubGenesisConfigOptions()
.arrowGlacierBlock(0)
.baseFeePerGas(0x0a)
.chainId(chainId))),
Map.entry(
"grayglacier", createSchedule(new StubGenesisConfigOptions().grayGlacierBlock(0))),
"grayglacier",
createSchedule(
new StubGenesisConfigOptions()
.grayGlacierBlock(0)
.baseFeePerGas(0x0a)
.chainId(chainId))),
Map.entry(
"merge",
createSchedule(
new StubGenesisConfigOptions().mergeNetSplitBlock(0).baseFeePerGas(0x0a))),
new StubGenesisConfigOptions()
.mergeNetSplitBlock(0)
.baseFeePerGas(0x0a)
.chainId(chainId))),
Map.entry(
"shanghai",
createSchedule(new StubGenesisConfigOptions().shanghaiTime(0).baseFeePerGas(0x0a))),
createSchedule(
new StubGenesisConfigOptions()
.shanghaiTime(0)
.baseFeePerGas(0x0a)
.chainId(chainId))),
Map.entry(
"cancun",
createSchedule(new StubGenesisConfigOptions().cancunTime(0).baseFeePerGas(0x0a))),
createSchedule(
new StubGenesisConfigOptions().cancunTime(0).baseFeePerGas(0x0a).chainId(chainId))),
Map.entry(
"cancuneof",
createSchedule(new StubGenesisConfigOptions().cancunEOFTime(0).baseFeePerGas(0x0a))),
createSchedule(
new StubGenesisConfigOptions()
.cancunEOFTime(0)
.baseFeePerGas(0x0a)
.chainId(chainId))),
Map.entry(
"prague",
createSchedule(new StubGenesisConfigOptions().pragueTime(0).baseFeePerGas(0x0a))),
createSchedule(
new StubGenesisConfigOptions().pragueTime(0).baseFeePerGas(0x0a).chainId(chainId))),
Map.entry(
"pragueeof",
createSchedule(new StubGenesisConfigOptions().pragueEOFTime(0).baseFeePerGas(0x0a))),
createSchedule(
new StubGenesisConfigOptions()
.pragueEOFTime(0)
.baseFeePerGas(0x0a)
.chainId(chainId))),
Map.entry(
"futureeips",
createSchedule(new StubGenesisConfigOptions().futureEipsTime(0).baseFeePerGas(0x0a))),
createSchedule(
new StubGenesisConfigOptions()
.futureEipsTime(0)
.baseFeePerGas(0x0a)
.chainId(chainId))),
Map.entry(
"experimentaleips",
createSchedule(
new StubGenesisConfigOptions().experimentalEipsTime(0).baseFeePerGas(0x0a))));
new StubGenesisConfigOptions()
.experimentalEipsTime(0)
.baseFeePerGas(0x0a)
.chainId(chainId))));
}

private static Supplier<ProtocolSchedule> createSchedule(final GenesisConfigOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"stdout": [
{"pc":0,"op":96,"gas":"0x2540be400","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"},
{"pc":2,"op":239,"gas":"0x2540be3fd","gasCost":"0x0","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"INVALID","error":"Bad instruction"},
{"gasUser":"0x2540be400","gasTotal":"0x2540be400","output":"0x"}
{"stateRoot":"0xfc9dc1be50c1b0a497afa545d770cc7064f0d71efbc4338f002dc2e086965d98","output":"0x","gasUsed":"0x2540be400","pass":false,"fork":"Cancun"}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@
{"pc":24,"op":243,"gas":"0x45","gasCost":"0x0","memSize":96,"stack":["0x1","0x40"],"depth":2,"refund":0,"opName":"RETURN"},
{"pc":22,"op":96,"gas":"0x71","gasCost":"0x3","memSize":96,"stack":["0x1"],"depth":1,"refund":0,"opName":"PUSH1"},
{"pc":24,"op":243,"gas":"0x6e","gasCost":"0x0","memSize":96,"stack":["0x1","0x40"],"depth":1,"refund":0,"opName":"RETURN"},
{"gasUser":"0x619e","gasTotal":"0x619e","output":"0x40"}
{"stateRoot":"0xcb5e8e232189003640b6f131ea2c09b1791ffd2e8357f64610f638e9a11ab2d2","output":"0x40","gasUsed":"0x619e","pass":true,"fork":"Cancun"}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"--coinbase",
"4444588443C3A91288C5002483449ABA1054192B",
"--fork",
"paris"
"london"
],
"stdin": "",
"stdout": [
{"pc":0,"op":65,"gas":"0x2540be400","gasCost":"0x2","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"COINBASE"},
{"pc":1,"op":49,"gas":"0x2540be3fe","gasCost":"0xa28","memSize":0,"stack":["0x4444588443c3a91288c5002483449aba1054192b"],"depth":1,"refund":0,"opName":"BALANCE"},
{"pc":2,"op":255,"gas":"0x2540bd9d6","gasCost":"0x1388","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"SELFDESTRUCT"},
{"gasUser":"0x1db2","gasTotal":"0x1db2","output":"0x"}
{"pc":2,"op":255,"gas":"0x2540bd9d6","gasCost":"0x1db0","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"SELFDESTRUCT"},
{"stateRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","output":"0x","gasUsed":"0x27da","pass":true,"fork":"London"}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"stdout": [
{"pc":0,"op":65,"gas":"0x2540be400","gasCost":"0x2","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"COINBASE"},
{"pc":1,"op":49,"gas":"0x2540be3fe","gasCost":"0x64","memSize":0,"stack":["0x4444588443c3a91288c5002483449aba1054192b"],"depth":1,"refund":0,"opName":"BALANCE"},
{"pc":2,"op":255,"gas":"0x2540be39a","gasCost":"0x1388","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"SELFDESTRUCT"},
{"gasUser":"0x13ee","gasTotal":"0x13ee","output":"0x"}
{"pc":2,"op":255,"gas":"0x2540be39a","gasCost":"0x1db0","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"SELFDESTRUCT"},
{"stateRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","output":"0x","gasUsed":"0x1e16","pass":true,"fork":"Shanghai"}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
{"pc":5,"section":0,"op":95,"gas":"0x2540be109","gasCost":"0x2","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH0"},
{"pc":6,"section":0,"op":95,"gas":"0x2540be107","gasCost":"0x2","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"PUSH0"},
{"pc":7,"section":0,"op":238,"immediate":"0x00","gas":"0x2540be105","gasCost":"0x0","memSize":0,"stack":["0x0","0x0"],"depth":1,"refund":0,"opName":"RETURNCONTRACT"},
{"gasUser":"0x129b","gasTotal":"0x129b","output":"0x"}
{"stateRoot":"0x9790b070a5749acec6a7252a867f795df3c2cb5b800fb509ea259a1c0b5d96c1","output":"0x","gasUsed":"0x129b","pass":true,"fork":"CancunEOF"}
]
}
Loading

0 comments on commit 94f7c7d

Please sign in to comment.