Skip to content

Commit 7837c7c

Browse files
committed
Refactor ExternalUrlKeyReader
1 parent 4770e62 commit 7837c7c

File tree

2 files changed

+28
-38
lines changed

2 files changed

+28
-38
lines changed

validator/client/src/main/java/tech/pegasys/teku/validator/client/loader/ExternalUrlKeyReader.java

+24-31
Original file line numberDiff line numberDiff line change
@@ -33,55 +33,48 @@ public class ExternalUrlKeyReader {
3333
private static final Duration FIXED_DELAY = Duration.ofSeconds(5);
3434
private static final int MAX_RETRIES = 59;
3535

36-
private final String url;
36+
private final URL url;
3737
private final ObjectMapper mapper;
3838
private final AsyncRunner asyncRunner;
3939

4040
public ExternalUrlKeyReader(
41-
final String url, final ObjectMapper mapper, final AsyncRunner asyncRunner) {
42-
this.url = url;
41+
final String urlString, final ObjectMapper mapper, final AsyncRunner asyncRunner) {
42+
this.url = getUrl(urlString);
4343
this.mapper = mapper;
4444
this.asyncRunner = asyncRunner;
4545
}
4646

4747
public Stream<BLSPublicKey> readKeys() {
48-
final String[] keys =
49-
readUrl()
50-
.exceptionallyCompose(
51-
ex -> {
52-
if (ex instanceof MalformedURLException) {
53-
return SafeFuture.failedFuture(
54-
new InvalidConfigurationException(
55-
"Failed to load public keys from invalid URL: " + url, ex));
56-
}
57-
return retry();
58-
})
59-
.join();
48+
final String[] keys = getKeysWithRetry().join();
6049
return Arrays.stream(keys).map(key -> BLSPublicKey.fromSSZBytes(Bytes.fromHexString(key)));
6150
}
6251

63-
private SafeFuture<String[]> readUrl() {
64-
try {
65-
return SafeFuture.completedFuture(mapper.readValue(new URL(url), String[].class));
66-
} catch (IOException e) {
67-
return SafeFuture.failedFuture(e);
68-
}
69-
}
70-
7152
@VisibleForTesting
72-
SafeFuture<String[]> retry() {
53+
SafeFuture<String[]> getKeysWithRetry() {
7354
return asyncRunner
74-
.runWithRetry(
75-
() -> {
76-
STATUS_LOG.failedToLoadPublicKeysFromUrl(url);
77-
return readUrl();
78-
},
79-
FIXED_DELAY,
80-
MAX_RETRIES)
55+
.runWithRetry(this::readUrl, FIXED_DELAY, MAX_RETRIES)
8156
.exceptionallyCompose(
8257
ex ->
8358
SafeFuture.failedFuture(
8459
new InvalidConfigurationException(
8560
"Failed to load public keys from URL: " + url, ex)));
8661
}
62+
63+
private SafeFuture<String[]> readUrl() {
64+
try {
65+
return SafeFuture.completedFuture(mapper.readValue(url, String[].class));
66+
} catch (IOException e) {
67+
STATUS_LOG.failedToLoadPublicKeysFromUrl(url.toString());
68+
return SafeFuture.failedFuture(e);
69+
}
70+
}
71+
72+
private URL getUrl(final String url) {
73+
try {
74+
return new URL(url);
75+
} catch (MalformedURLException e) {
76+
throw new InvalidConfigurationException(
77+
"Failed to load public keys from invalid URL: " + url, e);
78+
}
79+
}
8780
}

validator/client/src/test/java/tech/pegasys/teku/validator/client/loader/ExternalUrlKeyReaderTest.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,8 @@ void readKeys_validUrlReturnsInvalidKeys() throws IOException {
9191
@Test
9292
void readKeys_malformedUrlString() {
9393
final String invalidUrl = "invalid:url";
94-
final ExternalUrlKeyReader reader = new ExternalUrlKeyReader(invalidUrl, mapper, asyncRunner);
95-
96-
assertThatThrownBy(reader::readKeys)
97-
.isInstanceOf(CompletionException.class)
98-
.hasCauseInstanceOf(InvalidConfigurationException.class)
94+
assertThatThrownBy(() -> new ExternalUrlKeyReader(invalidUrl, mapper, asyncRunner))
95+
.isInstanceOf(InvalidConfigurationException.class)
9996
.hasMessageContaining("Failed to load public keys from invalid URL: " + invalidUrl)
10097
.hasRootCauseInstanceOf(MalformedURLException.class);
10198
verifyNoInteractions(mapper);
@@ -109,7 +106,7 @@ void readKeysWithRetry_unreachableUrlRetryUntilReachable() throws IOException {
109106
.thenReturn(expectedKeys);
110107
final ExternalUrlKeyReader reader = new ExternalUrlKeyReader(VALID_URL, mapper, asyncRunner);
111108

112-
final SafeFuture<String[]> keys = reader.retry();
109+
final SafeFuture<String[]> keys = reader.getKeysWithRetry();
113110
for (int i = 0; i < 3; i++) {
114111
assertThat(keys).isNotCompleted();
115112
timeProvider.advanceTimeBy(DELAY);
@@ -125,7 +122,7 @@ void readKeysWithRetry_unreachableUrlRetryUntilMaxRetries() throws IOException {
125122
when(mapper.readValue(any(URL.class), eq(String[].class))).thenThrow(exception);
126123
final ExternalUrlKeyReader reader = new ExternalUrlKeyReader(VALID_URL, mapper, asyncRunner);
127124

128-
final SafeFuture<String[]> keys = reader.retry();
125+
final SafeFuture<String[]> keys = reader.getKeysWithRetry();
129126
for (int i = 0; i < MAX_RETRIES; i++) {
130127
assertThat(keys).isNotCompleted();
131128
timeProvider.advanceTimeBy(DELAY);

0 commit comments

Comments
 (0)