Skip to content

Commit 68b5b24

Browse files
committed
Make GraffitiManagementException a RuntimeException
1 parent bf419b9 commit 68b5b24

File tree

10 files changed

+46
-53
lines changed

10 files changed

+46
-53
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
package tech.pegasys.teku.validator.api;
1515

16-
public class GraffitiManagementException extends Exception {
16+
public class GraffitiManagementException extends RuntimeException {
1717

1818
public GraffitiManagementException(final String message) {
1919
super(message);

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
package tech.pegasys.teku.validator.api;
1515

1616
import java.util.Optional;
17+
import java.util.function.Supplier;
1718
import org.apache.tuweni.bytes.Bytes32;
18-
import tech.pegasys.teku.infrastructure.async.ExceptionThrowingSupplier;
1919

2020
public class UpdatableGraffitiProvider implements GraffitiProvider {
21-
private final ExceptionThrowingSupplier<Optional<Bytes32>> storageProvider;
21+
private final Supplier<Optional<Bytes32>> storageProvider;
2222
private final GraffitiProvider defaultProvider;
2323

2424
public UpdatableGraffitiProvider(
25-
final ExceptionThrowingSupplier<Optional<Bytes32>> storageProvider,
26-
final GraffitiProvider defaultProvider) {
25+
final Supplier<Optional<Bytes32>> storageProvider, final GraffitiProvider defaultProvider) {
2726
this.storageProvider = storageProvider;
2827
this.defaultProvider = defaultProvider;
2928
}
@@ -36,12 +35,15 @@ public Optional<Bytes32> get() {
3635
private Optional<Bytes32> getFromStorage() {
3736
try {
3837
return storageProvider.get();
39-
} catch (Throwable e) {
38+
} catch (Exception e) {
4039
return Optional.empty();
4140
}
4241
}
4342

44-
public Optional<Bytes32> getUnsafe() throws Throwable {
43+
/**
44+
* @return graffiti without checking for thrown Exceptions.
45+
*/
46+
public Optional<Bytes32> getUnsafe() {
4547
return storageProvider.get().or(defaultProvider::get);
4648
}
4749
}

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

+15-17
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515

1616
import static org.assertj.core.api.Assertions.assertThat;
1717
import static org.assertj.core.api.Assertions.assertThatThrownBy;
18-
import static org.mockito.Mockito.mock;
19-
import static org.mockito.Mockito.when;
2018

2119
import java.util.Optional;
20+
import java.util.function.Supplier;
2221
import org.apache.tuweni.bytes.Bytes32;
2322
import org.junit.jupiter.api.Test;
24-
import tech.pegasys.teku.infrastructure.async.ExceptionThrowingSupplier;
2523
import tech.pegasys.teku.spec.TestSpecFactory;
2624
import tech.pegasys.teku.spec.util.DataStructureUtil;
2725

@@ -59,49 +57,49 @@ void get_shouldBeEmptyWhenBothEmpty() {
5957
}
6058

6159
@Test
62-
@SuppressWarnings("unchecked")
63-
public void get_shouldDelegateToDefaultProviderWhenStorageProviderFails() throws Throwable {
64-
final ExceptionThrowingSupplier<Optional<Bytes32>> storageProvider =
65-
mock(ExceptionThrowingSupplier.class);
66-
when(storageProvider.get()).thenThrow(new RuntimeException("Error"));
60+
public void get_shouldDelegateToDefaultProviderWhenStorageProviderFails() {
61+
final Supplier<Optional<Bytes32>> storageProvider =
62+
() -> {
63+
throw new RuntimeException("Error");
64+
};
6765

6866
provider = new UpdatableGraffitiProvider(storageProvider, () -> Optional.of(defaultGraffiti));
6967
assertThat(provider.get()).hasValue(defaultGraffiti);
7068
}
7169

7270
@Test
73-
void getWithThrowable_shouldGetStorageGraffitiWhenAvailable() throws Throwable {
71+
void getWithThrowable_shouldGetStorageGraffitiWhenAvailable() {
7472
provider = new UpdatableGraffitiProvider(() -> Optional.of(storageGraffiti), Optional::empty);
7573
assertThat(provider.getUnsafe()).hasValue(storageGraffiti);
7674
}
7775

7876
@Test
79-
void getWithThrowable_shouldGetStorageGraffitiWhenBothAvailable() throws Throwable {
77+
void getWithThrowable_shouldGetStorageGraffitiWhenBothAvailable() {
8078
provider =
8179
new UpdatableGraffitiProvider(
8280
() -> Optional.of(storageGraffiti), () -> Optional.of(defaultGraffiti));
8381
assertThat(provider.getUnsafe()).hasValue(storageGraffiti);
8482
}
8583

8684
@Test
87-
void getWithThrowable_shouldGetDefaultGraffitiWhenStorageEmpty() throws Throwable {
85+
void getWithThrowable_shouldGetDefaultGraffitiWhenStorageEmpty() {
8886
provider = new UpdatableGraffitiProvider(Optional::empty, () -> Optional.of(defaultGraffiti));
8987
assertThat(provider.getUnsafe()).hasValue(defaultGraffiti);
9088
}
9189

9290
@Test
93-
void getWithThrowable_shouldBeEmptyWhenBothEmpty() throws Throwable {
91+
void getWithThrowable_shouldBeEmptyWhenBothEmpty() {
9492
provider = new UpdatableGraffitiProvider(Optional::empty, Optional::empty);
9593
assertThat(provider.getUnsafe()).isEmpty();
9694
}
9795

9896
@Test
99-
@SuppressWarnings("unchecked")
100-
public void getWithThrowable_shouldThrowExceptionWhenStorageProviderFails() throws Throwable {
97+
public void getWithThrowable_shouldThrowExceptionWhenStorageProviderFails() {
10198
final RuntimeException exception = new RuntimeException("Error");
102-
final ExceptionThrowingSupplier<Optional<Bytes32>> storageProvider =
103-
mock(ExceptionThrowingSupplier.class);
104-
when(storageProvider.get()).thenThrow(exception);
99+
final Supplier<Optional<Bytes32>> storageProvider =
100+
() -> {
101+
throw exception;
102+
};
105103

106104
provider = new UpdatableGraffitiProvider(storageProvider, () -> Optional.of(defaultGraffiti));
107105
assertThatThrownBy(() -> provider.getUnsafe()).isEqualTo(exception);

validator/client/src/main/java/tech/pegasys/teku/validator/client/ValidatorClientService.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import java.util.List;
2424
import java.util.Optional;
2525
import java.util.Random;
26+
import java.util.function.Function;
2627
import java.util.function.Supplier;
2728
import org.apache.logging.log4j.LogManager;
2829
import org.apache.logging.log4j.Logger;
2930
import org.apache.tuweni.bytes.Bytes32;
3031
import org.hyperledger.besu.plugin.services.MetricsSystem;
3132
import tech.pegasys.teku.bls.BLSPublicKey;
3233
import tech.pegasys.teku.infrastructure.async.AsyncRunner;
33-
import tech.pegasys.teku.infrastructure.async.ExceptionThrowingFunction;
3434
import tech.pegasys.teku.infrastructure.async.SafeFuture;
3535
import tech.pegasys.teku.infrastructure.events.EventChannels;
3636
import tech.pegasys.teku.infrastructure.exceptions.ExceptionUtil;
@@ -163,14 +163,8 @@ public static ValidatorClientService create(
163163
validatorApiConfig.isRestApiEnabled()
164164
? new GraffitiManager(services.getDataDirLayout())
165165
: null);
166-
final ExceptionThrowingFunction<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider =
167-
(publicKey) -> {
168-
if (graffitiManager.isPresent()) {
169-
final GraffitiManager manager = graffitiManager.get();
170-
return manager.getGraffiti(publicKey);
171-
}
172-
return Optional.empty();
173-
};
166+
final Function<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider =
167+
(publicKey) -> graffitiManager.flatMap(manager -> manager.getGraffiti(publicKey));
174168

175169
final ValidatorLoader validatorLoader =
176170
createValidatorLoader(services, config, asyncRunner, updatableGraffitiProvider);
@@ -414,7 +408,7 @@ private static ValidatorLoader createValidatorLoader(
414408
final ServiceConfig services,
415409
final ValidatorClientConfiguration config,
416410
final AsyncRunner asyncRunner,
417-
final ExceptionThrowingFunction<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider) {
411+
final Function<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider) {
418412
final Path slashingProtectionPath = getSlashingProtectionPath(services.getDataDirLayout());
419413
final SlashingProtector slashingProtector =
420414
config.getValidatorConfig().isLocalSlashingProtectionSynchronizedModeEnabled()

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import java.util.concurrent.Executors;
2525
import java.util.concurrent.Future;
2626
import java.util.concurrent.atomic.AtomicInteger;
27+
import java.util.function.Function;
2728
import org.apache.tuweni.bytes.Bytes32;
2829
import tech.pegasys.teku.bls.BLSPublicKey;
29-
import tech.pegasys.teku.infrastructure.async.ExceptionThrowingFunction;
3030
import tech.pegasys.teku.service.serviceutils.layout.DataDirLayout;
3131
import tech.pegasys.teku.spec.signatures.DeletableSigner;
3232
import tech.pegasys.teku.validator.api.GraffitiProvider;
@@ -49,7 +49,7 @@ public static void loadValidators(
4949
final OwnedValidators ownedValidators,
5050
final Map<BLSPublicKey, ValidatorProvider> providers,
5151
final GraffitiProvider defaultGraffitiProvider,
52-
final ExceptionThrowingFunction<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider,
52+
final Function<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider,
5353
final Optional<DataDirLayout> maybeDataDirLayout) {
5454
final int totalValidatorCount = providers.size();
5555
STATUS_LOG.loadingValidators(totalValidatorCount);

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Map;
2222
import java.util.Optional;
23+
import java.util.function.Function;
2324
import java.util.function.Supplier;
2425
import org.apache.logging.log4j.LogManager;
2526
import org.apache.logging.log4j.Logger;
@@ -30,7 +31,6 @@
3031
import tech.pegasys.teku.bls.keystore.model.KeyStoreData;
3132
import tech.pegasys.teku.data.SlashingProtectionImporter;
3233
import tech.pegasys.teku.infrastructure.async.AsyncRunner;
33-
import tech.pegasys.teku.infrastructure.async.ExceptionThrowingFunction;
3434
import tech.pegasys.teku.service.serviceutils.layout.DataDirLayout;
3535
import tech.pegasys.teku.spec.Spec;
3636
import tech.pegasys.teku.spec.signatures.DeletableSigner;
@@ -55,8 +55,7 @@ public class ValidatorLoader {
5555
private final Optional<ValidatorSource> mutableExternalValidatorSource;
5656
private final OwnedValidators ownedValidators = new OwnedValidators();
5757
private final GraffitiProvider defaultGraffitiProvider;
58-
private final ExceptionThrowingFunction<BLSPublicKey, Optional<Bytes32>>
59-
updatableGraffitiProvider;
58+
private final Function<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider;
6059
private final Optional<DataDirLayout> maybeDataDirLayout;
6160
private final SlashingProtectionLogger slashingProtectionLogger;
6261

@@ -65,7 +64,7 @@ private ValidatorLoader(
6564
final Optional<ValidatorSource> mutableLocalValidatorSource,
6665
final Optional<ValidatorSource> mutableExternalValidatorSource,
6766
final GraffitiProvider defaultGraffitiProvider,
68-
final ExceptionThrowingFunction<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider,
67+
final Function<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider,
6968
final Optional<DataDirLayout> maybeDataDirLayout,
7069
final SlashingProtectionLogger slashingProtectionLogger) {
7170
this.validatorSources = validatorSources;
@@ -242,7 +241,7 @@ public static ValidatorLoader create(
242241
final AsyncRunner asyncRunner,
243242
final MetricsSystem metricsSystem,
244243
final Optional<DataDirLayout> maybeMutableDir,
245-
final ExceptionThrowingFunction<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider) {
244+
final Function<BLSPublicKey, Optional<Bytes32>> updatableGraffitiProvider) {
246245
final ValidatorSourceFactory validatorSources =
247246
new ValidatorSourceFactory(
248247
spec,

validator/client/src/main/java/tech/pegasys/teku/validator/client/restapi/apis/GetGraffiti.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
3535
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
3636
import tech.pegasys.teku.validator.api.Bytes32Parser;
37+
import tech.pegasys.teku.validator.api.GraffitiManagementException;
3738
import tech.pegasys.teku.validator.api.UpdatableGraffitiProvider;
3839
import tech.pegasys.teku.validator.client.KeyManager;
3940
import tech.pegasys.teku.validator.client.Validator;
@@ -94,7 +95,7 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
9495
final UpdatableGraffitiProvider provider =
9596
(UpdatableGraffitiProvider) maybeValidator.get().getGraffitiProvider();
9697
request.respondOk(new GraffitiResponse(publicKey, provider.getUnsafe()));
97-
} catch (Throwable e) {
98+
} catch (GraffitiManagementException e) {
9899
request.respondError(SC_INTERNAL_SERVER_ERROR, e.getMessage());
99100
}
100101
}

validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/DeleteGraffitiTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class DeleteGraffitiTest {
6060
.build();
6161

6262
@Test
63-
void shouldSuccessfullyDeleteGraffiti() throws IOException, GraffitiManagementException {
63+
void shouldSuccessfullyDeleteGraffiti() throws JsonProcessingException {
6464
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
6565
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.of(validator));
6666

@@ -89,7 +89,7 @@ void shouldReturnErrorWhenIssueDeletingGraffiti()
8989
}
9090

9191
@Test
92-
void shouldRespondNotFoundWhenNoValidator() throws IOException {
92+
void shouldRespondNotFoundWhenNoValidator() throws JsonProcessingException {
9393
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.empty());
9494

9595
handler.handleRequest(request);

validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/GetGraffitiTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ class GetGraffitiTest {
6262
.build();
6363

6464
@Test
65-
void shouldGetGraffiti() throws Throwable {
65+
void shouldGetGraffiti() throws JsonProcessingException {
6666
checkGraffiti(Optional.of(bytesGraffiti));
6767
}
6868

6969
@Test
70-
void shouldGetEmptyGraffiti() throws Throwable {
70+
void shouldGetEmptyGraffiti() throws JsonProcessingException {
7171
checkGraffiti(Optional.empty());
7272
}
7373

7474
@Test
75-
void shouldHandleGraffitiManagementException() throws Throwable {
75+
void shouldHandleGraffitiManagementException() throws JsonProcessingException {
7676
final GraffitiManagementException exception =
7777
new GraffitiManagementException("Unable to retrieve graffiti from storage");
7878
final UpdatableGraffitiProvider provider = mock(UpdatableGraffitiProvider.class);
@@ -131,7 +131,7 @@ void metadata_shouldHandle500() throws JsonProcessingException {
131131
verifyMetadataErrorResponse(handler, SC_INTERNAL_SERVER_ERROR);
132132
}
133133

134-
private void checkGraffiti(final Optional<Bytes32> graffiti) throws Throwable {
134+
private void checkGraffiti(final Optional<Bytes32> graffiti) throws JsonProcessingException {
135135
final UpdatableGraffitiProvider provider = mock(UpdatableGraffitiProvider.class);
136136
when(provider.getUnsafe()).thenReturn(graffiti);
137137
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, provider);

validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetGraffitiTest.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import static tech.pegasys.teku.spec.generator.signatures.NoOpLocalSigner.NO_OP_SIGNER;
3434

3535
import com.fasterxml.jackson.core.JsonProcessingException;
36-
import java.io.IOException;
3736
import java.util.Optional;
3837
import org.junit.jupiter.api.Test;
3938
import tech.pegasys.teku.bls.BLSPublicKey;
@@ -62,7 +61,7 @@ class SetGraffitiTest {
6261
.build();
6362

6463
@Test
65-
void shouldSuccessfullySetGraffiti() throws IOException, GraffitiManagementException {
64+
void shouldSuccessfullySetGraffiti() throws JsonProcessingException {
6665
request.setRequestBody(graffiti);
6766

6867
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
@@ -76,7 +75,7 @@ void shouldSuccessfullySetGraffiti() throws IOException, GraffitiManagementExcep
7675
}
7776

7877
@Test
79-
void shouldReturnErrorWhenIssueSettingGraffiti() throws IOException, GraffitiManagementException {
78+
void shouldReturnErrorWhenIssueSettingGraffiti() throws JsonProcessingException {
8079
final GraffitiManagementException exception =
8180
new GraffitiManagementException("Unable to update graffiti for validator " + publicKey);
8281
request.setRequestBody(graffiti);
@@ -94,7 +93,7 @@ void shouldReturnErrorWhenIssueSettingGraffiti() throws IOException, GraffitiMan
9493
}
9594

9695
@Test
97-
void shouldThrowExceptionWhenInvalidGraffitiInput() throws GraffitiManagementException {
96+
void shouldThrowExceptionWhenInvalidGraffitiInput() {
9897
final String invalidGraffiti = "This graffiti is a bit too long!!";
9998
final String errorMessage =
10099
String.format(
@@ -114,7 +113,7 @@ void shouldThrowExceptionWhenInvalidGraffitiInput() throws GraffitiManagementExc
114113
}
115114

116115
@Test
117-
void shouldRespondNotFoundWhenNoValidator() throws IOException {
116+
void shouldRespondNotFoundWhenNoValidator() throws JsonProcessingException {
118117
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.empty());
119118

120119
handler.handleRequest(request);

0 commit comments

Comments
 (0)