-
Notifications
You must be signed in to change notification settings - Fork 367
Add peer via api #9431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add peer via api #9431
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c4dd65b
add initial impl of endpoint
gfukushima bcd256c
add catch to cover address conversion failure
gfukushima b90acee
add tests for 200, 400 and 500
gfukushima c11a4bf
spotless
gfukushima eb00b79
add test for when discovery is not enabled
gfukushima 1a47aab
Merge branch 'master' into add-peers-via-API
gfukushima 3c38c5a
spotless
gfukushima 0705d72
fix name of test after changing exception returned for invalid peer
gfukushima 16ccf27
add json def to paths
gfukushima b4e1c94
add changelog
gfukushima cf8505e
PR review
gfukushima 86533d4
spotless caught me again
gfukushima 6018d3f
change impl and tests to handle a single peer per api call
gfukushima 112ff35
spotless
gfukushima b5867bc
Merge branch 'master' into add-peers-via-API
gfukushima 71d7818
make request body type string rather than list of strings
gfukushima 1b0415f
Merge remote-tracking branch 'origin/add-peers-via-API' into add-peer…
gfukushima d8b9b73
spotless
gfukushima deb7afb
specify what type of address needs to be passed
gfukushima 1850b11
remove typo
gfukushima eb03818
Merge branch 'master' into add-peers-via-API
gfukushima 618d097
adding description of the request body to the field itself
gfukushima 3cb11ba
adding description to the field
gfukushima 7423c7c
Merge branch 'master' into add-peers-via-API
gfukushima 7fa6a73
Merge branch 'master' into add-peers-via-API
gfukushima e9f5674
adding the supported type of address to method description as well as…
gfukushima 8199f8d
Merge remote-tracking branch 'origin/add-peers-via-API' into add-peer…
gfukushima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
44 changes: 44 additions & 0 deletions
44
...-test/resources/tech/pegasys/teku/beaconrestapi/beacon/paths/_teku_v1_admin_add_peer.json
This file contains hidden or 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,44 @@ | ||
| { | ||
| "post" : { | ||
| "tags" : [ "Teku" ], | ||
| "operationId" : "AddPeer", | ||
| "summary" : "Add a static peer to the node", | ||
| "description" : "Add a static peer to the node passing a multiaddress.", | ||
| "requestBody" : { | ||
| "content" : { | ||
| "application/json" : { | ||
| "schema" : { | ||
| "type" : "string", | ||
| "description" : "Multiaddress of the peer to add" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "responses" : { | ||
| "200" : { | ||
| "description" : "Peer added successfully", | ||
| "content" : { } | ||
| }, | ||
| "400" : { | ||
| "description" : "Invalid peer address", | ||
| "content" : { | ||
| "application/json" : { | ||
| "schema" : { | ||
| "$ref" : "#/components/schemas/HttpErrorResponse" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "500" : { | ||
| "description" : "Internal server error", | ||
| "content" : { | ||
| "application/json" : { | ||
| "schema" : { | ||
| "$ref" : "#/components/schemas/HttpErrorResponse" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
78 changes: 78 additions & 0 deletions
78
...nrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/tekuv1/admin/AddPeer.java
This file contains hidden or 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,78 @@ | ||
| /* | ||
| * Copyright Consensys Software Inc., 2025 | ||
| * | ||
| * 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.beaconrestapi.handlers.tekuv1.admin; | ||
|
|
||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_INTERNAL_SERVER_ERROR; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK; | ||
| import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_TEKU; | ||
| import static tech.pegasys.teku.infrastructure.json.types.CoreTypes.STRING_TYPE; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import java.util.Optional; | ||
| import tech.pegasys.teku.api.DataProvider; | ||
| import tech.pegasys.teku.api.NetworkDataProvider; | ||
| import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata; | ||
| import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint; | ||
| import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest; | ||
| import tech.pegasys.teku.networking.p2p.discovery.DiscoveryNetwork; | ||
|
|
||
| public class AddPeer extends RestApiEndpoint { | ||
|
|
||
| public static final String ROUTE = "/teku/v1/admin/add_peer"; | ||
| final NetworkDataProvider networkDataProvider; | ||
|
|
||
| public AddPeer(final DataProvider dataProvider) { | ||
| this(dataProvider.getNetworkDataProvider()); | ||
| } | ||
|
|
||
| AddPeer(final NetworkDataProvider networkDataProvider) { | ||
| super( | ||
| EndpointMetadata.post(ROUTE) | ||
| .operationId("AddPeer") | ||
| .summary("Add a static peer to the node") | ||
| .description("Add a static peer to the node passing a multiaddress.") | ||
| .tags(TAG_TEKU) | ||
| .requestBodyType(STRING_TYPE.withDescription("Multiaddress of the peer to add")) | ||
| .response(SC_OK, "Peer added successfully") | ||
| .withBadRequestResponse(Optional.of("Invalid peer address")) | ||
| .withInternalErrorResponse() | ||
| .build()); | ||
| this.networkDataProvider = networkDataProvider; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleRequest(final RestApiRequest request) throws JsonProcessingException { | ||
| try { | ||
|
|
||
| final String peerAddress = request.getRequestBody(); | ||
| if (peerAddress.isEmpty()) { | ||
| request.respondWithCode(SC_OK); | ||
| return; | ||
| } | ||
|
|
||
| final DiscoveryNetwork<?> discoveryNetwork = | ||
| networkDataProvider | ||
| .getDiscoveryNetwork() | ||
| .orElseThrow(() -> new IllegalStateException("Discovery network not available")); | ||
|
|
||
| discoveryNetwork.addStaticPeer(peerAddress); | ||
| request.respondWithCode(SC_OK); | ||
| } catch (final IllegalArgumentException e) { | ||
| request.respondError(SC_BAD_REQUEST, e.getMessage()); | ||
| } catch (final IllegalStateException e) { | ||
| request.respondError(SC_INTERNAL_SERVER_ERROR, e.getMessage()); | ||
| } | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
...tapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/tekuv1/admin/AddPeerTest.java
This file contains hidden or 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,95 @@ | ||
| /* | ||
| * Copyright Consensys Software Inc., 2025 | ||
| * | ||
| * 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.beaconrestapi.handlers.tekuv1.admin; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_INTERNAL_SERVER_ERROR; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK; | ||
| import static tech.pegasys.teku.infrastructure.restapi.MetadataTestUtil.verifyMetadataEmptyResponse; | ||
| import static tech.pegasys.teku.infrastructure.restapi.MetadataTestUtil.verifyMetadataErrorResponse; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import tech.pegasys.teku.beaconrestapi.AbstractMigratedBeaconHandlerTest; | ||
| import tech.pegasys.teku.networking.p2p.discovery.DiscoveryNetwork; | ||
|
|
||
| class AddPeerTest extends AbstractMigratedBeaconHandlerTest { | ||
|
|
||
| private final DiscoveryNetwork<?> discoveryNetwork = mock(DiscoveryNetwork.class); | ||
|
|
||
| @BeforeEach | ||
| public void setup() { | ||
| setHandler(new AddPeer(network)); | ||
| } | ||
|
|
||
| @Test | ||
| void metadata_shouldHandle200() { | ||
| verifyMetadataEmptyResponse(handler, SC_OK); | ||
| } | ||
|
|
||
| @Test | ||
| void metadata_shouldHandle400() throws JsonProcessingException { | ||
| verifyMetadataErrorResponse(handler, SC_BAD_REQUEST); | ||
| } | ||
|
|
||
| @Test | ||
| void metadata_shouldHandle500() throws JsonProcessingException { | ||
| verifyMetadataErrorResponse(handler, SC_INTERNAL_SERVER_ERROR); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnOkWhenAValidListIsProvided() throws Exception { | ||
| final String peerAddress = "/ip4/someIP/udp/9001/p2p/somePeerId"; | ||
| request.setRequestBody(peerAddress); | ||
| when(network.getDiscoveryNetwork()).thenReturn(Optional.of(discoveryNetwork)); | ||
| handler.handleRequest(request); | ||
| assertThat(request.getResponseCode()).isEqualTo(SC_OK); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnOkWhenRequestBodyIsEmpty() throws Exception { | ||
| final String peerAddress = ""; | ||
| request.setRequestBody(peerAddress); | ||
| when(network.getDiscoveryNetwork()).thenReturn(Optional.of(discoveryNetwork)); | ||
| handler.handleRequest(request); | ||
| assertThat(request.getResponseCode()).isEqualTo(SC_OK); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnBadRequestIfInvalidPeerAddress() throws Exception { | ||
| final String peerAddress = "invalid-peer-address"; | ||
| request.setRequestBody(peerAddress); | ||
| when(network.getDiscoveryNetwork()).thenReturn(Optional.of(discoveryNetwork)); | ||
| doThrow(new IllegalArgumentException("Invalid peer address")) | ||
| .when(discoveryNetwork) | ||
| .addStaticPeer(peerAddress); | ||
| handler.handleRequest(request); | ||
| assertThat(request.getResponseCode()).isEqualTo(SC_BAD_REQUEST); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnInternalErrorWhenDiscoveryNetworkNotAvailable() throws Exception { | ||
| final String peerAddress = "/ip4/someIP/udp/9001/p2p/somePeerId"; | ||
| request.setRequestBody(peerAddress); | ||
| when(network.getDiscoveryNetwork()).thenReturn(Optional.empty()); | ||
| handler.handleRequest(request); | ||
| assertThat(request.getResponseCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR); | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.