Skip to content

Commit 4193a81

Browse files
committed
Differentiate IO exception from not saved file
1 parent ba2311a commit 4193a81

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

validator/api/src/main/java/tech/pegasys/teku/validator/api/GraffitiManager.java

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ private Optional<String> updateGraffiti(final BLSPublicKey publicKey, final Stri
7878

7979
public Optional<Bytes32> getGraffiti(final BLSPublicKey publicKey) {
8080
final Path filePath = graffitiPath.resolve(resolveFileName(publicKey));
81+
if (!filePath.toFile().exists()) {
82+
return Optional.empty();
83+
}
84+
8185
try {
8286
return Optional.of(GraffitiParser.loadFromFile(filePath)).filter(this::graffitiNotEmpty);
8387
} catch (GraffitiLoaderException | IllegalArgumentException e) {

validator/api/src/test/java/tech/pegasys/teku/validator/api/GraffitiManagerTest.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ private void checkStoredGraffitiFile(final BLSPublicKey publicKey, final String
162162
}
163163

164164
@Test
165-
void getGraffitiFromStorage_shouldGetGraffitiFromStorage(@TempDir final Path tempDir)
166-
throws IOException {
165+
void getGraffiti_shouldGetGraffitiFromStorage(@TempDir final Path tempDir) throws IOException {
167166
dataDirLayout = new SimpleDataDirLayout(tempDir);
168167
manager = new GraffitiManager(dataDirLayout);
169168
final Path filePath = getGraffitiManagementDir().resolve(getFileName(publicKey));
@@ -174,8 +173,15 @@ void getGraffitiFromStorage_shouldGetGraffitiFromStorage(@TempDir final Path tem
174173
}
175174

176175
@Test
177-
void getGraffitiFromStorage_shouldReturnEmptyWhenFileTooBig(@TempDir final Path tempDir)
178-
throws IOException {
176+
void getGraffiti_shouldReturnEmptyWhenFileNotExist(@TempDir final Path tempDir) {
177+
dataDirLayout = new SimpleDataDirLayout(tempDir);
178+
manager = new GraffitiManager(dataDirLayout);
179+
180+
assertThat(manager.getGraffiti(publicKey)).isEmpty();
181+
}
182+
183+
@Test
184+
void getGraffiti_shouldReturnEmptyWhenFileTooBig(@TempDir final Path tempDir) throws IOException {
179185
dataDirLayout = new SimpleDataDirLayout(tempDir);
180186
manager = new GraffitiManager(dataDirLayout);
181187

@@ -188,7 +194,7 @@ void getGraffitiFromStorage_shouldReturnEmptyWhenFileTooBig(@TempDir final Path
188194

189195
@Test
190196
@DisabledOnOs(OS.WINDOWS) // Can't set permissions on Windows
191-
void getGraffitiFromStorage_shouldReturnEmptyWhenNotReadableFile(@TempDir final Path tempDir)
197+
void getGraffiti_shouldReturnEmptyWhenNotReadableFile(@TempDir final Path tempDir)
192198
throws IOException {
193199
dataDirLayout = new SimpleDataDirLayout(tempDir);
194200
manager = new GraffitiManager(dataDirLayout);
@@ -200,8 +206,7 @@ void getGraffitiFromStorage_shouldReturnEmptyWhenNotReadableFile(@TempDir final
200206
}
201207

202208
@Test
203-
void getGraffitiFromStorage_shouldReturnEmptyWhenFileEmpty(@TempDir final Path tempDir)
204-
throws IOException {
209+
void getGraffiti_shouldReturnEmptyWhenFileEmpty(@TempDir final Path tempDir) throws IOException {
205210
dataDirLayout = new SimpleDataDirLayout(tempDir);
206211
manager = new GraffitiManager(dataDirLayout);
207212
final Path filePath = getGraffitiManagementDir().resolve(getFileName(publicKey));

0 commit comments

Comments
 (0)