Search in sources :

Example 16 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class DatagramAdapterTest method sendEachOtherAndReceive.

// @Test
// public void testEtaCrypt() throws Exception {
// 
// SymmetricKey symmetricKey1 = new SymmetricKey();
// byte[] payload = "test data set 1".getBytes();
// byte[] crc32 = new Crc32().digest(payload);
// 
// for (int i = 0; i < 100000000; i++) {
// byte[] encrypted = symmetricKey1.etaEncrypt(payload);
// byte[] decrypted = symmetricKey1.etaDecrypt(encrypted);
// byte[] crc32Decrypted = new Crc32().digest(decrypted);
// if(!Arrays.equals(crc32, crc32Decrypted)) {
// System.out.println("Eta error: " + i + " data: " + new String(payload) + " decrypted: " + new String(decrypted));
// }
// }
// }
@Test
public void sendEachOtherAndReceive() 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);
    // 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);
    d1.addErrorsCallback(m -> {
        System.err.println(m);
        return m;
    });
    d2.addErrorsCallback(m -> {
        System.err.println(m);
        return m;
    });
    d3.addErrorsCallback(m -> {
        System.err.println(m);
        return m;
    });
    d4.addErrorsCallback(m -> {
        System.err.println(m);
        return m;
    });
    d5.addErrorsCallback(m -> {
        System.err.println(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 = 500;
    int numSends = 100;
    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<>();
    BlockingQueue<String> waitStatusQueueFor1 = new ArrayBlockingQueue<String>(1, true);
    BlockingQueue<String> waitStatusQueueFor2 = new ArrayBlockingQueue<String>(1, true);
    BlockingQueue<String> waitStatusQueueFor3 = new ArrayBlockingQueue<String>(1, true);
    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(20));
    }
    try {
        ae.await(60000);
    } 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()));
    d1.shutdown();
    d2.shutdown();
    d3.shutdown();
    d4.shutdown();
    d5.shutdown();
    assertTrue((numSends * attempts) - (receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) < 50);
}
Also used : ArrayList(java.util.ArrayList) SymmetricKey(com.icodici.crypto.SymmetricKey) AsyncEvent(net.sergeych.tools.AsyncEvent) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Random(java.util.Random) NodeInfo(com.icodici.universa.node2.NodeInfo) NetConfig(com.icodici.universa.node2.NetConfig) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 17 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class DatagramAdapterTest method createNodeToMany.

@Test
public void createNodeToMany() throws Exception {
    int numNodes = 100;
    NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 1, "test_node_1", "localhost", 16201, 16202, 16301);
    List<NodeInfo> nodeInfos = new ArrayList<>();
    nodeInfos.add(node1);
    NetConfig nc = new NetConfig(nodeInfos);
    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_r_" + 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++) {
                d1.send(nodes.get(k), 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 18 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class Client method getState.

public ItemResult getState(HashId itemId, Reporter reporter) throws ClientError {
    final ExecutorService pool = Executors.newCachedThreadPool();
    final List<ErrorRecord> errors = new ArrayList<>();
    final AsyncEvent<Void> consensusFound = new AsyncEvent<>();
    final int checkConsensus = getNodes().size() / 3;
    final AtomicInteger nodesLeft = new AtomicInteger(nodes.size());
    return protect(() -> {
        final Map<ItemState, List<ItemResult>> states = new HashMap<>();
        for (int i = 0; i < nodes.size(); i++) {
            final int nn = i;
            pool.submit(() -> {
                for (int retry = 0; retry < 5; retry++) {
                    // reporter.verbose("trying "+reporter+" for node "+nn);
                    try {
                        Client c = getClient(nn);
                        ItemResult r = c.command("getState", "itemId", itemId).getOrThrow("itemResult");
                        r.meta.put("url", c.getNodeNumber());
                        synchronized (states) {
                            List<ItemResult> list = states.get(r.state);
                            if (list == null) {
                                list = new ArrayList();
                                states.put(r.state, list);
                            }
                            list.add(r);
                            if (r.errors.size() > 0)
                                reporter.warning("errors from " + c.getNodeNumber() + ": " + r.errors);
                            break;
                        }
                    } catch (IOException e) {
                    // reporter.warning("can't get answer from node " + nn + ", retry #" + retry + ": " + e);
                    }
                }
                // Now we should check the consensus
                states.forEach((itemState, itemResults) -> {
                    if (itemResults.size() >= checkConsensus)
                        if (itemResults.size() >= checkConsensus) {
                            consensusFound.fire();
                            return;
                        }
                });
                if (nodesLeft.decrementAndGet() < 1)
                    consensusFound.fire();
            });
        }
        consensusFound.await(5000);
        pool.shutdownNow();
        final ItemResult[] consensus = new ItemResult[1];
        states.forEach((itemState, itemResults) -> {
            if (itemResults.size() >= checkConsensus)
                consensus[0] = itemResults.get(0);
        });
        if (consensus[0] != null)
            reporter.message("State consensus found:" + consensus[0]);
        else {
            reporter.warning("no consensus found " + states.size());
        }
        if (states.size() > 1) {
            states.entrySet().stream().sorted(Comparator.comparingInt(o -> o.getValue().size())).forEach(kv -> {
                List<ItemResult> itemResults = kv.getValue();
                reporter.message("" + kv.getKey() + ": " + itemResults.size() + ": " + itemResults.stream().map(x -> x.meta.getStringOrThrow("url")).collect(Collectors.toSet()));
            });
        }
        return consensus[0];
    });
}
Also used : BiDeserializer(net.sergeych.biserializer.BiDeserializer) HttpURLConnection(java.net.HttpURLConnection) NonNull(org.checkerframework.checker.nullness.qual.NonNull) java.util(java.util) URL(java.net.URL) Binder(net.sergeych.tools.Binder) ErrorRecord(com.icodici.universa.ErrorRecord) PublicKey(com.icodici.crypto.PublicKey) Contract(com.icodici.universa.contract.Contract) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncEvent(net.sergeych.tools.AsyncEvent) ExecutorService(java.util.concurrent.ExecutorService) ItemResult(com.icodici.universa.node.ItemResult) Reporter(net.sergeych.tools.Reporter) PrivateKey(com.icodici.crypto.PrivateKey) com.icodici.universa.node2(com.icodici.universa.node2) Do(net.sergeych.tools.Do) IOException(java.io.IOException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ItemState(com.icodici.universa.node.ItemState) Executors(java.util.concurrent.Executors) EncryptionError(com.icodici.crypto.EncryptionError) Approvable(com.icodici.universa.Approvable) Parcel(com.icodici.universa.contract.Parcel) HashId(com.icodici.universa.HashId) DefaultBiMapper(net.sergeych.biserializer.DefaultBiMapper) Boss(net.sergeych.boss.Boss) IOException(java.io.IOException) AsyncEvent(net.sergeych.tools.AsyncEvent) ItemResult(com.icodici.universa.node.ItemResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ItemState(com.icodici.universa.node.ItemState) ExecutorService(java.util.concurrent.ExecutorService) ErrorRecord(com.icodici.universa.ErrorRecord)

Example 19 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class Node2EmulatedNetworkTest method checkRegisterContractOnLostPacketsNetwork.

@Test
public void checkRegisterContractOnLostPacketsNetwork() throws Exception {
    ((TestEmulatedNetwork) network).setTest_nodeBeingOffedChance(75);
    AsyncEvent ae = new AsyncEvent();
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    contract.addSignerKeyFromFile(Config.tuKeyPath);
    contract.seal();
    addDetailsToAllLedgers(contract);
    contract.check();
    contract.traceErrors();
    assertTrue(contract.isOk());
    node.registerItem(contract);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            System.out.println("-----------nodes state--------------");
            boolean all_is_approved = true;
            for (Node n : nodes) {
                ItemResult r = n.checkItem(contract.getId());
                System.out.println("Node: " + n.toString() + " state: " + r.state);
                if (r.state != ItemState.APPROVED) {
                    all_is_approved = false;
                }
            }
            if (all_is_approved)
                ae.fire();
        }
    }, 0, 1000);
    boolean time_is_up = false;
    try {
        ae.await(60000);
    } catch (TimeoutException e) {
        time_is_up = true;
        System.out.println("time is up");
    }
    timer.cancel();
    ((TestEmulatedNetwork) network).setTest_nodeBeingOffedChance(0);
    assertFalse(time_is_up);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 20 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class Node2LocalNetworkTest method checkRegisterContractOnTemporaryOffedNetwork.

@Test
public void checkRegisterContractOnTemporaryOffedNetwork() throws Exception {
    // switch off half network
    for (int i = 0; i < NODES / 2; i++) {
        networks_s.get(NODES - i - 1).setUDPAdapterTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
        networks_s.get(NODES - i - 1).setUDPAdapterLostPacketsPercentInTestMode(100);
    }
    AsyncEvent ae = new AsyncEvent();
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    contract.addSignerKeyFromFile(Config.tuKeyPath);
    contract.seal();
    addDetailsToAllLedgers(contract);
    contract.check();
    contract.traceErrors();
    assertTrue(contract.isOk());
    node.registerItem(contract);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            System.out.println("-----------nodes state--------------");
            boolean all_is_approved = true;
            for (Node n : nodesMap_s.values()) {
                ItemResult r = n.checkItem(contract.getId());
                System.out.println("Node: " + n.toString() + " state: " + r.state);
                if (r.state != ItemState.APPROVED) {
                    all_is_approved = false;
                }
            }
            assertEquals(all_is_approved, false);
        }
    }, 0, 1000);
    // wait and now switch on full network
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        timer.cancel();
        System.out.println("switching on network");
        for (TestLocalNetwork ln : networks_s) {
            ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
        }
    }
    Timer timer2 = new Timer();
    timer2.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            System.out.println("-----------nodes state--------------");
            Object lock = new Object();
            synchronized (lock) {
                int num_approved = 0;
                for (Node n : nodesMap_s.values()) {
                    ItemResult r = n.checkItem(contract.getId());
                    if (r.state == ItemState.APPROVED) {
                        num_approved++;
                    }
                    System.out.println("Node: " + n.toString() + " state: " + r.state);
                }
                if (num_approved == NODES) {
                    System.out.println("All approved: " + num_approved);
                    ae.fire();
                }
            }
        }
    }, 0, 1000);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer2.cancel();
    boolean all_is_approved = true;
    for (Node n : nodesMap_s.values()) {
        ItemResult r = n.waitItem(contract.getId(), 13000);
        if (r.state != ItemState.APPROVED) {
            all_is_approved = false;
        }
    }
    assertEquals(all_is_approved, true);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

AsyncEvent (net.sergeych.tools.AsyncEvent)26 TimeoutException (java.util.concurrent.TimeoutException)22 Contract (com.icodici.universa.contract.Contract)17 Test (org.junit.Test)8 Parcel (com.icodici.universa.contract.Parcel)6 SymmetricKey (com.icodici.crypto.SymmetricKey)5 NetConfig (com.icodici.universa.node2.NetConfig)5 NodeInfo (com.icodici.universa.node2.NodeInfo)5 ArrayList (java.util.ArrayList)5 Arrays.asList (java.util.Arrays.asList)4 List (java.util.List)4 Random (java.util.Random)4 Timer (java.util.Timer)3 TimerTask (java.util.TimerTask)3 PrivateKey (com.icodici.crypto.PrivateKey)2 Duration (java.time.Duration)2 JsonObject (com.eclipsesource.json.JsonObject)1 EncryptionError (com.icodici.crypto.EncryptionError)1 PublicKey (com.icodici.crypto.PublicKey)1 Approvable (com.icodici.universa.Approvable)1