Skip to content

Commit

Permalink
Moved PostStateValidators to minimal typedef client
Browse files Browse the repository at this point in the history
 - Allows VoluntaryExitCommand to access via typedef, changed it to use the POST rather than the GET, which simplifies things a bit.
 - changed the json that drives the tests to match new format.

Signed-off-by: Paul Harris <[email protected]>
  • Loading branch information
rolfyone committed Jul 24, 2024
1 parent e55c9cb commit 2c8a1b3
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.junit.jupiter.api.io.TempDir;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.junit.jupiter.MockServerExtension;
import org.mockserver.model.Parameter;
import tech.pegasys.teku.api.ConfigProvider;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.bls.BLSSignature;
Expand Down Expand Up @@ -615,25 +614,27 @@ private void configureSuccessfulValidatorResponses() throws IOException {
server
.when(
request()
.withMethod("POST")
.withPath("/eth/v1/beacon/states/head/validators")
.withQueryStringParameters(
Parameter.param(
"id",
String.join(
.withBody(
"{\"ids\":["
+ String.join(
",",
validatorPubKey1,
keyManagerPubKey1,
keyManagerPubKey2,
validatorPubKey2))))
quoted(validatorPubKey1),
quoted(keyManagerPubKey1),
quoted(keyManagerPubKey2),
quoted(validatorPubKey2))
+ "]}"))
.respond(response().withStatusCode(200).withBody(validators));
}

private void setupValidatorStatusResponse(final String publicKey, final String responseString) {
server
.when(
request()
.withMethod("POST")
.withPath("/eth/v1/beacon/states/head/validators")
.withQueryStringParameters(Parameter.param("id", publicKey)))
.withBody("{\"ids\":[" + String.join(",", "\"" + publicKey + "\"") + "]}"))
.respond(response().withStatusCode(200).withBody(responseString));
}

Expand Down Expand Up @@ -695,12 +696,14 @@ private void configureExternalSignerPublicKeys(final List<String> keys) {
.map(
k ->
"{"
+ "\"validating_pubkey\": \""
+ k
+ "\","
+ "\"derivation_path\": \"\","
+ "\"readonly\": false"
+ "}")
+ quotedFieldName("validating_pubkey")
+ quoted(k)
+ ","
+ quotedFieldName("derivation_path")
+ quoted("")
+ ","
+ quotedFieldName("readonly")
+ "false}")
.collect(Collectors.joining(","))
+ "]}";
server
Expand All @@ -720,6 +723,14 @@ private void configureRejectedVoluntaryExitResponse() throws IOException {
.respond(response().withStatusCode(400).withBody(rejectedExitResponseBody));
}

private String quoted(final String unquotedString) {
return "\"" + unquotedString + "\"";
}

private String quotedFieldName(final String unquotedString) {
return "\"" + unquotedString + "\":";
}

private String getTestSpecJsonString(final Spec spec) throws JsonProcessingException {
return JsonUtil.serialize(new ConfigProvider(spec).getConfig(), GET_SPEC_RESPONSE_TYPE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"execution_optimistic": false,
"finalized": false,
"data": [
{
"index": "2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"execution_optimistic": false,
"finalized": false,
"data": [
{
"index": "1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"execution_optimistic": false,
"finalized": false,
"data": [
{
"index": "3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"execution_optimistic": false,
"finalized": false,
"data": [
{
"index": "4",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"execution_optimistic": false,
"finalized": false,
"data": [
{
"index": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.bytes.Bytes48;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import picocli.CommandLine;
Expand Down Expand Up @@ -95,7 +94,6 @@
public class VoluntaryExitCommand implements Callable<Integer> {

public static final SubCommandLogger SUB_COMMAND_LOG = new SubCommandLogger();
private static final int MAX_PUBLIC_KEY_BATCH_SIZE = 50;
private OkHttpValidatorMinimalTypeDefClient typeDefClient;
private OkHttpValidatorRestApiClient apiClient;
private Fork fork;
Expand Down Expand Up @@ -268,27 +266,15 @@ private Object2IntMap<BLSPublicKey> getValidatorIndices(
final Object2IntMap<BLSPublicKey> validatorIndices = new Object2IntOpenHashMap<>();
final List<String> publicKeys =
validatorsMap.keySet().stream().map(BLSPublicKey::toString).toList();
for (int i = 0; i < publicKeys.size(); i += MAX_PUBLIC_KEY_BATCH_SIZE) {
final List<String> batch =
publicKeys.subList(i, Math.min(publicKeys.size(), i + MAX_PUBLIC_KEY_BATCH_SIZE));
apiClient
.getValidators(batch)
.ifPresent(
validatorResponses ->
validatorResponses.forEach(
response ->
validatorIndices.put(
response.validator.pubkey.asBLSPublicKey(),
response.index.intValue())));

batch.forEach(
key -> {
if (!validatorIndices.containsKey(
BLSPublicKey.fromBytesCompressed(Bytes48.fromHexString(key)))) {
SUB_COMMAND_LOG.error("Validator not found: " + key);
}
});
}
typeDefClient
.postStateValidators(publicKeys)
.ifPresent(
validatorList ->
validatorList.forEach(
validator ->
validatorIndices.put(
validator.getPublicKey(), validator.getIntegerIndex().intValue())));

return validatorIndices;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,33 @@

package tech.pegasys.teku.validator.remote.typedef;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import tech.pegasys.teku.ethereum.json.types.beacon.StateValidatorData;
import tech.pegasys.teku.spec.datastructures.genesis.GenesisData;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.validator.remote.typedef.handlers.GetGenesisRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.GetSpecRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.PostStateValidatorsRequest;

public class OkHttpValidatorMinimalTypeDefClient {
private final OkHttpClient okHttpClient;
private final HttpUrl baseEndpoint;
private final GetGenesisRequest getGenesisRequest;

private final GetSpecRequest getSpecRequest;
private final PostStateValidatorsRequest postStateValidatorsRequest;

public OkHttpValidatorMinimalTypeDefClient(
final HttpUrl baseEndpoint, final OkHttpClient okHttpClient) {
this.okHttpClient = okHttpClient;
this.baseEndpoint = baseEndpoint;
this.getSpecRequest = new GetSpecRequest(baseEndpoint, okHttpClient);

this.getGenesisRequest = new GetGenesisRequest(baseEndpoint, okHttpClient);
this.postStateValidatorsRequest = new PostStateValidatorsRequest(baseEndpoint, okHttpClient);
}

public Optional<Map<String, String>> getSpec() {
Expand All @@ -56,4 +61,10 @@ public Optional<GenesisData> getGenesis() {
response ->
new GenesisData(response.getGenesisTime(), response.getGenesisValidatorsRoot()));
}

public Optional<List<StateValidatorData>> postStateValidators(final List<String> validatorIds) {
return postStateValidatorsRequest
.postStateValidators(validatorIds)
.map(ObjectAndMetaData::getData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import tech.pegasys.teku.validator.remote.typedef.handlers.GetStateValidatorsRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.GetSyncingStatusRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.PostAttesterDutiesRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.PostStateValidatorsRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.PostSyncDutiesRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.ProduceBlockRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.RegisterValidatorsRequest;
Expand All @@ -68,7 +67,6 @@ public class OkHttpValidatorTypeDefClient extends OkHttpValidatorMinimalTypeDefC
private final GetPeerCountRequest getPeerCountRequest;
private final GetStateValidatorsRequest getStateValidatorsRequest;
private final PostAttesterDutiesRequest postAttesterDutiesRequest;
private final PostStateValidatorsRequest postStateValidatorsRequest;
private final PostSyncDutiesRequest postSyncDutiesRequest;
private final SendSignedBlockRequest sendSignedBlockRequest;
private final RegisterValidatorsRequest registerValidatorsRequest;
Expand All @@ -89,7 +87,6 @@ public OkHttpValidatorTypeDefClient(
this.getProposerDutiesRequest = new GetProposerDutiesRequest(baseEndpoint, okHttpClient);
this.getPeerCountRequest = new GetPeerCountRequest(baseEndpoint, okHttpClient);
this.getStateValidatorsRequest = new GetStateValidatorsRequest(baseEndpoint, okHttpClient);
this.postStateValidatorsRequest = new PostStateValidatorsRequest(baseEndpoint, okHttpClient);
this.postSyncDutiesRequest = new PostSyncDutiesRequest(baseEndpoint, okHttpClient);
this.postAttesterDutiesRequest = new PostAttesterDutiesRequest(baseEndpoint, okHttpClient);
this.sendSignedBlockRequest =
Expand Down Expand Up @@ -124,12 +121,6 @@ public Optional<List<StateValidatorData>> getStateValidators(final List<String>
.map(ObjectAndMetaData::getData);
}

public Optional<List<StateValidatorData>> postStateValidators(final List<String> validatorIds) {
return postStateValidatorsRequest
.postStateValidators(validatorIds)
.map(ObjectAndMetaData::getData);
}

public Optional<SyncCommitteeDuties> postSyncDuties(
final UInt64 epoch, final Collection<Integer> validatorIndices) {
return postSyncDutiesRequest.postSyncDuties(epoch, validatorIndices);
Expand Down

0 comments on commit 2c8a1b3

Please sign in to comment.