Skip to content

Commit

Permalink
fix: ensure trace files are always deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef authored and delehef committed Dec 8, 2023
1 parent d7d4c40 commit 5330574
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package net.consensys.linea.continoustracing;

import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -35,7 +37,7 @@ public class ContinuousTracingBlockAddedListener implements BesuEvents.BlockAdde
final TraceFailureHandler traceFailureHandler;
final String zkEvmBin;

static final int BLOCK_PARALLELISM = 10; // Higher and IOs bite the dust
static final int BLOCK_PARALLELISM = 5;
final ThreadPoolExecutor pool =
new ThreadPoolExecutor(
BLOCK_PARALLELISM,
Expand All @@ -44,7 +46,6 @@ public class ContinuousTracingBlockAddedListener implements BesuEvents.BlockAdde
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(BLOCK_PARALLELISM),
new ThreadPoolExecutor.CallerRunsPolicy());
;

public ContinuousTracingBlockAddedListener(
final ContinuousTracer continuousTracer,
Expand All @@ -66,13 +67,13 @@ public void onBlockAdded(final AddedBlockContext addedBlockContext) {
try {
final CorsetValidator.Result traceResult =
continuousTracer.verifyTraceOfBlock(blockHash, zkEvmBin, new ZkTracer());
Files.delete(traceResult.traceFile().toPath());

if (!traceResult.isValid()) {
log.error("Corset returned and error for block {}", blockHeader.getNumber());
traceFailureHandler.handleCorsetFailure(blockHeader, traceResult);
return;
}

log.info("Trace for block {} verified successfully", blockHeader.getNumber());
} catch (InvalidBlockTraceException e) {
log.error("Error while tracing block {}: {}", blockHeader.getNumber(), e.getMessage());
Expand All @@ -81,6 +82,8 @@ public void onBlockAdded(final AddedBlockContext addedBlockContext) {
log.error(e.getMessage());
} catch (InvalidTraceHandlerException e) {
log.error("Error while handling invalid trace: {}", e.getMessage());
} catch (IOException e) {
log.error("IO error: {}", e.getMessage());
} finally {
log.info("End of tracing block {}", blockHeader.getNumber());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,17 @@
*/
package net.consensys.linea.continoustracing;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.continoustracing.exception.InvalidTraceHandlerException;
import net.consensys.linea.corset.CorsetValidator;
import org.apache.commons.io.FileUtils;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.plugin.data.BlockHeader;

@Slf4j
public class TraceFailureHandler {
final SlackNotificationService slackNotificationService;
static final File INVALID_TRACE_DIRECTORY =
new File(FileUtils.getUserDirectory(), "invalid-traces");

public TraceFailureHandler(final SlackNotificationService slackNotificationService) {
this.slackNotificationService = slackNotificationService;
Expand All @@ -38,36 +33,6 @@ public TraceFailureHandler(final SlackNotificationService slackNotificationServi
public void handleCorsetFailure(
final BlockHeader blockHeader, final CorsetValidator.Result result)
throws InvalidTraceHandlerException {
final File traceFile =
FileUtils.getFile(
INVALID_TRACE_DIRECTORY,
"trace_"
+ blockHeader.getNumber()
+ "_"
+ blockHeader.getBlockHash().toHexString()
+ ".lt");
final File corsetOutputFile =
FileUtils.getFile(
INVALID_TRACE_DIRECTORY,
"corset_output_"
+ blockHeader.getNumber()
+ "_"
+ blockHeader.getBlockHash().toHexString()
+ ".txt");

try {
FileUtils.createParentDirectories(traceFile);

FileUtils.moveFile(FileUtils.getFile(result.traceFile()), traceFile);
FileUtils.writeStringToFile(corsetOutputFile, result.corsetOutput(), StandardCharsets.UTF_8);
} catch (IOException e) {
log.error(
"Cannot save files for invalid trace to directory {}: {}",
INVALID_TRACE_DIRECTORY,
e.getMessage());
throw new InvalidTraceHandlerException(e);
}

try {
slackNotificationService.sendCorsetFailureNotification(
blockHeader.getNumber(), blockHeader.getBlockHash().toHexString(), result);
Expand Down

0 comments on commit 5330574

Please # to comment.