[FIX] Changed Enode length error String implementation. (#1486)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
mark-terry 6 years ago committed by GitHub
parent 2aecc7fdd4
commit 7e29b0954b
  1. 5
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminModifyPeer.java
  2. 6
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/response/JsonRpcError.java
  3. 19
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeerTest.java
  4. 8
      util/src/main/java/tech/pegasys/pantheon/util/enode/EnodeURL.java
  5. 3
      util/src/test/java/tech/pegasys/pantheon/util/enode/EnodeURLTest.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);

@ -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;

@ -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);

@ -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();

@ -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

Loading…
Cancel
Save