Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Create EnodeURL builder #1275

Merged
merged 2 commits into from
Apr 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import tech.pegasys.pantheon.metrics.MetricCategory;
import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.util.Subscribers;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.enode.EnodeURL;

import java.net.InetAddress;
Expand Down Expand Up @@ -653,10 +654,14 @@ private boolean isPeerAllowed(final Peer peer) {
}

private EnodeURL peerInfoToEnodeURL(final PeerInfo ourPeerInfo, final InetSocketAddress address) {
final String localNodeId = ourPeerInfo.getNodeId().toString().substring(2);
final BytesValue localNodeId = ourPeerInfo.getNodeId();
final InetAddress localHostAddress = address.getAddress();
final int localPort = ourPeerInfo.getPort();
return new EnodeURL(localNodeId, localHostAddress, localPort);
return EnodeURL.builder()
.nodeId(localNodeId)
.ipAddress(localHostAddress)
.listeningPort(localPort)
.build();
}

@VisibleForTesting
Expand Down Expand Up @@ -734,7 +739,7 @@ public Optional<EnodeURL> getSelfEnodeURL() {
}

private EnodeURL buildSelfEnodeURL() {
final String nodeId = ourPeerInfo.getNodeId().toUnprefixedString();
final BytesValue nodeId = ourPeerInfo.getNodeId();
final int listeningPort = ourPeerInfo.getPort();
final OptionalInt discoveryPort =
peerDiscoveryAgent
Expand All @@ -743,7 +748,12 @@ private EnodeURL buildSelfEnodeURL() {
.filter(port -> port.getAsInt() != listeningPort)
.orElse(OptionalInt.empty());

return new EnodeURL(nodeId, advertisedHost, listeningPort, discoveryPort);
return EnodeURL.builder()
.nodeId(nodeId)
.ipAddress(advertisedHost)
.listeningPort(listeningPort)
.discoveryPort(discoveryPort)
.build();
}

private void onConnectionEstablished(final PeerConnection connection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.enode.EnodeURL;

import java.util.OptionalInt;

public interface Peer extends PeerId {

/**
Expand Down Expand Up @@ -68,14 +66,11 @@ default EnodeURL getEnodeURL() {
final int tcpPort = endpoint.getFunctionalTcpPort();
final int udpPort = endpoint.getUdpPort();

if (tcpPort != udpPort) {
return new EnodeURL(
this.getId().toUnprefixedString(),
endpoint.getHost(),
tcpPort,
OptionalInt.of(endpoint.getUdpPort()));
} else {
return new EnodeURL(this.getId().toUnprefixedString(), endpoint.getHost(), udpPort);
}
return EnodeURL.builder()
.nodeId(this.getId())
.ipAddress(endpoint.getHost())
.listeningPort(tcpPort)
.discoveryPort(udpPort)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,29 @@ public class StaticNodesParserTest {
// First peer ion the valid_static_nodes file.
private final List<EnodeURL> validFileItems =
Lists.newArrayList(
new EnodeURL(
"50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa",
"127.0.0.1",
30303),
new EnodeURL(
"02beb46bc17227616be44234071dfa18516684e45eed88049190b6cb56b0bae218f045fd0450f123b8f55c60b96b78c45e8e478004293a8de6818aa4e02eff97",
"127.0.0.1",
30304),
new EnodeURL(
"819e5cbd81f123516b10f04bf620daa2b385efef06d77253148b814bf1bb6197ff58ebd1fd7bf5dc765b49a4440c733bf941e479c800173f2bfeb887e4fbcbc2",
"127.0.0.1",
30305),
new EnodeURL(
"6cf53e25d2a98a22e7e205a86bda7077e3c8a7bc99e5ff88ddfd2037a550969ab566f069ffa455df0cfae0c21f7aec3447e414eccc473a3e8b20984b90f164ac",
"127.0.0.1",
30306));
EnodeURL.builder()
.nodeId(
"50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa")
.ipAddress("127.0.0.1")
.build(),
EnodeURL.builder()
.nodeId(
"02beb46bc17227616be44234071dfa18516684e45eed88049190b6cb56b0bae218f045fd0450f123b8f55c60b96b78c45e8e478004293a8de6818aa4e02eff97")
.ipAddress("127.0.0.1")
.listeningPort(30304)
.build(),
EnodeURL.builder()
.nodeId(
"819e5cbd81f123516b10f04bf620daa2b385efef06d77253148b814bf1bb6197ff58ebd1fd7bf5dc765b49a4440c733bf941e479c800173f2bfeb887e4fbcbc2")
.ipAddress("127.0.0.1")
.listeningPort(30305)
.build(),
EnodeURL.builder()
.nodeId(
"6cf53e25d2a98a22e7e205a86bda7077e3c8a7bc99e5ff88ddfd2037a550969ab566f069ffa455df0cfae0c21f7aec3447e414eccc473a3e8b20984b90f164ac")
.ipAddress("127.0.0.1")
.listeningPort(30306)
.build());

@Rule public TemporaryFolder testFolder = new TemporaryFolder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ public class RunnerBuilder {
private Collection<EnodeURL> staticNodes = Collections.emptyList();

private EnodeURL getSelfEnode() {
String nodeId = pantheonController.getLocalNodeKeyPair().getPublicKey().toString();
return new EnodeURL(nodeId, discoveryHost, listenPort);
BytesValue nodeId = pantheonController.getLocalNodeKeyPair().getPublicKey().getEncodedBytes();
return EnodeURL.builder()
.nodeId(nodeId)
.ipAddress(discoveryHost)
.listeningPort(listenPort)
.build();
}

public RunnerBuilder vertx(final Vertx vertx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2339,16 +2339,19 @@ public void errorIsRaisedIfStaticNodesAreNotWhitelisted() throws IOException {
permissioningConfig.deleteOnExit();

final EnodeURL staticNodeURI =
new EnodeURL(
"50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa",
"127.0.0.1",
30303);
EnodeURL.builder()
.nodeId(
"50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa")
.ipAddress("127.0.0.1")
.build();

final EnodeURL whiteListedNode =
new EnodeURL(
"50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa",
"127.0.0.1",
30304);
EnodeURL.builder()
.nodeId(
"50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa")
.ipAddress("127.0.0.1")
.listeningPort(30304)
.build();

Files.write(
staticNodesFile.toPath(), ("[\"" + staticNodeURI.toString() + "\"]").getBytes(UTF_8));
Expand Down
82 changes: 62 additions & 20 deletions util/src/main/java/tech/pegasys/pantheon/util/enode/EnodeURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,12 @@ public class EnodeURL {
// the discovery port is assumed to match the listening port
private final OptionalInt discoveryPort;

public EnodeURL(
final String nodeId,
final String ip,
final Integer listeningPort,
final OptionalInt discoveryPort) {
this(nodeId, InetAddresses.forUriString(ip), listeningPort, discoveryPort);
}

public EnodeURL(final String nodeId, final String ip, final Integer listeningPort) {
this(nodeId, ip, listeningPort, OptionalInt.empty());
}

public EnodeURL(final String nodeId, final InetAddress address, final Integer listeningPort) {
this(nodeId, address, listeningPort, OptionalInt.empty());
}

public EnodeURL(
final String nodeId,
private EnodeURL(
final BytesValue nodeId,
final InetAddress address,
final Integer listeningPort,
final OptionalInt discoveryPort) {
this.nodeId = BytesValue.fromHexString(nodeId);
this.nodeId = nodeId;
this.ip = address;
this.listeningPort = listeningPort;
// Only explicitly define a discovery port if it differs from the listening port
Expand All @@ -78,6 +62,10 @@ public EnodeURL(
}
}

public static Builder builder() {
return new Builder();
}

public static EnodeURL fromString(final String value) {
checkArgument(
value != null && !value.isEmpty(), "Can't convert null/empty string to EnodeURLProperty.");
Expand All @@ -91,7 +79,12 @@ public static EnodeURL fromString(final String value) {
final InetAddress ip = getAndValidateIp(enodeMatcher);
final int listeningPort = getAndValidatePort(enodeMatcher, "listening");
final OptionalInt discoveryPort = getAndValidateDiscoveryPort(enodeMatcher);
return new EnodeURL(nodeId, ip, listeningPort, discoveryPort);
return builder()
.nodeId(nodeId)
.ipAddress(ip)
.listeningPort(listeningPort)
.discoveryPort(discoveryPort)
.build();
}

public URI toURI() {
Expand Down Expand Up @@ -201,4 +194,53 @@ public int hashCode() {
public String toString() {
return this.toURI().toString();
}

public static class Builder {

private BytesValue nodeId;
private Integer listeningPort = 30303;
private OptionalInt discoveryPort = OptionalInt.empty();
private InetAddress ip;

private Builder() {};

public EnodeURL build() {
return new EnodeURL(nodeId, ip, listeningPort, discoveryPort);
}

public Builder nodeId(final BytesValue nodeId) {
this.nodeId = nodeId;
return this;
}

public Builder nodeId(final String nodeId) {
this.nodeId = BytesValue.fromHexString(nodeId);
return this;
}

public Builder ipAddress(final InetAddress ip) {
this.ip = ip;
return this;
}

public Builder ipAddress(final String ip) {
this.ip = InetAddresses.forUriString(ip);
return this;
}

public Builder listeningPort(final Integer listeningPort) {
this.listeningPort = listeningPort;
return this;
}

public Builder discoveryPort(final OptionalInt discoveryPort) {
this.discoveryPort = discoveryPort;
return this;
}

public Builder discoveryPort(final int discoveryPort) {
this.discoveryPort = OptionalInt.of(discoveryPort);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public class EnodeURLTest {
@Test
public void new_withMatchingDiscoveryAndListeningPorts() {
final EnodeURL enode =
new EnodeURL(VALID_NODE_ID, IPV4_ADDRESS, P2P_PORT, OptionalInt.of(P2P_PORT));
EnodeURL.builder()
.nodeId(VALID_NODE_ID)
.ipAddress(IPV4_ADDRESS)
.listeningPort(P2P_PORT)
.discoveryPort(OptionalInt.of(P2P_PORT))
.build();
assertThat(enode.getListeningPort()).isEqualTo(P2P_PORT);
// A discovery port matching the listening port should not be explicitly specified
assertThat(enode.getDiscoveryPort()).isEmpty();
Expand All @@ -43,7 +48,12 @@ public void new_withMatchingDiscoveryAndListeningPorts() {
@Test
public void new_withNonMatchingDiscoveryAndListeningPorts() {
final EnodeURL enode =
new EnodeURL(VALID_NODE_ID, IPV4_ADDRESS, P2P_PORT, OptionalInt.of(DISCOVERY_PORT));
EnodeURL.builder()
.nodeId(VALID_NODE_ID)
.ipAddress(IPV4_ADDRESS)
.listeningPort(P2P_PORT)
.discoveryPort(OptionalInt.of(DISCOVERY_PORT))
.build();
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));
Expand All @@ -52,7 +62,12 @@ public void new_withNonMatchingDiscoveryAndListeningPorts() {
@Test
public void fromString_withDiscoveryPortShouldBuildExpectedEnodeURLObject() {
final EnodeURL expectedEnodeURL =
new EnodeURL(VALID_NODE_ID, IPV4_ADDRESS, P2P_PORT, OptionalInt.of(DISCOVERY_PORT));
EnodeURL.builder()
.nodeId(VALID_NODE_ID)
.ipAddress(IPV4_ADDRESS)
.listeningPort(P2P_PORT)
.discoveryPort(OptionalInt.of(DISCOVERY_PORT))
.build();
final String enodeURLString =
"enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":" + P2P_PORT + "?" + DISCOVERY_QUERY;

Expand All @@ -63,7 +78,12 @@ public void fromString_withDiscoveryPortShouldBuildExpectedEnodeURLObject() {

@Test
public void fromString_withoutDiscoveryPortShouldBuildExpectedEnodeURLObject() {
final EnodeURL expectedEnodeURL = new EnodeURL(VALID_NODE_ID, IPV4_ADDRESS, P2P_PORT);
final EnodeURL expectedEnodeURL =
EnodeURL.builder()
.nodeId(VALID_NODE_ID)
.ipAddress(IPV4_ADDRESS)
.listeningPort(P2P_PORT)
.build();
final String enodeURLString = "enode://" + VALID_NODE_ID + "@" + IPV4_ADDRESS + ":" + P2P_PORT;

final EnodeURL enodeURL = EnodeURL.fromString(enodeURLString);
Expand All @@ -74,7 +94,12 @@ public void fromString_withoutDiscoveryPortShouldBuildExpectedEnodeURLObject() {
@Test
public void fromString_withIPV6ShouldBuildExpectedEnodeURLObject() {
final EnodeURL expectedEnodeURL =
new EnodeURL(VALID_NODE_ID, IPV6_FULL_ADDRESS, P2P_PORT, OptionalInt.of(DISCOVERY_PORT));
EnodeURL.builder()
.nodeId(VALID_NODE_ID)
.ipAddress(IPV6_FULL_ADDRESS)
.listeningPort(P2P_PORT)
.discoveryPort(OptionalInt.of(DISCOVERY_PORT))
.build();
final String enodeURLString =
"enode://"
+ VALID_NODE_ID
Expand All @@ -93,7 +118,12 @@ public void fromString_withIPV6ShouldBuildExpectedEnodeURLObject() {
@Test
public void fromString_ithIPV6InCompactFormShouldBuildExpectedEnodeURLObject() {
final EnodeURL expectedEnodeURL =
new EnodeURL(VALID_NODE_ID, IPV6_COMPACT_ADDRESS, P2P_PORT, OptionalInt.of(DISCOVERY_PORT));
EnodeURL.builder()
.nodeId(VALID_NODE_ID)
.ipAddress(IPV6_COMPACT_ADDRESS)
.listeningPort(P2P_PORT)
.discoveryPort(OptionalInt.of(DISCOVERY_PORT))
.build();
final String enodeURLString =
"enode://"
+ VALID_NODE_ID
Expand Down