Keep enode nodeId stored as a BytesValue (#1274)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
mbaxter 6 years ago committed by GitHub
parent 545578983b
commit c7947b8185
  1. 2
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/api/PeerConnection.java
  2. 2
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/peers/DefaultPeer.java
  3. 3
      ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/peers/StaticNodesParserTest.java
  4. 10
      ethereum/permissioning/src/main/java/tech/pegasys/pantheon/ethereum/permissioning/SmartContractPermissioningController.java
  5. 15
      util/src/main/java/tech/pegasys/pantheon/util/enode/EnodeURL.java

@ -102,7 +102,7 @@ public interface PeerConnection {
} }
default boolean isRemoteEnode(final EnodeURL remoteEnodeUrl) { default boolean isRemoteEnode(final EnodeURL remoteEnodeUrl) {
return ((remoteEnodeUrl.getNodeId().equals(this.getPeer().getAddress().toString())) return ((remoteEnodeUrl.getNodeId().equals(this.getPeer().getAddress()))
&& (remoteEnodeUrl.getListeningPort() == this.getPeer().getPort()) && (remoteEnodeUrl.getListeningPort() == this.getPeer().getPort())
&& (remoteEnodeUrl && (remoteEnodeUrl
.getInetAddress() .getInetAddress()

@ -52,7 +52,7 @@ public class DefaultPeer extends DefaultPeerId implements Peer {
udpPort, udpPort,
OptionalInt.of(enodeURL.getListeningPort())); OptionalInt.of(enodeURL.getListeningPort()));
return new DefaultPeer(BytesValue.fromHexString(enodeURL.getNodeId()), endpoint); return new DefaultPeer(enodeURL.getNodeId(), endpoint);
} }
/** /**

@ -67,7 +67,8 @@ public class StaticNodesParserTest {
final File validFile = new File(resource.getFile()); final File validFile = new File(resource.getFile());
final Set<EnodeURL> enodes = StaticNodesParser.fromPath(validFile.toPath()); final Set<EnodeURL> enodes = StaticNodesParser.fromPath(validFile.toPath());
assertThat(enodes).containsExactly(validFileItems.toArray(new EnodeURL[validFileItems.size()])); assertThat(enodes)
.containsExactlyInAnyOrder(validFileItems.toArray(new EnodeURL[validFileItems.size()]));
} }
@Test @Test

@ -125,9 +125,7 @@ public class SmartContractPermissioningController implements NodePermissioningPr
private static BytesValue encodeEnodeUrl(final EnodeURL enode) { private static BytesValue encodeEnodeUrl(final EnodeURL enode) {
return BytesValues.concatenate( return BytesValues.concatenate(
encodeEnodeId(enode.getNodeId()), enode.getNodeId(), encodeIp(enode.getInetAddress()), encodePort(enode.getListeningPort()));
encodeIp(enode.getInetAddress()),
encodePort(enode.getListeningPort()));
} }
// As a function parameter an ip needs to be the appropriate number of bytes, big endian, and // As a function parameter an ip needs to be the appropriate number of bytes, big endian, and
@ -156,10 +154,4 @@ public class SmartContractPermissioningController implements NodePermissioningPr
res[30] = (byte) ((port >> 8) & 0xFF); res[30] = (byte) ((port >> 8) & 0xFF);
return BytesValue.wrap(res); return BytesValue.wrap(res);
} }
// The enode high and low need to be 32 bytes each. They then get concatenated as they are
// adjacent parameters
private static BytesValue encodeEnodeId(final String id) {
return BytesValue.fromHexString(id);
}
} }

@ -15,6 +15,7 @@ package tech.pegasys.pantheon.util.enode;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import tech.pegasys.pantheon.util.NetworkUtility; import tech.pegasys.pantheon.util.NetworkUtility;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.URI; import java.net.URI;
@ -38,7 +39,7 @@ public class EnodeURL {
+ "(?<listening>\\d+)" + "(?<listening>\\d+)"
+ "(\\?discport=(?<discovery>\\d+))?$"; + "(\\?discport=(?<discovery>\\d+))?$";
private final String nodeId; private final BytesValue nodeId;
private final InetAddress ip; private final InetAddress ip;
private final Integer listeningPort; private final Integer listeningPort;
// DiscoveryPort will only be present if it differs from listening port, otherwise // DiscoveryPort will only be present if it differs from listening port, otherwise
@ -66,11 +67,7 @@ public class EnodeURL {
final InetAddress address, final InetAddress address,
final Integer listeningPort, final Integer listeningPort,
final OptionalInt discoveryPort) { final OptionalInt discoveryPort) {
if (nodeId.startsWith("0x")) { this.nodeId = BytesValue.fromHexString(nodeId);
this.nodeId = nodeId.substring(2);
} else {
this.nodeId = nodeId;
}
this.ip = address; this.ip = address;
this.listeningPort = listeningPort; this.listeningPort = listeningPort;
// Only explicitly define a discovery port if it differs from the listening port // Only explicitly define a discovery port if it differs from the listening port
@ -99,7 +96,9 @@ public class EnodeURL {
public URI toURI() { public URI toURI() {
final String uri = final String uri =
String.format("enode://%s@%s:%d", nodeId, InetAddresses.toUriString(ip), listeningPort); String.format(
"enode://%s@%s:%d",
nodeId.toUnprefixedString(), InetAddresses.toUriString(ip), listeningPort);
if (discoveryPort.isPresent()) { if (discoveryPort.isPresent()) {
return URI.create(uri + String.format("?discport=%d", discoveryPort.getAsInt())); return URI.create(uri + String.format("?discport=%d", discoveryPort.getAsInt()));
} else { } else {
@ -152,7 +151,7 @@ public class EnodeURL {
} }
} }
public String getNodeId() { public BytesValue getNodeId() {
return nodeId; return nodeId;
} }

Loading…
Cancel
Save