Skip to content

Commit

Permalink
[NC-1970] Added handling in admin_addPeer for when p2p is disabled (P…
Browse files Browse the repository at this point in the history
  • Loading branch information
Errorific authored and tmohay committed Jan 29, 2019
1 parent f52c616 commit cbd5a4b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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.DefaultPeer;
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer;
Expand Down Expand Up @@ -52,6 +53,8 @@ public JsonRpcResponse response(final JsonRpcRequest req) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INVALID_PARAMS);
} catch (final IllegalArgumentException e) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.PARSE_ERROR);
} catch (final P2pDisabledException e) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.P2P_DISABLED);
} catch (final Exception e) {
LOG.error("Error processing request: " + req, e);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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 org.junit.Before;
Expand Down Expand Up @@ -113,4 +114,31 @@ public void requestReturnsFalseIfAddFails() {

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}

@Test
public void requestReturnsErrorWhenP2pDisabled() {
when(p2pNetwork.addMaintainConnectionPeer(any()))
.thenThrow(
new P2pDisabledException("P2P networking disabled. Unable to connect to add peer."));

final JsonRpcRequest request =
new JsonRpcRequest(
"2.0",
"admin_addPeer",
new String[] {
"enode://"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "@127.0.0.1:30303"
});

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

final JsonRpcResponse actualResponse = method.response(request);

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
}

0 comments on commit cbd5a4b

Please sign in to comment.