Search in sources :

Example 16 with Node

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

Example 17 with 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));
}
Also used : NodeDistanceTable(co.rsk.net.discovery.table.NodeDistanceTable) Node(org.ethereum.net.rlpx.Node) ECKey(org.ethereum.crypto.ECKey) Test(org.junit.Test)

Example 18 with Node

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());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Node(org.ethereum.net.rlpx.Node) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) ECKey(org.ethereum.crypto.ECKey) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) NodeDistanceTable(co.rsk.net.discovery.table.NodeDistanceTable) Test(org.junit.Test)

Example 19 with Node

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()));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Node(org.ethereum.net.rlpx.Node) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) ECKey(org.ethereum.crypto.ECKey) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) NodeDistanceTable(co.rsk.net.discovery.table.NodeDistanceTable) Test(org.junit.Test)

Example 20 with Node

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"));
}
Also used : NodeDistanceTable(co.rsk.net.discovery.table.NodeDistanceTable) Node(org.ethereum.net.rlpx.Node) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) ECKey(org.ethereum.crypto.ECKey) 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