use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.
the class NodeManager method init.
@PostConstruct
void init() {
discoveryEnabled = config.isPeerDiscoveryEnabled();
homeNode = new Node(config.nodeId(), config.getPublicIp(), config.getPeerPort());
for (Node node : config.peerActive()) {
NodeHandler handler = new NodeHandler(node, this);
handler.getNodeStatistics().setPredefined(true);
createNodeHandler(node);
}
}
use of org.ethereum.net.rlpx.Node 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 org.ethereum.net.rlpx.Node 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 org.ethereum.net.rlpx.Node 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 org.ethereum.net.rlpx.Node 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"));
}
Aggregations