-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reject a tx, sent via eth_sendRawTransaction, if its simulation fails
Signed-off-by: Fabio Di Fabio <[email protected]>
- Loading branch information
Showing
6 changed files
with
369 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...n/java/net/consensys/linea/sequencer/txpoolvalidation/validators/SimulationValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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.sequencer.txpoolvalidation.validators; | ||
|
||
import static net.consensys.linea.sequencer.modulelimit.ModuleLineCountValidator.ModuleLineCountResult.MODULE_NOT_DEFINED; | ||
import static net.consensys.linea.sequencer.modulelimit.ModuleLineCountValidator.ModuleLineCountResult.TX_MODULE_LINE_COUNT_OVERFLOW; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import net.consensys.linea.config.LineaL1L2BridgeConfiguration; | ||
import net.consensys.linea.sequencer.modulelimit.ModuleLimitsValidationResult; | ||
import net.consensys.linea.sequencer.modulelimit.ModuleLineCountValidator; | ||
import net.consensys.linea.zktracer.ZkTracer; | ||
import org.hyperledger.besu.datatypes.Transaction; | ||
import org.hyperledger.besu.plugin.data.BlockHeader; | ||
import org.hyperledger.besu.plugin.services.BlockchainService; | ||
import org.hyperledger.besu.plugin.services.TransactionSimulationService; | ||
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionPoolValidator; | ||
|
||
@Slf4j | ||
public class SimulationValidator implements PluginTransactionPoolValidator { | ||
private final BlockchainService blockchainService; | ||
private final TransactionSimulationService transactionSimulationService; | ||
private final Map<String, Integer> moduleLineLimitsMap; | ||
private final LineaL1L2BridgeConfiguration l1L2BridgeConfiguration; | ||
|
||
public SimulationValidator( | ||
final BlockchainService blockchainService, | ||
final TransactionSimulationService transactionSimulationService, | ||
final Map<String, Integer> moduleLineLimitsMap, | ||
final LineaL1L2BridgeConfiguration l1L2BridgeConfiguration) { | ||
this.blockchainService = blockchainService; | ||
this.transactionSimulationService = transactionSimulationService; | ||
this.moduleLineLimitsMap = moduleLineLimitsMap; | ||
this.l1L2BridgeConfiguration = l1L2BridgeConfiguration; | ||
} | ||
|
||
@Override | ||
public Optional<String> validateTransaction( | ||
final Transaction transaction, final boolean isLocal, final boolean hasPriority) { | ||
|
||
final ModuleLineCountValidator moduleLineCountValidator = | ||
new ModuleLineCountValidator(moduleLineLimitsMap); | ||
final var chainHeadHeader = blockchainService.getChainHeadHeader(); | ||
|
||
final var zkTracer = createZkTracer(chainHeadHeader); | ||
final var maybeSimulationResults = | ||
transactionSimulationService.simulate( | ||
transaction, chainHeadHeader.getBlockHash(), zkTracer, true); | ||
|
||
ModuleLimitsValidationResult moduleLimit = | ||
moduleLineCountValidator.validate(zkTracer.getModulesLineCount()); | ||
|
||
if (moduleLimit.getResult() != ModuleLineCountValidator.ModuleLineCountResult.VALID) { | ||
return Optional.of(handleModuleOverLimit(moduleLimit)); | ||
} | ||
|
||
if (maybeSimulationResults.isPresent()) { | ||
final var simulationResult = maybeSimulationResults.get(); | ||
if (simulationResult.isInvalid()) { | ||
return Optional.of( | ||
"Invalid transaction" | ||
+ simulationResult.getInvalidReason().map(ir -> ": " + ir).orElse("")); | ||
} | ||
if (!simulationResult.isSuccessful()) { | ||
return Optional.of( | ||
"Reverted transaction" | ||
+ simulationResult.getRevertReason().map(rr -> ": " + rr.toHexString()).orElse("")); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
private ZkTracer createZkTracer(final BlockHeader chainHeadHeader) { | ||
var zkTracer = new ZkTracer(l1L2BridgeConfiguration); | ||
zkTracer.traceStartConflation(1L); | ||
zkTracer.traceStartBlock(chainHeadHeader); | ||
return zkTracer; | ||
} | ||
|
||
private String handleModuleOverLimit(ModuleLimitsValidationResult moduleLimitResult) { | ||
if (moduleLimitResult.getResult() == MODULE_NOT_DEFINED) { | ||
String moduleNotDefinedMsg = | ||
String.format( | ||
"Module %s does not exist in the limits file.", moduleLimitResult.getModuleName()); | ||
log.error(moduleNotDefinedMsg); | ||
return moduleNotDefinedMsg; | ||
} | ||
if (moduleLimitResult.getResult() == TX_MODULE_LINE_COUNT_OVERFLOW) { | ||
String txOverflowMsg = | ||
String.format( | ||
"Transaction line count for module %s=%s is above the limit %s", | ||
moduleLimitResult.getModuleName(), | ||
moduleLimitResult.getModuleLineCount(), | ||
moduleLimitResult.getModuleLineLimit()); | ||
log.warn(txOverflowMsg); | ||
return txOverflowMsg; | ||
} | ||
return "Internal Error: do not know what to do with result: " + moduleLimitResult.getResult(); | ||
} | ||
} |
Oops, something went wrong.