Skip to content

Commit

Permalink
Add remote get peer count remote validator api handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Jul 1, 2024
1 parent 9dee303 commit 7d0a1ac
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ public class PeerCount {
this.disconnecting = disconnecting;
}

UInt64 getDisconnected() {
public UInt64 getDisconnected() {
return disconnected;
}

UInt64 getConnecting() {
public UInt64 getConnecting() {
return connecting;
}

UInt64 getConnected() {
public UInt64 getConnected() {
return connected;
}

UInt64 getDisconnecting() {
public UInt64 getDisconnecting() {
return disconnecting;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.remote.typedef.handlers;

import static org.assertj.core.api.Assertions.assertThat;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;

import java.util.Optional;
import okhttp3.mockwebserver.MockResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import tech.pegasys.teku.ethereum.json.types.node.PeerCount;
import tech.pegasys.teku.spec.TestSpecContext;
import tech.pegasys.teku.spec.networks.Eth2Network;
import tech.pegasys.teku.validator.remote.typedef.AbstractTypeDefRequestTestBase;

@TestSpecContext(network = Eth2Network.MINIMAL)
public class GetPeerCountRequestTest extends AbstractTypeDefRequestTestBase {

private GetPeerCountRequest getPeerCountRequest;

@BeforeEach
public void setupRequest() {
getPeerCountRequest = new GetPeerCountRequest(mockWebServer.url("/"), okHttpClient);
}

@TestTemplate
public void getPeerCount_correctResponseDeserialization() {
final String mockResponse = readResource("responses/get_peer_count.json");

mockWebServer.enqueue(new MockResponse().setResponseCode(SC_OK).setBody(mockResponse));

final Optional<PeerCount> maybePeerCount = getPeerCountRequest.getPeerCount();
assertThat(maybePeerCount).isPresent();

final PeerCount peerCount = maybePeerCount.get();
assertThat(peerCount.getConnected().longValue()).isEqualTo(56);
assertThat(peerCount.getDisconnected().longValue()).isEqualTo(12);
assertThat(peerCount.getConnecting().longValue()).isEqualTo(0);
assertThat(peerCount.getDisconnecting().longValue()).isEqualTo(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"disconnected": "12",
"connecting": "0",
"connected": "56",
"disconnecting": "0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.stream.Collectors;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes32;
Expand Down Expand Up @@ -222,8 +221,7 @@ public SafeFuture<Optional<ProposerDuties>> getProposerDuties(final UInt64 epoch

@Override
public SafeFuture<Optional<PeerCount>> getPeerCount() {
// TODO add peer count request
throw new NotImplementedException();
return sendRequest(typeDefClient::getPeerCount);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum ValidatorApiMethod {
GET_SYNC_COMMITTEE_DUTIES("eth/v1/validator/duties/sync/:epoch"),
GET_SYNC_COMMITTEE_CONTRIBUTION("eth/v1/validator/sync_committee_contribution"),
GET_PROPOSER_DUTIES("eth/v1/validator/duties/proposer/:epoch"),
GET_PEER_COUNT("eth/v1/node/peer_count"),
PREPARE_BEACON_PROPOSER("/eth/v1/validator/prepare_beacon_proposer"),
REGISTER_VALIDATOR("/eth/v1/validator/register_validator"),
GET_BLOCK_HEADER("eth/v1/beacon/headers/:block_id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.ethereum.json.types.beacon.StateValidatorData;
import tech.pegasys.teku.ethereum.json.types.node.PeerCount;
import tech.pegasys.teku.ethereum.json.types.validator.AttesterDuties;
import tech.pegasys.teku.ethereum.json.types.validator.BeaconCommitteeSelectionProof;
import tech.pegasys.teku.ethereum.json.types.validator.ProposerDuties;
Expand All @@ -44,6 +45,7 @@
import tech.pegasys.teku.validator.remote.typedef.handlers.CreateAttestationDataRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.CreateBlockRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.GetGenesisRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.GetPeerCountRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.GetProposerDutiesRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.GetStateValidatorsRequest;
import tech.pegasys.teku.validator.remote.typedef.handlers.GetSyncingStatusRequest;
Expand All @@ -64,6 +66,7 @@ public class OkHttpValidatorTypeDefClient extends OkHttpValidatorMinimalTypeDefC
private final GetSyncingStatusRequest getSyncingStatusRequest;
private final GetGenesisRequest getGenesisRequest;
private final GetProposerDutiesRequest getProposerDutiesRequest;
private final GetPeerCountRequest getPeerCountRequest;
private final GetStateValidatorsRequest getStateValidatorsRequest;
private final PostAttesterDutiesRequest postAttesterDutiesRequest;
private final PostStateValidatorsRequest postStateValidatorsRequest;
Expand All @@ -85,6 +88,7 @@ public OkHttpValidatorTypeDefClient(
this.getSyncingStatusRequest = new GetSyncingStatusRequest(okHttpClient, baseEndpoint);
this.getGenesisRequest = new GetGenesisRequest(okHttpClient, baseEndpoint);
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);
Expand Down Expand Up @@ -117,6 +121,10 @@ public Optional<ProposerDuties> getProposerDuties(final UInt64 epoch) {
return getProposerDutiesRequest.getProposerDuties(epoch);
}

public Optional<PeerCount> getPeerCount() {
return getPeerCountRequest.getPeerCount();
}

public Optional<List<StateValidatorData>> getStateValidators(final List<String> validatorIds) {
return getStateValidatorsRequest
.getStateValidators(validatorIds)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.remote.typedef.handlers;

import java.util.Optional;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import tech.pegasys.teku.ethereum.json.types.node.PeerCount;
import tech.pegasys.teku.ethereum.json.types.node.PeerCountBuilder;
import tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod;
import tech.pegasys.teku.validator.remote.typedef.ResponseHandler;

public class GetPeerCountRequest extends AbstractTypeDefRequest {
public GetPeerCountRequest(final HttpUrl baseEndpoint, final OkHttpClient okHttpClient) {
super(baseEndpoint, okHttpClient);
}

public Optional<PeerCount> getPeerCount() {
return get(
ValidatorApiMethod.GET_PEER_COUNT, new ResponseHandler<>(PeerCountBuilder.PEER_COUNT_TYPE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.ethereum.json.types.beacon.StateValidatorData;
import tech.pegasys.teku.ethereum.json.types.node.PeerCount;
import tech.pegasys.teku.ethereum.json.types.node.PeerCountBuilder;
import tech.pegasys.teku.ethereum.json.types.validator.AttesterDuties;
import tech.pegasys.teku.ethereum.json.types.validator.AttesterDuty;
import tech.pegasys.teku.ethereum.json.types.validator.BeaconCommitteeSelectionProof;
Expand Down Expand Up @@ -439,6 +441,19 @@ public void getProposerDuties_WhenFound_ReturnsDuties() {
assertThat(validatorDuties.getDependentRoot()).isEqualTo(response.getDependentRoot());
}

@Test
public void getPeerCount_WhenAvailable_ReturnPeerCount() {
final PeerCount response =
new PeerCountBuilder()
.connected(UInt64.valueOf(10))
.disconnected(UInt64.valueOf(5))
.build();
when(typeDefClient.getPeerCount()).thenReturn(Optional.of(response));
final SafeFuture<Optional<PeerCount>> peerCountFuture = apiHandler.getPeerCount();
PeerCount peerCount = unwrapToValue(peerCountFuture);
assertThat(peerCount).isEqualTo(response);
}

@Test
public void createAttestationData_WhenNone_ReturnsEmpty() {
when(typeDefClient.createAttestationData(any(), anyInt())).thenReturn(Optional.empty());
Expand Down

0 comments on commit 7d0a1ac

Please sign in to comment.