use of co.rsk.net.discovery.table.NodeDistanceTable in project rskj by rsksmart.
the class PeerExplorerTest method sendInitialMessageToNodesNoNodes.
@Test
public void sendInitialMessageToNodesNoNodes() {
Node node = new Node(new ECKey().getNodeId(), HOST_2, PORT_2);
NodeDistanceTable distanceTable = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node);
PeerExplorer peerExplorer = new PeerExplorer(new ArrayList<>(), node, distanceTable, new ECKey(), TIMEOUT, REFRESH);
peerExplorer.setUDPChannel(Mockito.mock(UDPChannel.class));
Set<String> nodesWithMessage = peerExplorer.startConversationWithNewNodes();
Assert.assertTrue(CollectionUtils.isEmpty(nodesWithMessage));
peerExplorer = new PeerExplorer(null, node, distanceTable, new ECKey(), TIMEOUT, REFRESH);
peerExplorer.setUDPChannel(Mockito.mock(UDPChannel.class));
nodesWithMessage = peerExplorer.startConversationWithNewNodes();
Assert.assertTrue(CollectionUtils.isEmpty(nodesWithMessage));
}
use of co.rsk.net.discovery.table.NodeDistanceTable in project rskj by rsksmart.
the class PeerExplorerTest method handlePongMessage.
@Test
public void handlePongMessage() throws Exception {
List<String> nodes = new ArrayList<>();
nodes.add(HOST_1 + ":" + PORT_1);
nodes.add(HOST_3 + ":" + PORT_3);
ECKey key1 = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress();
ECKey key2 = ECKey.fromPrivate(Hex.decode(KEY_2)).decompress();
Node node = new Node(key2.getNodeId(), HOST_2, PORT_2);
NodeDistanceTable distanceTable = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node);
PeerExplorer peerExplorer = new PeerExplorer(nodes, node, distanceTable, key2, TIMEOUT, REFRESH);
Channel internalChannel = Mockito.mock(Channel.class);
UDPTestChannel channel = new UDPTestChannel(internalChannel, peerExplorer);
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
peerExplorer.setUDPChannel(channel);
Assert.assertTrue(CollectionUtils.isEmpty(peerExplorer.getNodes()));
// A incoming pong for a Ping we did not sent.
String check = UUID.randomUUID().toString();
PongPeerMessage incomingPongMessage = PongPeerMessage.create(HOST_1, PORT_1, check, key1);
DiscoveryEvent incomingPongEvent = new DiscoveryEvent(incomingPongMessage, new InetSocketAddress(HOST_1, PORT_1));
channel.clearEvents();
channel.channelRead0(ctx, incomingPongEvent);
List<DiscoveryEvent> sentEvents = channel.getEventsWritten();
Assert.assertEquals(0, sentEvents.size());
Assert.assertEquals(0, peerExplorer.getNodes().size());
// Now we send the ping first
peerExplorer.startConversationWithNewNodes();
sentEvents = channel.getEventsWritten();
Assert.assertEquals(2, sentEvents.size());
incomingPongMessage = PongPeerMessage.create(HOST_1, PORT_1, ((PingPeerMessage) sentEvents.get(0).getMessage()).getMessageId(), key1);
incomingPongEvent = new DiscoveryEvent(incomingPongMessage, new InetSocketAddress(HOST_1, PORT_1));
channel.clearEvents();
List<Node> addedNodes = peerExplorer.getNodes();
Assert.assertEquals(0, addedNodes.size());
channel.channelRead0(ctx, incomingPongEvent);
Assert.assertEquals(1, peerExplorer.getNodes().size());
addedNodes = peerExplorer.getNodes();
Assert.assertEquals(1, addedNodes.size());
}
use of co.rsk.net.discovery.table.NodeDistanceTable in project rskj by rsksmart.
the class PeerExplorerTest method handleFindNodeMessageWithExtraNodes.
@Test
public void handleFindNodeMessageWithExtraNodes() throws Exception {
List<String> nodes = new ArrayList<>();
nodes.add(HOST_1 + ":" + PORT_1);
nodes.add(HOST_3 + ":" + PORT_3);
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();
Node node = new Node(key2.getNodeId(), HOST_2, PORT_2);
NodeDistanceTable distanceTable = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node);
PeerExplorer peerExplorer = new PeerExplorer(nodes, node, distanceTable, key2, TIMEOUT, REFRESH);
Channel internalChannel = Mockito.mock(Channel.class);
UDPTestChannel channel = new UDPTestChannel(internalChannel, peerExplorer);
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
peerExplorer.setUDPChannel(channel);
Assert.assertTrue(CollectionUtils.isEmpty(peerExplorer.getNodes()));
// we send the ping first
peerExplorer.startConversationWithNewNodes();
List<DiscoveryEvent> sentEvents = channel.getEventsWritten();
Assert.assertEquals(2, sentEvents.size());
PongPeerMessage incomingPongMessage1 = PongPeerMessage.create(HOST_1, PORT_1, ((PingPeerMessage) sentEvents.get(0).getMessage()).getMessageId(), key1);
DiscoveryEvent incomingPongEvent1 = new DiscoveryEvent(incomingPongMessage1, new InetSocketAddress(HOST_1, PORT_1));
PongPeerMessage incomingPongMessage2 = PongPeerMessage.create(HOST_3, PORT_3, ((PingPeerMessage) sentEvents.get(1).getMessage()).getMessageId(), key3);
DiscoveryEvent incomingPongEvent2 = new DiscoveryEvent(incomingPongMessage2, new InetSocketAddress(HOST_3, PORT_3));
channel.clearEvents();
channel.channelRead0(ctx, incomingPongEvent1);
channel.channelRead0(ctx, incomingPongEvent2);
List<Node> foundNodes = peerExplorer.getNodes();
Assert.assertEquals(2, foundNodes.size());
Assert.assertEquals(NODE_ID_3, Hex.toHexString(foundNodes.get(0).getId().getID()));
Assert.assertEquals(NODE_ID_1, Hex.toHexString(foundNodes.get(1).getId().getID()));
String check = UUID.randomUUID().toString();
FindNodePeerMessage findNodePeerMessage = FindNodePeerMessage.create(key1.getNodeId(), check, key1);
channel.clearEvents();
channel.channelRead0(ctx, new DiscoveryEvent(findNodePeerMessage, new InetSocketAddress(HOST_1, PORT_1)));
sentEvents = channel.getEventsWritten();
Assert.assertEquals(1, sentEvents.size());
NeighborsPeerMessage neighborsPeerMessage = (NeighborsPeerMessage) sentEvents.get(0).getMessage();
Assert.assertEquals(2, neighborsPeerMessage.getNodes().size());
Assert.assertTrue(cotainsNode(NODE_ID_1, neighborsPeerMessage.getNodes()));
Assert.assertTrue(cotainsNode(NODE_ID_3, neighborsPeerMessage.getNodes()));
}
use of co.rsk.net.discovery.table.NodeDistanceTable in project rskj by rsksmart.
the class PeerExplorerTest method sendInitialMessageToNodes.
@Test
public void sendInitialMessageToNodes() {
List<String> nodes = new ArrayList<>();
nodes.add("localhost:3306");
nodes.add("localhost:3307");
nodes.add("localhost:3306:abd");
nodes.add("");
nodes.add(null);
Node node = new Node(new ECKey().getNodeId(), HOST_1, PORT_1);
NodeDistanceTable distanceTable = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node);
PeerExplorer peerExplorer = new PeerExplorer(nodes, node, distanceTable, new ECKey(), TIMEOUT, REFRESH);
UDPChannel channel = new UDPChannel(Mockito.mock(Channel.class), peerExplorer);
peerExplorer.setUDPChannel(channel);
Set<String> nodesWithMessage = peerExplorer.startConversationWithNewNodes();
Assert.assertTrue(CollectionUtils.size(nodesWithMessage) == 2);
Assert.assertTrue(nodesWithMessage.contains("localhost/127.0.0.1:3307"));
Assert.assertTrue(nodesWithMessage.contains("localhost/127.0.0.1:3306"));
}
use of co.rsk.net.discovery.table.NodeDistanceTable 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));
}
Aggregations