diff --git a/CHANGELOG.md b/CHANGELOG.md index dd734d13..daf69628 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.1.4-test30 +Test pre-release 30 from branch `release-v0.1.4-testX` with code ported from [temp/issue-248/count-stack-only](https://github.com/Consensys/besu-sequencer-plugins/tree/temp/issue-248/count-stack-only) +* Improve ZkTracer initialization time [#11](https://github.com/Consensys/linea-sequencer/pull/11) (This PR was removed by error in 0.1.4-test29) +* Parallelize refundedGas for big transactions [linea-arithmetization#793](https://github.com/Consensys/linea-arithmetization/pull/793) + + ## 0.1.4-test29 Test pre-release 29 from branch `release-v0.1.4-testX` with code ported from [temp/issue-248/count-stack-only](https://github.com/Consensys/besu-sequencer-plugins/tree/temp/issue-248/count-stack-only) * Implement linea_setExtraData [#19](https://github.com/Consensys/linea-sequencer/pull/19) diff --git a/sequencer/src/main/java/net/consensys/linea/continoustracing/ContinuousTracingPlugin.java b/sequencer/src/main/java/net/consensys/linea/continoustracing/ContinuousTracingPlugin.java index e0b22dd5..8a76acf8 100644 --- a/sequencer/src/main/java/net/consensys/linea/continoustracing/ContinuousTracingPlugin.java +++ b/sequencer/src/main/java/net/consensys/linea/continoustracing/ContinuousTracingPlugin.java @@ -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; @@ -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()), diff --git a/sequencer/src/main/java/net/consensys/linea/rpc/capture/CaptureEndpointServicePlugin.java b/sequencer/src/main/java/net/consensys/linea/rpc/capture/CaptureEndpointServicePlugin.java index b7d7bb78..7c67d118 100644 --- a/sequencer/src/main/java/net/consensys/linea/rpc/capture/CaptureEndpointServicePlugin.java +++ b/sequencer/src/main/java/net/consensys/linea/rpc/capture/CaptureEndpointServicePlugin.java @@ -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; @@ -63,10 +62,4 @@ private void createAndRegister( rpcEndpointService.registerRPCEndpoint( method.getNamespace(), method.getName(), method::execute); } - - /** Start the RPC service. This method loads the OpCodes. */ - @Override - public void start() { - OpCodes.load(); - } } diff --git a/sequencer/src/main/java/net/consensys/linea/rpc/counters/CountersEndpointServicePlugin.java b/sequencer/src/main/java/net/consensys/linea/rpc/counters/CountersEndpointServicePlugin.java index 67ec2c93..d0af38d7 100644 --- a/sequencer/src/main/java/net/consensys/linea/rpc/counters/CountersEndpointServicePlugin.java +++ b/sequencer/src/main/java/net/consensys/linea/rpc/counters/CountersEndpointServicePlugin.java @@ -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; @@ -70,10 +69,4 @@ private void createAndRegister( rpcEndpointService.registerRPCEndpoint( method.getNamespace(), method.getName(), method::execute); } - - /** Start the RPC service. This method loads the OpCodes. */ - @Override - public void start() { - OpCodes.load(); - } } diff --git a/sequencer/src/main/java/net/consensys/linea/rpc/tracegeneration/TracesEndpointServicePlugin.java b/sequencer/src/main/java/net/consensys/linea/rpc/tracegeneration/TracesEndpointServicePlugin.java index 168640d0..34668154 100644 --- a/sequencer/src/main/java/net/consensys/linea/rpc/tracegeneration/TracesEndpointServicePlugin.java +++ b/sequencer/src/main/java/net/consensys/linea/rpc/tracegeneration/TracesEndpointServicePlugin.java @@ -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; @@ -72,10 +71,4 @@ private void createAndRegister( rpcEndpointService.registerRPCEndpoint( method.getNamespace(), method.getName(), method::execute); } - - /** Start the RPC service. This method loads the OpCodes. */ - @Override - public void start() { - OpCodes.load(); - } } diff --git a/sequencer/src/main/java/net/consensys/linea/zktracer/ZkTracer.java b/sequencer/src/main/java/net/consensys/linea/zktracer/ZkTracer.java index 30384718..1097dc8d 100644 --- a/sequencer/src/main/java/net/consensys/linea/zktracer/ZkTracer.java +++ b/sequencer/src/main/java/net/consensys/linea/zktracer/ZkTracer.java @@ -33,10 +33,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.Address; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.PendingTransaction; @@ -56,8 +54,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 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 spillings = new HashMap<>(); private Hash hashOfLastTransactionTraced = Hash.EMPTY; public ZkTracer() { @@ -66,25 +77,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); } } @@ -234,7 +231,7 @@ public Map getModulesLineCount() { modulesLineCount.put( m.moduleKey(), m.lineCount() - + Optional.ofNullable(this.spillings.get(m.moduleKey())) + + Optional.ofNullable(spillings.get(m.moduleKey())) .orElseThrow( () -> new IllegalStateException( diff --git a/sequencer/src/main/java/net/consensys/linea/zktracer/module/hub/TxTrace.java b/sequencer/src/main/java/net/consensys/linea/zktracer/module/hub/TxTrace.java index 8c5897d1..b9d7c722 100644 --- a/sequencer/src/main/java/net/consensys/linea/zktracer/module/hub/TxTrace.java +++ b/sequencer/src/main/java/net/consensys/linea/zktracer/module/hub/TxTrace.java @@ -33,6 +33,9 @@ public class TxTrace { /** A cache for the line count of this transaction */ private int cachedLineCount = 0; + private long refundedGas = -1; + private static final int PARALLELIZATION_THRESHOLD = 10_000; + public int size() { return this.trace.size(); } @@ -67,13 +70,22 @@ public void add(TraceSection section) { } public long refundedGas() { - long refundedGas = 0; - for (TraceSection section : this.trace) { - if (!section.hasReverted()) { - refundedGas += section.refundDelta(); + if (this.trace.size() >= PARALLELIZATION_THRESHOLD) { + return this.trace.parallelStream() + .filter(section -> !section.hasReverted()) + .mapToLong(TraceSection::refundDelta) + .sum(); + } else { + if (this.refundedGas == -1) { + this.refundedGas = 0; + for (TraceSection section : this.trace) { + if (!section.hasReverted()) { + this.refundedGas += section.refundDelta(); + } + } } + return this.refundedGas; } - return refundedGas; } /** diff --git a/sequencer/src/main/java/net/consensys/linea/zktracer/opcode/OpCodes.java b/sequencer/src/main/java/net/consensys/linea/zktracer/opcode/OpCodes.java index 2b293bfb..3ed4a836 100644 --- a/sequencer/src/main/java/net/consensys/linea/zktracer/opcode/OpCodes.java +++ b/sequencer/src/main/java/net/consensys/linea/zktracer/opcode/OpCodes.java @@ -35,9 +35,13 @@ public class OpCodes { private static Map valueToOpCodeDataMap; private static Map 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() diff --git a/sequencer/src/main/java/net/consensys/linea/zktracer/types/Utils.java b/sequencer/src/main/java/net/consensys/linea/zktracer/types/Utils.java index 54a18464..222c91c7 100644 --- a/sequencer/src/main/java/net/consensys/linea/zktracer/types/Utils.java +++ b/sequencer/src/main/java/net/consensys/linea/zktracer/types/Utils.java @@ -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 { @@ -83,4 +88,15 @@ public static BitDecOutput bitDecomposition(int input, int nbStep) { } return output; } + + public static Map computeSpillings() throws IOException { + final Map 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; + } }