Skip to content

Commit

Permalink
A lot of finals + getting error message refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Sep 20, 2023
1 parent 039ef31 commit 3b5f701
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void shouldReturnOk() throws IOException {
dataStructureUtil.randomSignedValidatorRegistrations(10);
when(validatorApiChannel.registerValidators(request)).thenReturn(SafeFuture.COMPLETE);

try (Response response =
try (final Response response =
post(
PostRegisterValidator.ROUTE,
JsonUtil.serialize(
Expand All @@ -88,7 +88,7 @@ void shouldReturnServerErrorWhenThereIsAnExceptionWhileRegistering() throws IOEx
when(validatorApiChannel.registerValidators(request))
.thenReturn(SafeFuture.failedFuture(new IllegalStateException("oopsy")));

try (Response response =
try (final Response response =
post(
PostRegisterValidator.ROUTE,
JsonUtil.serialize(
Expand All @@ -106,7 +106,7 @@ void shouldReturnServerErrorWhenThereIsAnExceptionWhileGettingStatuses() throws
when(validatorApiChannel.getValidatorStatuses(anyCollection()))
.thenReturn(SafeFuture.failedFuture(new IllegalStateException("oopsy")));

try (Response response =
try (final Response response =
post(
PostRegisterValidator.ROUTE,
JsonUtil.serialize(
Expand All @@ -121,7 +121,7 @@ void shouldReturnServerErrorWhenThereIsAnExceptionWhileGettingStatuses() throws
@ValueSource(strings = {"[{}]", "{}", "invalid"})
void shouldReturnBadRequest(final String body) throws IOException {
when(validatorApiChannel.registerValidators(any())).thenReturn(SafeFuture.COMPLETE);
try (Response response = post(PostRegisterValidator.ROUTE, body)) {
try (final Response response = post(PostRegisterValidator.ROUTE, body)) {
assertThat(response.code()).isEqualTo(SC_BAD_REQUEST);
}
verifyNoInteractions(validatorApiChannel);
Expand All @@ -130,7 +130,7 @@ void shouldReturnBadRequest(final String body) throws IOException {
@Test
void shouldHandleEmptyRequest() throws IOException {
when(validatorApiChannel.registerValidators(any())).thenReturn(SafeFuture.COMPLETE);
try (Response response = post(PostRegisterValidator.ROUTE, "[]")) {
try (final Response response = post(PostRegisterValidator.ROUTE, "[]")) {
assertThat(response.code()).isEqualTo(SC_OK);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Optional;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.ValidatorDataProvider;
import tech.pegasys.teku.infrastructure.exceptions.ExceptionUtil;
import tech.pegasys.teku.infrastructure.restapi.endpoints.AsyncApiResponse;
import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
Expand Down Expand Up @@ -69,11 +70,8 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
.handle(
(__, error) -> {
if (error != null) {
final String message =
error.getCause() == null
? error.getMessage()
: error.getCause().getMessage();
return AsyncApiResponse.respondWithError(SC_INTERNAL_SERVER_ERROR, message);
return AsyncApiResponse.respondWithError(
SC_INTERNAL_SERVER_ERROR, ExceptionUtil.getRootCauseMessage(error));
}
return AsyncApiResponse.respondOk(null);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public SafeFuture<Optional<Attestation>> createAggregate(
}

public SafeFuture<List<SubmitDataError>> sendAggregateAndProofs(
List<SignedAggregateAndProof> aggregateAndProofs) {
final List<SignedAggregateAndProof> aggregateAndProofs) {
return validatorApiChannel.sendAggregateAndProofs(aggregateAndProofs);
}

Expand Down Expand Up @@ -295,12 +295,12 @@ public SafeFuture<Void> sendContributionAndProofs(
}

public SafeFuture<Void> prepareBeaconProposer(
List<BeaconPreparableProposer> beaconPreparableProposers) {
final List<BeaconPreparableProposer> beaconPreparableProposers) {
return validatorApiChannel.prepareBeaconProposer(beaconPreparableProposers);
}

public SafeFuture<Void> registerValidators(
SszList<SignedValidatorRegistration> validatorRegistrations) {
final SszList<SignedValidatorRegistration> validatorRegistrations) {
return validatorApiChannel
.getValidatorStatuses(
validatorRegistrations.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public static ValidatorClientService create(
proposerConfigManager = Optional.empty();
}

ValidatorClientService validatorClientService =
final ValidatorClientService validatorClientService =
new ValidatorClientService(
eventChannels,
validatorLoader,
Expand Down Expand Up @@ -273,12 +273,12 @@ public static ValidatorClientService create(
}

private void initializeDoppelgangerDetector(
AsyncRunner asyncRunner,
ValidatorApiChannel validatorApiChannel,
Spec spec,
TimeProvider timeProvider,
GenesisDataProvider genesisDataProvider) {
DoppelgangerDetector doppelgangerDetector =
final AsyncRunner asyncRunner,
final ValidatorApiChannel validatorApiChannel,
final Spec spec,
final TimeProvider timeProvider,
final GenesisDataProvider genesisDataProvider) {
final DoppelgangerDetector doppelgangerDetector =
new DoppelgangerDetector(
asyncRunner,
validatorApiChannel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public class ValidatorStatusLogger implements ValidatorStatusesChannel {
final AtomicReference<Map<BLSPublicKey, ValidatorStatus>> latestValidatorStatuses =
new AtomicReference<>();

public ValidatorStatusLogger(OwnedValidators validators) {
public ValidatorStatusLogger(final OwnedValidators validators) {
this.validators = validators;
}

@Override
public void onNewValidatorStatuses(
final Map<BLSPublicKey, ValidatorStatus> newValidatorStatuses) {
Map<BLSPublicKey, ValidatorStatus> oldValidatorStatuses =
final Map<BLSPublicKey, ValidatorStatus> oldValidatorStatuses =
latestValidatorStatuses.getAndSet(newValidatorStatuses);
// first run
if (oldValidatorStatuses == null) {
Expand All @@ -51,10 +51,10 @@ public void onNewValidatorStatuses(
}

// updates
for (Map.Entry<BLSPublicKey, ValidatorStatus> entry : newValidatorStatuses.entrySet()) {
BLSPublicKey key = entry.getKey();
ValidatorStatus newStatus = entry.getValue();
ValidatorStatus oldStatus = oldValidatorStatuses.get(key);
for (final Map.Entry<BLSPublicKey, ValidatorStatus> entry : newValidatorStatuses.entrySet()) {
final BLSPublicKey key = entry.getKey();
final ValidatorStatus newStatus = entry.getValue();
final ValidatorStatus oldStatus = oldValidatorStatuses.get(key);

// report the status of a new validator
if (oldStatus == null) {
Expand All @@ -70,9 +70,9 @@ public void onNewValidatorStatuses(
}

private void printValidatorStatusesOneByOne(
Map<BLSPublicKey, ValidatorStatus> validatorStatuses) {
for (BLSPublicKey publicKey : validators.getPublicKeys()) {
Optional<ValidatorStatus> maybeValidatorStatus =
final Map<BLSPublicKey, ValidatorStatus> validatorStatuses) {
for (final BLSPublicKey publicKey : validators.getPublicKeys()) {
final Optional<ValidatorStatus> maybeValidatorStatus =
Optional.ofNullable(validatorStatuses.get(publicKey));
maybeValidatorStatus.ifPresentOrElse(
validatorStatus ->
Expand All @@ -81,22 +81,24 @@ private void printValidatorStatusesOneByOne(
}
}

private void printValidatorStatusSummary(Map<BLSPublicKey, ValidatorStatus> validatorStatuses) {
Map<ValidatorStatus, AtomicInteger> validatorStatusCount = new HashMap<>();
private void printValidatorStatusSummary(
final Map<BLSPublicKey, ValidatorStatus> validatorStatuses) {
final Map<ValidatorStatus, AtomicInteger> validatorStatusCount = new HashMap<>();
final AtomicInteger unknownValidatorCountReference = new AtomicInteger(0);
for (BLSPublicKey publicKey : validators.getPublicKeys()) {
Optional<ValidatorStatus> maybeValidatorStatus =
for (final BLSPublicKey publicKey : validators.getPublicKeys()) {
final Optional<ValidatorStatus> maybeValidatorStatus =
Optional.ofNullable(validatorStatuses.get(publicKey));
maybeValidatorStatus.ifPresentOrElse(
status -> {
AtomicInteger count =
final AtomicInteger count =
validatorStatusCount.computeIfAbsent(status, __ -> new AtomicInteger(0));
count.incrementAndGet();
},
unknownValidatorCountReference::incrementAndGet);
}

for (Map.Entry<ValidatorStatus, AtomicInteger> statusCount : validatorStatusCount.entrySet()) {
for (final Map.Entry<ValidatorStatus, AtomicInteger> statusCount :
validatorStatusCount.entrySet()) {
STATUS_LOG.validatorStatusSummary(statusCount.getValue().get(), statusCount.getKey().name());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ValidatorRegistrationSigningServiceTest {
private DataStructureUtil dataStructureUtil;

@BeforeEach
void setUp(SpecContext specContext) {
void setUp(final SpecContext specContext) {
dataStructureUtil = specContext.getDataStructureUtil();
feeRecipient = dataStructureUtil.randomEth1Address();
gasLimit = dataStructureUtil.randomUInt64();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ValidatorRegistratorTest {

@BeforeEach
@SuppressWarnings("unchecked")
void setUp(SpecContext specContext) {
void setUp(final SpecContext specContext) {
this.specContext = specContext;
slotsPerEpoch = specContext.getSpec().getGenesisSpecConfig().getSlotsPerEpoch();
dataStructureUtil = specContext.getDataStructureUtil();
Expand Down

0 comments on commit 3b5f701

Please sign in to comment.