Skip to content

Commit

Permalink
feat: implement modulesToCount selection from module limits TOML config
Browse files Browse the repository at this point in the history
  • Loading branch information
powerslider committed Oct 9, 2024
1 parent 31b6ba1 commit 16248a8
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ public abstract class AbstractLineaPrivateOptionsPlugin extends AbstractLineaSha

@Override
public Map<String, LineaOptionsPluginConfiguration> getLineaPluginConfigMap() {
final var configMap = new HashMap<>(super.getLineaPluginConfigMap());
Map<String, LineaOptionsPluginConfiguration> configMap =
new HashMap<>(super.getLineaPluginConfigMap());

final RpcCliOptions rpcCliOptions = RpcCliOptions.create();
configMap.put(RpcCliOptions.CONFIG_KEY, rpcCliOptions.asPluginConfig());

configMap = getPrivateLineaPluginConfigMap(configMap);

return configMap;
}

public Map<String, LineaOptionsPluginConfiguration> getPrivateLineaPluginConfigMap(
Map<String, LineaOptionsPluginConfiguration> configMap) {
return configMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package net.consensys.linea.plugins.rpc.linecounts;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -42,11 +44,17 @@ public class GenerateLineCountsV2 {
private final RequestLimiter requestLimiter;

private final BesuContext besuContext;
private final Path modulesToCountConfigFilePath;
private TraceService traceService;

public GenerateLineCountsV2(final BesuContext context, final RequestLimiter requestLimiter) {
public GenerateLineCountsV2(
final BesuContext context,
final RequestLimiter requestLimiter,
final LineCountsEndpointConfiguration endpointConfiguration) {
this.besuContext = context;
this.requestLimiter = requestLimiter;
this.modulesToCountConfigFilePath =
Paths.get(endpointConfiguration.modulesToCountConfigFilePath());
}

public String getNamespace() {
Expand Down Expand Up @@ -97,7 +105,7 @@ private LineCounts getLineCounts(PluginRpcRequest request) {
.computeIfAbsent(
requestedBlockNumber,
blockNumber -> {
final ZkTracer tracer = new ZkTracer();
final ZkTracer tracer = new ZkTracer(modulesToCountConfigFilePath);
traceService.trace(
blockNumber,
blockNumber,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.plugins.rpc.linecounts;

import com.google.common.base.MoreObjects;
import net.consensys.linea.plugins.LineaCliOptions;
import picocli.CommandLine;

class LineCountsEndpointCliOptions implements LineaCliOptions {

static final String CONFIG_KEY = "line-counts-endpoint-config";

static final String MODULES_TO_COUNT_CONFIG_FILE_PATH =
"--plugin-linea-line-counts-modules-to-count-config-file-path";

@CommandLine.Option(
required = true,
names = {MODULES_TO_COUNT_CONFIG_FILE_PATH},
hidden = true,
paramLabel = "<PATH>",
description = "TOML config file path with a list of modules to count")
private String modulesToCountConfigFilePath = null;

private LineCountsEndpointCliOptions() {}

/**
* Create Linea cli options.
*
* @return the Linea cli options
*/
static LineCountsEndpointCliOptions create() {
return new LineCountsEndpointCliOptions();
}

/**
* Linea cli options from config.
*
* @param config the config
* @return the Linea cli options
*/
static LineCountsEndpointCliOptions fromConfig(final LineCountsEndpointConfiguration config) {
final LineCountsEndpointCliOptions options = create();
options.modulesToCountConfigFilePath = config.modulesToCountConfigFilePath();
return options;
}

/**
* To domain object Linea factory configuration.
*
* @return the Linea factory configuration
*/
@Override
public LineCountsEndpointConfiguration toDomainObject() {
return LineCountsEndpointConfiguration.builder()
.modulesToCountConfigFilePath(modulesToCountConfigFilePath)
.build();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add(MODULES_TO_COUNT_CONFIG_FILE_PATH, modulesToCountConfigFilePath)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.plugins.rpc.linecounts;

import lombok.Builder;
import net.consensys.linea.plugins.LineaOptionsConfiguration;

/** The Linea tracer configuration private to this repo. */
@Builder(toBuilder = true)
public record LineCountsEndpointConfiguration(String modulesToCountConfigFilePath)
implements LineaOptionsConfiguration {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

package net.consensys.linea.plugins.rpc.linecounts;

import java.util.Map;

import com.google.auto.service.AutoService;
import net.consensys.linea.plugins.AbstractLineaPrivateOptionsPlugin;
import net.consensys.linea.plugins.BesuServiceProvider;
import net.consensys.linea.plugins.LineaOptionsPluginConfiguration;
import net.consensys.linea.plugins.rpc.RequestLimiter;
import net.consensys.linea.plugins.rpc.RequestLimiterDispatcher;
import org.hyperledger.besu.plugin.BesuContext;
Expand All @@ -36,6 +39,17 @@ public class LineCountsEndpointServicePlugin extends AbstractLineaPrivateOptions
private BesuContext besuContext;
private RpcEndpointService rpcEndpointService;

@Override
public Map<String, LineaOptionsPluginConfiguration> getPrivateLineaPluginConfigMap(
Map<String, LineaOptionsPluginConfiguration> configMap) {
final LineCountsEndpointCliOptions lineCountsEndpointCliOptions =
LineCountsEndpointCliOptions.create();
configMap.put(
LineCountsEndpointCliOptions.CONFIG_KEY, lineCountsEndpointCliOptions.asPluginConfig());

return configMap;
}

/**
* Register the RPC service.
*
Expand All @@ -52,14 +66,19 @@ public void register(final BesuContext context) {
public void beforeExternalServices() {
super.beforeExternalServices();

final LineCountsEndpointConfiguration endpointConfiguration =
(LineCountsEndpointConfiguration)
getConfigurationByKey(LineCountsEndpointCliOptions.CONFIG_KEY).optionsConfig();

RequestLimiterDispatcher.setLimiterIfMissing(
RequestLimiterDispatcher.SINGLE_INSTANCE_REQUEST_LIMITER_KEY,
rpcConfiguration().concurrentRequestsLimit());
final RequestLimiter reqLimiter =
RequestLimiterDispatcher.getLimiter(
RequestLimiterDispatcher.SINGLE_INSTANCE_REQUEST_LIMITER_KEY);

final GenerateLineCountsV2 method = new GenerateLineCountsV2(besuContext, reqLimiter);
final GenerateLineCountsV2 method =
new GenerateLineCountsV2(besuContext, reqLimiter, endpointConfiguration);
createAndRegister(method, rpcEndpointService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class TracesEndpointCliOptions implements LineaCliOptions {
static final String CONFLATED_TRACE_GENERATION_TRACES_OUTPUT_PATH =
"--plugin-linea-conflated-trace-generation-traces-output-path";

static final String CONFLATED_TRACE_GENERATION_CONCURRENT_REQUESTS_LIMIT =
"--plugin-linea-conflated-trace-generation-concurrent-requests-limit";

@CommandLine.Option(
required = true,
names = {CONFLATED_TRACE_GENERATION_TRACES_OUTPUT_PATH},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ public class TracesEndpointServicePlugin extends AbstractLineaPrivateOptionsPlug
private RpcEndpointService rpcEndpointService;

@Override
public Map<String, LineaOptionsPluginConfiguration> getLineaPluginConfigMap() {
public Map<String, LineaOptionsPluginConfiguration> getPrivateLineaPluginConfigMap(
Map<String, LineaOptionsPluginConfiguration> configMap) {
final TracesEndpointCliOptions tracesEndpointCliOptions = TracesEndpointCliOptions.create();
configMap.put(TracesEndpointCliOptions.CONFIG_KEY, tracesEndpointCliOptions.asPluginConfig());

return Map.of(TracesEndpointCliOptions.CONFIG_KEY, tracesEndpointCliOptions.asPluginConfig());
return configMap;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class ZkTracer implements ConflationAwareOperationTracer {

private static final Map<String, Integer> spillings;

private static List<Module> modulesToCount;

static {
try {
// Load spillings configured in src/main/resources/spillings.toml.
Expand All @@ -79,22 +81,38 @@ public class ZkTracer implements ConflationAwareOperationTracer {
/** Accumulate all the exceptions that happened at tracing time. */
@Getter private final List<Exception> tracingExceptions = new FiniteList<>(50);

public ZkTracer(final Path modulesToCountConfigFilePath) {}

public ZkTracer() {
this(
LineaL1L2BridgeSharedConfiguration.EMPTY,
Bytes.fromHexString("c0ffee").toBigInteger().abs());
Bytes.fromHexString("c0ffee").toBigInteger().abs(),
null);
}

public ZkTracer(BigInteger nonnegativeChainId) {
this(LineaL1L2BridgeSharedConfiguration.EMPTY, nonnegativeChainId);
this(LineaL1L2BridgeSharedConfiguration.EMPTY, nonnegativeChainId, null);
}

public ZkTracer(
final LineaL1L2BridgeSharedConfiguration bridgeConfiguration, BigInteger chainId) {
BigInteger nonnegativeChainId = chainId.abs();
final LineaL1L2BridgeSharedConfiguration bridgeConfiguration,
BigInteger chainId,
Path modulesToCountConfig) {
final BigInteger nonNegativeChainId = chainId.abs();

final Optional<Path> modulesToCountConfigFilePath = Optional.ofNullable(modulesToCountConfig);
if (modulesToCountConfigFilePath.isPresent() && modulesToCount == null) {
// Process TOML file

}

this.hub =
new Hub(bridgeConfiguration.contract(), bridgeConfiguration.topic(), nonnegativeChainId);
for (Module m : this.hub.getModulesToCount()) {
new Hub(
bridgeConfiguration.contract(),
bridgeConfiguration.topic(),
nonNegativeChainId,
modulesToCount);
for (Module m : this.hub.modulesToCount()) {
if (!spillings.containsKey(m.moduleKey())) {
throw new IllegalStateException(
"Spilling for module " + m.moduleKey() + " not defined in spillings.toml");
Expand Down Expand Up @@ -319,7 +337,7 @@ public Map<String, Integer> getModulesLineCount() {
maybeThrowTracingExceptions();
final HashMap<String, Integer> modulesLineCount = new HashMap<>();

hub.getModulesToCount()
hub.modulesToCount()
.forEach(
m ->
modulesLineCount.put(
Expand Down
Loading

0 comments on commit 16248a8

Please sign in to comment.