Skip to content

Commit

Permalink
Make sure chainId is present and positive
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Oct 8, 2024
1 parent 18498af commit 1acfa70
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.BlockchainService;

@Slf4j
public abstract class AbstractLineaRequiredPlugin extends AbstractLineaPrivateOptionsPlugin {
protected BlockchainService blockchainService;

/**
* Linea plugins extending this class will halt startup of Besu in case of exception during
Expand All @@ -34,7 +36,15 @@ public abstract class AbstractLineaRequiredPlugin extends AbstractLineaPrivateOp
public void register(final BesuContext context) {
super.register(context);
try {
log.info("Registering Linea plugin " + this.getClass().getName());
log.info("Registering Linea plugin {}", this.getClass().getName());

blockchainService =
context
.getService(BlockchainService.class)
.orElseThrow(
() ->
new RuntimeException(
"Failed to obtain BlockchainService from the BesuContext."));

doRegister(context);

Expand All @@ -52,4 +62,21 @@ public void register(final BesuContext context) {
* @param context
*/
public abstract void doRegister(final BesuContext context);

@Override
public void beforeExternalServices() {
super.beforeExternalServices();

blockchainService
.getChainId()
.ifPresentOrElse(
chainId -> {
if (chainId.signum() <= 0) {
throw new IllegalArgumentException("Chain id must be greater than zero.");
}
},
() -> {
throw new IllegalArgumentException("Chain id required");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.BesuEvents;
import org.hyperledger.besu.plugin.services.BlockchainService;
import org.hyperledger.besu.plugin.services.RpcEndpointService;

/** This plugin registers handlers that are activated when new blocks are imported */
Expand All @@ -33,7 +32,6 @@ public class LineaExtraDataPlugin extends AbstractLineaRequiredPlugin {
public static final String NAME = "linea";
private BesuContext besuContext;
private RpcEndpointService rpcEndpointService;
private BlockchainService blockchainService;

@Override
public Optional<String> getName() {
Expand All @@ -50,13 +48,6 @@ public void doRegister(final BesuContext context) {
() ->
new RuntimeException(
"Failed to obtain RpcEndpointService from the BesuContext."));
blockchainService =
context
.getService(BlockchainService.class)
.orElseThrow(
() ->
new RuntimeException(
"Failed to obtain BlockchainService from the BesuContext."));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ private Long estimateGasUsed(

final var estimateGasTracer = new EstimateGasOperationTracer();
final var chainHeadHeader = blockchainService.getChainHeadHeader();
final BigInteger nonnegativeChainId = blockchainService.getChainId().orElseThrow().abs();
final var zkTracer = createZkTracer(chainHeadHeader, nonnegativeChainId);
final var zkTracer = createZkTracer(chainHeadHeader, blockchainService.getChainId().get());
final TracerAggregator zkAndGasTracer = TracerAggregator.create(estimateGasTracer, zkTracer);

final var chainHeadHash = chainHeadHeader.getBlockHash();
Expand Down Expand Up @@ -492,9 +491,8 @@ private Transaction createTransactionForSimulation(
return txBuilder.build();
}

private ZkTracer createZkTracer(
final BlockHeader chainHeadHeader, final BigInteger nonnegativeChainId) {
var zkTracer = new ZkTracer(l1L2BridgeConfiguration, nonnegativeChainId);
private ZkTracer createZkTracer(final BlockHeader chainHeadHeader, final BigInteger chainId) {
var zkTracer = new ZkTracer(l1L2BridgeConfiguration, chainId);
zkTracer.traceStartConflation(1L);
zkTracer.traceStartBlock(chainHeadHeader);
return zkTracer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.BesuConfiguration;
import org.hyperledger.besu.plugin.services.BlockchainService;
import org.hyperledger.besu.plugin.services.RpcEndpointService;
import org.hyperledger.besu.plugin.services.TransactionSimulationService;

Expand All @@ -35,7 +34,6 @@ public class LineaEstimateGasEndpointPlugin extends AbstractLineaRequiredPlugin
private BesuConfiguration besuConfiguration;
private RpcEndpointService rpcEndpointService;
private TransactionSimulationService transactionSimulationService;
private BlockchainService blockchainService;
private LineaEstimateGas lineaEstimateGasMethod;

/**
Expand Down Expand Up @@ -69,14 +67,6 @@ public void doRegister(final BesuContext context) {
new RuntimeException(
"Failed to obtain TransactionSimulatorService from the BesuContext."));

blockchainService =
context
.getService(BlockchainService.class)
.orElseThrow(
() ->
new RuntimeException(
"Failed to obtain BlockchainService from the BesuContext."));

lineaEstimateGasMethod =
new LineaEstimateGas(
besuConfiguration, transactionSimulationService, blockchainService, rpcEndpointService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.BesuConfiguration;
import org.hyperledger.besu.plugin.services.BlockchainService;
import org.hyperledger.besu.plugin.services.TransactionPoolValidatorService;
import org.hyperledger.besu.plugin.services.TransactionSimulationService;

Expand All @@ -49,7 +48,6 @@
public class LineaTransactionPoolValidatorPlugin extends AbstractLineaRequiredPlugin {
public static final String NAME = "linea";
private BesuConfiguration besuConfiguration;
private BlockchainService blockchainService;
private TransactionPoolValidatorService transactionPoolValidatorService;
private TransactionSimulationService transactionSimulationService;
private Optional<JsonRpcManager> rejectedTxJsonRpcManager = Optional.empty();
Expand All @@ -69,14 +67,6 @@ public void doRegister(final BesuContext context) {
new RuntimeException(
"Failed to obtain BesuConfiguration from the BesuContext."));

blockchainService =
context
.getService(BlockchainService.class)
.orElseThrow(
() ->
new RuntimeException(
"Failed to obtain BlockchainService from the BesuContext."));

transactionPoolValidatorService =
context
.getService(TransactionPoolValidatorService.class)
Expand All @@ -97,6 +87,7 @@ public void doRegister(final BesuContext context) {
@Override
public void start() {
super.start();

try (Stream<String> lines =
Files.lines(
Path.of(new File(transactionPoolValidatorConfiguration().denyListPath()).toURI()))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ public Optional<String> validateTransaction(
final ModuleLineCountValidator moduleLineCountValidator =
new ModuleLineCountValidator(moduleLineLimitsMap);
final var chainHeadHeader = blockchainService.getChainHeadHeader();
final BigInteger nonnegativeChainId = blockchainService.getChainId().orElseThrow().abs();

final var zkTracer = createZkTracer(chainHeadHeader, nonnegativeChainId);
final var zkTracer = createZkTracer(chainHeadHeader, blockchainService.getChainId().get());
final var maybeSimulationResults =
transactionSimulationService.simulate(
transaction, chainHeadHeader.getBlockHash(), zkTracer, true);
Expand Down Expand Up @@ -172,9 +171,8 @@ private void logSimulationResult(
.log();
}

private ZkTracer createZkTracer(
final BlockHeader chainHeadHeader, BigInteger nonnegativeChainId) {
var zkTracer = new ZkTracer(l1L2BridgeConfiguration, nonnegativeChainId);
private ZkTracer createZkTracer(final BlockHeader chainHeadHeader, BigInteger chainId) {
var zkTracer = new ZkTracer(l1L2BridgeConfiguration, chainId);
zkTracer.traceStartConflation(1L);
zkTracer.traceStartBlock(chainHeadHeader);
return zkTracer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.BesuConfiguration;
import org.hyperledger.besu.plugin.services.BlockchainService;
import org.hyperledger.besu.plugin.services.TransactionSelectionService;

/**
Expand All @@ -41,7 +40,6 @@
public class LineaTransactionSelectorPlugin extends AbstractLineaRequiredPlugin {
public static final String NAME = "linea";
private TransactionSelectionService transactionSelectionService;
private BlockchainService blockchainService;
private Optional<JsonRpcManager> rejectedTxJsonRpcManager = Optional.empty();
private BesuConfiguration besuConfiguration;

Expand All @@ -60,14 +58,6 @@ public void doRegister(final BesuContext context) {
new RuntimeException(
"Failed to obtain TransactionSelectionService from the BesuContext."));

blockchainService =
context
.getService(BlockchainService.class)
.orElseThrow(
() ->
new RuntimeException(
"Failed to obtain BlockchainService from the BesuContext."));

besuConfiguration =
context
.getService(BesuConfiguration.class)
Expand All @@ -80,6 +70,7 @@ public void doRegister(final BesuContext context) {
@Override
public void start() {
super.start();

final LineaTransactionSelectorConfiguration txSelectorConfiguration =
transactionSelectorConfiguration();
final LineaRejectedTxReportingConfiguration lineaRejectedTxReportingConfiguration =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ private List<PluginTransactionSelector> createTransactionSelectors(

traceLineLimitTransactionSelector =
new TraceLineLimitTransactionSelector(
limitsMap, txSelectorConfiguration, l1L2BridgeConfiguration, tracerConfiguration);
blockchainService.getChainId().get(),
limitsMap,
txSelectorConfiguration,
l1L2BridgeConfiguration,
tracerConfiguration);

return List.of(
new MaxBlockCallDataTransactionSelector(txSelectorConfiguration.maxBlockCallDataSize()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static net.consensys.linea.sequencer.txselection.LineaTransactionSelectionResult.TX_MODULE_LINE_COUNT_OVERFLOW_CACHED;
import static org.hyperledger.besu.plugin.data.TransactionSelectionResult.SELECTED;

import java.math.BigInteger;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -56,13 +57,15 @@ public class TraceLineLimitTransactionSelector implements PluginTransactionSelec
private static final Marker BLOCK_LINE_COUNT_MARKER = MarkerFactory.getMarker("BLOCK_LINE_COUNT");
@VisibleForTesting protected static Set<Hash> overLineCountLimitCache = new LinkedHashSet<>();
private final ZkTracer zkTracer;
private final BigInteger chainId;
private final String limitFilePath;
private final Map<String, Integer> moduleLimits;
private final int overLimitCacheSize;
private final ModuleLineCountValidator moduleLineCountAccumulator;
private Map<String, Integer> currCumulatedLineCount;

public TraceLineLimitTransactionSelector(
final BigInteger chainId,
final Map<String, Integer> moduleLimits,
final LineaTransactionSelectorConfiguration txSelectorConfiguration,
final LineaL1L2BridgeSharedConfiguration l1L2BridgeConfiguration,
Expand All @@ -72,6 +75,7 @@ public TraceLineLimitTransactionSelector(
System.exit(1);
}

this.chainId = chainId;
this.moduleLimits = moduleLimits;
this.limitFilePath = tracerConfiguration.moduleLimitsFilePath();
this.overLimitCacheSize = txSelectorConfiguration.overLinesLimitCacheSize();
Expand Down Expand Up @@ -217,7 +221,7 @@ private String logTxLineCount() {

private class ZkTracerWithLog extends ZkTracer {
public ZkTracerWithLog(final LineaL1L2BridgeSharedConfiguration bridgeConfiguration) {
super(bridgeConfiguration);
super(bridgeConfiguration, chainId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void initialize(final WireMockRuntimeInfo wmInfo) throws MalformedURLExce
final var blockHeader = mock(BlockHeader.class);
when(blockHeader.getBaseFee()).thenReturn(Optional.of(BASE_FEE));
when(blockchainService.getChainHeadHeader()).thenReturn(blockHeader);
when(blockchainService.getChainId()).thenReturn(Optional.of(BigInteger.ONE));

final var rejectedTxReportingConf =
LineaRejectedTxReportingConfiguration.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
Expand Down Expand Up @@ -250,6 +251,7 @@ private class TestableTraceLineLimitTransactionSelector
final Map<String, Integer> moduleLimits,
final int overLimitCacheSize) {
super(
BigInteger.ONE,
moduleLimits,
LineaTransactionSelectorConfiguration.builder()
.overLinesLimitCacheSize(overLimitCacheSize)
Expand Down

0 comments on commit 1acfa70

Please sign in to comment.