Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ZkTracer initialization time #11

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
import net.consensys.linea.config.LineaL1L2BridgeConfiguration;
import net.consensys.linea.zktracer.module.Module;
import net.consensys.linea.zktracer.module.hub.Hub;
import net.consensys.linea.zktracer.opcode.OpCodes;
import net.consensys.linea.zktracer.types.Utils;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.toml.Toml;
import org.apache.tuweni.toml.TomlTable;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.PendingTransaction;
import org.hyperledger.besu.datatypes.Transaction;
Expand All @@ -54,8 +52,21 @@ public class ZkTracer implements ConflationAwareOperationTracer {
/** The {@link GasCalculator} used in this version of the arithmetization */
public static final GasCalculator gasCalculator = new LondonGasCalculator();

private static final Map<String, Integer> spillings;

static {
try {
// Load spillings configured in src/main/resources/spillings.toml.
spillings = Utils.computeSpillings();
} catch (final Exception e) {
final String errorMsg =
"A problem happened during spillings initialization, cause " + e.getCause();
log.error(errorMsg);
throw new RuntimeException(e);
ahamlat marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Getter private final Hub hub;
private final Map<String, Integer> spillings = new HashMap<>();
private Hash hashOfLastTransactionTraced = Hash.EMPTY;

public ZkTracer() {
Expand All @@ -64,25 +75,11 @@ public ZkTracer() {

public ZkTracer(final LineaL1L2BridgeConfiguration bridgeConfiguration) {
this.hub = new Hub(bridgeConfiguration.contract(), bridgeConfiguration.topic());

// Load opcodes configured in src/main/resources/opcodes.yml.
OpCodes.load();

// Load spillings configured in src/main/resources/spillings.toml.
try {
final TomlTable table =
Toml.parse(getClass().getClassLoader().getResourceAsStream("spillings.toml"))
.getTable("spillings");
table.toMap().keySet().forEach(k -> spillings.put(k, Math.toIntExact(table.getLong(k))));

for (Module m : this.hub.getModulesToCount()) {
if (!this.spillings.containsKey(m.moduleKey())) {
throw new IllegalStateException(
"Spilling for module " + m.moduleKey() + " not defined in spillings.toml");
}
for (Module m : this.hub.getModulesToCount()) {
if (!spillings.containsKey(m.moduleKey())) {
ahamlat marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalStateException(
"Spilling for module " + m.moduleKey() + " not defined in spillings.toml");
ahamlat marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -231,7 +228,7 @@ public Map<String, Integer> getModulesLineCount() {
modulesLineCount.put(
m.moduleKey(),
m.lineCount()
+ Optional.ofNullable(this.spillings.get(m.moduleKey()))
+ Optional.ofNullable(spillings.get(m.moduleKey()))
ahamlat marked this conversation as resolved.
Show resolved Hide resolved
.orElseThrow(
() ->
new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ public class OpCodes {
private static Map<Integer, OpCodeData> valueToOpCodeDataMap;
private static Map<OpCode, OpCodeData> opCodeToOpCodeDataMap;

/** Loads all opcode metadata from src/main/resources/opcodes.yml. */
static {
/** Loads all opcode metadata from src/main/resources/opcodes.yml. */
init();
}

@SneakyThrows(IOException.class)
public static void load() {
private static void init() {
JsonNode rootNode =
YAML_CONVERTER
.getObjectMapper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@

package net.consensys.linea.zktracer.types;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Preconditions;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.toml.Toml;
import org.apache.tuweni.toml.TomlTable;

public class Utils {

Expand Down Expand Up @@ -83,4 +88,15 @@ public static BitDecOutput bitDecomposition(int input, int nbStep) {
}
return output;
}

public static Map<String, Integer> computeSpillings() throws IOException {
final Map<String, Integer> spillings = new HashMap<>();

final TomlTable table =
Toml.parse(Utils.class.getClassLoader().getResourceAsStream("spillings.toml"))
.getTable("spillings");
table.toMap().keySet().forEach(k -> spillings.put(k, Math.toIntExact(table.getLong(k))));

return spillings;
}
}
Loading