Skip to content

Commit 7e2db7b

Browse files
committed
Handle unknown arrival time
1 parent c499511 commit 7e2db7b

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

Diff for: ethereum/statetransition/src/main/java/tech/pegasys/teku/statetransition/util/DebugDataDumper.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.apache.logging.log4j.Logger;
2727
import org.apache.tuweni.bytes.Bytes;
2828
import org.apache.tuweni.bytes.Bytes32;
29+
import tech.pegasys.teku.infrastructure.time.SystemTimeProvider;
30+
import tech.pegasys.teku.infrastructure.time.TimeProvider;
2931
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
3032

3133
public class DebugDataDumper {
@@ -62,7 +64,7 @@ public void saveGossipMessageDecodingError(
6264
if (!enabled) {
6365
return;
6466
}
65-
final String formattedTimestamp = formatTimestamp(arrivalTimestamp);
67+
final String formattedTimestamp = formatOptionalTimestamp(arrivalTimestamp);
6668
final String fileName = String.format("%s.ssz", formattedTimestamp);
6769
final Path topicPath =
6870
Path.of(GOSSIP_MESSAGES_DIR).resolve(DECODING_ERROR_SUB_DIR).resolve(topic);
@@ -79,7 +81,7 @@ public void saveGossipRejectedMessageToFile(
7981
if (!enabled) {
8082
return;
8183
}
82-
final String formattedTimestamp = formatTimestamp(arrivalTimestamp);
84+
final String formattedTimestamp = formatOptionalTimestamp(arrivalTimestamp);
8385
final String fileName = String.format("%s.ssz", formattedTimestamp);
8486
final Path topicPath = Path.of(GOSSIP_MESSAGES_DIR).resolve(REJECTED_SUB_DIR).resolve(topic);
8587
final String identifiers = String.format("on topic %s at %s", topic, formattedTimestamp);
@@ -154,13 +156,23 @@ private void createDirectory(
154156
}
155157

156158
@VisibleForTesting
157-
String formatTimestamp(final Optional<UInt64> arrivalTimestamp) {
158-
if (arrivalTimestamp.isEmpty()) {
159-
return "unknown";
160-
}
159+
String formatOptionalTimestamp(final Optional<UInt64> maybeTimestamp) {
160+
return maybeTimestamp
161+
.map(this::formatTimestamp)
162+
.orElse(generateTimestamp(new SystemTimeProvider()));
163+
}
164+
165+
@VisibleForTesting
166+
String formatTimestamp(final UInt64 arrivalTimestamp) {
167+
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS");
168+
final Date date = new Date(arrivalTimestamp.longValue());
169+
return df.format(date);
170+
}
161171

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

Diff for: ethereum/statetransition/src/test/java/tech/pegasys/teku/statetransition/util/DebugDataDumperTest.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ void saveGossipMessageDecodingError_shouldSaveToFile(@TempDir Path tempDir) {
4747
final String topic = "test_topic";
4848
manager.saveGossipMessageDecodingError("test_topic", arrivalTimestamp, messageBytes);
4949

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

68-
final String fileName = String.format("%s.ssz", manager.formatTimestamp(arrivalTimestamp));
69+
final String fileName =
70+
String.format("%s.ssz", manager.formatOptionalTimestamp(arrivalTimestamp));
6971
final Path expectedFile =
7072
tempDir.resolve("gossip_messages").resolve("decoding_error").resolve(fileName);
7173
checkFileNotExist(expectedFile);
@@ -79,7 +81,8 @@ void saveGossipRejectedMessageToFile_shouldSaveToFile(@TempDir Path tempDir) {
7981
final String topic = "test_topic";
8082
manager.saveGossipRejectedMessageToFile("test_topic", arrivalTimestamp, messageBytes);
8183

82-
final String fileName = String.format("%s.ssz", manager.formatTimestamp(arrivalTimestamp));
84+
final String fileName =
85+
String.format("%s.ssz", manager.formatOptionalTimestamp(arrivalTimestamp));
8386
final Path expectedFile =
8487
tempDir.resolve("gossip_messages").resolve("rejected").resolve(topic).resolve(fileName);
8588
checkBytesSavedToFile(expectedFile, messageBytes);
@@ -158,15 +161,15 @@ void constructionOfDirectories_shouldDisableWhenFailedToCreate(@TempDir Path tem
158161
void formatTimestamp_shouldFormatDate() {
159162
final DebugDataDumper manager = new DebugDataDumper(Path.of("."), true);
160163
final String formattedTimestamp =
161-
manager.formatTimestamp(Optional.of(timeProvider.getTimeInMillis()));
164+
manager.formatOptionalTimestamp(Optional.of(timeProvider.getTimeInMillis()));
162165
assertThat(formattedTimestamp).isEqualTo("1970-01-01T12:46:40.00");
163166
}
164167

165168
@Test
166-
void formatTimestamp_shouldReturnConsistentUnknown() {
169+
void generateTimestamp_shouldGenerateTimestamp() {
167170
final DebugDataDumper manager = new DebugDataDumper(Path.of("."), true);
168-
final String formattedTimestamp = manager.formatTimestamp(Optional.empty());
169-
assertThat(formattedTimestamp).isEqualTo("unknown");
171+
final String formattedTimestamp = manager.generateTimestamp(timeProvider);
172+
assertThat(formattedTimestamp).isEqualTo("1970-01-01T12:46:40.00");
170173
}
171174

172175
private void checkBytesSavedToFile(final Path path, final Bytes expectedBytes) {

Diff for: networking/eth2/src/main/java/tech/pegasys/teku/networking/eth2/gossip/topics/topichandlers/Eth2TopicHandler.java

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ protected ValidationResult handleMessageProcessingError(
170170
final PreparedGossipMessage message, final Throwable err) {
171171
final ValidationResult response;
172172
if (ExceptionUtil.hasCause(err, DecodingException.class)) {
173+
173174
debugDataDumper.saveGossipMessageDecodingError(
174175
getTopic(), message.getArrivalTimestamp(), message.getOriginalMessage());
175176
P2P_LOG.onGossipMessageDecodingError(getTopic(), message.getOriginalMessage(), err);

0 commit comments

Comments
 (0)