Search in sources :

Example 6 with NetConfig

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

the class DatagramAdapterTest method create5NodesSend10Times.

public void create5NodesSend10Times() 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);
    NodeInfo node4 = new NodeInfo(TestKeys.publicKey(0), 13, "test_node_13", "localhost", 16205, 16206, 16304);
    NodeInfo node5 = new NodeInfo(TestKeys.publicKey(1), 14, "test_node_14", "localhost", 16206, 16207, 16305);
    List<NodeInfo> nodes = new ArrayList<>();
    nodes.add(node1);
    nodes.add(node2);
    nodes.add(node3);
    nodes.add(node4);
    nodes.add(node5);
    NetConfig nc = new NetConfig(nodes);
    SymmetricKey symmetricKey1 = new SymmetricKey();
    SymmetricKey symmetricKey2 = new SymmetricKey();
    SymmetricKey symmetricKey3 = new SymmetricKey();
    SymmetricKey symmetricKey4 = new SymmetricKey();
    SymmetricKey symmetricKey5 = new SymmetricKey();
    // create implemented class with node1
    DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), symmetricKey1, node1, nc);
    // create implemented class with node1
    DatagramAdapter d2 = new UDPAdapter(TestKeys.privateKey(1), symmetricKey2, node2, nc);
    // create implemented class with node1
    DatagramAdapter d3 = new UDPAdapter(TestKeys.privateKey(2), symmetricKey3, node3, nc);
    // create implemented class with node1
    DatagramAdapter d4 = new UDPAdapter(TestKeys.privateKey(0), symmetricKey4, node4, nc);
    // create implemented class with node1
    DatagramAdapter d5 = new UDPAdapter(TestKeys.privateKey(1), symmetricKey5, node5, nc);
    // d1.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d2.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d3.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d4.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d5.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d1.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // d2.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // d3.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // d4.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // d5.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // 
    // d1.setLostPacketsPercentInTestMode(75);
    // d2.setLostPacketsPercentInTestMode(75);
    // d3.setLostPacketsPercentInTestMode(75);
    // d4.setLostPacketsPercentInTestMode(75);
    // d5.setLostPacketsPercentInTestMode(75);
    List symmetricKeyErrors = new ArrayList();
    d1.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    d2.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    d3.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    d4.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    d5.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    byte[] payload1 = "test data set 1".getBytes();
    byte[] payload2 = "test data set 2".getBytes();
    byte[] payload3 = "test data set 3".getBytes();
    int attempts = 100;
    int numSends = 10;
    ArrayList<byte[]> receviedFor1 = new ArrayList<>();
    ArrayList<byte[]> receviedFor2 = new ArrayList<>();
    ArrayList<byte[]> receviedFor3 = new ArrayList<>();
    ArrayList<byte[]> receviedFor4 = new ArrayList<>();
    ArrayList<byte[]> receviedFor5 = new ArrayList<>();
    AsyncEvent<Void> ae = new AsyncEvent<>();
    d1.receive(d -> {
        receviedFor1.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            ae.fire();
    });
    d2.receive(d -> {
        receviedFor2.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            ae.fire();
    });
    d3.receive(d -> {
        receviedFor3.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            ae.fire();
    });
    d4.receive(d -> {
        receviedFor4.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            ae.fire();
    });
    d5.receive(d -> {
        receviedFor5.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            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++) {
            int rnd1 = new Random().nextInt(3);
            int rnd2 = 0;
            int rnd3 = 0;
            while (rnd2 == rnd3) {
                rnd2 = new Random().nextInt(5);
                rnd3 = new Random().nextInt(5);
            }
            byte[] payload;
            DatagramAdapter sender;
            NodeInfo receiverNode;
            if (rnd1 == 0)
                payload = payload1;
            else if (rnd1 == 1)
                payload = payload2;
            else
                payload = payload3;
            if (rnd2 == 0)
                sender = d1;
            else if (rnd2 == 1)
                sender = d2;
            else if (rnd2 == 2)
                sender = d3;
            else if (rnd2 == 3)
                sender = d4;
            else
                sender = d5;
            if (rnd3 == 0)
                receiverNode = node1;
            else if (rnd3 == 1)
                receiverNode = node2;
            else if (rnd3 == 2)
                receiverNode = node3;
            else if (rnd3 == 3)
                receiverNode = node4;
            else
                receiverNode = node5;
            sender.send(receiverNode, 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(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    System.out.println("receviedFor1 got: " + (receviedFor1.size()));
    System.out.println("receviedFor2 got: " + (receviedFor2.size()));
    System.out.println("receviedFor3 got: " + (receviedFor3.size()));
    System.out.println("receviedFor4 got: " + (receviedFor4.size()));
    System.out.println("receviedFor5 got: " + (receviedFor5.size()));
    System.out.println("all got: " + (receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()));
    // assertEquals(numSends * attempts, receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size());
    assertEquals(0, symmetricKeyErrors.size());
    d1.shutdown();
    d2.shutdown();
    d3.shutdown();
    d4.shutdown();
    d5.shutdown();
}
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)

Example 7 with NetConfig

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

the class DatagramAdapterTest method shufflePackets.

@Test
public void shufflePackets() throws Exception {
    // create pair of connected adapters
    // and simulate packets received in random order
    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.SHUFFLE_PACKETS);
    d2.setTestMode(DatagramAdapter.TestModes.SHUFFLE_PACKETS);
    byte[] payload1 = "test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 ".getBytes();
    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 {
            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);
    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)

Example 8 with NetConfig

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

the class PostgresLedger method loadConfig.

@Override
public Object[] loadConfig() {
    try {
        Object[] result = new Object[3];
        result[0] = null;
        result[2] = null;
        ArrayList<NodeInfo> nodeInfos = new ArrayList<>();
        try (PooledDb db = dbPool.db();
            ResultSet rs = db.queryRow("SELECT * FROM config;")) {
            if (rs == null)
                throw new Exception("config not found");
            do {
                NodeInfo nodeInfo = NodeInfo.initFrom(rs);
                nodeInfos.add(nodeInfo);
                byte[] packedKey = rs.getBytes("private_key");
                if (packedKey != null) {
                    result[0] = nodeInfo;
                    result[2] = new PrivateKey(packedKey);
                }
            } while (rs.next());
            if (nodeInfos.isEmpty())
                throw new Exception("config not found");
            result[1] = new NetConfig(nodeInfos);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Failed to load config", e);
    }
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PooledDb(com.icodici.db.PooledDb) NodeInfo(com.icodici.universa.node2.NodeInfo) ResultSet(java.sql.ResultSet) NetConfig(com.icodici.universa.node2.NetConfig) SQLException(java.sql.SQLException)

Example 9 with NetConfig

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

the class PostgresLedger method saveConfig.

@Override
public void saveConfig(NodeInfo myInfo, NetConfig netConfig, PrivateKey nodeKey) {
    try (PooledDb db = dbPool.db()) {
        try (PreparedStatement statement = db.statement("delete from config;")) {
            db.updateWithStatement(statement);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        for (NodeInfo nodeInfo : netConfig.toList()) {
            String sqlText;
            if (nodeInfo.getNumber() == myInfo.getNumber()) {
                sqlText = "insert into config(http_client_port,http_server_port,udp_server_port, node_number, node_name, public_host,host,public_key,private_key) values(?,?,?,?,?,?,?,?,?);";
            } else {
                sqlText = "insert into config(http_client_port,http_server_port,udp_server_port, node_number, node_name, public_host,host,public_key) values(?,?,?,?,?,?,?,?);";
            }
            try (PreparedStatement statement = db.statementReturningKeys(sqlText)) {
                statement.setInt(1, nodeInfo.getClientAddress().getPort());
                statement.setInt(2, nodeInfo.getServerAddress().getPort());
                statement.setInt(3, nodeInfo.getNodeAddress().getPort());
                statement.setInt(4, nodeInfo.getNumber());
                statement.setString(5, nodeInfo.getName());
                statement.setString(6, nodeInfo.getPublicHost());
                statement.setString(7, nodeInfo.getClientAddress().getHostName());
                statement.setBytes(8, nodeInfo.getPublicKey().pack());
                if (statement.getParameterMetaData().getParameterCount() > 8) {
                    statement.setBytes(9, nodeKey.pack());
                }
                db.updateWithStatement(statement);
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    } catch (SQLException se) {
        se.printStackTrace();
        throw new Failure("config save failed:" + se);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PooledDb(com.icodici.db.PooledDb) SQLException(java.sql.SQLException) NodeInfo(com.icodici.universa.node2.NodeInfo) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 10 with NetConfig

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

the class DatagramAdapterTest method sendTrippleAndReceive.

@Test
public void sendTrippleAndReceive() 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();
    byte[] payload2 = "test data set 2222".getBytes();
    byte[] payload3 = "test data set 333333333333333".getBytes();
    ArrayList<byte[]> receviedFor2 = new ArrayList<>();
    BlockingQueue<String> waitStatusQueue = new ArrayBlockingQueue<String>(1, true);
    d2.receive(d -> {
        receviedFor2.add(d);
        try {
            if (receviedFor2.size() >= 3) {
                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);
    d1.send(node2, payload2);
    d1.send(node2, payload3);
    while (!((waitStatusQueue.take()).equals("DONE"))) {
    // wait until it is delivered
    }
    assertEquals(3, receviedFor2.size());
    byte[] data1 = receviedFor2.get(0);
    byte[] data2 = receviedFor2.get(1);
    byte[] data3 = receviedFor2.get(2);
    // receiver must s
    assertArrayEquals(payload1, data1);
    assertArrayEquals(payload2, data2);
    assertArrayEquals(payload3, data3);
    // 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)

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