Search in sources :

Example 21 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class UDPServerTest method run3NodesFullTest.

@Test
public void run3NodesFullTest() throws InterruptedException {
    ECKey key1 = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress();
    ECKey key2 = ECKey.fromPrivate(Hex.decode(KEY_2)).decompress();
    ECKey key3 = ECKey.fromPrivate(Hex.decode(KEY_3)).decompress();
    List<String> node1BootNode = new ArrayList<>();
    node1BootNode.add(HOST + ":5555");
    node1BootNode.add(HOST + ":" + PORT_2);
    List<String> node2BootNode = new ArrayList<>();
    node2BootNode.add(HOST + ":" + PORT_3);
    List<String> node3BootNode = new ArrayList<>();
    Node node1 = new Node(key1.getNodeId(), HOST, PORT_1);
    Node node2 = new Node(key2.getNodeId(), HOST, PORT_2);
    Node node3 = new Node(key3.getNodeId(), HOST, PORT_3);
    NodeDistanceTable distanceTable1 = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node1);
    NodeDistanceTable distanceTable2 = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node2);
    NodeDistanceTable distanceTable3 = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node3);
    PeerExplorer peerExplorer1 = new PeerExplorer(node1BootNode, node1, distanceTable1, key1, TIMEOUT, REFRESH);
    PeerExplorer peerExplorer2 = new PeerExplorer(node2BootNode, node2, distanceTable2, key2, TIMEOUT, REFRESH);
    PeerExplorer peerExplorer3 = new PeerExplorer(node3BootNode, node3, distanceTable3, key3, TIMEOUT, REFRESH);
    Assert.assertEquals(0, peerExplorer1.getNodes().size());
    Assert.assertEquals(0, peerExplorer2.getNodes().size());
    Assert.assertEquals(0, peerExplorer3.getNodes().size());
    UDPServer udpServer1 = new UDPServer(HOST, PORT_1, peerExplorer1);
    UDPServer udpServer2 = new UDPServer(HOST, PORT_2, peerExplorer2);
    UDPServer udpServer3 = new UDPServer(HOST, PORT_3, peerExplorer3);
    udpServer3.start();
    TimeUnit.SECONDS.sleep(2);
    peerExplorer3.cleanAndUpdate();
    udpServer2.start();
    TimeUnit.SECONDS.sleep(2);
    peerExplorer2.cleanAndUpdate();
    peerExplorer3.cleanAndUpdate();
    udpServer1.start();
    TimeUnit.SECONDS.sleep(2);
    peerExplorer1.cleanAndUpdate();
    peerExplorer2.cleanAndUpdate();
    peerExplorer3.cleanAndUpdate();
    TimeUnit.SECONDS.sleep(2);
    List<Node> foundNodes1 = peerExplorer1.getNodes();
    List<Node> foundNodes2 = peerExplorer2.getNodes();
    List<Node> foundNodes3 = peerExplorer3.getNodes();
    Assert.assertEquals(2, foundNodes1.size());
    Assert.assertEquals(2, foundNodes2.size());
    Assert.assertEquals(2, foundNodes3.size());
    udpServer1.stop();
    udpServer2.stop();
    udpServer3.stop();
    TimeUnit.SECONDS.sleep(5);
    Assert.assertTrue(checkNodeIds(foundNodes1, NODE_ID_2, NODE_ID_3));
    Assert.assertTrue(checkNodeIds(foundNodes2, NODE_ID_1, NODE_ID_3));
    Assert.assertTrue(checkNodeIds(foundNodes3, NODE_ID_2, NODE_ID_1));
}
Also used : NodeDistanceTable(co.rsk.net.discovery.table.NodeDistanceTable) Node(org.ethereum.net.rlpx.Node) ArrayList(java.util.ArrayList) ECKey(org.ethereum.crypto.ECKey) Test(org.junit.Test)

Example 22 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class NodeDistanceTableTest method addNode.

@Test
public void addNode() {
    Node localNode = 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, localNode);
    OperationResult result = table.addNode(node3);
    Assert.assertTrue(result.isSuccess());
    result = table.addNode(node2);
    Assert.assertTrue(result.isSuccess());
    Assert.assertEquals(2, table.getClosestNodes(EMPTY_NODE_ID).size());
    result = table.addNode(node2);
    Assert.assertTrue(result.isSuccess());
    Assert.assertEquals(2, table.getClosestNodes(EMPTY_NODE_ID).size());
    NodeDistanceTable smallerTable = new NodeDistanceTable(KademliaOptions.BINS, 1, localNode);
    // If a bucket is full, the operations fails (returns false) and we get a candidate for eviction
    result = smallerTable.addNode(node3);
    Assert.assertTrue(result.isSuccess());
    Node sameDistanceNode = new Node(Hex.decode("00"), HOST, PORT_3);
    result = smallerTable.addNode(sameDistanceNode);
    Assert.assertFalse(result.isSuccess());
    Assert.assertEquals(NODE_ID_3, result.getAffectedEntry().getNode().getHexId());
}
Also used : Node(org.ethereum.net.rlpx.Node) Test(org.junit.Test)

Example 23 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class NodeDistanceTableTest method creation.

@Test
public void creation() {
    Node localNode = new Node(Hex.decode(NODE_ID_1), HOST, PORT_1);
    NodeDistanceTable table = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, localNode);
    Assert.assertTrue(table != null);
    Assert.assertEquals(0, table.getClosestNodes(EMPTY_NODE_ID).size());
}
Also used : Node(org.ethereum.net.rlpx.Node) Test(org.junit.Test)

Example 24 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class NodeDistanceTableTest method remove.

@Test
public void remove() {
    Node localNode = 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, localNode);
    OperationResult result = table.addNode(node2);
    Assert.assertTrue(result.isSuccess());
    Assert.assertEquals(1, table.getClosestNodes(EMPTY_NODE_ID).size());
    // Try to remove a node that was never added
    result = table.removeNode(node3);
    Assert.assertFalse(result.isSuccess());
    Assert.assertEquals(1, table.getClosestNodes(EMPTY_NODE_ID).size());
    // Add and remove node
    result = table.addNode(node3);
    Assert.assertTrue(result.isSuccess());
    Assert.assertEquals(2, table.getClosestNodes(EMPTY_NODE_ID).size());
    result = table.removeNode(node3);
    Assert.assertTrue(result.isSuccess());
    Assert.assertEquals(1, table.getClosestNodes(EMPTY_NODE_ID).size());
    // Leave the table empty
    result = table.removeNode(node2);
    Assert.assertTrue(result.isSuccess());
    Assert.assertEquals(0, table.getClosestNodes(EMPTY_NODE_ID).size());
}
Also used : Node(org.ethereum.net.rlpx.Node) Test(org.junit.Test)

Example 25 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class NodeManagerTest method getNodesPeerDiscoveryDisable.

@Test
public void getNodesPeerDiscoveryDisable() {
    List<Node> activePeers = new ArrayList<>();
    activePeers.add(new Node(Hex.decode(NODE_ID_2), "127.0.0.2", 8081));
    List<Node> bootNodes = new ArrayList<>();
    bootNodes.add(new Node(Hex.decode(NODE_ID_3), "127.0.0.3", 8083));
    Mockito.when(config.peerActive()).thenReturn(activePeers);
    Mockito.when(peerExplorer.getNodes()).thenReturn(bootNodes);
    Mockito.when(config.isPeerDiscoveryEnabled()).thenReturn(false);
    nodeManager.init();
    Set<String> nodesInUse = new HashSet<>();
    List<NodeHandler> availableNodes = nodeManager.getNodes(nodesInUse);
    Assert.assertEquals(1, availableNodes.size());
    Assert.assertEquals(NODE_ID_2, availableNodes.get(0).getNode().getHexId());
    // With nodes in use
    nodesInUse.add(NODE_ID_2);
    availableNodes = nodeManager.getNodes(nodesInUse);
    Assert.assertEquals(0, availableNodes.size());
}
Also used : Node(org.ethereum.net.rlpx.Node) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

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