Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Output enode URL on startup #1137

Merged
merged 7 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update net_enode to use getSefEnodeURL()
  • Loading branch information
lucassaldanha committed Mar 20, 2019
commit a9829b6baa71beb24752e05f5c8ac5536bd3cb37
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.p2p.P2pDisabledException;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer;
import tech.pegasys.pantheon.util.enode.EnodeURL;

import java.util.Optional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -41,26 +42,23 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequest req) {
try {
if (p2pNetwork.isP2pEnabled()) {
String enodeURI = p2pNetwork.getAdvertisedPeer().map(Peer::getEnodeURLString).orElse("");
if (!enodeURI.isEmpty()) {
return new JsonRpcSuccessResponse(req.getId(), enodeURI);
} else {
return p2pDisabledResponse(req);
}
} else {
return p2pDisabledResponse(req);
}
} catch (final P2pDisabledException e) {
if (!p2pNetwork.isP2pEnabled()) {
return p2pDisabledResponse(req);
} catch (final Exception e) {
LOG.error("Error processing request: " + req, e);
throw e;
}

final Optional<EnodeURL> enodeURL = p2pNetwork.getSelfEnodeURL();
if (!enodeURL.isPresent()) {
return enodeUrlNotAvailable(req);
}

return new JsonRpcSuccessResponse(req.getId(), enodeURL.get().toString());
}

private JsonRpcErrorResponse p2pDisabledResponse(final JsonRpcRequest req) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.P2P_DISABLED);
}

private JsonRpcErrorResponse enodeUrlNotAvailable(final JsonRpcRequest req) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.ENODE_NOT_AVAILABLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public enum JsonRpcError {
METHOD_NOT_FOUND(-32601, "Method not found"),
INVALID_PARAMS(-32602, "Invalid params"),
INTERNAL_ERROR(-32603, "Internal error"),

// P2P related errors
P2P_DISABLED(-32000, "P2P has been disabled. This functionality is not available"),
ENODE_NOT_AVAILABLE(-32000, "Enode URL not available"),

// Filter & Subscription Errors
FILTER_NOT_FOUND(-32000, "Filter not found"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer;
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.enode.EnodeURL;

import java.util.Optional;

Expand All @@ -37,16 +37,14 @@
@RunWith(MockitoJUnitRunner.class)
public class NetEnodeTest {

private static final String TESTED_METHOD_NAME = "net_enode";

private NetEnode method;

private final BytesValue nodeId =
BytesValue.fromHexString(
"0x0f1b319e32017c3fcb221841f0f978701b4e9513fe6a567a2db43d43381a9c7e3dfe7cae13cbc2f56943400bacaf9082576ab087cd51983b17d729ae796f6807");

private final DefaultPeer defaultPeer = new DefaultPeer(nodeId, "1.2.3.4", 7890, 30303);
private final Optional<Peer> advertisedPeer = Optional.of(defaultPeer);
private final Optional<EnodeURL> enodeURL = Optional.of(defaultPeer.getEnodeURL());

@Mock private P2PNetwork p2PNetwork;

Expand All @@ -57,17 +55,17 @@ public void before() {

@Test
public void shouldReturnExpectedMethodName() {
assertThat(method.getName()).isEqualTo(TESTED_METHOD_NAME);
assertThat(method.getName()).isEqualTo("net_enode");
}

@Test
public void shouldReturnEnode() {
when(p2PNetwork.isP2pEnabled()).thenReturn(true);
doReturn(advertisedPeer).when(p2PNetwork).getAdvertisedPeer();
doReturn(enodeURL).when(p2PNetwork).getSelfEnodeURL();

final JsonRpcRequest request = netEnodeRequest();
final JsonRpcResponse expectedResponse =
new JsonRpcSuccessResponse(request.getId(), advertisedPeer.get().getEnodeURLString());
new JsonRpcSuccessResponse(request.getId(), enodeURL.get().toString());

assertThat(method.response(request)).isEqualToComparingFieldByField(expectedResponse);
}
Expand All @@ -86,16 +84,16 @@ public void shouldReturnErrorWhenP2pDisabled() {
@Test
public void shouldReturnErrorWhenP2PEnabledButNoEnodeFound() {
when(p2PNetwork.isP2pEnabled()).thenReturn(true);
doReturn(Optional.empty()).when(p2PNetwork).getAdvertisedPeer();
doReturn(Optional.empty()).when(p2PNetwork).getSelfEnodeURL();

final JsonRpcRequest request = netEnodeRequest();
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.P2P_DISABLED);
new JsonRpcErrorResponse(request.getId(), JsonRpcError.ENODE_NOT_AVAILABLE);

assertThat(method.response(request)).isEqualToComparingFieldByField(expectedResponse);
}

private JsonRpcRequest netEnodeRequest() {
return new JsonRpcRequest("2.0", TESTED_METHOD_NAME, new Object[] {});
return new JsonRpcRequest("2.0", "net_enode", new Object[] {});
}
}