Skip to content

Commit

Permalink
Adjust discovery code readability after review
Browse files Browse the repository at this point in the history
  • Loading branch information
cupuyc committed Oct 14, 2016
1 parent 4f1befa commit 768edc5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
Expand Up @@ -73,7 +73,7 @@ public HandshakeHandler(final SystemProperties config, final NodeManager nodeMan
public void channelActive(ChannelHandlerContext ctx) throws Exception {
channel.setInetSocketAddress((InetSocketAddress) ctx.channel().remoteAddress());
if (remoteId.length == 64) {
channel.setNode(remoteId);
channel.initWithNode(remoteId);
initiate(ctx);
} else {
handshake = new EncryptionHandshake();
Expand Down Expand Up @@ -255,9 +255,10 @@ private void decodeHandshake(final ChannelHandlerContext ctx, ByteBuf buffer) th
}

final HelloMessage inboundHelloMessage = (HelloMessage) message;
channel.setNode(remoteId, inboundHelloMessage.getListenPort());
// use node into discovery
nodeManager.getNodeHandler(channel.getNode());

// now we know both remote nodeId and port
// let's set node, that will cause registering node in NodeManager
channel.initWithNode(remoteId, inboundHelloMessage.getListenPort());

// Secret authentication finish here
channel.sendHelloMessage(ctx, frameCodec, Hex.toHexString(nodeId), inboundHelloMessage);
Expand Down
19 changes: 14 additions & 5 deletions ethereumj-core/src/main/java/org/ethereum/net/rlpx/Node.java
@@ -1,6 +1,6 @@
package org.ethereum.net.rlpx;

import com.google.common.base.Strings;
import org.ethereum.crypto.ECKey;
import org.ethereum.datasource.mapdb.Serializers;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.util.RLP;
Expand All @@ -16,13 +16,12 @@
import java.net.UnknownHostException;
import java.util.Arrays;

import static org.ethereum.crypto.HashUtil.sha3;
import static org.ethereum.util.ByteUtil.byteArrayToInt;

public class Node implements Serializable {
private static final long serialVersionUID = -4267600517925770636L;

public static final String DISCOVERY_NODE_PREFIX = Strings.repeat("0", 128);

public static final Serializer<Node> MapDBSerializer = new Serializer<Node>() {
@Override
public void serialize(DataOutput out, Node value) throws IOException {
Expand Down Expand Up @@ -51,6 +50,16 @@ public Node deserialize(DataInput in, int available) throws IOException {
byte[] id;
String host;
int port;
// discovery endpoint doesn't have real nodeId for example
private boolean isFakeNodeId = false;

public static Node createWithoutId(String address) {
final ECKey generatedNodeKey = ECKey.fromPrivate(sha3(address.getBytes()));
final String generatedNodeId = Hex.toHexString(generatedNodeKey.getNodeId());
final Node node = new Node("enode://" + generatedNodeId + "@" + address);
node.isFakeNodeId = true;
return node;
}

public Node(String enodeURL) {
try {
Expand All @@ -72,6 +81,7 @@ public Node(byte[] id, String host, int port) {
this.port = port;
}


public Node(byte[] rlp) {

RLPList nodeRLP = RLP.decode2(rlp);
Expand Down Expand Up @@ -109,8 +119,7 @@ public Node(byte[] rlp) {
* @return true if this node is endpoint for discovery loaded from config
*/
public boolean isDiscoveryNode() {
// discovery node has nodeId with doubled size
return id.length > 64;
return isFakeNodeId;
}


Expand Down
Expand Up @@ -207,13 +207,13 @@ public synchronized NodeHandler getNodeHandler(Node n) {
ethereumListener.onNodeDiscovered(ret.getNode());
}
} else if (ret.getNode().isDiscoveryNode() && !n.isDiscoveryNode()) {
// if we found discovery node with same host:port,
// then replace node with correct nodeId
// we found discovery node with same host:port,
// replace node with correct nodeId
ret.node = n;
if (!n.getHexId().equals(homeNode.getHexId())) {
ethereumListener.onNodeDiscovered(ret.getNode());
}
logger.debug("Found real nodeId for discovery endpoint {}", n);
logger.debug(" +++ Found real nodeId for discovery endpoint {}", n);
}

return ret;
Expand Down
Expand Up @@ -90,9 +90,7 @@ public void start(String[] args) throws Exception {

for (String boot: args) {
// since discover IP list has no NodeIds we will generate random but persistent
final ECKey generatedNodeKey = ECKey.fromPrivate(sha3(boot.getBytes()));
final String generatedNodeId = Node.DISCOVERY_NODE_PREFIX + Hex.toHexString(generatedNodeKey.getNodeId());
bootNodes.add(new Node("enode://" + generatedNodeId + "@" + boot));
bootNodes.add(Node.createWithoutId(boot));
}

nodeManager.setBootNodes(bootNodes);
Expand Down
Expand Up @@ -216,13 +216,16 @@ public NodeStatistics getNodeStatistics() {
return nodeStatistics;
}

public void setNode(byte[] nodeId, int remotePort) {
/**
* Set node and register it in NodeManager if it is not registered yet.
*/
public void initWithNode(byte[] nodeId, int remotePort) {
node = new Node(nodeId, inetSocketAddress.getHostString(), remotePort);
nodeStatistics = nodeManager.getNodeStatistics(node);
}

public void setNode(byte[] nodeId) {
setNode(nodeId, inetSocketAddress.getPort());
public void initWithNode(byte[] nodeId) {
initWithNode(nodeId, inetSocketAddress.getPort());
}

public Node getNode() {
Expand Down

0 comments on commit 768edc5

Please sign in to comment.