Skip to content

Commit

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

Signed-off-by: Ameziane H <ameziane.hamlat@consensys.net>
  • Loading branch information
ahamlat committed May 20, 2024
1 parent c21c4f3 commit 551c2a3
Show file tree
Hide file tree
Showing 27 changed files with 40 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@
import net.consensys.linea.zktracer.module.Module;
import net.consensys.linea.zktracer.module.Pin55;
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 @@ -62,10 +60,22 @@ public class ZkTracer implements ConflationAwareOperationTracer {
public static final GasCalculator gasCalculator = new LondonGasCalculator();

public static final FeeMarket feeMarket = FeeMarket.london(-1);
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 Optional<Pin55> pin55;
private final Map<String, Integer> spillings = new HashMap<>();
private Hash hashOfLastTransactionTraced = Hash.EMPTY;
/** Accumulate all the exceptions that happened at tracing time. */
@Getter private final List<Exception> tracingExceptions = new FiniteList<>(50);
Expand All @@ -76,27 +86,12 @@ 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);
}

// >>>> CHANGE ME >>>>
// >>>> CHANGE ME >>>>
// >>>> CHANGE ME >>>>
Expand Down Expand Up @@ -326,7 +321,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;

static {
init();
}

/** Loads all opcode metadata from src/main/resources/opcodes.yml. */
@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 BlockhashTest {

// @Disabled("in our framework, BLOCKNUMBER is 0 thus breaking the wcp_lower_bound lookup")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@
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.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@Slf4j
@ExtendWith(EvmExtension.class)
public class ExpTest {
// Generates 128, 64, 2, 1 as LD
private static final int[] LD_INDICES = new int[] {1, 2, 7, 8};
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 @@ -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)
class MemoryTests {
private final Random rnd = new Random(666);

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 PrecompileTests {}
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
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.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,17 +40,14 @@
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;

@ExtendWith(EvmExtension.class)
public class StpTest {
private static final Random RAND = new Random(666L);
final int NB_CALL = 200;
final int NB_CREATE = 200;

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

Expand All @@ -78,7 +73,6 @@ void testCall() {

@Test
void testCreate() {
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 @@ -18,13 +18,10 @@
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.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(EvmExtension.class)
public class TrmTracerTest {
private final Bytes32 RANDOM_STRING_FROM_THE_INTERNET =
Bytes32.fromHexString(
Expand Down
Loading

0 comments on commit 551c2a3

Please sign in to comment.