Search in sources :

Example 6 with AsyncEvent

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

the class Node2LocalNetworkTest method resyncContractWithSomeUndefindSubContractsWithTimeout.

@Test
public void resyncContractWithSomeUndefindSubContractsWithTimeout() throws Exception {
    // Test should run resync of each unknown part of a contract
    // But resync should failed by timeout. And complex contract should be declined.
    AsyncEvent ae = new AsyncEvent();
    int numSubContracts = 5;
    List<Contract> subContracts = new ArrayList<>();
    for (int i = 0; i < numSubContracts; i++) {
        Contract c = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
        c.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
        assertTrue(c.check());
        c.seal();
        if (i < config.getKnownSubContractsToResync())
            addToAllLedgers(c, ItemState.APPROVED);
        else
            addToAllLedgers(c, ItemState.APPROVED, node);
        subContracts.add(c);
    }
    for (int i = 0; i < numSubContracts; i++) {
        ItemResult r = node.checkItem(subContracts.get(i).getId());
        System.out.println("Contract: " + i + " > " + subContracts.get(i).getId() + " state: " + r.state);
    }
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    contract.addSignerKeyFromFile(Config.tuKeyPath);
    assertTrue(contract.check());
    for (int i = 0; i < numSubContracts; i++) {
        contract.addRevokingItems(subContracts.get(i));
    }
    contract.seal();
    contract.check();
    contract.traceErrors();
    Duration wasDuration = config.getMaxResyncTime();
    config.setMaxResyncTime(Duration.ofMillis(2000));
    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);
    }
    // preparing is finished
    node.registerItem(contract);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            ItemResult r = node.checkItem(contract.getId());
            System.out.println("Complex contract state: " + r.state);
            if (r.state == ItemState.DECLINED)
                ae.fire();
        }
    }, 0, 500);
    try {
        ae.await(15000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer.cancel();
    // ItemResult r = node.waitItem(contract.getId(), 5000);
    ItemResult r = node.checkItem(contract.getId());
    // If resync broken but need more then oned nodes to decline, state should be PENDING_NEGATIVE
    Assert.assertThat(r.state, anyOf(equalTo(ItemState.PENDING_NEGATIVE), equalTo(ItemState.DECLINED)));
    for (TestLocalNetwork ln : networks_s) {
        ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
        ln.setUDPAdapterVerboseLevel(DatagramAdapter.VerboseLevel.NOTHING);
    }
    config.setMaxResyncTime(wasDuration);
}
Also used : Duration(java.time.Duration) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with AsyncEvent

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

the class Node2LocalNetworkTest method shouldNotResyncWithLessUnknownContractsEx.

public void shouldNotResyncWithLessUnknownContractsEx(ItemState definedState, ItemState undefinedState) throws Exception {
    // Test should broke condition to resync:
    // should be at least one unknown subcontract to start resync
    AsyncEvent ae = new AsyncEvent();
    List<Contract> subContracts = new ArrayList<>();
    RunnableWithException<ItemState> addContract = (ItemState state) -> {
        Contract c = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
        c.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
        assertTrue(c.check());
        c.seal();
        addToAllLedgers(c, state);
        subContracts.add(c);
    };
    int wantedSubContracts = 5;
    System.out.println("add " + wantedSubContracts + " " + definedState + " subcontract");
    for (int i = 0; i < wantedSubContracts; ++i) addContract.run(definedState);
    for (int i = 0; i < subContracts.size(); i++) {
        ItemResult r = node.checkItem(subContracts.get(i).getId());
        System.out.println("Contract: " + i + " state: " + r.state);
    }
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    for (int i = 0; i < subContracts.size(); i++) {
        contract.addRevokingItems(subContracts.get(i));
    }
    contract.seal();
    contract.check();
    contract.traceErrors();
    assertTrue(contract.check());
    if (definedState == ItemState.LOCKED) {
        for (Node n : nodesMap_s.values()) {
            for (int i = 0; i < subContracts.size(); i++) {
                StateRecord sr = n.getLedger().getRecord(subContracts.get(i).getId());
                sr.setLockedByRecordId(sr.getRecordId());
                sr.save();
            }
        }
    }
    LogPrinter.showDebug(true);
    Parcel parcel = registerWithNewParcel(contract);
    node.waitParcel(parcel.getId(), 30000);
    ItemResult r = node.waitItem(parcel.getPayloadContract().getId(), 8000);
    System.out.println("Complex contract state: " + r.state);
    ItemState expectedState = (definedState == ItemState.APPROVED) ? ItemState.APPROVED : ItemState.DECLINED;
    assertEquals(expectedState, r.state);
}
Also used : Parcel(com.icodici.universa.contract.Parcel) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract)

Example 8 with AsyncEvent

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

the class Node2LocalNetworkTest method resyncContractWithSomeUndefindSubContracts.

@Test
public void resyncContractWithSomeUndefindSubContracts() throws Exception {
    // Test should run resync of each unknown part of a contract
    AsyncEvent ae = new AsyncEvent();
    int numSubContracts = 5;
    List<Contract> subContracts = new ArrayList<>();
    for (int i = 0; i < numSubContracts; i++) {
        Contract c = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
        c.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
        assertTrue(c.check());
        c.seal();
        if (i < config.getKnownSubContractsToResync())
            addToAllLedgers(c, ItemState.APPROVED);
        else
            addToAllLedgers(c, ItemState.APPROVED, node);
        subContracts.add(c);
    }
    for (int i = 0; i < numSubContracts; i++) {
        ItemResult r = node.checkItem(subContracts.get(i).getId());
        System.out.println("Contract: " + i + " state: " + r.state);
    }
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    assertTrue(contract.check());
    for (int i = 0; i < numSubContracts; i++) {
        contract.addRevokingItems(subContracts.get(i));
    }
    contract.seal();
    contract.check();
    contract.traceErrors();
    Parcel parcel = registerWithNewParcel(contract);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            ItemResult r = node.checkItem(contract.getId());
            System.out.println("Complex contract state: " + r.state);
            if (r.state == ItemState.APPROVED)
                ae.fire();
        }
    }, 0, 500);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer.cancel();
    for (TestLocalNetwork ln : networks_s) {
        ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
        ln.setUDPAdapterVerboseLevel(DatagramAdapter.VerboseLevel.NOTHING);
    }
    node.waitParcel(parcel.getId(), 30000);
    ItemResult r = node.waitItem(parcel.getPayloadContract().getId(), 3000);
    assertEquals(ItemState.APPROVED, r.state);
}
Also used : Parcel(com.icodici.universa.contract.Parcel) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 9 with AsyncEvent

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

the class Node2LocalNetworkTest method resyncComplex.

@Test(timeout = 30000)
public void resyncComplex() throws Exception {
    int numSubContracts = 5;
    List<Contract> subContracts = new ArrayList<>();
    for (int i = 0; i < numSubContracts; i++) {
        Contract c = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
        c.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
        assertTrue(c.check());
        c.seal();
        if (i < config.getKnownSubContractsToResync())
            addToAllLedgers(c, ItemState.APPROVED);
        else
            addToAllLedgers(c, ItemState.APPROVED, node);
        subContracts.add(c);
    }
    for (int i = 0; i < numSubContracts; i++) {
        ItemResult r = node.checkItem(subContracts.get(i).getId());
        System.out.println("Contract: " + i + " state: " + r.state);
    }
    AsyncEvent ae = new AsyncEvent();
    Contract contract = new Contract(TestKeys.privateKey(0));
    contract.seal();
    for (int i = 0; i < numSubContracts; i++) {
        contract.addRevokingItems(subContracts.get(i));
    }
    addToAllLedgers(contract, ItemState.PENDING_POSITIVE);
    node.getLedger().getRecord(contract.getId()).destroy();
    assertEquals(ItemState.UNDEFINED, node.checkItem(contract.getId()).state);
    node.resync(contract.getId());
    assertEquals(ItemState.PENDING, node.checkItem(contract.getId()).state);
    // Start checking nodes
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            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.UNDEFINED) {
                    all_is_approved = false;
                }
            }
            if (all_is_approved)
                ae.fire();
        }
    }, 0, 1000);
    try {
        ae.await(6000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer.cancel();
    assertEquals(ItemState.UNDEFINED, node.waitItem(contract.getId(), 13000).state);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with AsyncEvent

use of net.sergeych.tools.AsyncEvent 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)

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