Search in sources :

Example 1 with NetConfig

use of com.icodici.universa.node2.NetConfig in project universa by UniversaBlockchain.

the class DatagramAdapterTest method sendBadNetConfig.

@Test
public void sendBadNetConfig() throws Exception {
    NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 10, "test_node_10", "localhost", 16201, 16202, 16301);
    NodeInfo node2 = new NodeInfo(TestKeys.publicKey(1), 11, "test_node_11", "localhost", 16203, 16204, 16302);
    NodeInfo node3 = new NodeInfo(TestKeys.publicKey(2), 12, "test_node_12", "localhost", 16204, 16205, 16303);
    List<NodeInfo> nodes = new ArrayList<>();
    // nodes.add(node1);
    nodes.add(node2);
    nodes.add(node3);
    NetConfig nc = new NetConfig(nodes);
    // create implemented class with node1
    DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), new SymmetricKey(), node1, nc);
    // create implemented class with node1
    DatagramAdapter d2 = new UDPAdapter(TestKeys.privateKey(1), new SymmetricKey(), node2, nc);
    // create implemented class with node1
    DatagramAdapter d3 = new UDPAdapter(TestKeys.privateKey(2), new SymmetricKey(), node3, nc);
    // d1.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d2.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d3.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    byte[] payload1 = "test data set 1".getBytes();
    AsyncEvent<Void> ae = new AsyncEvent<>();
    List keyErrors = new ArrayList();
    d2.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("BAD_VALUE: block got from unknown node") >= 0)
            keyErrors.add(m);
        // ae.fire();
        return m;
    });
    // send from adapter d1, to d2 as it is connected with node2 credentials:
    d1.send(node2, payload1);
    d1.send(node2, payload1);
    d1.send(node2, payload1);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    assertTrue(keyErrors.size() > 0);
    // And test it for all interfaceces and big arrays of data
    d1.shutdown();
    d2.shutdown();
    d3.shutdown();
}
Also used : ArrayList(java.util.ArrayList) SymmetricKey(com.icodici.crypto.SymmetricKey) AsyncEvent(net.sergeych.tools.AsyncEvent) NodeInfo(com.icodici.universa.node2.NodeInfo) NetConfig(com.icodici.universa.node2.NetConfig) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 2 with NetConfig

use of com.icodici.universa.node2.NetConfig in project universa by UniversaBlockchain.

the class DatagramAdapterTest method sendAndReceive.

@Test
public void sendAndReceive() throws Exception {
    NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 10, "test_node_10", "localhost", 16201, 16202, 16301);
    NodeInfo node2 = new NodeInfo(TestKeys.publicKey(1), 11, "test_node_11", "localhost", 16203, 16204, 16302);
    NodeInfo node3 = new NodeInfo(TestKeys.publicKey(2), 12, "test_node_12", "localhost", 16204, 16205, 16303);
    List<NodeInfo> nodes = new ArrayList<>();
    nodes.add(node1);
    nodes.add(node2);
    nodes.add(node3);
    NetConfig nc = new NetConfig(nodes);
    // create implemented class with node1
    DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), new SymmetricKey(), node1, nc);
    // create implemented class with node1
    DatagramAdapter d2 = new UDPAdapter(TestKeys.privateKey(1), new SymmetricKey(), node2, nc);
    // create implemented class with node1
    DatagramAdapter d3 = new UDPAdapter(TestKeys.privateKey(2), new SymmetricKey(), node3, nc);
    // d1.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d2.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d3.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    byte[] payload1 = "test data set 1".getBytes();
    ArrayList<byte[]> receviedFor2 = new ArrayList<>();
    BlockingQueue<String> waitStatusQueue = new ArrayBlockingQueue<String>(1, true);
    d2.receive(d -> {
        receviedFor2.add(d);
        try {
            waitStatusQueue.put("DONE");
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("DONE error");
        }
    });
    // send from adapter d1, to d2 as it is connected with node2 credentials:
    d1.send(node2, payload1);
    while (!((waitStatusQueue.take()).equals("DONE"))) {
    // wait until it is delivered
    }
    assertEquals(1, receviedFor2.size());
    byte[] data = receviedFor2.get(0);
    // receiver must s
    assertArrayEquals(payload1, data);
    // And test it for all interfaceces and big arrays of data
    d1.shutdown();
    d2.shutdown();
    d3.shutdown();
}
Also used : ArrayList(java.util.ArrayList) SymmetricKey(com.icodici.crypto.SymmetricKey) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) NodeInfo(com.icodici.universa.node2.NodeInfo) NetConfig(com.icodici.universa.node2.NetConfig) Test(org.junit.Test)

Example 3 with NetConfig

use of com.icodici.universa.node2.NetConfig in project universa by UniversaBlockchain.

the class DatagramAdapterTest method createManyNodesToOne.

@Test
public void createManyNodesToOne() throws Exception {
    int numNodes = 100;
    NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 10, "test_node_10", "localhost", 16201, 16202, 16301);
    NetConfig nc = new NetConfig(asList(node1));
    SymmetricKey symmetricKey1 = new SymmetricKey();
    // create implemented class with node1
    DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), symmetricKey1, node1, nc);
    List symmetricKeyErrors = new ArrayList();
    d1.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    byte[] payload = "test data set 1".getBytes();
    int attempts = 5;
    int numSends = 5;
    ArrayList<byte[]> receviedFor = new ArrayList<>();
    AsyncEvent<Void> ae = new AsyncEvent<>();
    d1.receive(data -> {
        receviedFor.add(data);
        if ((receviedFor.size()) == attempts * numSends * numNodes)
            ae.fire();
    });
    List<NodeInfo> nodes = new ArrayList<>();
    List<DatagramAdapter> adapters = new ArrayList<>();
    for (int i = 0; i < numNodes; i++) {
        int keyIndex = new Random().nextInt(2);
        NodeInfo n = new NodeInfo(TestKeys.publicKey(keyIndex), 2 + i, "test_node_2" + i, "localhost", 16203 + i, 16204 + i, 16302 + i);
        nc.addNode(n);
        nodes.add(n);
        SymmetricKey sk = new SymmetricKey();
        // create implemented class with node1
        DatagramAdapter d = new UDPAdapter(TestKeys.privateKey(keyIndex), sk, n, nc);
        adapters.add(d);
        d.addErrorsCallback(m -> {
            System.err.println(m);
            if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
                symmetricKeyErrors.add(m);
            return m;
        });
        d.receive(data -> {
            receviedFor.add(data);
            if ((receviedFor.size()) == attempts * numSends * numNodes)
                ae.fire();
        });
    }
    for (int i = 0; i < attempts; i++) {
        System.out.println("Send part: " + i);
        // send from adapter d1, to d2 as it is connected with node2 credentials:
        for (int j = 0; j < numSends; j++) {
            for (int k = 0; k < numNodes; k++) {
                adapters.get(k).send(node1, payload);
            }
        }
        Thread.sleep(new Random().nextInt(200));
    // if(new Random().nextBoolean()) ((UDPAdapter)d1).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d2).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d3).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d4).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d5).brakeSessions();
    }
    try {
        ae.await(30000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    assertEquals(0, symmetricKeyErrors.size());
    System.out.println("all got: " + (receviedFor.size()));
    d1.shutdown();
    for (int i = 0; i < numNodes; i++) {
        adapters.get(i).shutdown();
    }
    assertEquals(attempts * numSends * numNodes, receviedFor.size());
}
Also used : ArrayList(java.util.ArrayList) SymmetricKey(com.icodici.crypto.SymmetricKey) AsyncEvent(net.sergeych.tools.AsyncEvent) Random(java.util.Random) NodeInfo(com.icodici.universa.node2.NodeInfo) NetConfig(com.icodici.universa.node2.NetConfig) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 4 with NetConfig

use of com.icodici.universa.node2.NetConfig in project universa by UniversaBlockchain.

the class DatagramAdapterTest method reconnectWithLostAndShuffle.

// @Test
public void reconnectWithLostAndShuffle() throws Exception {
    // Tottaly hard test with reconnect, shuffled and lost packets and multiple send.
    NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 10, "test_node_10", "localhost", 16201, 16202, 16301);
    NodeInfo node2 = new NodeInfo(TestKeys.publicKey(1), 11, "test_node_11", "localhost", 16203, 16204, 16302);
    List<NodeInfo> nodes = new ArrayList<>();
    nodes.add(node1);
    nodes.add(node2);
    NetConfig nc = new NetConfig(nodes);
    // create implemented class with node1
    DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), new SymmetricKey(), node1, nc);
    // create implemented class with node1
    DatagramAdapter d2 = new UDPAdapter(TestKeys.privateKey(1), new SymmetricKey(), node2, nc);
    d1.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    d2.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    d1.setTestMode(DatagramAdapter.TestModes.LOST_AND_SHUFFLE_PACKETS);
    d2.setTestMode(DatagramAdapter.TestModes.LOST_AND_SHUFFLE_PACKETS);
    // byte[] payload1 = "test data set with rnd: ".getBytes();
    String payloadtring;
    final int timesToSend = 1;
    ArrayList<byte[]> receviedFor1 = new ArrayList<>();
    ArrayList<byte[]> receviedFor2 = new ArrayList<>();
    BlockingQueue<String> waitStatusQueue = new ArrayBlockingQueue<String>(1, true);
    d2.receive(d -> {
        receviedFor2.add(d);
        try {
            if (receviedFor2.size() >= timesToSend) {
                waitStatusQueue.put("DONE");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("DONE error");
        }
    });
    // send from adapter d1, to d2 as it is connected with node2 credentials, ten times:
    for (int i = 0; i < timesToSend; i++) {
        payloadtring = "test data set with rnd: " + Do.randomInt(10000);
        d1.send(node2, payloadtring.getBytes());
    }
    while (!((waitStatusQueue.take()).equals("DONE"))) {
    // wait until it is delivered
    }
    assertEquals(timesToSend, receviedFor2.size());
    // byte[] data = receviedFor2.get(0);
    // 
    // // receiver must s
    // assertArrayEquals(payload1, data);
    // send data back
    d1.receive(d -> {
        receviedFor1.add(d);
        try {
            if (receviedFor1.size() >= timesToSend) {
                waitStatusQueue.put("DONE");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("DONE error");
        }
    });
    // send from adapter d2, to d1, ten times:
    for (int i = 0; i < timesToSend; i++) {
        payloadtring = "test data set with rnd: " + Do.randomInt(10000);
        d2.send(node1, payloadtring.getBytes());
    }
    while (!((waitStatusQueue.take()).equals("DONE"))) {
    // wait until it is delivered
    }
    assertEquals(timesToSend, receviedFor1.size());
    // data = receviedFor1.get(0);
    // 
    // // receiver must s
    // assertArrayEquals(payload2, data);
    // test with close and reopen socket
    System.out.println("-------");
    System.out.println("close socket and reopen with new adapter");
    System.out.println("-------");
    d2.shutdown();
    // create new adapter with d2 credentials
    // create implemented class with node1
    DatagramAdapter d3 = new UDPAdapter(TestKeys.privateKey(1), new SymmetricKey(), node2, nc);
    ArrayList<byte[]> receviedFor3 = new ArrayList<>();
    d3.setTestMode(DatagramAdapter.TestModes.LOST_AND_SHUFFLE_PACKETS);
    d3.receive(d -> {
        receviedFor3.add(d);
        try {
            if (receviedFor3.size() >= timesToSend) {
                waitStatusQueue.put("DONE");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("DONE error");
        }
    });
    // send from adapter d1, to d3
    for (int i = 0; i < timesToSend; i++) {
        payloadtring = "test data set with rnd: " + Do.randomInt(10000);
        d1.send(node2, payloadtring.getBytes());
    }
    while (!((waitStatusQueue.take()).equals("DONE"))) {
    // wait until it is delivered
    }
    assertEquals(timesToSend, receviedFor3.size());
    // data = receviedFor3.get(0);
    // receiver must s
    // assertArrayEquals(payload1, data);
    d1.shutdown();
    d2.shutdown();
    d3.shutdown();
}
Also used : ArrayList(java.util.ArrayList) SymmetricKey(com.icodici.crypto.SymmetricKey) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) NodeInfo(com.icodici.universa.node2.NodeInfo) NetConfig(com.icodici.universa.node2.NetConfig)

Example 5 with NetConfig

use of com.icodici.universa.node2.NetConfig in project universa by UniversaBlockchain.

the class DatagramAdapterTest method sendBigData.

@Test
public void sendBigData() throws Exception {
    NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 10, "test_node_10", "localhost", 16201, 16202, 16301);
    NodeInfo node2 = new NodeInfo(TestKeys.publicKey(1), 11, "test_node_11", "localhost", 16203, 16204, 16302);
    List<NodeInfo> nodes = new ArrayList<>();
    nodes.add(node1);
    nodes.add(node2);
    NetConfig nc = new NetConfig(nodes);
    // create implemented class with node1
    DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), new SymmetricKey(), node1, nc);
    // create implemented class with node1
    DatagramAdapter d2 = new UDPAdapter(TestKeys.privateKey(1), new SymmetricKey(), node2, nc);
    d1.setTestMode(DatagramAdapter.TestModes.SHUFFLE_PACKETS);
    d2.setTestMode(DatagramAdapter.TestModes.SHUFFLE_PACKETS);
    // d1.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d2.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    byte[] payload1 = Do.randomBytes(1024 * 10);
    ArrayList<byte[]> receviedFor2 = new ArrayList<>();
    BlockingQueue<String> waitStatusQueue = new ArrayBlockingQueue<String>(1, true);
    d2.receive(d -> {
        receviedFor2.add(d);
        try {
            waitStatusQueue.put("DONE");
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("DONE error");
        }
    });
    // send from adapter d1, to d2 as it is connected with node2 credentials:
    d1.send(node2, payload1);
    while (!((waitStatusQueue.take()).equals("DONE"))) {
    // wait until it is delivered
    }
    assertEquals(1, receviedFor2.size());
    byte[] data = receviedFor2.get(0);
    // receiver must s
    assertArrayEquals(payload1, data);
    // And test it for all interfaceces and big arrays of data
    d1.shutdown();
    d2.shutdown();
}
Also used : ArrayList(java.util.ArrayList) SymmetricKey(com.icodici.crypto.SymmetricKey) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) NodeInfo(com.icodici.universa.node2.NodeInfo) NetConfig(com.icodici.universa.node2.NetConfig) Test(org.junit.Test)

Aggregations

NodeInfo (com.icodici.universa.node2.NodeInfo)15 NetConfig (com.icodici.universa.node2.NetConfig)14 SymmetricKey (com.icodici.crypto.SymmetricKey)13 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)11 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)9 Random (java.util.Random)5 TimeoutException (java.util.concurrent.TimeoutException)5 AsyncEvent (net.sergeych.tools.AsyncEvent)5 Arrays.asList (java.util.Arrays.asList)4 List (java.util.List)4 PooledDb (com.icodici.db.PooledDb)2 SQLException (java.sql.SQLException)2 PrivateKey (com.icodici.crypto.PrivateKey)1 StateRecord (com.icodici.universa.node.StateRecord)1 ClientHTTPServer (com.icodici.universa.node2.network.ClientHTTPServer)1 NetworkV2 (com.icodici.universa.node2.network.NetworkV2)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1