use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.
the class NodeDistanceTableTest method getCloseNodes.
@Test
public void getCloseNodes() {
Node node1 = new Node(Hex.decode(NODE_ID_1), HOST, PORT_1);
Node node2 = new Node(Hex.decode(NODE_ID_2), HOST, PORT_2);
Node node3 = new Node(Hex.decode(NODE_ID_3), HOST, PORT_3);
NodeDistanceTable table = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node1);
Assert.assertTrue(table.addNode(node1).isSuccess());
Assert.assertTrue(table.addNode(node2).isSuccess());
Assert.assertTrue(table.addNode(node3).isSuccess());
List<Node> sortedNodes = table.getClosestNodes(node2.getId());
Assert.assertEquals(3, sortedNodes.size());
DistanceCalculator calculator = new DistanceCalculator(KademliaOptions.BINS);
int d1 = calculator.calculateDistance(node2.getId(), sortedNodes.get(0).getId());
int d2 = calculator.calculateDistance(node2.getId(), sortedNodes.get(1).getId());
int d3 = calculator.calculateDistance(node2.getId(), sortedNodes.get(2).getId());
Assert.assertTrue(d1 <= d2);
Assert.assertTrue(d2 <= d3);
}
use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.
the class PeerExplorer method addConnection.
private void addConnection(PongPeerMessage message, String ip, int port) {
Node senderNode = new Node(message.getNodeId().getID(), ip, port);
if (!StringUtils.equals(senderNode.getHexId(), this.localNode.getHexId())) {
OperationResult result = this.distanceTable.addNode(senderNode);
if (result.isSuccess()) {
NodeID senderId = senderNode.getId();
this.establishedConnections.put(senderId, senderNode);
logger.debug("New Peer found ip:[{}] port[{}]", ip, port);
} else {
this.challengeManager.startChallenge(result.getAffectedEntry().getNode(), senderNode, this);
}
}
}
use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.
the class PeerExplorer method handleFindNode.
public void handleFindNode(FindNodePeerMessage message) {
NodeID nodeId = message.getNodeId();
Node connectedNode = this.establishedConnections.get(nodeId);
if (connectedNode != null) {
List<Node> nodesToSend = this.distanceTable.getClosestNodes(nodeId);
logger.debug("About to send [{}] neighbors to ip[{}] port[{}] nodeId[{}]", nodesToSend.size(), connectedNode.getHost(), connectedNode.getPort(), connectedNode.getHexIdShort());
this.sendNeighbors(connectedNode.getAddress(), nodesToSend, message.getMessageId());
updateEntry(connectedNode);
}
}
use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.
the class PeerExplorer method handleNeighborsMessage.
public void handleNeighborsMessage(NeighborsPeerMessage message) {
Node connectedNode = this.establishedConnections.get(message.getNodeId());
if (connectedNode != null) {
logger.debug("Neighbors received from [{}]", connectedNode.getHexIdShort());
PeerDiscoveryRequest request = this.pendingFindNodeRequests.remove(message.getMessageId());
if (request != null && request.validateMessageResponse(message)) {
List<Node> nodes = (message.countNodes() > MAX_NODES_PER_MSG) ? message.getNodes().subList(0, MAX_NODES_PER_MSG - 1) : message.getNodes();
nodes.stream().filter(n -> !StringUtils.equals(n.getHexId(), this.localNode.getHexId())).forEach(node -> this.bootNodes.add(new InetSocketAddress(node.getAddress().getHostName(), node.getPort())));
this.startConversationWithNewNodes();
}
updateEntry(connectedNode);
}
}
use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.
the class DefaultConfig method peerExplorer.
@Bean
public PeerExplorer peerExplorer(RskSystemProperties rskConfig) {
ECKey key = rskConfig.getMyKey();
Node localNode = new Node(key.getNodeId(), rskConfig.getPublicIp(), rskConfig.getPeerPort());
NodeDistanceTable distanceTable = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, localNode);
long msgTimeOut = rskConfig.peerDiscoveryMessageTimeOut();
long refreshPeriod = rskConfig.peerDiscoveryRefreshPeriod();
List<String> initialBootNodes = rskConfig.peerDiscoveryIPList();
List<Node> activePeers = rskConfig.peerActive();
if (CollectionUtils.isNotEmpty(activePeers)) {
for (Node n : activePeers) {
InetSocketAddress address = n.getAddress();
initialBootNodes.add(address.getHostName() + ":" + address.getPort());
}
}
return new PeerExplorer(initialBootNodes, localNode, distanceTable, key, msgTimeOut, refreshPeriod);
}
Aggregations