16
16
import static tech .pegasys .teku .ethereum .json .types .SharedApiTypes .PUBKEY_API_TYPE ;
17
17
import static tech .pegasys .teku .infrastructure .http .HttpStatusCodes .SC_NOT_FOUND ;
18
18
import static tech .pegasys .teku .infrastructure .http .HttpStatusCodes .SC_OK ;
19
- import static tech .pegasys .teku .infrastructure .json .types .CoreTypes .STRING_TYPE ;
20
19
import static tech .pegasys .teku .validator .client .restapi .ValidatorRestApi .TAG_GRAFFITI ;
21
20
import static tech .pegasys .teku .validator .client .restapi .ValidatorTypes .PARAM_PUBKEY_TYPE ;
22
21
25
24
import java .util .Objects ;
26
25
import java .util .Optional ;
27
26
import java .util .function .Function ;
27
+ import org .apache .tuweni .bytes .Bytes ;
28
28
import org .apache .tuweni .bytes .Bytes32 ;
29
29
import tech .pegasys .teku .bls .BLSPublicKey ;
30
+ import tech .pegasys .teku .infrastructure .json .types .DeserializableTypeDefinition ;
30
31
import tech .pegasys .teku .infrastructure .json .types .SerializableTypeDefinition ;
31
32
import tech .pegasys .teku .infrastructure .restapi .endpoints .EndpointMetadata ;
32
33
import tech .pegasys .teku .infrastructure .restapi .endpoints .RestApiEndpoint ;
33
34
import tech .pegasys .teku .infrastructure .restapi .endpoints .RestApiRequest ;
35
+ import tech .pegasys .teku .validator .api .Bytes32Parser ;
34
36
import tech .pegasys .teku .validator .client .KeyManager ;
35
37
import tech .pegasys .teku .validator .client .Validator ;
36
38
37
39
public class GetGraffiti extends RestApiEndpoint {
38
- public static final String ROUTE = "/eth/v1/validator/{pubkey}/graffiti" ;
40
+ static final String ROUTE = "/eth/v1/validator/{pubkey}/graffiti" ;
39
41
private final KeyManager keyManager ;
40
42
41
- private static final SerializableTypeDefinition <GraffitiResponse > GRAFFITI_TYPE =
43
+ public static final DeserializableTypeDefinition <Bytes32 > GRAFFITI_TYPE =
44
+ DeserializableTypeDefinition .string (Bytes32 .class )
45
+ .formatter (GetGraffiti ::processGraffitiString )
46
+ .parser (Bytes32Parser ::toBytes32 )
47
+ .build ();
48
+
49
+ private static final SerializableTypeDefinition <GraffitiResponse > GRAFFITI_RESPONSE_TYPE =
42
50
SerializableTypeDefinition .object (GraffitiResponse .class )
43
51
.withOptionalField ("pubkey" , PUBKEY_API_TYPE , GraffitiResponse ::getPublicKey )
44
- .withField ("graffiti" , STRING_TYPE , GraffitiResponse ::getGraffiti )
52
+ .withField ("graffiti" , GRAFFITI_TYPE , GraffitiResponse ::getGraffiti )
45
53
.build ();
46
54
47
55
private static final SerializableTypeDefinition <GraffitiResponse > RESPONSE_TYPE =
48
56
SerializableTypeDefinition .object (GraffitiResponse .class )
49
57
.name ("GraffitiResponse" )
50
- .withField ("data" , GRAFFITI_TYPE , Function .identity ())
58
+ .withField ("data" , GRAFFITI_RESPONSE_TYPE , Function .identity ())
51
59
.build ();
52
60
53
61
public GetGraffiti (final KeyManager keyManager ) {
@@ -68,7 +76,7 @@ public GetGraffiti(final KeyManager keyManager) {
68
76
}
69
77
70
78
@ Override
71
- public void handleRequest (RestApiRequest request ) throws JsonProcessingException {
79
+ public void handleRequest (final RestApiRequest request ) throws JsonProcessingException {
72
80
final BLSPublicKey publicKey = request .getPathParameter (PARAM_PUBKEY_TYPE );
73
81
74
82
final Optional <Validator > maybeValidator = keyManager .getValidatorByPublicKey (publicKey );
@@ -77,20 +85,22 @@ public void handleRequest(RestApiRequest request) throws JsonProcessingException
77
85
return ;
78
86
}
79
87
80
- final String graffiti =
81
- maybeValidator .get ().getGraffiti ().map (this ::processGraffitiBytes ).orElse ("" );
82
- request .respondOk (new GraffitiResponse (publicKey , graffiti ));
88
+ request .respondOk (new GraffitiResponse (publicKey , maybeValidator .get ().getGraffiti ()));
83
89
}
84
90
85
- private String processGraffitiBytes (final Bytes32 graffiti ) {
91
+ private static String processGraffitiString (final Bytes32 graffiti ) {
86
92
return new String (graffiti .toArrayUnsafe (), StandardCharsets .UTF_8 ).strip ().replace ("\0 " , "" );
87
93
}
88
94
89
95
static class GraffitiResponse {
90
96
private final Optional <BLSPublicKey > publicKey ;
91
- private final String graffiti ;
97
+ private final Bytes32 graffiti ;
98
+
99
+ GraffitiResponse (final BLSPublicKey publicKey , final Optional <Bytes32 > graffiti ) {
100
+ this (publicKey , graffiti .orElse (Bytes32Parser .toBytes32 (Bytes .EMPTY .toArray ())));
101
+ }
92
102
93
- GraffitiResponse (final BLSPublicKey publicKey , final String graffiti ) {
103
+ GraffitiResponse (final BLSPublicKey publicKey , final Bytes32 graffiti ) {
94
104
this .publicKey = Optional .of (publicKey );
95
105
this .graffiti = graffiti ;
96
106
}
@@ -99,19 +109,19 @@ Optional<BLSPublicKey> getPublicKey() {
99
109
return publicKey ;
100
110
}
101
111
102
- String getGraffiti () {
112
+ Bytes32 getGraffiti () {
103
113
return graffiti ;
104
114
}
105
115
106
116
@ Override
107
- public boolean equals (Object o ) {
117
+ public boolean equals (final Object o ) {
108
118
if (this == o ) {
109
119
return true ;
110
120
}
111
121
if (o == null || getClass () != o .getClass ()) {
112
122
return false ;
113
123
}
114
- GraffitiResponse that = (GraffitiResponse ) o ;
124
+ final GraffitiResponse that = (GraffitiResponse ) o ;
115
125
return Objects .equals (publicKey , that .publicKey ) && Objects .equals (graffiti , that .graffiti );
116
126
}
117
127
0 commit comments