Search in sources :

Example 51 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class PeerDiscoveryRequestTest method create.

@Test
public void create() {
    ECKey key = new ECKey();
    String check = UUID.randomUUID().toString();
    PingPeerMessage pingPeerMessage = PingPeerMessage.create("localhost", 80, check, key);
    PongPeerMessage pongPeerMessage = PongPeerMessage.create("localhost", 80, check, key);
    InetSocketAddress address = new InetSocketAddress("localhost", 8080);
    PeerDiscoveryRequest request = PeerDiscoveryRequestBuilder.builder().messageId(check).message(pingPeerMessage).address(address).expectedResponse(DiscoveryMessageType.PONG).expirationPeriod(1000).attemptNumber(1).build();
    Assert.assertNotNull(request);
    Assert.assertTrue(request.validateMessageResponse(pongPeerMessage));
    Assert.assertFalse(request.validateMessageResponse(pingPeerMessage));
}
Also used : PongPeerMessage(co.rsk.net.discovery.message.PongPeerMessage) InetSocketAddress(java.net.InetSocketAddress) PingPeerMessage(co.rsk.net.discovery.message.PingPeerMessage) ECKey(org.ethereum.crypto.ECKey) Test(org.junit.Test)

Example 52 with ECKey

use of org.ethereum.crypto.ECKey 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 53 with ECKey

use of org.ethereum.crypto.ECKey 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 54 with ECKey

use of org.ethereum.crypto.ECKey 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 55 with ECKey

use of org.ethereum.crypto.ECKey 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

ECKey (org.ethereum.crypto.ECKey)76 Test (org.junit.Test)43 BigInteger (java.math.BigInteger)17 NodeDistanceTable (co.rsk.net.discovery.table.NodeDistanceTable)10 ArrayList (java.util.ArrayList)10 Node (org.ethereum.net.rlpx.Node)10 InetSocketAddress (java.net.InetSocketAddress)9 Transaction (org.ethereum.core.Transaction)8 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)7 RepositoryImpl (co.rsk.db.RepositoryImpl)7 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)7 Channel (io.netty.channel.Channel)7 org.ethereum.core (org.ethereum.core)7 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)6 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 Account (org.ethereum.core.Account)4 RskAddress (co.rsk.core.RskAddress)3