Skip to content

Commit

Permalink
feat: improve design of shared and private CLI options (#864)
Browse files Browse the repository at this point in the history
Signed-off-by: Tsvetan Dimitrov <[email protected]>
Co-authored-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
powerslider and fab-10 authored Jul 25, 2024
1 parent ec0cb14 commit 89ddb14
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 178 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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;

import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.PicoCLIOptions;

/**
* In this class we put CLI options that are private to plugins in this repo.
*
* <p>For the moment is just a placeholder since there are no private options
*/
@Slf4j
public abstract class AbstractLineaOptionsPlugin implements BesuPlugin {
private static final String CLI_OPTIONS_PREFIX = "linea";
private static final Map<String, LineaOptionsPluginConfiguration> LINEA_PLUGIN_CONFIG_MAP =
new HashMap<>();

protected abstract Map<String, LineaOptionsPluginConfiguration> getLineaPluginConfigMap();

protected LineaOptionsPluginConfiguration getConfigurationByKey(final String key) {
return LINEA_PLUGIN_CONFIG_MAP.get(key);
}

@Override
public synchronized void register(final BesuContext context) {
final PicoCLIOptions cmdlineOptions =
context
.getService(PicoCLIOptions.class)
.orElseThrow(
() ->
new IllegalStateException(
"Failed to obtain PicoCLI options from the BesuContext"));

getLineaPluginConfigMap()
.forEach(
(key, pluginConfiguration) -> {
if (!LINEA_PLUGIN_CONFIG_MAP.containsKey(key)) {
LINEA_PLUGIN_CONFIG_MAP.put(key, pluginConfiguration);
cmdlineOptions.addPicoCLIOptions(
CLI_OPTIONS_PREFIX, pluginConfiguration.cliOptions());
}
});
}

@Override
public void beforeExternalServices() {
// TODO: find a way to do this only once and check fro that.
LINEA_PLUGIN_CONFIG_MAP.forEach((opts, config) -> config.initOptionsConfig());

LINEA_PLUGIN_CONFIG_MAP.forEach(
(opts, config) -> {
log.debug(
"Configured plugin {} with configuration: {}", getName(), config.optionsConfig());
});
}

@Override
public void start() {}

@Override
public void stop() {
LINEA_PLUGIN_CONFIG_MAP.clear();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.hyperledger.besu.plugin.BesuPlugin;

@Slf4j
public abstract class AbstractLineaRequiredPlugin extends AbstractLineaPrivateOptionsPlugin {
public abstract class AbstractLineaRequiredPlugin extends AbstractLineaSharedOptionsPlugin {

/**
* Linea plugins extending this class will halt startup of Besu in case of exception during
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,32 @@

package net.consensys.linea.plugins;

import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.plugins.config.LineaL1L2BridgeCliOptions;
import net.consensys.linea.plugins.config.LineaL1L2BridgeConfiguration;
import net.consensys.linea.plugins.config.LineaTracerCliOptions;
import net.consensys.linea.plugins.config.LineaTracerConfiguration;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.PicoCLIOptions;
import net.consensys.linea.plugins.config.LineaL1L2BridgeSharedCliOptions;
import net.consensys.linea.plugins.config.LineaL1L2BridgeSharedConfiguration;

/** In this class we put CLI options that are shared with other plugins not defined here */
@Slf4j
public abstract class AbstractLineaSharedOptionsPlugin implements BesuPlugin {
private static final String CLI_OPTIONS_PREFIX = "linea";
private static boolean cliOptionsRegistered = false;
private static boolean configured = false;
private static LineaTracerCliOptions tracerCliOptions;
private static LineaL1L2BridgeCliOptions l1L2BridgeCliOptions;
protected static LineaTracerConfiguration tracerConfiguration;
protected static LineaL1L2BridgeConfiguration l1L2BridgeConfiguration;
public abstract class AbstractLineaSharedOptionsPlugin extends AbstractLineaOptionsPlugin {

@Override
public synchronized void register(final BesuContext context) {
if (!cliOptionsRegistered) {
final PicoCLIOptions cmdlineOptions =
context
.getService(PicoCLIOptions.class)
.orElseThrow(
() ->
new IllegalStateException(
"Failed to obtain PicoCLI options from the BesuContext"));
tracerCliOptions = LineaTracerCliOptions.create();
l1L2BridgeCliOptions = LineaL1L2BridgeCliOptions.create();
public Map<String, LineaOptionsPluginConfiguration> getLineaPluginConfigMap() {
final LineaL1L2BridgeSharedCliOptions l1L2BridgeCliOptions =
LineaL1L2BridgeSharedCliOptions.create();

cmdlineOptions.addPicoCLIOptions(CLI_OPTIONS_PREFIX, tracerCliOptions);
cmdlineOptions.addPicoCLIOptions(CLI_OPTIONS_PREFIX, l1L2BridgeCliOptions);
cliOptionsRegistered = true;
}
return Map.of(
LineaL1L2BridgeSharedCliOptions.CONFIG_KEY, l1L2BridgeCliOptions.asPluginConfig());
}

@Override
public void beforeExternalServices() {
if (!configured) {
tracerConfiguration = tracerCliOptions.toDomainObject();
l1L2BridgeConfiguration = l1L2BridgeCliOptions.toDomainObject();
configured = true;
}

log.debug("Configured plugin {} with tracer configuration: {}", getName(), tracerConfiguration);
log.debug(
"Configured plugin {} with L1 L2 bridge configuration: {}",
getName(),
l1L2BridgeCliOptions);
public LineaL1L2BridgeSharedConfiguration l1L2BridgeSharedConfiguration() {
return (LineaL1L2BridgeSharedConfiguration)
getConfigurationByKey(LineaL1L2BridgeSharedCliOptions.CONFIG_KEY).optionsConfig();
}

@Override
public void start() {}

@Override
public void stop() {
cliOptionsRegistered = false;
configured = false;
public void start() {
super.start();
}
}
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;

public interface LineaCliOptions {
LineaOptionsConfiguration toDomainObject();

default LineaOptionsPluginConfiguration asPluginConfig() {
return new LineaOptionsPluginConfiguration(this, this::toDomainObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.plugins.config;
package net.consensys.linea.plugins;

import lombok.Builder;

/** The Linea tracer configuration. */
@Builder(toBuilder = true)
public record LineaTracerConfiguration(String moduleLimitsFilePath, String tracesOutputPath) {}
public interface LineaOptionsConfiguration {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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;

import java.util.function.Supplier;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

@Accessors(fluent = true)
@RequiredArgsConstructor
public class LineaOptionsPluginConfiguration {
@Getter private final LineaCliOptions cliOptions;
private final Supplier<LineaOptionsConfiguration> optionsConfigSupplier;
@Getter private LineaOptionsConfiguration optionsConfig;

public void initOptionsConfig() {
optionsConfig = optionsConfigSupplier.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
package net.consensys.linea.plugins.config;

import com.google.common.base.MoreObjects;
import net.consensys.linea.plugins.LineaCliOptions;
import net.consensys.linea.plugins.config.converters.AddressConverter;
import net.consensys.linea.plugins.config.converters.BytesConverter;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.datatypes.Address;
import picocli.CommandLine;

/** The Linea L1 L2 Bridge CLI options. */
public class LineaL1L2BridgeCliOptions {
public class LineaL1L2BridgeSharedCliOptions implements LineaCliOptions {
public static final String CONFIG_KEY = "l1-l2-bridge-shared-config";

private static final String L1L2_BRIDGE_CONTRACT = "--plugin-linea-l1l2-bridge-contract";
private static final String L1L2_BRIDGE_TOPIC = "--plugin-linea-l1l2-bridge-topic";

Expand All @@ -41,15 +44,15 @@ public class LineaL1L2BridgeCliOptions {
description = "The log topic of the L1 L2 bridge (default: ${DEFAULT-VALUE})")
private Bytes l1l2BridgeTopic = Bytes.EMPTY;

private LineaL1L2BridgeCliOptions() {}
private LineaL1L2BridgeSharedCliOptions() {}

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

/**
Expand All @@ -58,8 +61,9 @@ public static LineaL1L2BridgeCliOptions create() {
* @param config the config
* @return the Linea cli options
*/
public static LineaL1L2BridgeCliOptions fromConfig(final LineaL1L2BridgeConfiguration config) {
final LineaL1L2BridgeCliOptions options = create();
public static LineaL1L2BridgeSharedCliOptions fromConfig(
final LineaL1L2BridgeSharedConfiguration config) {
final LineaL1L2BridgeSharedCliOptions options = create();
options.l1l2BridgeContract = config.contract();
options.l1l2BridgeTopic = config.topic();
return options;
Expand All @@ -70,8 +74,9 @@ public static LineaL1L2BridgeCliOptions fromConfig(final LineaL1L2BridgeConfigur
*
* @return the Linea factory configuration
*/
public LineaL1L2BridgeConfiguration toDomainObject() {
return LineaL1L2BridgeConfiguration.builder()
@Override
public LineaL1L2BridgeSharedConfiguration toDomainObject() {
return LineaL1L2BridgeSharedConfiguration.builder()
.contract(l1l2BridgeContract)
.topic(l1l2BridgeTopic)
.build();
Expand Down
Loading

0 comments on commit 89ddb14

Please sign in to comment.