Skip to content

Commit

Permalink
Handle unknown arrival time
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh committed Apr 12, 2024
1 parent c499511 commit 7e2db7b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.infrastructure.time.SystemTimeProvider;
import tech.pegasys.teku.infrastructure.time.TimeProvider;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;

public class DebugDataDumper {
Expand Down Expand Up @@ -62,7 +64,7 @@ public void saveGossipMessageDecodingError(
if (!enabled) {
return;
}
final String formattedTimestamp = formatTimestamp(arrivalTimestamp);
final String formattedTimestamp = formatOptionalTimestamp(arrivalTimestamp);
final String fileName = String.format("%s.ssz", formattedTimestamp);
final Path topicPath =
Path.of(GOSSIP_MESSAGES_DIR).resolve(DECODING_ERROR_SUB_DIR).resolve(topic);
Expand All @@ -79,7 +81,7 @@ public void saveGossipRejectedMessageToFile(
if (!enabled) {
return;
}
final String formattedTimestamp = formatTimestamp(arrivalTimestamp);
final String formattedTimestamp = formatOptionalTimestamp(arrivalTimestamp);
final String fileName = String.format("%s.ssz", formattedTimestamp);
final Path topicPath = Path.of(GOSSIP_MESSAGES_DIR).resolve(REJECTED_SUB_DIR).resolve(topic);
final String identifiers = String.format("on topic %s at %s", topic, formattedTimestamp);
Expand Down Expand Up @@ -154,13 +156,23 @@ private void createDirectory(
}

@VisibleForTesting
String formatTimestamp(final Optional<UInt64> arrivalTimestamp) {
if (arrivalTimestamp.isEmpty()) {
return "unknown";
}
String formatOptionalTimestamp(final Optional<UInt64> maybeTimestamp) {
return maybeTimestamp
.map(this::formatTimestamp)
.orElse(generateTimestamp(new SystemTimeProvider()));
}

@VisibleForTesting
String formatTimestamp(final UInt64 arrivalTimestamp) {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS");
final Date date = new Date(arrivalTimestamp.longValue());
return df.format(date);
}

@VisibleForTesting
String generateTimestamp(final TimeProvider timeProvider) {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS");
final Date date = new Date(arrivalTimestamp.get().longValue());
final Date date = new Date(timeProvider.getTimeInMillis().longValue());
return df.format(date);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ void saveGossipMessageDecodingError_shouldSaveToFile(@TempDir Path tempDir) {
final String topic = "test_topic";
manager.saveGossipMessageDecodingError("test_topic", arrivalTimestamp, messageBytes);

final String fileName = String.format("%s.ssz", manager.formatTimestamp(arrivalTimestamp));
final String fileName =
String.format("%s.ssz", manager.formatOptionalTimestamp(arrivalTimestamp));
final Path expectedFile =
tempDir
.resolve("gossip_messages")
Expand All @@ -65,7 +66,8 @@ void saveGossipMessageDecodingError_shouldNotSaveToFileWhenDisabled(@TempDir Pat
manager.saveGossipMessageDecodingError("test_topic", arrivalTimestamp, messageBytes);
assertThat(manager.isEnabled()).isFalse();

final String fileName = String.format("%s.ssz", manager.formatTimestamp(arrivalTimestamp));
final String fileName =
String.format("%s.ssz", manager.formatOptionalTimestamp(arrivalTimestamp));
final Path expectedFile =
tempDir.resolve("gossip_messages").resolve("decoding_error").resolve(fileName);
checkFileNotExist(expectedFile);
Expand All @@ -79,7 +81,8 @@ void saveGossipRejectedMessageToFile_shouldSaveToFile(@TempDir Path tempDir) {
final String topic = "test_topic";
manager.saveGossipRejectedMessageToFile("test_topic", arrivalTimestamp, messageBytes);

final String fileName = String.format("%s.ssz", manager.formatTimestamp(arrivalTimestamp));
final String fileName =
String.format("%s.ssz", manager.formatOptionalTimestamp(arrivalTimestamp));
final Path expectedFile =
tempDir.resolve("gossip_messages").resolve("rejected").resolve(topic).resolve(fileName);
checkBytesSavedToFile(expectedFile, messageBytes);
Expand Down Expand Up @@ -158,15 +161,15 @@ void constructionOfDirectories_shouldDisableWhenFailedToCreate(@TempDir Path tem
void formatTimestamp_shouldFormatDate() {
final DebugDataDumper manager = new DebugDataDumper(Path.of("."), true);
final String formattedTimestamp =
manager.formatTimestamp(Optional.of(timeProvider.getTimeInMillis()));
manager.formatOptionalTimestamp(Optional.of(timeProvider.getTimeInMillis()));
assertThat(formattedTimestamp).isEqualTo("1970-01-01T12:46:40.00");
}

@Test
void formatTimestamp_shouldReturnConsistentUnknown() {
void generateTimestamp_shouldGenerateTimestamp() {
final DebugDataDumper manager = new DebugDataDumper(Path.of("."), true);
final String formattedTimestamp = manager.formatTimestamp(Optional.empty());
assertThat(formattedTimestamp).isEqualTo("unknown");
final String formattedTimestamp = manager.generateTimestamp(timeProvider);
assertThat(formattedTimestamp).isEqualTo("1970-01-01T12:46:40.00");
}

private void checkBytesSavedToFile(final Path path, final Bytes expectedBytes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ protected ValidationResult handleMessageProcessingError(
final PreparedGossipMessage message, final Throwable err) {
final ValidationResult response;
if (ExceptionUtil.hasCause(err, DecodingException.class)) {

debugDataDumper.saveGossipMessageDecodingError(
getTopic(), message.getArrivalTimestamp(), message.getOriginalMessage());
P2P_LOG.onGossipMessageDecodingError(getTopic(), message.getOriginalMessage(), err);
Expand Down

0 comments on commit 7e2db7b

Please sign in to comment.