Skip to content

Commit 326f496

Browse files
committed
Format date in filename
1 parent 37cf086 commit 326f496

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import java.nio.file.Files;
1919
import java.nio.file.NoSuchFileException;
2020
import java.nio.file.Path;
21+
import java.sql.Date;
22+
import java.text.DateFormat;
23+
import java.text.SimpleDateFormat;
24+
import java.util.Optional;
2125
import org.apache.logging.log4j.LogManager;
2226
import org.apache.logging.log4j.Logger;
2327
import org.apache.tuweni.bytes.Bytes;
@@ -54,23 +58,23 @@ public DebugDataDumper(final Path directory, final boolean enabled) {
5458
}
5559

5660
public void saveGossipMessageDecodingError(
57-
final String topic, final String arrivalTimestamp, final Bytes originalMessage) {
61+
final String topic, final Optional<UInt64> arrivalTimestamp, final Bytes originalMessage) {
5862
if (!enabled) {
5963
return;
6064
}
61-
final String fileName = String.format("%s.ssz", arrivalTimestamp);
65+
final String fileName = String.format("%s.ssz", formatTimestamp(arrivalTimestamp));
6266
final Path topicPath =
6367
Path.of(GOSSIP_MESSAGES_DIR).resolve(DECODING_ERROR_SUB_DIR).resolve(topic);
6468
saveBytesToFile(
6569
"gossip message with decoding error", topicPath.resolve(fileName), originalMessage);
6670
}
6771

6872
public void saveGossipRejectedMessageToFile(
69-
final String topic, final String arrivalTimestamp, final Bytes decodedMessage) {
73+
final String topic, final Optional<UInt64> arrivalTimestamp, final Bytes decodedMessage) {
7074
if (!enabled) {
7175
return;
7276
}
73-
final String fileName = String.format("%s.ssz", arrivalTimestamp);
77+
final String fileName = String.format("%s.ssz", formatTimestamp(arrivalTimestamp));
7478
final Path topicPath = Path.of(GOSSIP_MESSAGES_DIR).resolve(REJECTED_SUB_DIR).resolve(topic);
7579
saveBytesToFile("rejected gossip message", topicPath.resolve(fileName), decodedMessage);
7680
}
@@ -135,6 +139,17 @@ private void createDirectory(final Path path, final String directoryName, final
135139
}
136140
}
137141

142+
@VisibleForTesting
143+
protected static String formatTimestamp(final Optional<UInt64> arrivalTimestamp) {
144+
if (arrivalTimestamp.isEmpty()) {
145+
return "unknown";
146+
}
147+
148+
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS");
149+
Date date = new Date(arrivalTimestamp.get().longValue());
150+
return df.format(date);
151+
}
152+
138153
@VisibleForTesting
139154
protected boolean isEnabled() {
140155
return enabled;

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package tech.pegasys.teku.statetransition.util.noop;
1515

1616
import java.nio.file.Path;
17+
import java.util.Optional;
1718
import org.apache.tuweni.bytes.Bytes;
1819
import org.apache.tuweni.bytes.Bytes32;
1920
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
@@ -27,11 +28,11 @@ public NoOpDebugDataDumper() {
2728

2829
@Override
2930
public void saveGossipMessageDecodingError(
30-
final String topic, final String arrivalTimestamp, final Bytes originalMessage) {}
31+
final String topic, final Optional<UInt64> arrivalTimestamp, final Bytes originalMessage) {}
3132

3233
@Override
3334
public void saveGossipRejectedMessageToFile(
34-
final String topic, final String arrivalTimestamp, final Bytes decodedMessage) {}
35+
final String topic, final Optional<UInt64> arrivalTimestamp, final Bytes decodedMessage) {}
3536

3637
@Override
3738
public void saveInvalidBlockToFile(

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

+25-7
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import java.nio.file.Files;
2323
import java.nio.file.NoSuchFileException;
2424
import java.nio.file.Path;
25+
import java.util.Optional;
2526
import org.apache.tuweni.bytes.Bytes;
2627
import org.junit.jupiter.api.Test;
2728
import org.junit.jupiter.api.condition.DisabledOnOs;
2829
import org.junit.jupiter.api.condition.OS;
2930
import org.junit.jupiter.api.io.TempDir;
3031
import tech.pegasys.teku.infrastructure.time.StubTimeProvider;
32+
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
3133
import tech.pegasys.teku.spec.TestSpecFactory;
3234
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
3335
import tech.pegasys.teku.spec.util.DataStructureUtil;
@@ -41,11 +43,12 @@ class DebugDataDumperTest {
4143
void saveGossipMessageDecodingError_shouldSaveToFile(@TempDir Path tempDir) {
4244
final DebugDataDumper manager = new DebugDataDumper(tempDir, true);
4345
final Bytes messageBytes = dataStructureUtil.stateBuilderPhase0().build().sszSerialize();
44-
final String arrivalTimestamp = timeProvider.getTimeInMillis().toString();
46+
final Optional<UInt64> arrivalTimestamp = Optional.of(timeProvider.getTimeInMillis());
4547
final String topic = "test_topic";
4648
manager.saveGossipMessageDecodingError("test_topic", arrivalTimestamp, messageBytes);
4749

48-
final String fileName = String.format("%s.ssz", arrivalTimestamp);
50+
final String fileName =
51+
String.format("%s.ssz", DebugDataDumper.formatTimestamp(arrivalTimestamp));
4952
final Path expectedFile =
5053
tempDir
5154
.resolve("gossip_messages")
@@ -59,11 +62,12 @@ void saveGossipMessageDecodingError_shouldSaveToFile(@TempDir Path tempDir) {
5962
void saveGossipMessageDecodingError_shouldNotSaveToFileWhenDisabled(@TempDir Path tempDir) {
6063
final DebugDataDumper manager = new DebugDataDumper(tempDir, false);
6164
final Bytes messageBytes = dataStructureUtil.stateBuilderPhase0().build().sszSerialize();
62-
final String arrivalTimestamp = timeProvider.getTimeInMillis().toString();
65+
final Optional<UInt64> arrivalTimestamp = Optional.of(timeProvider.getTimeInMillis());
6366
manager.saveGossipMessageDecodingError("test_topic", arrivalTimestamp, messageBytes);
6467
assertThat(manager.isEnabled()).isFalse();
6568

66-
final String fileName = String.format("%s.ssz", arrivalTimestamp);
69+
final String fileName =
70+
String.format("%s.ssz", DebugDataDumper.formatTimestamp(arrivalTimestamp));
6771
final Path expectedFile =
6872
tempDir.resolve("gossip_messages").resolve("decoding_error").resolve(fileName);
6973
checkFileNotExist(expectedFile);
@@ -73,11 +77,12 @@ void saveGossipMessageDecodingError_shouldNotSaveToFileWhenDisabled(@TempDir Pat
7377
void saveGossipRejectedMessageToFile_shouldSaveToFile(@TempDir Path tempDir) {
7478
final DebugDataDumper manager = new DebugDataDumper(tempDir, true);
7579
final Bytes messageBytes = dataStructureUtil.stateBuilderPhase0().build().sszSerialize();
76-
final String arrivalTimestamp = timeProvider.getTimeInMillis().toString();
80+
final Optional<UInt64> arrivalTimestamp = Optional.of(timeProvider.getTimeInMillis());
7781
final String topic = "test_topic";
7882
manager.saveGossipRejectedMessageToFile("test_topic", arrivalTimestamp, messageBytes);
7983

80-
final String fileName = String.format("%s.ssz", arrivalTimestamp);
84+
final String fileName =
85+
String.format("%s.ssz", DebugDataDumper.formatTimestamp(arrivalTimestamp));
8186
final Path expectedFile =
8287
tempDir.resolve("gossip_messages").resolve("rejected").resolve(topic).resolve(fileName);
8388
checkBytesSavedToFile(expectedFile, messageBytes);
@@ -87,7 +92,7 @@ void saveGossipRejectedMessageToFile_shouldSaveToFile(@TempDir Path tempDir) {
8792
void saveGossipRejectedMessageToFile_shouldNotSaveToFileWhenDisabled(@TempDir Path tempDir) {
8893
final DebugDataDumper manager = new DebugDataDumper(tempDir, false);
8994
final Bytes messageBytes = dataStructureUtil.stateBuilderPhase0().build().sszSerialize();
90-
final String arrivalTimestamp = timeProvider.getTimeInMillis().toString();
95+
final Optional<UInt64> arrivalTimestamp = Optional.of(timeProvider.getTimeInMillis());
9196
manager.saveGossipRejectedMessageToFile("test_topic", arrivalTimestamp, messageBytes);
9297
assertThat(manager.isEnabled()).isFalse();
9398

@@ -150,6 +155,19 @@ void constructionOfDirectories_shouldDisableWhenFailedToCreate(@TempDir Path tem
150155
assertThat(manager.isEnabled()).isFalse();
151156
}
152157

158+
@Test
159+
void formatTimestamp_shouldFormatDate() {
160+
final String formattedTimestamp =
161+
DebugDataDumper.formatTimestamp(Optional.of(dataStructureUtil.randomUInt64()));
162+
assertThat(formattedTimestamp).isEqualTo("147882977-02-28T19:22:42.956");
163+
}
164+
165+
@Test
166+
void formatTimestamp_shouldReturnConsistentUnknown() {
167+
final String formattedTimestamp = DebugDataDumper.formatTimestamp(Optional.empty());
168+
assertThat(formattedTimestamp).isEqualTo("unknown");
169+
}
170+
153171
private void checkBytesSavedToFile(final Path path, final Bytes expectedBytes) {
154172
try {
155173
final Bytes bytes = Bytes.wrap(Files.readAllBytes(path));

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private void processMessage(
137137
case REJECT:
138138
debugDataDumper.saveGossipRejectedMessageToFile(
139139
getTopic(),
140-
message.getArrivalTimestamp().map(UInt64::toString).orElse("unknown"),
140+
message.getArrivalTimestamp(),
141141
message.getDecodedMessage().getDecodedMessage().orElse(Bytes.EMPTY));
142142
P2P_LOG.onGossipRejected(
143143
getTopic(),
@@ -171,9 +171,7 @@ protected ValidationResult handleMessageProcessingError(
171171
final ValidationResult response;
172172
if (ExceptionUtil.hasCause(err, DecodingException.class)) {
173173
debugDataDumper.saveGossipMessageDecodingError(
174-
getTopic(),
175-
message.getArrivalTimestamp().map(UInt64::toString).orElse("unknown"),
176-
message.getOriginalMessage());
174+
getTopic(), message.getArrivalTimestamp(), message.getOriginalMessage());
177175
P2P_LOG.onGossipMessageDecodingError(getTopic(), message.getOriginalMessage(), err);
178176
response = ValidationResult.Invalid;
179177
} else if (ExceptionUtil.hasCause(err, RejectedExecutionException.class)) {

0 commit comments

Comments
 (0)