forked from Consensys/orion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OR-339: Added /knownnodes client API method (Consensys#303)
* OR-339: Added /knownnodes client API method * Fix errorprone warnings
- Loading branch information
1 parent
ddc561f
commit c801086
Showing
5 changed files
with
223 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
66 changes: 66 additions & 0 deletions
66
src/main/java/net/consensys/orion/http/handler/knownnodes/KnownNode.java
This file contains 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,66 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* 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 net.consensys.orion.http.handler.knownnodes; | ||
|
||
import net.consensys.cava.crypto.sodium.Box.PublicKey; | ||
|
||
import java.net.URL; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.base.Objects; | ||
import org.postgresql.util.Base64; | ||
|
||
class KnownNode { | ||
|
||
private final String publicKey; | ||
private final String nodeUrl; | ||
|
||
KnownNode(final PublicKey publicKey, final URL nodeUrl) { | ||
this.publicKey = Base64.encodeBytes(publicKey.bytesArray()); | ||
this.nodeUrl = nodeUrl.toString(); | ||
} | ||
|
||
@JsonCreator | ||
KnownNode(@JsonProperty("publicKey") final String publicKey, final @JsonProperty("nodeUrl") String nodeUrl) { | ||
this.publicKey = publicKey; | ||
this.nodeUrl = nodeUrl; | ||
} | ||
|
||
@JsonProperty("publicKey") | ||
String getPublicKey() { | ||
return publicKey; | ||
} | ||
|
||
@JsonProperty("nodeUrl") | ||
String getNodeUrl() { | ||
return nodeUrl; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final KnownNode knownNode = (KnownNode) o; | ||
return Objects.equal(publicKey, knownNode.publicKey) && Objects.equal(nodeUrl, knownNode.nodeUrl); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(publicKey, nodeUrl); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/net/consensys/orion/http/handler/knownnodes/KnownNodesHandler.java
This file contains 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 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* 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 net.consensys.orion.http.handler.knownnodes; | ||
|
||
import net.consensys.orion.http.server.HttpContentType; | ||
import net.consensys.orion.network.ConcurrentNetworkNodes; | ||
import net.consensys.orion.utils.Serializer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import io.vertx.core.Handler; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class KnownNodesHandler implements Handler<RoutingContext> { | ||
|
||
private final ConcurrentNetworkNodes networkNodes; | ||
|
||
public KnownNodesHandler(final ConcurrentNetworkNodes networkNodes) { | ||
this.networkNodes = networkNodes; | ||
} | ||
|
||
@Override | ||
public void handle(final RoutingContext routingContext) { | ||
final List<KnownNode> knownNodes = new ArrayList<>(); | ||
|
||
networkNodes.nodePKs().forEach((publicKey, url) -> knownNodes.add(new KnownNode(publicKey, url))); | ||
|
||
final Buffer bufferResponse = Buffer.buffer(Serializer.serialize(HttpContentType.JSON, knownNodes)); | ||
|
||
routingContext.response().end(bufferResponse); | ||
} | ||
} |
This file contains 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
105 changes: 105 additions & 0 deletions
105
src/test/java/net/consensys/orion/http/handler/knownnodes/KnownNodesHandlerTest.java
This file contains 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,105 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* 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 net.consensys.orion.http.handler.knownnodes; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import net.consensys.cava.crypto.sodium.Box.KeyPair; | ||
import net.consensys.cava.crypto.sodium.Box.PublicKey; | ||
import net.consensys.cava.io.Base64; | ||
import net.consensys.orion.http.handler.HandlerTest; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class KnownNodesHandlerTest extends HandlerTest { | ||
|
||
@Test | ||
void shouldReturnListOfKnownNodesWhenOrionHasNodes() throws Exception { | ||
final List<KnownNode> nodes = createNodes(); | ||
addNodesToNetwork(nodes); | ||
|
||
final Request request = new Request.Builder().get().url(clientBaseUrl + "/knownnodes").build(); | ||
final Response response = httpClient.newCall(request).execute(); | ||
final List<KnownNode> knownNodes = readList(response); | ||
|
||
assertEquals(nodes.size(), knownNodes.size()); | ||
assertTrue(knownNodes.containsAll(nodes)); | ||
} | ||
|
||
@Test | ||
void shouldReturnEmptyListWhenOrionHasNoNodes() throws Exception { | ||
final Request request = new Request.Builder().get().url(clientBaseUrl + "/knownnodes").build(); | ||
final Response response = httpClient.newCall(request).execute(); | ||
final List<KnownNode> knownNodes = readList(response); | ||
|
||
assertTrue(knownNodes.isEmpty()); | ||
} | ||
|
||
@Test | ||
void knownNodesMethodIsAvailableOnClientApi() throws Exception { | ||
final Request request = new Request.Builder().get().url(clientBaseUrl + "/knownnodes").build(); | ||
final Response response = httpClient.newCall(request).execute(); | ||
|
||
assertEquals(200, response.code()); | ||
} | ||
|
||
@Test | ||
void knownNodesMethodIsNotAvailableOnNodeApi() throws Exception { | ||
final Request request = new Request.Builder().get().url(nodeBaseUrl + "/knownnodes").build(); | ||
final Response response = httpClient.newCall(request).execute(); | ||
|
||
assertEquals(404, response.code()); | ||
} | ||
|
||
private List<KnownNode> readList(final Response response) throws java.io.IOException { | ||
return new ObjectMapper().readValue(response.body().bytes(), new TypeReference<>() {}); | ||
} | ||
|
||
private List<KnownNode> createNodes() { | ||
try { | ||
final List<KnownNode> knownNodes = new ArrayList<>(); | ||
knownNodes.add(new KnownNode(KeyPair.random().publicKey(), new URL("http://127.0.0.1:9001/"))); | ||
knownNodes.add(new KnownNode(KeyPair.random().publicKey(), new URL("http://127.0.0.1:9002/"))); | ||
return knownNodes; | ||
} catch (Exception e) { | ||
fail(e); | ||
return new ArrayList<>(); | ||
} | ||
} | ||
|
||
private void addNodesToNetwork(final Collection<KnownNode> nodes) { | ||
nodes.forEach(node -> { | ||
try { | ||
final PublicKey publicKey = PublicKey.fromBytes(Base64.decodeBytes(node.getPublicKey())); | ||
final URL nodeUrl = new URL(node.getNodeUrl()); | ||
networkNodes.addNode(Collections.singletonList(publicKey), nodeUrl); | ||
} catch (MalformedURLException e) { | ||
fail(e); | ||
} | ||
}); | ||
} | ||
|
||
} |