From 3b4e6d451bcaa54f9ca59762dee2ba35cf3303df Mon Sep 17 00:00:00 2001 From: Chris Mckay Date: Fri, 25 Jan 2019 08:59:41 +1000 Subject: [PATCH] [NC-1970] Added handling in admin_addPeer for when p2p is disabled (#655) Signed-off-by: Adrian Sutton --- .../internal/methods/AdminAddPeer.java | 3 ++ .../internal/methods/AdminAddPeerTest.java | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeer.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeer.java index 75e43de590..8f626ee154 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeer.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeer.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; diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeerTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeerTest.java index 0b0c654244..cb471f1adc 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeerTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeerTest.java @@ -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); + } }