Skip to content

Commit

Permalink
Add more log to txpool simulation validator (#12)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 authored May 21, 2024
1 parent 764914e commit 693a8e3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,22 @@ public static ModuleLimitsValidationResult blockModuleLineCountFull(
cumulativeModuleLineCount,
cumulativeModuleLineLimit);
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder(result.name());
if (moduleName != null) {
sb.append("[module=").append(moduleName);

if (moduleLineCount != null) {
sb.append(",lineCount=").append(moduleLineCount);
sb.append(",lineLimit=").append(moduleLineLimit);
sb.append(",cumulativeLineCount=").append(cumulativeModuleLineCount);
sb.append(",cumulativeLineLimit=").append(cumulativeModuleLineLimit);
}

sb.append(']');
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.consensys.linea.zktracer.ZkTracer;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.plugin.data.BlockHeader;
import org.hyperledger.besu.plugin.data.TransactionSimulationResult;
import org.hyperledger.besu.plugin.services.BlockchainService;
import org.hyperledger.besu.plugin.services.TransactionSimulationService;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionPoolValidator;
Expand Down Expand Up @@ -62,6 +63,13 @@ public Optional<String> validateTransaction(
final boolean isRemoteAndP2pEnabled =
!isLocal && txPoolValidatorConf.txPoolSimulationCheckP2pEnabled();
if (isRemoteAndP2pEnabled || isLocalAndApiEnabled) {
log.atTrace()
.setMessage(
"Starting simulation validation for tx with hash={}, isLocal={}, hasPriority={}")
.addArgument(transaction::getHash)
.addArgument(isLocal)
.addArgument(hasPriority)
.log();

final ModuleLineCountValidator moduleLineCountValidator =
new ModuleLineCountValidator(moduleLineLimitsMap);
Expand All @@ -72,34 +80,65 @@ public Optional<String> validateTransaction(
transactionSimulationService.simulate(
transaction, chainHeadHeader.getBlockHash(), zkTracer, true);

ModuleLimitsValidationResult moduleLimit =
ModuleLimitsValidationResult moduleLimitResult =
moduleLineCountValidator.validate(zkTracer.getModulesLineCount());

if (moduleLimit.getResult() != ModuleLineCountValidator.ModuleLineCountResult.VALID) {
return Optional.of(handleModuleOverLimit(moduleLimit));
logSimulationResult(
transaction, isLocal, hasPriority, maybeSimulationResults, moduleLimitResult);

if (moduleLimitResult.getResult() != ModuleLineCountValidator.ModuleLineCountResult.VALID) {
return Optional.of(handleModuleOverLimit(moduleLimitResult));
}

if (maybeSimulationResults.isPresent()) {
final var simulationResult = maybeSimulationResults.get();
if (simulationResult.isInvalid()) {
return Optional.of(
final String errMsg =
"Invalid transaction"
+ simulationResult.getInvalidReason().map(ir -> ": " + ir).orElse(""));
+ simulationResult.getInvalidReason().map(ir -> ": " + ir).orElse("");
log.debug(errMsg);
return Optional.of(errMsg);
}
if (!simulationResult.isSuccessful()) {
return Optional.of(
final String errMsg =
"Reverted transaction"
+ simulationResult
.getRevertReason()
.map(rr -> ": " + rr.toHexString())
.orElse(""));
.orElse("");
log.debug(errMsg);
return Optional.of(errMsg);
}
}
}
log.atTrace()
.setMessage(
"Simulation validation not enabled for tx with hash={}, isLocal={}, hasPriority={}")
.addArgument(transaction::getHash)
.addArgument(isLocal)
.addArgument(hasPriority)
.log();

return Optional.empty();
}

private void logSimulationResult(
final Transaction transaction,
final boolean isLocal,
final boolean hasPriority,
final Optional<TransactionSimulationResult> maybeSimulationResults,
final ModuleLimitsValidationResult moduleLimitResult) {
log.atTrace()
.setMessage(
"Result of simulation validation for tx with hash={}, isLocal={}, hasPriority={}, is {}, module line counts {}")
.addArgument(transaction::getHash)
.addArgument(isLocal)
.addArgument(hasPriority)
.addArgument(maybeSimulationResults)
.addArgument(moduleLimitResult)
.log();
}

private ZkTracer createZkTracer(final BlockHeader chainHeadHeader) {
var zkTracer = new ZkTracer(l1L2BridgeConfiguration);
zkTracer.traceStartConflation(1L);
Expand Down

0 comments on commit 693a8e3

Please sign in to comment.