From dbc58279e6525dc27caa1f5ba74672d7626c1860 Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Wed, 1 May 2024 16:29:32 +0400 Subject: [PATCH] Make graffiti append able to add codes to 28 bytes graffiti (without space) (#8276) --- .../coordinator/GraffitiBuilder.java | 10 ++++++ .../coordinator/GraffitiBuilderTest.java | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/GraffitiBuilder.java b/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/GraffitiBuilder.java index 611803cf3a3..16d415681ef 100644 --- a/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/GraffitiBuilder.java +++ b/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/GraffitiBuilder.java @@ -85,6 +85,11 @@ public Bytes32 buildGraffiti(final Optional userGraffiti) { return switch (clientGraffitiAppendFormat) { case AUTO -> { final int clientInfoLength = Bytes32.SIZE - 1 - userGraffitiLength; + // Could drop SPACE's `-1` in a corner case + if (clientInfoLength == 3) { + yield joinNonEmpty( + "", extractGraffiti(userGraffiti, userGraffitiLength), formatClientsInfo(4)); + } yield joinNonEmpty( SPACE, extractGraffiti(userGraffiti, userGraffitiLength), @@ -92,6 +97,11 @@ yield joinNonEmpty( } case CLIENT_CODES -> { final int clientInfoLength = Integer.min(Bytes32.SIZE - 1 - userGraffitiLength, 4); + // Could drop SPACE's `-1` in a corner case + if (clientInfoLength == 3) { + yield joinNonEmpty( + "", extractGraffiti(userGraffiti, userGraffitiLength), formatClientsInfo(4)); + } yield joinNonEmpty( SPACE, extractGraffiti(userGraffiti, userGraffitiLength), diff --git a/beacon/validator/src/test/java/tech/pegasys/teku/validator/coordinator/GraffitiBuilderTest.java b/beacon/validator/src/test/java/tech/pegasys/teku/validator/coordinator/GraffitiBuilderTest.java index 734da134a1c..45611cf9f7f 100644 --- a/beacon/validator/src/test/java/tech/pegasys/teku/validator/coordinator/GraffitiBuilderTest.java +++ b/beacon/validator/src/test/java/tech/pegasys/teku/validator/coordinator/GraffitiBuilderTest.java @@ -48,6 +48,8 @@ public class GraffitiBuilderTest { private final String asciiGraffiti0 = ""; private static final String ASCII_GRAFFITI_20 = "I've proposed ablock"; + private static final String ASCII_GRAFFITI_27 = "27 bytes of user's graffiti"; + private static final String ASCII_GRAFFITI_28 = "28 bytes of user's graffiti!"; private final String asciiGraffiti32 = "I've proposed a good Teku block!"; private static final String UTF_8_GRAFFITI_4 = "\uD83D\uDE80"; @@ -541,6 +543,14 @@ private static Stream getBuildGraffitiFixtures() { + TEKU_CLIENT_VERSION.commit().toUnprefixedHexString().substring(0, 2) + BESU_CLIENT_VERSION.code() + BESU_CLIENT_VERSION.commit().toUnprefixedHexString().substring(0, 2)), + Arguments.of( + AUTO, + Optional.of(ASCII_GRAFFITI_27), + ASCII_GRAFFITI_27 + " " + TEKU_CLIENT_VERSION.code() + BESU_CLIENT_VERSION.code()), + Arguments.of( + AUTO, + Optional.of(ASCII_GRAFFITI_28), + ASCII_GRAFFITI_28 + TEKU_CLIENT_VERSION.code() + BESU_CLIENT_VERSION.code()), Arguments.of( CLIENT_CODES, Optional.empty(), @@ -557,6 +567,14 @@ private static Stream getBuildGraffitiFixtures() { CLIENT_CODES, Optional.of(ASCII_GRAFFITI_20), ASCII_GRAFFITI_20 + " " + TEKU_CLIENT_VERSION.code() + BESU_CLIENT_VERSION.code()), + Arguments.of( + CLIENT_CODES, + Optional.of(ASCII_GRAFFITI_27), + ASCII_GRAFFITI_27 + " " + TEKU_CLIENT_VERSION.code() + BESU_CLIENT_VERSION.code()), + Arguments.of( + CLIENT_CODES, + Optional.of(ASCII_GRAFFITI_28), + ASCII_GRAFFITI_28 + TEKU_CLIENT_VERSION.code() + BESU_CLIENT_VERSION.code()), Arguments.of(DISABLED, Optional.empty(), ""), Arguments.of(DISABLED, Optional.of("small"), "small"), Arguments.of(DISABLED, Optional.of(UTF_8_GRAFFITI_4), UTF_8_GRAFFITI_4), @@ -589,6 +607,12 @@ private static Stream getBuildGraffitiFixturesElInfoNa() { + " " + TEKU_CLIENT_VERSION.code() + TEKU_CLIENT_VERSION.commit().toUnprefixedHexString().substring(0, 2)), + Arguments.of( + AUTO, + Optional.of(ASCII_GRAFFITI_27), + ASCII_GRAFFITI_27 + " " + TEKU_CLIENT_VERSION.code()), + Arguments.of( + AUTO, Optional.of(ASCII_GRAFFITI_28), ASCII_GRAFFITI_28 + TEKU_CLIENT_VERSION.code()), Arguments.of(CLIENT_CODES, Optional.empty(), TEKU_CLIENT_VERSION.code()), Arguments.of(CLIENT_CODES, Optional.of("small"), "small " + TEKU_CLIENT_VERSION.code()), Arguments.of( @@ -599,6 +623,14 @@ private static Stream getBuildGraffitiFixturesElInfoNa() { CLIENT_CODES, Optional.of(ASCII_GRAFFITI_20), ASCII_GRAFFITI_20 + " " + TEKU_CLIENT_VERSION.code()), + Arguments.of( + CLIENT_CODES, + Optional.of(ASCII_GRAFFITI_27), + ASCII_GRAFFITI_27 + " " + TEKU_CLIENT_VERSION.code()), + Arguments.of( + CLIENT_CODES, + Optional.of(ASCII_GRAFFITI_28), + ASCII_GRAFFITI_28 + TEKU_CLIENT_VERSION.code()), Arguments.of(DISABLED, Optional.empty(), ""), Arguments.of(DISABLED, Optional.of("small"), "small"), Arguments.of(DISABLED, Optional.of(UTF_8_GRAFFITI_4), UTF_8_GRAFFITI_4),