From 3ae1f5ee3f03a4edf4412dd684e7e04ccaade4a5 Mon Sep 17 00:00:00 2001 From: courtneyeh Date: Sun, 21 Apr 2024 23:41:19 +1000 Subject: [PATCH] Create NOOP graffiti manager --- .../subcommand/debug/DebugToolsCommand.java | 4 +- .../teku/validator/api/GraffitiManager.java | 14 +++---- .../api/noop/NoOpGraffitiManager.java | 41 +++++++++++++++++++ .../validator/api/GraffitiManagerTest.java | 2 +- .../client/restapi/ValidatorOpenApiTest.java | 4 +- 5 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 validator/api/src/main/java/tech/pegasys/teku/validator/api/noop/NoOpGraffitiManager.java diff --git a/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/DebugToolsCommand.java b/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/DebugToolsCommand.java index 413aebde457..1151b1d2d77 100644 --- a/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/DebugToolsCommand.java +++ b/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/DebugToolsCommand.java @@ -41,8 +41,8 @@ import tech.pegasys.teku.spec.SpecFactory; import tech.pegasys.teku.spec.datastructures.state.CommitteeAssignment; import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState; -import tech.pegasys.teku.validator.api.GraffitiManager; import tech.pegasys.teku.validator.api.ValidatorApiChannel; +import tech.pegasys.teku.validator.api.noop.NoOpGraffitiManager; import tech.pegasys.teku.validator.beaconnode.GenesisDataProvider; import tech.pegasys.teku.validator.client.KeyManager; import tech.pegasys.teku.validator.client.NoOpKeyManager; @@ -167,7 +167,7 @@ public int generateSwaggerDocs( new SystemTimeProvider(), Optional.empty(), new DoppelgangerDetectionAlert(), - new GraffitiManager(dataDirLayout)); + new NoOpGraffitiManager()); if (api.getRestApiDocs().isPresent()) { final String docs = api.getRestApiDocs().get(); diff --git a/validator/api/src/main/java/tech/pegasys/teku/validator/api/GraffitiManager.java b/validator/api/src/main/java/tech/pegasys/teku/validator/api/GraffitiManager.java index 70836d6b528..ec460d7456f 100644 --- a/validator/api/src/main/java/tech/pegasys/teku/validator/api/GraffitiManager.java +++ b/validator/api/src/main/java/tech/pegasys/teku/validator/api/GraffitiManager.java @@ -26,22 +26,20 @@ import tech.pegasys.teku.service.serviceutils.layout.DataDirLayout; public class GraffitiManager { + private static final Logger LOG = LogManager.getLogger(); static final String GRAFFITI_DIR = "graffiti"; - private static final Logger LOG = LogManager.getLogger(); private final Path graffitiPath; public GraffitiManager(final DataDirLayout dataDirLayout) { - this.graffitiPath = createManagementDirectory(dataDirLayout); + this(dataDirLayout.getValidatorDataDirectory().resolve(GRAFFITI_DIR)); } - private Path createManagementDirectory(final DataDirLayout dataDirLayout) { - final Path graffitiDirectory = dataDirLayout.getValidatorDataDirectory().resolve(GRAFFITI_DIR); - if (!graffitiDirectory.toFile().exists() && !graffitiDirectory.toFile().mkdirs()) { - throw new IllegalStateException( - "Unable to create " + GRAFFITI_DIR + " directory for graffiti management."); + public GraffitiManager(final Path graffitiPath) { + this.graffitiPath = graffitiPath; + if (!graffitiPath.toFile().exists() && !graffitiPath.toFile().mkdirs()) { + throw new IllegalStateException("Unable to create directory for graffiti management."); } - return graffitiDirectory; } public Optional setGraffiti(final BLSPublicKey publicKey, final String graffiti) { diff --git a/validator/api/src/main/java/tech/pegasys/teku/validator/api/noop/NoOpGraffitiManager.java b/validator/api/src/main/java/tech/pegasys/teku/validator/api/noop/NoOpGraffitiManager.java new file mode 100644 index 00000000000..2d81aa5e11e --- /dev/null +++ b/validator/api/src/main/java/tech/pegasys/teku/validator/api/noop/NoOpGraffitiManager.java @@ -0,0 +1,41 @@ +/* + * 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.noop; + +import java.nio.file.Path; +import java.util.Optional; +import org.apache.tuweni.bytes.Bytes32; +import tech.pegasys.teku.bls.BLSPublicKey; +import tech.pegasys.teku.validator.api.GraffitiManager; + +public class NoOpGraffitiManager extends GraffitiManager { + public NoOpGraffitiManager() { + super(Path.of(".")); + } + + @Override + public Optional setGraffiti(final BLSPublicKey publicKey, final String graffiti) { + return Optional.empty(); + } + + @Override + public Optional deleteGraffiti(final BLSPublicKey publicKey) { + return Optional.empty(); + } + + @Override + public Optional getGraffiti(final BLSPublicKey publicKey) { + return Optional.empty(); + } +} diff --git a/validator/api/src/test/java/tech/pegasys/teku/validator/api/GraffitiManagerTest.java b/validator/api/src/test/java/tech/pegasys/teku/validator/api/GraffitiManagerTest.java index 301a869c138..9106dc9ff25 100644 --- a/validator/api/src/test/java/tech/pegasys/teku/validator/api/GraffitiManagerTest.java +++ b/validator/api/src/test/java/tech/pegasys/teku/validator/api/GraffitiManagerTest.java @@ -50,7 +50,7 @@ void shouldThrowExceptionWhenUnableToCreateManagementDirectory(@TempDir final Pa assertThatThrownBy(() -> new GraffitiManager(dataDirLayout)) .isInstanceOf(IllegalStateException.class) - .hasMessage("Unable to create graffiti directory for graffiti management."); + .hasMessage("Unable to create directory for graffiti management."); } @Test diff --git a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/ValidatorOpenApiTest.java b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/ValidatorOpenApiTest.java index a3ef8f92ec4..86f4b91a3ac 100644 --- a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/ValidatorOpenApiTest.java +++ b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/ValidatorOpenApiTest.java @@ -31,8 +31,8 @@ import tech.pegasys.teku.service.serviceutils.layout.DataDirLayout; import tech.pegasys.teku.spec.Spec; import tech.pegasys.teku.spec.SpecFactory; -import tech.pegasys.teku.validator.api.GraffitiManager; import tech.pegasys.teku.validator.api.ValidatorApiChannel; +import tech.pegasys.teku.validator.api.noop.NoOpGraffitiManager; import tech.pegasys.teku.validator.beaconnode.GenesisDataProvider; import tech.pegasys.teku.validator.client.OwnedKeyManager; import tech.pegasys.teku.validator.client.ProposerConfigManager; @@ -71,7 +71,7 @@ void setup() throws IOException { new SystemTimeProvider(), Optional.empty(), doppelgangerDetectionAction, - new GraffitiManager(dataDirLayout)); + new NoOpGraffitiManager()); final Optional maybeJson = restApi.getRestApiDocs(); assertThat(maybeJson).isPresent(); jsonNode = util.parseSwagger(maybeJson.orElseThrow());