Normalize EnodeURLs (#1264)

Only specify discovery port explicitly when the discovery port differs
from the listening port.
Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
mbaxter 6 years ago committed by GitHub
parent 39bdf5bf7d
commit e870ade816
  1. 2
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/perm/SmartContractPermissioningAllowNodeTransaction.java
  2. 4
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/perm/SmartContractPermissioningConnectionIsAllowedTransaction.java
  3. 2
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/perm/SmartContractPermissioningForbidNodeTransaction.java
  4. 2
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/perm/SmartContractPermissioningNodeIsAllowedTransaction.java
  5. 2
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeer.java
  6. 2
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminRemovePeer.java
  7. 2
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/peers/StaticNodesParser.java
  8. 10
      ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/InsufficientPeersPermissioningProviderTest.java
  9. 2
      ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/PeerDiscoveryControllerTest.java
  10. 6
      ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetworkTest.java
  11. 10
      ethereum/permissioning/src/main/java/tech/pegasys/pantheon/ethereum/permissioning/NodeLocalConfigPermissioningController.java
  12. 29
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/NodeLocalConfigPermissioningControllerTest.java
  13. 24
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/SmartContractPermissioningControllerTest.java
  14. 2
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/node/NodePermissioningControllerFactoryTest.java
  15. 4
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/node/NodePermissioningControllerTest.java
  16. 6
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/node/provider/SyncStatusNodePermissioningProviderTest.java
  17. 2
      pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java
  18. 3
      pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java
  19. 2
      pantheon/src/main/java/tech/pegasys/pantheon/cli/custom/EnodeToURIPropertyConverter.java
  20. 22
      util/src/main/java/tech/pegasys/pantheon/util/enode/EnodeURL.java
  21. 86
      util/src/test/java/tech/pegasys/pantheon/util/enode/EnodeURLTest.java

@ -66,7 +66,7 @@ public class SmartContractPermissioningAllowNodeTransaction implements Transacti
final String enodeURL = ((RunnableNode) node).enodeUrl().toASCIIString();
final BytesValue payload =
SmartContractPermissioningController.createPayload(
ADD_ENODE_SIGNATURE, new EnodeURL(enodeURL));
ADD_ENODE_SIGNATURE, EnodeURL.fromString(enodeURL));
RawTransaction transaction =
RawTransaction.createTransaction(

@ -66,8 +66,8 @@ public class SmartContractPermissioningConnectionIsAllowedTransaction
final BytesValue payload =
SmartContractPermissioningController.createPayload(
IS_CONNECTION_ALLOWED_SIGNATURE,
new EnodeURL(sourceEnodeURL),
new EnodeURL(targetEnodeURL));
EnodeURL.fromString(sourceEnodeURL),
EnodeURL.fromString(targetEnodeURL));
return org.web3j.protocol.core.methods.request.Transaction.createFunctionCallTransaction(
null, null, null, null, contractAddress.toString(), payload.toString());

@ -66,7 +66,7 @@ public class SmartContractPermissioningForbidNodeTransaction implements Transact
final String enodeURL = ((RunnableNode) node).enodeUrl().toASCIIString();
final BytesValue payload =
SmartContractPermissioningController.createPayload(
REMOVE_ENODE_SIGNATURE, new EnodeURL(enodeURL));
REMOVE_ENODE_SIGNATURE, EnodeURL.fromString(enodeURL));
RawTransaction transaction =
RawTransaction.createTransaction(

@ -59,7 +59,7 @@ public class SmartContractPermissioningNodeIsAllowedTransaction implements Trans
final String sourceEnodeURL = ((RunnableNode) node).enodeUrl().toASCIIString();
final BytesValue payload =
SmartContractPermissioningController.createPayload(
IS_NODE_ALLOWED_SIGNATURE, new EnodeURL(sourceEnodeURL));
IS_NODE_ALLOWED_SIGNATURE, EnodeURL.fromString(sourceEnodeURL));
return org.web3j.protocol.core.methods.request.Transaction.createFunctionCallTransaction(
null, null, null, null, contractAddress.toString(), payload.toString());

@ -44,7 +44,7 @@ public class AdminAddPeer extends AdminModifyPeer {
protected JsonRpcResponse performOperation(final Object id, final String enode) {
try {
LOG.debug("Adding ({}) to peers", enode);
final EnodeURL enodeURL = new EnodeURL(enode);
final EnodeURL enodeURL = EnodeURL.fromString(enode);
final Peer peer = DefaultPeer.fromEnodeURL(enodeURL);
boolean addedToNetwork = peerNetwork.addMaintainConnectionPeer(peer);
return new JsonRpcSuccessResponse(id, addedToNetwork);

@ -38,7 +38,7 @@ public class AdminRemovePeer extends AdminModifyPeer {
@Override
protected JsonRpcResponse performOperation(final Object id, final String enode) {
LOG.debug("Remove ({}) to peer cache", enode);
final EnodeURL enodeURL = new EnodeURL(enode);
final EnodeURL enodeURL = EnodeURL.fromString(enode);
final boolean result =
peerNetwork.removeMaintainedConnectionPeer(DefaultPeer.fromEnodeURL(enodeURL));
return new JsonRpcSuccessResponse(id, result);

@ -68,7 +68,7 @@ public class StaticNodesParser {
private static EnodeURL decodeString(final String input) {
try {
return new EnodeURL(input);
return EnodeURL.fromString(input);
} catch (IllegalArgumentException ex) {
LOG.info("Illegally constructed enode supplied ({})", input);
throw ex;

@ -41,19 +41,19 @@ import org.mockito.junit.MockitoJUnitRunner;
public class InsufficientPeersPermissioningProviderTest {
@Mock private P2PNetwork p2pNetwork;
private final EnodeURL SELF_ENODE =
new EnodeURL(
EnodeURL.fromString(
"enode://00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001@192.168.0.1:30303");
private final EnodeURL ENODE_2 =
new EnodeURL(
EnodeURL.fromString(
"enode://00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002@192.168.0.2:30303");
private final EnodeURL ENODE_3 =
new EnodeURL(
EnodeURL.fromString(
"enode://00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003@192.168.0.3:30303");
private final EnodeURL ENODE_4 =
new EnodeURL(
EnodeURL.fromString(
"enode://00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004@192.168.0.4:30303");
private final EnodeURL ENODE_5 =
new EnodeURL(
EnodeURL.fromString(
"enode://00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005@192.168.0.5:30303");
@Test

@ -92,7 +92,7 @@ public class PeerDiscoveryControllerTest {
private final PeerDiscoveryTestHelper helper = new PeerDiscoveryTestHelper();
private final String selfEnodeString =
"enode://5f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:1111";
private final EnodeURL selfEnode = new EnodeURL(selfEnodeString);
private final EnodeURL selfEnode = EnodeURL.fromString(selfEnodeString);
@Before
public void initializeMocks() {

@ -98,7 +98,7 @@ public final class NettyP2PNetworkTest {
private final String selfEnodeString =
"enode://5f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:1111";
private final EnodeURL selfEnode = new EnodeURL(selfEnodeString);
private final EnodeURL selfEnode = EnodeURL.fromString(selfEnodeString);
@Before
public void before() {
@ -723,8 +723,8 @@ public final class NettyP2PNetworkTest {
final PeerConnection notPermittedPeerConnection =
mockPeerConnection(localPeer, notPermittedPeer);
final EnodeURL permittedEnodeURL = new EnodeURL(permittedPeer.getEnodeURLString());
final EnodeURL notPermittedEnodeURL = new EnodeURL(notPermittedPeer.getEnodeURLString());
final EnodeURL permittedEnodeURL = EnodeURL.fromString(permittedPeer.getEnodeURLString());
final EnodeURL notPermittedEnodeURL = EnodeURL.fromString(notPermittedPeer.getEnodeURLString());
nettyP2PNetwork.start();
nettyP2PNetwork.connect(permittedPeer).complete(permittedPeerConnection);

@ -70,7 +70,7 @@ public class NodeLocalConfigPermissioningController implements NodePermissioning
private void readNodesFromConfig(final LocalPermissioningConfiguration configuration) {
if (configuration.isNodeWhitelistEnabled() && configuration.getNodeWhitelist() != null) {
for (URI uri : configuration.getNodeWhitelist()) {
addNode(new EnodeURL(uri.toString()));
addNode(EnodeURL.fromString(uri.toString()));
}
}
}
@ -80,7 +80,8 @@ public class NodeLocalConfigPermissioningController implements NodePermissioning
if (inputValidationResult.result() != WhitelistOperationResult.SUCCESS) {
return inputValidationResult;
}
final List<EnodeURL> peers = enodeURLs.stream().map(EnodeURL::new).collect(Collectors.toList());
final List<EnodeURL> peers =
enodeURLs.stream().map(EnodeURL::fromString).collect(Collectors.toList());
for (EnodeURL peer : peers) {
if (nodesWhitelist.contains(peer)) {
@ -111,7 +112,8 @@ public class NodeLocalConfigPermissioningController implements NodePermissioning
if (inputValidationResult.result() != WhitelistOperationResult.SUCCESS) {
return inputValidationResult;
}
final List<EnodeURL> peers = enodeURLs.stream().map(EnodeURL::new).collect(Collectors.toList());
final List<EnodeURL> peers =
enodeURLs.stream().map(EnodeURL::fromString).collect(Collectors.toList());
boolean anyBootnode = peers.stream().anyMatch(bootnodes::contains);
if (anyBootnode) {
@ -213,7 +215,7 @@ public class NodeLocalConfigPermissioningController implements NodePermissioning
}
public boolean isPermitted(final String enodeURL) {
return isPermitted(new EnodeURL(enodeURL));
return isPermitted(EnodeURL.fromString(enodeURL));
}
public boolean isPermitted(final EnodeURL node) {

@ -68,7 +68,7 @@ public class NodeLocalConfigPermissioningControllerTest {
new NodeLocalConfigPermissioningController(
LocalPermissioningConfiguration.createDefault(),
bootnodesList,
new EnodeURL(selfEnode),
EnodeURL.fromString(selfEnode),
whitelistPersistor);
}
@ -219,15 +219,17 @@ public class NodeLocalConfigPermissioningControllerTest {
@Test
public void whenCheckingIfNodeIsPermittedOrderDoesNotMatter() {
controller.addNodes(Arrays.asList(enode1));
assertThat(controller.isPermitted(new EnodeURL(enode1), new EnodeURL(selfEnode))).isTrue();
assertThat(controller.isPermitted(new EnodeURL(selfEnode), new EnodeURL(enode1))).isTrue();
assertThat(controller.isPermitted(EnodeURL.fromString(enode1), EnodeURL.fromString(selfEnode)))
.isTrue();
assertThat(controller.isPermitted(EnodeURL.fromString(selfEnode), EnodeURL.fromString(enode1)))
.isTrue();
}
@Test
public void stateShouldRevertIfWhitelistPersistFails()
throws IOException, WhitelistFileSyncException {
List<String> newNode1 = singletonList(new EnodeURL(enode1).toString());
List<String> newNode2 = singletonList(new EnodeURL(enode2).toString());
List<String> newNode1 = singletonList(EnodeURL.fromString(enode1).toString());
List<String> newNode2 = singletonList(EnodeURL.fromString(enode2).toString());
assertThat(controller.getNodesWhitelist().size()).isEqualTo(0);
@ -260,7 +262,7 @@ public class NodeLocalConfigPermissioningControllerTest {
.thenReturn(Arrays.asList(URI.create(expectedEnodeURL)));
controller =
new NodeLocalConfigPermissioningController(
permissioningConfig, bootnodesList, new EnodeURL(selfEnode));
permissioningConfig, bootnodesList, EnodeURL.fromString(selfEnode));
controller.reload();
@ -280,7 +282,7 @@ public class NodeLocalConfigPermissioningControllerTest {
.thenReturn(Arrays.asList(URI.create(expectedEnodeURI)));
controller =
new NodeLocalConfigPermissioningController(
permissioningConfig, bootnodesList, new EnodeURL(selfEnode));
permissioningConfig, bootnodesList, EnodeURL.fromString(selfEnode));
final Throwable thrown = catchThrowable(() -> controller.reload());
@ -297,7 +299,7 @@ public class NodeLocalConfigPermissioningControllerTest {
final Consumer<NodeWhitelistUpdatedEvent> consumer = mock(Consumer.class);
final NodeWhitelistUpdatedEvent expectedEvent =
new NodeWhitelistUpdatedEvent(
Lists.newArrayList(new EnodeURL(enode1)), Collections.emptyList());
Lists.newArrayList(EnodeURL.fromString(enode1)), Collections.emptyList());
controller.subscribeToListUpdatedEvent(consumer);
controller.addNodes(Lists.newArrayList(enode1));
@ -328,7 +330,7 @@ public class NodeLocalConfigPermissioningControllerTest {
final Consumer<NodeWhitelistUpdatedEvent> consumer = mock(Consumer.class);
final NodeWhitelistUpdatedEvent expectedEvent =
new NodeWhitelistUpdatedEvent(
Collections.emptyList(), Lists.newArrayList(new EnodeURL(enode1)));
Collections.emptyList(), Lists.newArrayList(EnodeURL.fromString(enode1)));
controller.subscribeToListUpdatedEvent(consumer);
controller.removeNodes(Lists.newArrayList(enode1));
@ -352,7 +354,7 @@ public class NodeLocalConfigPermissioningControllerTest {
public void whenRemovingBootnodeShouldReturnRemoveBootnodeError() {
NodesWhitelistResult expected =
new NodesWhitelistResult(WhitelistOperationResult.ERROR_BOOTNODE_CANNOT_BE_REMOVED);
bootnodesList.add(new EnodeURL(enode1));
bootnodesList.add(EnodeURL.fromString(enode1));
controller.addNodes(Lists.newArrayList(enode1, enode2));
NodesWhitelistResult actualResult = controller.removeNodes(Lists.newArrayList(enode1));
@ -370,7 +372,8 @@ public class NodeLocalConfigPermissioningControllerTest {
final Consumer<NodeWhitelistUpdatedEvent> consumer = mock(Consumer.class);
final NodeWhitelistUpdatedEvent expectedEvent =
new NodeWhitelistUpdatedEvent(
Lists.newArrayList(new EnodeURL(enode2)), Lists.newArrayList(new EnodeURL(enode1)));
Lists.newArrayList(EnodeURL.fromString(enode2)),
Lists.newArrayList(EnodeURL.fromString(enode1)));
when(permissioningConfig.getNodePermissioningConfigFilePath())
.thenReturn(permissionsFile.toAbsolutePath().toString());
@ -378,7 +381,7 @@ public class NodeLocalConfigPermissioningControllerTest {
when(permissioningConfig.getNodeWhitelist()).thenReturn(Arrays.asList(URI.create(enode1)));
controller =
new NodeLocalConfigPermissioningController(
permissioningConfig, bootnodesList, new EnodeURL(selfEnode));
permissioningConfig, bootnodesList, EnodeURL.fromString(selfEnode));
controller.subscribeToListUpdatedEvent(consumer);
controller.reload();
@ -401,7 +404,7 @@ public class NodeLocalConfigPermissioningControllerTest {
when(permissioningConfig.getNodeWhitelist()).thenReturn(Arrays.asList(URI.create(enode1)));
controller =
new NodeLocalConfigPermissioningController(
permissioningConfig, bootnodesList, new EnodeURL(selfEnode));
permissioningConfig, bootnodesList, EnodeURL.fromString(selfEnode));
controller.subscribeToListUpdatedEvent(consumer);
controller.reload();

@ -63,9 +63,9 @@ public class SmartContractPermissioningControllerTest {
assertThat(
controller.isPermitted(
new EnodeURL(
EnodeURL.fromString(
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.1:30303"),
new EnodeURL(
EnodeURL.fromString(
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.1:30304")))
.isTrue();
}
@ -79,9 +79,9 @@ public class SmartContractPermissioningControllerTest {
assertThat(
controller.isPermitted(
new EnodeURL(
EnodeURL.fromString(
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.1:30303"),
new EnodeURL(
EnodeURL.fromString(
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.1:30305")))
.isFalse();
}
@ -95,9 +95,9 @@ public class SmartContractPermissioningControllerTest {
assertThat(
controller.isPermitted(
new EnodeURL(
EnodeURL.fromString(
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.1:30302"),
new EnodeURL(
EnodeURL.fromString(
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.1:30304")))
.isFalse();
}
@ -111,9 +111,9 @@ public class SmartContractPermissioningControllerTest {
assertThat(
controller.isPermitted(
new EnodeURL(
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab61@[1:2:3:4:5:6:7:8]:30303"),
new EnodeURL(
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab62@[1:2:3:4:5:6:7:8]:30304")))
.isTrue();
}
@ -127,9 +127,9 @@ public class SmartContractPermissioningControllerTest {
assertThat(
controller.isPermitted(
new EnodeURL(
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab63@[1:2:3:4:5:6:7:8]:30303"),
new EnodeURL(
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab62@[1:2:3:4:5:6:7:8]:30304")))
.isFalse();
}
@ -143,9 +143,9 @@ public class SmartContractPermissioningControllerTest {
assertThat(
controller.isPermitted(
new EnodeURL(
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab61@[1:2:3:4:5:6:7:8]:30303"),
new EnodeURL(
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab63@[1:2:3:4:5:6:7:8]:30304")))
.isFalse();
}

@ -44,7 +44,7 @@ public class NodePermissioningControllerFactoryTest {
private final String enode =
"enode://5f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:1111";
Collection<EnodeURL> bootnodes = Collections.emptyList();
EnodeURL selfEnode = new EnodeURL(enode);
EnodeURL selfEnode = EnodeURL.fromString(enode);
LocalPermissioningConfiguration localPermissioningConfig;
SmartContractPermissioningConfiguration smartContractPermissioningConfiguration;
PermissioningConfiguration config;

@ -40,10 +40,10 @@ import org.mockito.junit.MockitoJUnitRunner;
public class NodePermissioningControllerTest {
private static final EnodeURL enode1 =
new EnodeURL(
EnodeURL.fromString(
"enode://94c15d1b9e2fe7ce56e458b9a3b672ef11894ddedd0c6f247e0f1d3487f52b66208fb4aeb8179fce6e3a749ea93ed147c37976d67af557508d199d9594c35f09@192.168.0.2:1234");
private static final EnodeURL enode2 =
new EnodeURL(
EnodeURL.fromString(
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.3:5678");
@Mock private SyncStatusNodePermissioningProvider syncStatusNodePermissioningProvider;

@ -39,13 +39,13 @@ import org.mockito.junit.MockitoJUnitRunner;
public class SyncStatusNodePermissioningProviderTest {
private static final EnodeURL bootnode =
new EnodeURL(
EnodeURL.fromString(
"enode://6332792c4a00e3e4ee0926ed89e0d27ef985424d97b6a45bf0f23e51f0dcb5e66b875777506458aea7af6f9e4ffb69f43f3778ee73c81ed9d34c51c4b16b0b0f@192.168.0.1:9999");
private static final EnodeURL enode1 =
new EnodeURL(
EnodeURL.fromString(
"enode://94c15d1b9e2fe7ce56e458b9a3b672ef11894ddedd0c6f247e0f1d3487f52b66208fb4aeb8179fce6e3a749ea93ed147c37976d67af557508d199d9594c35f09@192.168.0.2:1234");
private static final EnodeURL enode2 =
new EnodeURL(
EnodeURL.fromString(
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.3:5678");
@Mock private Synchronizer synchronizer;

@ -238,7 +238,7 @@ public class RunnerBuilder {
final List<EnodeURL> bootnodesAsEnodeURLs =
discoveryConfiguration.getBootstrapPeers().stream()
.map(p -> new EnodeURL(p.getEnodeURLString()))
.map(p -> EnodeURL.fromString(p.getEnodeURLString()))
.collect(Collectors.toList());
final Optional<LocalPermissioningConfiguration> localPermissioningConfiguration =

@ -207,7 +207,8 @@ public class PantheonCommand implements DefaultCommandValues, Runnable {
arity = "0..*")
void setBootnodes(final List<String> values) {
try {
bootNodes = values.stream().map((s) -> new EnodeURL(s).toURI()).collect(Collectors.toList());
bootNodes =
values.stream().map((s) -> EnodeURL.fromString(s).toURI()).collect(Collectors.toList());
} catch (final IllegalArgumentException e) {
throw new ParameterException(commandLine, e.getMessage());
}

@ -25,7 +25,7 @@ public class EnodeToURIPropertyConverter implements ITypeConverter<URI> {
private final Function<String, URI> converter;
EnodeToURIPropertyConverter() {
this.converter = (s) -> new EnodeURL(s).toURI();
this.converter = (s) -> EnodeURL.fromString(s).toURI();
}
@VisibleForTesting

@ -41,6 +41,8 @@ public class EnodeURL {
private final String nodeId;
private final InetAddress ip;
private final Integer listeningPort;
// DiscoveryPort will only be present if it differs from listening port, otherwise
// the discovery port is assumed to match the listening port
private final OptionalInt discoveryPort;
public EnodeURL(
@ -71,10 +73,15 @@ public class EnodeURL {
}
this.ip = address;
this.listeningPort = listeningPort;
this.discoveryPort = discoveryPort;
// Only explicitly define a discovery port if it differs from the listening port
if (discoveryPort.isPresent() && discoveryPort.getAsInt() != listeningPort) {
this.discoveryPort = discoveryPort;
} else {
this.discoveryPort = OptionalInt.empty();
}
}
public EnodeURL(final String value) {
public static EnodeURL fromString(final String value) {
checkArgument(
value != null && !value.isEmpty(), "Can't convert null/empty string to EnodeURLProperty.");
@ -83,10 +90,11 @@ public class EnodeURL {
enodeMatcher.matches(),
"Invalid enode URL syntax. Enode URL should have the following format 'enode://<node_id>@<ip>:<listening_port>[?discport=<discovery_port>]'.");
this.nodeId = getAndValidateNodeId(enodeMatcher);
this.ip = getAndValidateIp(enodeMatcher);
this.listeningPort = getAndValidatePort(enodeMatcher, "listening");
this.discoveryPort = getAndValidateDiscoveryPort(enodeMatcher);
final String nodeId = getAndValidateNodeId(enodeMatcher);
final InetAddress ip = getAndValidateIp(enodeMatcher);
final int listeningPort = getAndValidatePort(enodeMatcher, "listening");
final OptionalInt discoveryPort = getAndValidateDiscoveryPort(enodeMatcher);
return new EnodeURL(nodeId, ip, listeningPort, discoveryPort);
}
public URI toURI() {
@ -100,7 +108,7 @@ public class EnodeURL {
}
public static URI asURI(final String url) {
return new EnodeURL(url).toURI();
return fromString(url).toURI();
}
private static String getAndValidateNodeId(final Matcher matcher) {

@ -32,29 +32,47 @@ public class EnodeURLTest {
private final String DISCOVERY_QUERY = "discport=" + DISCOVERY_PORT;
@Test
public void createEnodeURLWithDiscoveryPortShouldBuildExpectedEnodeURLObject() {
public void new_withMatchingDiscoveryAndListeningPorts() {
final EnodeURL enode =
new EnodeURL(VALID_NODE_ID, IPV4_ADDRESS, P2P_PORT, OptionalInt.of(P2P_PORT));
assertThat(enode.getListeningPort()).isEqualTo(P2P_PORT);
// A discovery port matching the listening port should not be explicitly specified
assertThat(enode.getDiscoveryPort()).isEmpty();
}
@Test
public void new_withNonMatchingDiscoveryAndListeningPorts() {
final EnodeURL enode =
new EnodeURL(VALID_NODE_ID, IPV4_ADDRESS, P2P_PORT, OptionalInt.of(DISCOVERY_PORT));
assertThat(enode.getListeningPort()).isEqualTo(P2P_PORT);
// A discovery port matching the listening port should not be explicitly specified
assertThat(enode.getDiscoveryPort()).isEqualTo(OptionalInt.of(DISCOVERY_PORT));
}
@Test
public void fromString_withDiscoveryPortShouldBuildExpectedEnodeURLObject() {
final EnodeURL expectedEnodeURL =
new EnodeURL(VALID_NODE_ID, IPV4_ADDRESS, P2P_PORT, OptionalInt.of(DISCOVERY_PORT));
final String enodeURLString =
"enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":" + P2P_PORT + "?" + DISCOVERY_QUERY;
final EnodeURL enodeURL = new EnodeURL(enodeURLString);
final EnodeURL enodeURL = EnodeURL.fromString(enodeURLString);
assertThat(enodeURL).isEqualTo(expectedEnodeURL);
}
@Test
public void createEnodeURLWithoutDiscoveryPortShouldBuildExpectedEnodeURLObject() {
public void fromString_withoutDiscoveryPortShouldBuildExpectedEnodeURLObject() {
final EnodeURL expectedEnodeURL = new EnodeURL(VALID_NODE_ID, IPV4_ADDRESS, P2P_PORT);
final String enodeURLString = "enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":" + P2P_PORT;
final EnodeURL enodeURL = new EnodeURL(enodeURLString);
final EnodeURL enodeURL = EnodeURL.fromString(enodeURLString);
assertThat(enodeURL).isEqualTo(expectedEnodeURL);
}
@Test
public void createEnodeURLWithIPV6ShouldBuildExpectedEnodeURLObject() {
public void fromString_withIPV6ShouldBuildExpectedEnodeURLObject() {
final EnodeURL expectedEnodeURL =
new EnodeURL(VALID_NODE_ID, IPV6_FULL_ADDRESS, P2P_PORT, OptionalInt.of(DISCOVERY_PORT));
final String enodeURLString =
@ -67,13 +85,13 @@ public class EnodeURLTest {
+ "?"
+ DISCOVERY_QUERY;
final EnodeURL enodeURL = new EnodeURL(enodeURLString);
final EnodeURL enodeURL = EnodeURL.fromString(enodeURLString);
assertThat(enodeURL).isEqualTo(expectedEnodeURL);
}
@Test
public void createEnodeURLWithIPV6InCompactFormShouldBuildExpectedEnodeURLObject() {
public void fromString_ithIPV6InCompactFormShouldBuildExpectedEnodeURLObject() {
final EnodeURL expectedEnodeURL =
new EnodeURL(VALID_NODE_ID, IPV6_COMPACT_ADDRESS, P2P_PORT, OptionalInt.of(DISCOVERY_PORT));
final String enodeURLString =
@ -86,15 +104,15 @@ public class EnodeURLTest {
+ "?"
+ DISCOVERY_QUERY;
final EnodeURL enodeURL = new EnodeURL(enodeURLString);
final EnodeURL enodeURL = EnodeURL.fromString(enodeURLString);
assertThat(enodeURL).isEqualTo(expectedEnodeURL);
}
@Test
public void createEnodeURLWithoutNodeIdShouldFail() {
public void fromString_withoutNodeIdShouldFail() {
final String enodeURLString = "enode://@" + IPV4_ADDRESS + ":" + P2P_PORT;
final Throwable thrown = catchThrowable(() -> new EnodeURL(enodeURLString));
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(enodeURLString));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -103,9 +121,9 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithInvalidSizeNodeIdShouldFail() {
public void fromString_withInvalidSizeNodeIdShouldFail() {
final String enodeURLString = "enode://wrong_size_string@" + IPV4_ADDRESS + ":" + P2P_PORT;
final Throwable thrown = catchThrowable(() -> new EnodeURL(enodeURLString));
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(enodeURLString));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -114,13 +132,13 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithInvalidHexCharacterNodeIdShouldFail() {
public void fromString_withInvalidHexCharacterNodeIdShouldFail() {
final String enodeURLString =
"enode://0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000@"
+ IPV4_ADDRESS
+ ":"
+ P2P_PORT;
final Throwable thrown = catchThrowable(() -> new EnodeURL(enodeURLString));
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(enodeURLString));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -129,9 +147,9 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithoutIpShouldFail() {
public void fromString_withoutIpShouldFail() {
final String enodeURLString = "enode://" + VALID_NODE_ID + "@:" + P2P_PORT;
final Throwable thrown = catchThrowable(() -> new EnodeURL(enodeURLString));
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(enodeURLString));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -139,9 +157,9 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithInvalidIpFormatShouldFail() {
public void fromString_withInvalidIpFormatShouldFail() {
final String enodeURLString = "enode://" + VALID_NODE_ID + "@192.0.1:" + P2P_PORT;
final Throwable thrown = catchThrowable(() -> new EnodeURL(enodeURLString));
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(enodeURLString));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -149,9 +167,9 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithoutListeningPortShouldFail() {
public void fromString_withoutListeningPortShouldFail() {
final String enodeURLString = "enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":";
final Throwable thrown = catchThrowable(() -> new EnodeURL(enodeURLString));
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(enodeURLString));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -160,9 +178,9 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithoutListeningPortAndWithDiscoveryPortShouldFail() {
public void fromString_withoutListeningPortAndWithDiscoveryPortShouldFail() {
final String enodeURLString = "enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":?30301";
final Throwable thrown = catchThrowable(() -> new EnodeURL(enodeURLString));
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(enodeURLString));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -171,9 +189,9 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithAboveRangeListeningPortShouldFail() {
public void fromString_withAboveRangeListeningPortShouldFail() {
final String enodeURLString = "enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":98765";
final Throwable thrown = catchThrowable(() -> new EnodeURL(enodeURLString));
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(enodeURLString));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -181,10 +199,10 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithAboveRangeDiscoveryPortShouldFail() {
public void fromString_withAboveRangeDiscoveryPortShouldFail() {
final String enodeURLString =
"enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":" + P2P_PORT + "?discport=98765";
final Throwable thrown = catchThrowable(() -> new EnodeURL(enodeURLString));
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(enodeURLString));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -192,8 +210,8 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithNullEnodeURLShouldFail() {
final Throwable thrown = catchThrowable(() -> new EnodeURL(null));
public void fromString_withNullEnodeURLShouldFail() {
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(null));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -201,8 +219,8 @@ public class EnodeURLTest {
}
@Test
public void createEnodeURLWithEmptyEnodeURLShouldFail() {
final Throwable thrown = catchThrowable(() -> new EnodeURL(""));
public void fromString_withEmptyEnodeURLShouldFail() {
final Throwable thrown = catchThrowable(() -> EnodeURL.fromString(""));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
@ -210,20 +228,20 @@ public class EnodeURLTest {
}
@Test
public void toURIWithDiscoveryPortCreateExpectedURI() {
public void toURI_WithDiscoveryPortCreateExpectedURI() {
final String enodeURLString =
"enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":" + P2P_PORT + "?" + DISCOVERY_QUERY;
final URI expectedURI = URI.create(enodeURLString);
final URI createdURI = new EnodeURL(enodeURLString).toURI();
final URI createdURI = EnodeURL.fromString(enodeURLString).toURI();
assertThat(createdURI).isEqualTo(expectedURI);
}
@Test
public void toURIWithoutDiscoveryPortCreateExpectedURI() {
public void toURI_WithoutDiscoveryPortCreateExpectedURI() {
final String enodeURLString = "enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":" + P2P_PORT;
final URI expectedURI = URI.create(enodeURLString);
final URI createdURI = new EnodeURL(enodeURLString).toURI();
final URI createdURI = EnodeURL.fromString(enodeURLString).toURI();
assertThat(createdURI).isEqualTo(expectedURI);
}

Loading…
Cancel
Save