[NC-1970] Added handling in admin_addPeer for when p2p is disabled (#655)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Chris Mckay 6 years ago committed by GitHub
parent 3a710854ff
commit 3b4e6d451b
  1. 3
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeer.java
  2. 28
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeerTest.java

@ -19,6 +19,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
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;
@ -52,6 +53,8 @@ public class AdminAddPeer implements JsonRpcMethod {
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;

@ -22,6 +22,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
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;
@ -113,4 +114,31 @@ public class AdminAddPeerTest {
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);
}
}

Loading…
Cancel
Save