Skip to content

Commit

Permalink
Improve ZkTracer initialization time (#11)
Browse files Browse the repository at this point in the history
Improve ZkTracer initialization time by doing only once Opcodes and spilling loading from disk resources

Signed-off-by: Ameziane H <[email protected]>
  • Loading branch information
ahamlat authored May 16, 2024
1 parent 6e52d3e commit 764914e
Show file tree
Hide file tree
Showing 29 changed files with 45 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.auto.service.AutoService;
import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.corset.CorsetValidator;
import net.consensys.linea.zktracer.opcode.OpCodes;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.BesuEvents;
Expand Down Expand Up @@ -97,8 +96,6 @@ public void start() {
System.exit(1);
}

OpCodes.load(); // must be loaded explicitly

besuEvents.addBlockAddedListener(
new ContinuousTracingBlockAddedListener(
new ContinuousTracer(traceService, new CorsetValidator()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.auto.service.AutoService;
import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.AbstractLineaRequiredPlugin;
import net.consensys.linea.zktracer.opcode.OpCodes;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.RpcEndpointService;
Expand Down Expand Up @@ -66,7 +65,5 @@ private void createAndRegister(

/** Start the RPC service. This method loads the OpCodes. */
@Override
public void start() {
OpCodes.load();
}
public void start() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.auto.service.AutoService;
import net.consensys.linea.AbstractLineaSharedOptionsPlugin;
import net.consensys.linea.zktracer.opcode.OpCodes;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.RpcEndpointService;
Expand Down Expand Up @@ -72,7 +71,5 @@ private void createAndRegister(

/** Start the RPC service. This method loads the OpCodes. */
@Override
public void start() {
OpCodes.load();
}
public void start() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.auto.service.AutoService;
import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.AbstractLineaSharedOptionsPlugin;
import net.consensys.linea.zktracer.opcode.OpCodes;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.RpcEndpointService;
Expand Down Expand Up @@ -74,7 +73,5 @@ private void createAndRegister(

/** Start the RPC service. This method loads the OpCodes. */
@Override
public void start() {
OpCodes.load();
}
public void start() {}
}
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);
}
}

@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())) {
throw new IllegalStateException(
"Spilling for module " + m.moduleKey() + " not defined in spillings.toml");
}
} 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()))
.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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
package net.consensys.linea.zktracer.module;

import net.consensys.linea.zktracer.testing.BytecodeRunner;
import net.consensys.linea.zktracer.testing.EvmExtension;
import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(EvmExtension.class)
public class EcDataTest {
@Test
void testEcData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.BytecodeRunner;
import net.consensys.linea.zktracer.testing.EvmExtension;
import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(EvmExtension.class)
public class addTest {
@Test
void testSmallZeroAdd() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.BytecodeRunner;
import net.consensys.linea.zktracer.testing.EvmExtension;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(EvmExtension.class)
public class BinTest {
@Test
public void edgeCase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.BytecodeRunner;
import net.consensys.linea.zktracer.testing.EvmExtension;
import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(EvmExtension.class)
public class TestDuplicatedOperations {
@Test
void testDuplicate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.EvmExtension;
import net.consensys.linea.zktracer.testing.ToyAccount;
import net.consensys.linea.zktracer.testing.ToyExecutionEnvironment;
import net.consensys.linea.zktracer.testing.ToyTransaction;
Expand All @@ -34,10 +33,8 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/** Ensure that calling a contract with empty code does not generate a virtual STOP trace */
@ExtendWith(EvmExtension.class)
public class CallEmptyNoStopTest {
@Test
void test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.BytecodeRunner;
import net.consensys.linea.zktracer.testing.EvmExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(EvmExtension.class)
public class OtherTests {
@Test
public void testMul() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.BytecodeRunner;
import net.consensys.linea.zktracer.testing.EvmExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(EvmExtension.class)
public class TestTwoPlusTwo {
@Test
void testAdd() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.BytecodeRunner;
import net.consensys.linea.zktracer.testing.EvmExtension;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(EvmExtension.class)
public class ModTest {
@Test
void testSignedSmod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import net.consensys.linea.zktracer.opcode.gas.MxpType;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.BytecodeRunner;
import net.consensys.linea.zktracer.testing.EvmExtension;
import net.consensys.linea.zktracer.testing.ToyAccount;
import net.consensys.linea.zktracer.testing.ToyExecutionEnvironment;
import net.consensys.linea.zktracer.testing.ToyTransaction;
Expand All @@ -42,11 +41,9 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

// https://github.com/Consensys/linea-besu-plugin/issues/197

@ExtendWith(EvmExtension.class)
public class MxpTest {
private static final Random RAND = new Random(123456789123456L);
public static final EWord TWO_POW_128 = EWord.of(EWord.ONE.shiftLeft(128));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@
*/
package net.consensys.linea.zktracer.module.precompiles;

import net.consensys.linea.zktracer.testing.EvmExtension;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(EvmExtension.class)
public class precTests {}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Random;

import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.opcode.OpCodes;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.ToyAccount;
import net.consensys.linea.zktracer.testing.ToyExecutionEnvironment;
Expand All @@ -48,8 +47,6 @@ public class TestRlpAddress {

@Test
void test() {
OpCodes.load();

ToyWorld.ToyWorldBuilder world = ToyWorld.builder();
List<Transaction> txList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Random;

import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.opcode.OpCodes;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.ToyAccount;
import net.consensys.linea.zktracer.testing.ToyExecutionEnvironment;
Expand All @@ -48,7 +47,6 @@ class TestRandomTxns {

@Test
void test() {
OpCodes.load();
ToyWorld.ToyWorldBuilder world = ToyWorld.builder();
List<Transaction> txList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.testing.BytecodeCompiler;
import net.consensys.linea.zktracer.testing.BytecodeRunner;
import net.consensys.linea.zktracer.testing.EvmExtension;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

@Slf4j
@ExtendWith(EvmExtension.class)
class ShfRtTracerTest {
private static final Random RAND = new Random();
private static final int TEST_REPETITIONS = 4;
Expand Down
Loading

0 comments on commit 764914e

Please sign in to comment.