forked from Consensys/teku
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create graffiti management (Consensys#8216)
- Loading branch information
1 parent
533fcc8
commit a3683f7
Showing
25 changed files
with
1,459 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
validator/api/src/main/java/tech/pegasys/teku/validator/api/GraffitiManagementException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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; | ||
|
||
public class GraffitiManagementException extends RuntimeException { | ||
|
||
public GraffitiManagementException(final String message) { | ||
super(message); | ||
} | ||
|
||
public GraffitiManagementException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
validator/api/src/main/java/tech/pegasys/teku/validator/api/GraffitiManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import tech.pegasys.teku.bls.BLSPublicKey; | ||
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 final Path directory; | ||
|
||
public GraffitiManager(final DataDirLayout dataDirLayout) { | ||
this(dataDirLayout.getValidatorDataDirectory().resolve(GRAFFITI_DIR)); | ||
} | ||
|
||
public GraffitiManager(final Path directory) { | ||
this.directory = directory; | ||
if (!directory.toFile().exists() && !directory.toFile().mkdirs()) { | ||
throw new IllegalStateException("Unable to create directory for graffiti management."); | ||
} | ||
} | ||
|
||
public synchronized 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) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"'%s' converts to %s bytes. Input must be 32 bytes or less.", | ||
strippedGraffiti, graffitiSize)); | ||
} | ||
|
||
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 synchronized void deleteGraffiti(final BLSPublicKey publicKey) | ||
throws GraffitiManagementException { | ||
final Path file = directory.resolve(resolveFileName(publicKey)); | ||
if (!file.toFile().exists()) { | ||
return; | ||
} | ||
|
||
try { | ||
Files.delete(file); | ||
} catch (IOException e) { | ||
throw new GraffitiManagementException( | ||
"Unable to delete graffiti for validator " + publicKey, e); | ||
} | ||
} | ||
|
||
public synchronized Optional<Bytes32> getGraffiti(final BLSPublicKey publicKey) | ||
throws GraffitiManagementException { | ||
final Path filePath = directory.resolve(resolveFileName(publicKey)); | ||
if (!filePath.toFile().exists()) { | ||
return Optional.empty(); | ||
} | ||
|
||
try { | ||
return Optional.of(GraffitiParser.loadFromFile(filePath)); | ||
} catch (GraffitiLoaderException | IllegalArgumentException e) { | ||
LOG.error("Loading graffiti from graffiti storage failed.", e); | ||
throw new GraffitiManagementException( | ||
"Unable to retrieve stored graffiti for validator " + publicKey, e); | ||
} | ||
} | ||
|
||
private String resolveFileName(final BLSPublicKey publicKey) { | ||
return publicKey.toSSZBytes().toUnprefixedHexString() + ".txt"; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
validator/api/src/main/java/tech/pegasys/teku/validator/api/UpdatableGraffitiProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.util.Optional; | ||
import java.util.function.Supplier; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
|
||
public class UpdatableGraffitiProvider implements GraffitiProvider { | ||
private final Supplier<Optional<Bytes32>> storageProvider; | ||
private final GraffitiProvider defaultProvider; | ||
|
||
public UpdatableGraffitiProvider( | ||
final Supplier<Optional<Bytes32>> storageProvider, final GraffitiProvider defaultProvider) { | ||
this.storageProvider = storageProvider; | ||
this.defaultProvider = defaultProvider; | ||
} | ||
|
||
@Override | ||
public Optional<Bytes32> get() { | ||
return getFromStorage().or(defaultProvider::get); | ||
} | ||
|
||
private Optional<Bytes32> getFromStorage() { | ||
try { | ||
return storageProvider.get(); | ||
} catch (Exception e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
/** | ||
* @return graffiti without checking for thrown Exceptions. | ||
*/ | ||
public Optional<Bytes32> getUnsafe() { | ||
return storageProvider.get().or(defaultProvider::get); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
validator/api/src/main/java/tech/pegasys/teku/validator/api/noop/NoOpGraffitiManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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 void setGraffiti(final BLSPublicKey publicKey, final String graffiti) {} | ||
|
||
@Override | ||
public void deleteGraffiti(final BLSPublicKey publicKey) {} | ||
|
||
@Override | ||
public Optional<Bytes32> getGraffiti(final BLSPublicKey publicKey) { | ||
return Optional.empty(); | ||
} | ||
} |
Oops, something went wrong.