Skip to content

Commit

Permalink
Create new GraffitiManagementException
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh committed Apr 22, 2024
1 parent 05fad61 commit 00536fb
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Consensys Software Inc., 2024
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.validator.api;

import java.io.IOException;

public class GraffitiManagementException extends IOException {

public GraffitiManagementException(final String message) {
super(message);
}

public GraffitiManagementException(final String message, final Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public GraffitiManager(final Path directory) {
}
}

public void setGraffiti(final BLSPublicKey publicKey, final String graffiti) throws IOException {
public void setGraffiti(final BLSPublicKey publicKey, final String graffiti)
throws GraffitiManagementException {
final String strippedGraffiti = graffiti.strip();
final int graffitiSize = strippedGraffiti.getBytes(StandardCharsets.UTF_8).length;
if (graffitiSize > 32) {
Expand All @@ -50,17 +51,27 @@ public void setGraffiti(final BLSPublicKey publicKey, final String graffiti) thr
strippedGraffiti, graffitiSize));
}

final Path file = directory.resolve(resolveFileName(publicKey));
Files.writeString(file, strippedGraffiti);
try {
final Path file = directory.resolve(resolveFileName(publicKey));
Files.writeString(file, strippedGraffiti);
} catch (IOException e) {
throw new GraffitiManagementException(
"Unable to update graffiti for validator " + publicKey, e);
}
}

public void deleteGraffiti(final BLSPublicKey publicKey) throws IOException {
public void deleteGraffiti(final BLSPublicKey publicKey) throws GraffitiManagementException {
final Path file = directory.resolve(resolveFileName(publicKey));
if (!file.toFile().exists()) {
return;
}

Files.delete(file);
try {
Files.delete(file);
} catch (IOException e) {
throw new GraffitiManagementException(
"Unable to delete graffiti for validator " + publicKey, e);
}
}

public Optional<Bytes32> getGraffiti(final BLSPublicKey publicKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ void deleteGraffiti_shouldReturnErrorMessageWhenUnableToDeleteFile(@TempDir fina
assertThat(file.createNewFile()).isTrue();
assertThat(file.getParentFile().setWritable(false)).isTrue();

assertThatThrownBy(() -> manager.deleteGraffiti(publicKey)).isInstanceOf(IOException.class);
assertThatThrownBy(() -> manager.deleteGraffiti(publicKey))
.isInstanceOf(GraffitiManagementException.class);
assertThat(file.exists()).isTrue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import static tech.pegasys.teku.validator.client.restapi.ValidatorTypes.PARAM_PUBKEY_TYPE;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.util.Optional;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
import tech.pegasys.teku.validator.api.GraffitiManagementException;
import tech.pegasys.teku.validator.api.GraffitiManager;
import tech.pegasys.teku.validator.client.KeyManager;
import tech.pegasys.teku.validator.client.Validator;
Expand Down Expand Up @@ -66,9 +66,8 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
try {
graffitiManager.deleteGraffiti(publicKey);
request.respondWithCode(SC_NO_CONTENT);
} catch (IOException e) {
request.respondError(
SC_INTERNAL_SERVER_ERROR, "Unable to delete graffiti for validator " + publicKey);
} catch (GraffitiManagementException e) {
request.respondError(SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import static tech.pegasys.teku.validator.client.restapi.ValidatorTypes.PARAM_PUBKEY_TYPE;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Function;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition;
import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
import tech.pegasys.teku.validator.api.GraffitiManagementException;
import tech.pegasys.teku.validator.api.GraffitiManager;
import tech.pegasys.teku.validator.client.KeyManager;
import tech.pegasys.teku.validator.client.Validator;
Expand Down Expand Up @@ -81,9 +81,8 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
try {
graffitiManager.setGraffiti(publicKey, graffiti);
request.respondWithCode(SC_NO_CONTENT);
} catch (IOException e) {
request.respondError(
SC_INTERNAL_SERVER_ERROR, "Unable to update graffiti for validator " + publicKey);
} catch (GraffitiManagementException e) {
request.respondError(SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import tech.pegasys.teku.infrastructure.restapi.StubRestApiRequest;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.util.DataStructureUtil;
import tech.pegasys.teku.validator.api.GraffitiManagementException;
import tech.pegasys.teku.validator.api.GraffitiManager;
import tech.pegasys.teku.validator.client.OwnedKeyManager;
import tech.pegasys.teku.validator.client.Validator;
Expand Down Expand Up @@ -71,19 +72,20 @@ void shouldSuccessfullyDeleteGraffiti() throws IOException {
}

@Test
void shouldReturnErrorWhenIssueDeleting() throws IOException {
void shouldReturnErrorWhenIssueDeletingGraffiti() throws IOException {
final String errorMessage = "Unable to delete graffiti for validator " + publicKey;
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.of(validator));
doThrow(IOException.class).when(graffitiManager).deleteGraffiti(any());
doThrow(new GraffitiManagementException(errorMessage))
.when(graffitiManager)
.deleteGraffiti(any());

handler.handleRequest(request);

verify(graffitiManager).deleteGraffiti(eq(publicKey));
assertThat(request.getResponseCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(request.getResponseBody())
.isEqualTo(
new HttpErrorResponse(
SC_INTERNAL_SERVER_ERROR, "Unable to delete graffiti for validator " + publicKey));
.isEqualTo(new HttpErrorResponse(SC_INTERNAL_SERVER_ERROR, errorMessage));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import tech.pegasys.teku.infrastructure.restapi.StubRestApiRequest;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.util.DataStructureUtil;
import tech.pegasys.teku.validator.api.GraffitiManagementException;
import tech.pegasys.teku.validator.api.GraffitiManager;
import tech.pegasys.teku.validator.client.OwnedKeyManager;
import tech.pegasys.teku.validator.client.Validator;
Expand Down Expand Up @@ -75,21 +76,22 @@ void shouldSuccessfullySetGraffiti() throws IOException {
}

@Test
void shouldReturnErrorWhenIssueSetting() throws IOException {
void shouldReturnErrorWhenIssueSettingGraffiti() throws IOException {
final String errorMessage = "Unable to update graffiti for validator " + publicKey;
request.setRequestBody(graffiti);

final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.of(validator));
doThrow(IOException.class).when(graffitiManager).setGraffiti(any(), eq(graffiti));
doThrow(new GraffitiManagementException(errorMessage))
.when(graffitiManager)
.setGraffiti(any(), eq(graffiti));

handler.handleRequest(request);

verify(graffitiManager).setGraffiti(eq(publicKey), eq(graffiti));
assertThat(request.getResponseCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(request.getResponseBody())
.isEqualTo(
new HttpErrorResponse(
SC_INTERNAL_SERVER_ERROR, "Unable to update graffiti for validator " + publicKey));
.isEqualTo(new HttpErrorResponse(SC_INTERNAL_SERVER_ERROR, errorMessage));
}

@Test
Expand Down

0 comments on commit 00536fb

Please sign in to comment.