Search in sources :

Example 6 with Node

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);
}
Also used : Node(org.ethereum.net.rlpx.Node) Test(org.junit.Test)

Example 7 with Node

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);
        }
    }
}
Also used : Node(org.ethereum.net.rlpx.Node) NodeID(co.rsk.net.NodeID) OperationResult(co.rsk.net.discovery.table.OperationResult)

Example 8 with Node

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);
    }
}
Also used : Node(org.ethereum.net.rlpx.Node) NodeID(co.rsk.net.NodeID)

Example 9 with Node

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);
    }
}
Also used : OperationResult(co.rsk.net.discovery.table.OperationResult) java.util(java.util) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Node(org.ethereum.net.rlpx.Node) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) co.rsk.net.discovery.message(co.rsk.net.discovery.message) StringUtils(org.apache.commons.lang3.StringUtils) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) NodeDistanceTable(co.rsk.net.discovery.table.NodeDistanceTable) CollectionUtils(org.apache.commons.collections4.CollectionUtils) SecureRandom(java.security.SecureRandom) Lock(java.util.concurrent.locks.Lock) PeerDiscoveryRequestBuilder(co.rsk.net.discovery.table.PeerDiscoveryRequestBuilder) IpUtils(co.rsk.util.IpUtils) NodeID(co.rsk.net.NodeID) ECKey(org.ethereum.crypto.ECKey) InetSocketAddress(java.net.InetSocketAddress) Node(org.ethereum.net.rlpx.Node)

Example 10 with Node

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);
}
Also used : NodeDistanceTable(co.rsk.net.discovery.table.NodeDistanceTable) PeerExplorer(co.rsk.net.discovery.PeerExplorer) InetSocketAddress(java.net.InetSocketAddress) Node(org.ethereum.net.rlpx.Node) ECKey(org.ethereum.crypto.ECKey) Bean(org.springframework.context.annotation.Bean)

Aggregations

Node (org.ethereum.net.rlpx.Node)26 Test (org.junit.Test)16 NodeDistanceTable (co.rsk.net.discovery.table.NodeDistanceTable)11 ECKey (org.ethereum.crypto.ECKey)11 ArrayList (java.util.ArrayList)9 InetSocketAddress (java.net.InetSocketAddress)8 Channel (io.netty.channel.Channel)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 NodeID (co.rsk.net.NodeID)3 OperationResult (co.rsk.net.discovery.table.OperationResult)2 SecureRandom (java.security.SecureRandom)2 HashSet (java.util.HashSet)2 PeerExplorer (co.rsk.net.discovery.PeerExplorer)1 co.rsk.net.discovery.message (co.rsk.net.discovery.message)1 PeerDiscoveryRequestBuilder (co.rsk.net.discovery.table.PeerDiscoveryRequestBuilder)1 IpUtils (co.rsk.util.IpUtils)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1