diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminModifyPeer.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminModifyPeer.java index 3649bac7ee..bae4884425 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminModifyPeer.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminModifyPeer.java @@ -20,7 +20,6 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResp import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; import tech.pegasys.pantheon.ethereum.p2p.P2pDisabledException; import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork; -import tech.pegasys.pantheon.util.enode.EnodeURL; public abstract class AdminModifyPeer implements JsonRpcMethod { @@ -43,7 +42,9 @@ public abstract class AdminModifyPeer implements JsonRpcMethod { } catch (final InvalidJsonRpcParameters e) { return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INVALID_PARAMS); } catch (final IllegalArgumentException e) { - if (e.getMessage().contains(EnodeURL.INVALID_NODE_ID_LENGTH)) { + if (e.getMessage() + .endsWith( + "Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix.")) { return new JsonRpcErrorResponse(req.getId(), JsonRpcError.ENODE_ID_INVALID); } else { return new JsonRpcErrorResponse(req.getId(), JsonRpcError.PARSE_ERROR); diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/response/JsonRpcError.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/response/JsonRpcError.java index e64745412f..e99b406757 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/response/JsonRpcError.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/response/JsonRpcError.java @@ -12,8 +12,6 @@ */ package tech.pegasys.pantheon.ethereum.jsonrpc.internal.response; -import tech.pegasys.pantheon.util.enode.EnodeURL; - import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonGetter; @@ -101,7 +99,9 @@ public enum JsonRpcError { CANT_CONNECT_TO_LOCAL_PEER(-32100, "Cannot add local node as peer."), // Invalid input errors - ENODE_ID_INVALID(-32000, EnodeURL.INVALID_NODE_ID_LENGTH); + ENODE_ID_INVALID( + -32000, + "Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix."); private final int code; private final String message; 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 79dac1b961..85ead9586e 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 @@ -100,6 +100,25 @@ public class AdminAddPeerTest { assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); } + @Test + public void requestHasInvalidEnodeLength() { + String invalidLengthEnode = + "enode://" + + "0000000000000000000000000000000" + + "00000000000000000000000000000000" + + "00000000000000000000000000000000" + + "00000000000000000000000000000000" + + "@127.0.0.1:30303"; + final JsonRpcRequest request = + new JsonRpcRequest("2.0", "admin_addPeer", new String[] {invalidLengthEnode}); + final JsonRpcResponse expectedResponse = + new JsonRpcErrorResponse(request.getId(), JsonRpcError.ENODE_ID_INVALID); + + final JsonRpcResponse actualResponse = method.response(request); + + assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); + } + @Test public void requestAddsValidEnode() { when(p2pNetwork.addMaintainConnectionPeer(any())).thenReturn(true); diff --git a/util/src/main/java/tech/pegasys/pantheon/util/enode/EnodeURL.java b/util/src/main/java/tech/pegasys/pantheon/util/enode/EnodeURL.java index 8461611dc5..f1b55898f5 100644 --- a/util/src/main/java/tech/pegasys/pantheon/util/enode/EnodeURL.java +++ b/util/src/main/java/tech/pegasys/pantheon/util/enode/EnodeURL.java @@ -41,10 +41,6 @@ public class EnodeURL { private final OptionalInt listeningPort; private final OptionalInt discoveryPort; - // Error messages - public static String INVALID_NODE_ID_LENGTH = - "Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix."; - private EnodeURL( final BytesValue nodeId, final InetAddress address, @@ -91,7 +87,9 @@ public class EnodeURL { checkArgument( uri.getScheme().equalsIgnoreCase("enode"), "Invalid URI scheme (must equal \"enode\")."); - checkArgument(NODE_ID_PATTERN.matcher(uri.getUserInfo()).matches(), INVALID_NODE_ID_LENGTH); + checkArgument( + NODE_ID_PATTERN.matcher(uri.getUserInfo()).matches(), + "Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix."); final BytesValue id = BytesValue.fromHexString(uri.getUserInfo()); String host = uri.getHost(); diff --git a/util/src/test/java/tech/pegasys/pantheon/util/enode/EnodeURLTest.java b/util/src/test/java/tech/pegasys/pantheon/util/enode/EnodeURLTest.java index f3bb88910f..41251eed19 100644 --- a/util/src/test/java/tech/pegasys/pantheon/util/enode/EnodeURLTest.java +++ b/util/src/test/java/tech/pegasys/pantheon/util/enode/EnodeURLTest.java @@ -374,7 +374,8 @@ public class EnodeURLTest { String.format("enode://%s@%s:%d", VALID_NODE_ID.substring(1), IPV4_ADDRESS, P2P_PORT); assertThatThrownBy(() -> EnodeURL.fromString(invalidEnodeURL)) .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("node ID must have exactly 128 hexadecimal"); + .hasMessageEndingWith( + "Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix."); } @Test