|
| 1 | +/* |
| 2 | + * Copyright Consensys Software Inc., 2024 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package tech.pegasys.teku.validator.api; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.Optional; |
| 21 | +import org.apache.logging.log4j.LogManager; |
| 22 | +import org.apache.logging.log4j.Logger; |
| 23 | +import org.apache.tuweni.bytes.Bytes32; |
| 24 | +import tech.pegasys.teku.bls.BLSPublicKey; |
| 25 | +import tech.pegasys.teku.service.serviceutils.layout.DataDirLayout; |
| 26 | + |
| 27 | +public class GraffitiManager { |
| 28 | + private static final Logger LOG = LogManager.getLogger(); |
| 29 | + static final String GRAFFITI_DIR = "graffiti"; |
| 30 | + private final Path directory; |
| 31 | + |
| 32 | + public GraffitiManager(final DataDirLayout dataDirLayout) { |
| 33 | + this(dataDirLayout.getValidatorDataDirectory().resolve(GRAFFITI_DIR)); |
| 34 | + } |
| 35 | + |
| 36 | + public GraffitiManager(final Path directory) { |
| 37 | + this.directory = directory; |
| 38 | + if (!directory.toFile().exists() && !directory.toFile().mkdirs()) { |
| 39 | + throw new IllegalStateException("Unable to create directory for graffiti management."); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public synchronized void setGraffiti(final BLSPublicKey publicKey, final String graffiti) |
| 44 | + throws GraffitiManagementException { |
| 45 | + final String strippedGraffiti = graffiti.strip(); |
| 46 | + final int graffitiSize = strippedGraffiti.getBytes(StandardCharsets.UTF_8).length; |
| 47 | + if (graffitiSize > 32) { |
| 48 | + throw new IllegalArgumentException( |
| 49 | + String.format( |
| 50 | + "'%s' converts to %s bytes. Input must be 32 bytes or less.", |
| 51 | + strippedGraffiti, graffitiSize)); |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + final Path file = directory.resolve(resolveFileName(publicKey)); |
| 56 | + Files.writeString(file, strippedGraffiti); |
| 57 | + } catch (IOException e) { |
| 58 | + throw new GraffitiManagementException( |
| 59 | + "Unable to update graffiti for validator " + publicKey, e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public synchronized void deleteGraffiti(final BLSPublicKey publicKey) |
| 64 | + throws GraffitiManagementException { |
| 65 | + final Path file = directory.resolve(resolveFileName(publicKey)); |
| 66 | + if (!file.toFile().exists()) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + Files.delete(file); |
| 72 | + } catch (IOException e) { |
| 73 | + throw new GraffitiManagementException( |
| 74 | + "Unable to delete graffiti for validator " + publicKey, e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public synchronized Optional<Bytes32> getGraffiti(final BLSPublicKey publicKey) |
| 79 | + throws GraffitiManagementException { |
| 80 | + final Path filePath = directory.resolve(resolveFileName(publicKey)); |
| 81 | + if (!filePath.toFile().exists()) { |
| 82 | + return Optional.empty(); |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + return Optional.of(GraffitiParser.loadFromFile(filePath)); |
| 87 | + } catch (GraffitiLoaderException | IllegalArgumentException e) { |
| 88 | + LOG.error("Loading graffiti from graffiti storage failed.", e); |
| 89 | + throw new GraffitiManagementException( |
| 90 | + "Unable to retrieve stored graffiti for validator " + publicKey, e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private String resolveFileName(final BLSPublicKey publicKey) { |
| 95 | + return publicKey.toSSZBytes().toUnprefixedHexString() + ".txt"; |
| 96 | + } |
| 97 | +} |
0 commit comments