Skip to content

Commit

Permalink
feat: implement RPC for line counting (#451)
Browse files Browse the repository at this point in the history
Signed-off-by: [email protected]
  • Loading branch information
delehef authored Dec 5, 2023
1 parent da61945 commit 5561663
Show file tree
Hide file tree
Showing 48 changed files with 411 additions and 98 deletions.
2 changes: 2 additions & 0 deletions acceptance-tests/src/test/resources/moduleLimits.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ROM_LEX = 1048576
SHF = 65536
SHF_RT = 262144
TX_RLP = 131072
TRM = 131072
WCP = 262144
LOG_DATA = 262144
LOG_INFO = 262144
Expand Down Expand Up @@ -60,4 +61,5 @@ PRECOMPILE_MODEXP = 10000
PRECOMPILE_ECADD = 10000
PRECOMPILE_ECMUL = 10000
PRECOMPILE_ECPAIRING = 10000
PRECOMPILE_ECPAIRING_WEIGHTED = 10000
PRECOMPILE_BLAKE2F = 512
2 changes: 2 additions & 0 deletions acceptance-tests/src/test/resources/noModuleLimits.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ROM_LEX = 1048576
SHF = 65536
SHF_RT = 262144
TX_RLP = 131072
TRM = 131072
WCP = 262144
LOG_DATA = 262144
LOG_INFO = 262144
Expand Down Expand Up @@ -60,4 +61,5 @@ PRECOMPILE_MODEXP = 10000
PRECOMPILE_ECADD = 10000
PRECOMPILE_ECMUL = 10000
PRECOMPILE_ECPAIRING = 10000
PRECOMPILE_ECPAIRING_WEIGHTED = 10000
PRECOMPILE_BLAKE2F = 512
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ ROM_LEX = 20
SHF = 20
SHF_RT = 2305
TX_RLP = 131072
TRM = 120
WCP = 120
LOG_DATA = 20
LOG_INFO = 20
RLP_ADDR = 20
RLP_TXN = 1300
RLP_TXRCPT = 90
TXN_DATA = 20
RLP_TXRCPT = 100
TXN_DATA = 30
STP = 20

#
Expand All @@ -61,4 +62,5 @@ PRECOMPILE_MODEXP = 10000
PRECOMPILE_ECADD = 10000
PRECOMPILE_ECMUL = 10000
PRECOMPILE_ECPAIRING = 10000
PRECOMPILE_ECPAIRING_WEIGHTED = 10000
PRECOMPILE_BLAKE2F = 512
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Consensys Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.rpc.counters;

import java.util.Map;

import com.fasterxml.jackson.annotation.JsonProperty;

/** FileTrace represents an execution trace. */
public record Counters(
@JsonProperty("tracesEngineVersion") String tracesEngineVersion,
@JsonProperty("blockNumber") long blockNumber,
@JsonProperty("tracesCounters") Map<String, Integer> traceCountersByModule) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Consensys Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.rpc.counters;

import java.security.InvalidParameterException;

import net.consensys.linea.zktracer.ZkTracer;

/** Holds needed parameters for sending an execution trace generation request. */
@SuppressWarnings("unused")
public record CountersRequestParams(long blockNumber, String runtimeVersion) {
private static final int EXPECTED_PARAMS_SIZE = 2;

/**
* Parses a list of params to a {@link CountersRequestParams} object.
*
* @param params an array of parameters.
* @return a parsed {@link CountersRequestParams} object..
*/
public static CountersRequestParams createTraceParams(final Object[] params) {
// validate params size
if (params.length != EXPECTED_PARAMS_SIZE) {
throw new InvalidParameterException(
String.format("Expected %d parameters but got %d", EXPECTED_PARAMS_SIZE, params.length));
}

long blockNumber = Long.parseLong(params[0].toString());
String version = params[1].toString();

if (!version.equals(getTracerRuntime())) {
throw new InvalidParameterException(
String.format(
"INVALID_TRACES_VERSION: Runtime version is %s, requesting version %s",
getTracerRuntime(), version));
}

return new CountersRequestParams(blockNumber, version);
}

private static String getTracerRuntime() {
return ZkTracer.class.getPackage().getSpecificationVersion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright Consensys Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.rpc.counters;

import java.util.Map;

import com.google.common.base.Stopwatch;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.zktracer.ZkTracer;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.services.TraceService;
import org.hyperledger.besu.plugin.services.exception.PluginRpcEndpointException;
import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest;

/** Responsible for trace counters generation. */
@Slf4j
public class RollupGenerateCountersV0 {
private static final int CACHE_SIZE = 10_000;
static final Cache<Long, Map<String, Integer>> cache =
CacheBuilder.newBuilder().maximumSize(CACHE_SIZE).build();

private final BesuContext besuContext;
private TraceService traceService;

public RollupGenerateCountersV0(final BesuContext besuContext) {
this.besuContext = besuContext;
}

public String getNamespace() {
return "rollup";
}

public String getName() {
return "getTracesCountersByBlockNumberV0";
}

/**
* Handles execution traces generation logic.
*
* @param request holds parameters of the RPC request.
* @return an execution file trace.
*/
public Counters execute(final PluginRpcRequest request) {
if (traceService == null) {
traceService = initTraceService();
}

try {
final Stopwatch sw = Stopwatch.createStarted();
final CountersRequestParams params =
CountersRequestParams.createTraceParams(request.getParams());
final long requestedBlockNumber = params.blockNumber();

final Counters r =
new Counters(
params.runtimeVersion(),
requestedBlockNumber,
cache
.asMap()
.computeIfAbsent(
requestedBlockNumber,
blockNumber -> {
final ZkTracer tracer = new ZkTracer();
traceService.trace(
blockNumber,
blockNumber,
worldStateBeforeTracing -> tracer.traceStartConflation(1),
worldStateAfterTracing -> tracer.traceEndConflation(),
tracer);

return tracer.getModulesLineCount();
}));
log.info("counters for {} returned in {}", requestedBlockNumber, sw);
return r;
} catch (Exception ex) {
throw new PluginRpcEndpointException(ex.getMessage());
}
}

private TraceService initTraceService() {
return besuContext
.getService(TraceService.class)
.orElseThrow(
() ->
new RuntimeException(
"Unable to find trace service. Please ensure TraceService is registered."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Consensys Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.rpc.counters;

import java.util.Optional;

import com.google.auto.service.AutoService;
import net.consensys.linea.LineaRequiredPlugin;
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;

/** Plugin with RPC endpoints. */
@AutoService(BesuPlugin.class)
public class RollupRpcEndpointServicePlugin extends LineaRequiredPlugin {
@Override
public void doRegister(final BesuContext context) {
RollupGenerateCountersV0 method = new RollupGenerateCountersV0(context);

Optional<RpcEndpointService> service = context.getService(RpcEndpointService.class);
createAndRegister(
method,
service.orElseThrow(
() ->
new RuntimeException("Failed to obtain RpcEndpointService from the BesuContext.")));
}

private void createAndRegister(
final RollupGenerateCountersV0 method, final RpcEndpointService rpcEndpointService) {
rpcEndpointService.registerRPCEndpoint(
method.getNamespace(), method.getName(), method::execute);
}

@Override
public void start() {
OpCodes.load();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.tracegeneration.rpc;
package net.consensys.linea.rpc.tracegeneration;

import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -53,7 +53,7 @@ public String getName() {
* @param request holds parameters of the RPC request.
* @return an execution file trace.
*/
public FileTrace execute(final PluginRpcRequest request) {
public TraceFile execute(final PluginRpcRequest request) {
Stopwatch sw = Stopwatch.createStarted();
if (this.traceService == null) {
this.traceService = getTraceService();
Expand All @@ -71,18 +71,14 @@ public FileTrace execute(final PluginRpcRequest request) {
traceService.trace(
fromBlock,
toBlock,
worldStateBeforeTracing -> {
tracer.traceStartConflation(toBlock - fromBlock + 1);
},
worldStateAfterTracing -> {
tracer.traceEndConflation();
},
worldStateBeforeTracing -> tracer.traceStartConflation(toBlock - fromBlock + 1),
worldStateAfterTracing -> tracer.traceEndConflation(),
tracer);
log.info("[TRACING] trace computed in {}", sw);
log.info("[TRACING] trace for {}-{} computed in {}", fromBlock, toBlock, sw);
sw.reset().start();
final String path = writeTraceToFile(tracer, params.runtimeVersion());
log.info("[TRACING] trace serialized to {} in {}", path, sw);
return new FileTrace(params.runtimeVersion(), path);
log.info("[TRACING] trace for {}-{} serialized to {} in {}", path, toBlock, fromBlock, sw);
return new TraceFile(params.runtimeVersion(), path);
} catch (Exception ex) {
throw new PluginRpcEndpointException(ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.tracegeneration;
package net.consensys.linea.rpc.tracegeneration;

import java.util.Optional;

import com.google.auto.service.AutoService;
import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.LineaRequiredPlugin;
import net.consensys.linea.tracegeneration.rpc.RollupGenerateConflatedTracesToFileV0;
import net.consensys.linea.zktracer.opcode.OpCodes;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
Expand Down Expand Up @@ -54,7 +53,4 @@ private void createAndRegister(
public void start() {
OpCodes.load();
}

@Override
public void stop() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.tracegeneration.rpc;
package net.consensys.linea.rpc.tracegeneration;

import com.fasterxml.jackson.annotation.JsonProperty;

/** FileTrace represents an execution trace. */
public record FileTrace(
public record TraceFile(
@JsonProperty("tracesEngineVersion") String tracesEngineVersion,
@JsonProperty("traceFileName") String traceFileName) {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.tracegeneration.rpc;
package net.consensys.linea.rpc.tracegeneration;

import java.security.InvalidParameterException;

Expand Down
Loading

0 comments on commit 5561663

Please sign in to comment.