mirror of
https://github.com/vacp2p/linea-besu.git
synced 2026-01-09 21:17:54 -05:00
Create EnodeURL Builder (#1275)
Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
This commit is contained in:
@@ -45,6 +45,7 @@ import tech.pegasys.pantheon.metrics.LabelledMetric;
|
||||
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;
|
||||
@@ -653,10 +654,14 @@ public class NettyP2PNetwork implements P2PNetwork {
|
||||
}
|
||||
|
||||
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
|
||||
@@ -734,7 +739,7 @@ public class NettyP2PNetwork implements P2PNetwork {
|
||||
}
|
||||
|
||||
private EnodeURL buildSelfEnodeURL() {
|
||||
final String nodeId = ourPeerInfo.getNodeId().toUnprefixedString();
|
||||
final BytesValue nodeId = ourPeerInfo.getNodeId();
|
||||
final int listeningPort = ourPeerInfo.getPort();
|
||||
final OptionalInt discoveryPort =
|
||||
peerDiscoveryAgent
|
||||
@@ -743,7 +748,12 @@ public class NettyP2PNetwork implements P2PNetwork {
|
||||
.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) {
|
||||
|
||||
@@ -17,8 +17,6 @@ import tech.pegasys.pantheon.ethereum.rlp.RLPOutput;
|
||||
import tech.pegasys.pantheon.util.bytes.BytesValue;
|
||||
import tech.pegasys.pantheon.util.enode.EnodeURL;
|
||||
|
||||
import java.util.OptionalInt;
|
||||
|
||||
public interface Peer extends PeerId {
|
||||
|
||||
/**
|
||||
@@ -68,14 +66,11 @@ public interface Peer extends PeerId {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -2339,16 +2339,19 @@ public class PantheonCommandTest extends CommandTestAbstract {
|
||||
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));
|
||||
|
||||
@@ -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
|
||||
@@ -78,6 +62,10 @@ public class 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.");
|
||||
@@ -91,7 +79,12 @@ public class EnodeURL {
|
||||
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() {
|
||||
@@ -201,4 +194,53 @@ public class EnodeURL {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
@@ -43,7 +48,12 @@ public class EnodeURLTest {
|
||||
@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));
|
||||
@@ -52,7 +62,12 @@ public class EnodeURLTest {
|
||||
@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;
|
||||
|
||||
@@ -63,7 +78,12 @@ public class EnodeURLTest {
|
||||
|
||||
@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);
|
||||
@@ -74,7 +94,12 @@ public class EnodeURLTest {
|
||||
@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
|
||||
@@ -93,7 +118,12 @@ public class EnodeURLTest {
|
||||
@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
|
||||
|
||||
Reference in New Issue
Block a user