Skip to content

Commit

Permalink
Update directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh committed Apr 11, 2024
1 parent 5abd7e0 commit c6a9ea9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.statetransition.util;

import com.google.common.annotations.VisibleForTesting;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -26,15 +27,15 @@
public class P2PDumpManager {
private static final Logger LOG = LogManager.getLogger();

private static final String GOSSIP_DECODING_ERROR_DIR = "gossip_decoding_error_messages";
private static final String GOSSIP_REJECTED_DIR = "rejected_gossip_messages";
private static final String INVALID_BLOCK_DIR = "invalid_blocks";
private static final String GOSSIP_DECODING_ERROR_DIR = "gossip-decoding-error-messages";
private static final String GOSSIP_REJECTED_DIR = "rejected-gossip-messages";
private static final String INVALID_BLOCK_DIR = "invalid-blocks";

private final boolean enabled;
private final Path directory;

public P2PDumpManager(final Path directory, final boolean enabled) {
this.enabled = enabled; // TODO fix directory structure
this.enabled = enabled;
this.directory = directory;
if (!enabled) {
return;
Expand Down Expand Up @@ -66,11 +67,12 @@ public void saveGossipMessageDecodingError(
}
final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, topic);
final String identifiers = String.format("Topic: %s", topic);
final Path topicPath = Path.of(GOSSIP_DECODING_ERROR_DIR).resolve(topic);
checkTopicDirExists(topicPath);
saveBytesToFile(
"gossip message with decoding error",
GOSSIP_DECODING_ERROR_DIR,
fileName,
identifiers,
Path.of(GOSSIP_DECODING_ERROR_DIR).resolve(topic).resolve(fileName),
originalMessage);
}

Expand All @@ -81,8 +83,10 @@ public void saveGossipRejectedMessageToFile(
}
final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, topic);
final String identifiers = String.format("Topic: %s", topic);
final Path topicPath = Path.of(GOSSIP_REJECTED_DIR).resolve(topic);
checkTopicDirExists(topicPath);
saveBytesToFile(
"rejected gossip message", GOSSIP_REJECTED_DIR, fileName, identifiers, decodedMessage);
"rejected gossip message", identifiers, topicPath.resolve(fileName), decodedMessage);
}

public void saveInvalidBlockToFile(
Expand All @@ -93,23 +97,31 @@ public void saveInvalidBlockToFile(
final String fileName =
String.format("slot%s_root%s.ssz", slot, blockRoot.toUnprefixedHexString());
final String identifiers = String.format("Slot: %s, Block Root: %s", slot, blockRoot);
saveBytesToFile("invalid block", INVALID_BLOCK_DIR, fileName, identifiers, blockSsz);
saveBytesToFile(
"invalid block", identifiers, Path.of(INVALID_BLOCK_DIR).resolve(fileName), blockSsz);
}

private void saveBytesToFile(
final String object,
final String errorDirectory,
final String fileName,
final String identifiers,
final Path relativeFilePath,
final Bytes bytes) {
final Path path = directory.resolve(errorDirectory).resolve(fileName);
final Path path = directory.resolve(relativeFilePath);
try {
final String file =
Files.write(path, bytes.toArray())
.toString(); // todo change to not be printing the whole path
LOG.info("Saved {} bytes to file. {}. Location: {}", object, identifiers, file);
Files.write(path, bytes.toArray());
LOG.info("Saved {} bytes to file. {}. Location: {}", object, identifiers, relativeFilePath);
} catch (IOException e) {
LOG.error("Failed to save {}} bytes to file. {}", object, identifiers, e);
LOG.error("Failed to save {} bytes to file. {}", object, identifiers, e);
}
}

private void checkTopicDirExists(final Path topicDir) {
final File topicDirFile = directory.resolve(topicDir).toFile();
if (topicDirFile.exists()) {
return;
}
if (!topicDirFile.mkdirs()) {
LOG.debug("Failed to create topic directory: {}", topicDir);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ void saveGossipMessageDecodingError_shouldSaveToFile(@TempDir Path tempDir) {
final P2PDumpManager manager = new P2PDumpManager(tempDir, true);
final Bytes messageBytes = dataStructureUtil.stateBuilderPhase0().build().sszSerialize();
final String arrivalTimestamp = timeProvider.getTimeInMillis().toString();
final String topic = "test_topic";
manager.saveGossipMessageDecodingError("test_topic", arrivalTimestamp, messageBytes);

final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, "test_topic");
final Path expectedFile = tempDir.resolve("gossip_decoding_error_messages").resolve(fileName);
final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, topic);
final Path expectedFile =
tempDir.resolve("gossip-decoding-error-messages").resolve(topic).resolve(fileName);
checkBytesSavedToFile(expectedFile, messageBytes);
}

Expand All @@ -54,7 +56,7 @@ void saveGossipMessageDecodingError_shouldNotSaveToFileWhenDisabled(@TempDir Pat
assertThat(manager.isEnabled()).isFalse();

final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, "test_topic");
final Path expectedFile = tempDir.resolve("gossip_decoding_error_messages").resolve(fileName);
final Path expectedFile = tempDir.resolve("gossip-decoding-error-messages").resolve(fileName);
checkFileNotExist(expectedFile);
}

Expand All @@ -63,10 +65,12 @@ void saveGossipRejectedMessageToFile_shouldSaveToFile(@TempDir Path tempDir) {
final P2PDumpManager manager = new P2PDumpManager(tempDir, true);
final Bytes messageBytes = dataStructureUtil.stateBuilderPhase0().build().sszSerialize();
final String arrivalTimestamp = timeProvider.getTimeInMillis().toString();
final String topic = "test_topic";
manager.saveGossipRejectedMessageToFile("test_topic", arrivalTimestamp, messageBytes);

final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, "test_topic");
final Path expectedFile = tempDir.resolve("rejected_gossip_messages").resolve(fileName);
final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, topic);
final Path expectedFile =
tempDir.resolve("rejected-gossip-messages").resolve(topic).resolve(fileName);
checkBytesSavedToFile(expectedFile, messageBytes);
}

Expand All @@ -79,7 +83,7 @@ void saveGossipRejectedMessageToFile_shouldNotSaveToFileWhenDisabled(@TempDir Pa
assertThat(manager.isEnabled()).isFalse();

final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, "test_topic");
final Path expectedFile = tempDir.resolve("rejected_gossip_messages").resolve(fileName);
final Path expectedFile = tempDir.resolve("rejected-gossip-messages").resolve(fileName);
checkFileNotExist(expectedFile);
}

Expand All @@ -92,7 +96,7 @@ void saveInvalidBlockToFile_shouldSaveToFile(@TempDir Path tempDir) {
final String fileName =
String.format(
"slot%s_root%s.ssz", block.getSlot(), block.getRoot().toUnprefixedHexString());
final Path expectedFile = tempDir.resolve("invalid_blocks").resolve(fileName);
final Path expectedFile = tempDir.resolve("invalid-blocks").resolve(fileName);
checkBytesSavedToFile(expectedFile, block.sszSerialize());
}

Expand All @@ -106,7 +110,7 @@ void saveInvalidBlockToFile_shouldNotSaveToFileWhenDisabled(@TempDir Path tempDi
final String fileName =
String.format(
"slot%s_root%s.ssz", block.getSlot(), block.getRoot().toUnprefixedHexString());
final Path expectedFile = tempDir.resolve("invalid_blocks").resolve(fileName);
final Path expectedFile = tempDir.resolve("invalid-blocks").resolve(fileName);
checkFileNotExist(expectedFile);
}

Expand Down

0 comments on commit c6a9ea9

Please sign in to comment.