Search in sources :

Example 21 with AsyncEvent

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

the class Node2LocalNetworkTest method shouldNotResyncWithFalseComplexState.

@Test
public void shouldNotResyncWithFalseComplexState() throws Exception {
    // LogPrinter.showDebug(true);
    // Test should broke condition to resync:
    // complex contract should has no errors itself
    ItemState definedState = ItemState.APPROVED;
    ItemState undefinedState = ItemState.UNDEFINED;
    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;
    int knownSubContractsToResync = config.getKnownSubContractsToResync();
    System.out.println("knownSubContractsToResync: " + knownSubContractsToResync);
    int numDefinedSubContracts = Math.min(wantedSubContracts, knownSubContractsToResync);
    System.out.println("add " + numDefinedSubContracts + " defined subcontracts (with state=" + definedState + ")");
    for (int i = 0; i < numDefinedSubContracts; ++i) addContract.run(definedState);
    int numUndefinedSubContracts = Math.max(0, wantedSubContracts - subContracts.size());
    System.out.println("add " + numUndefinedSubContracts + " " + undefinedState + " subcontract");
    for (int i = 0; i < numUndefinedSubContracts; ++i) addContract.run(undefinedState);
    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.addSignerKey(new PrivateKey(2048));
    for (int i = 0; i < subContracts.size(); i++) {
        contract.addRevokingItems(subContracts.get(i));
    }
    contract.seal();
    contract.check();
    contract.traceErrors();
    assertFalse(contract.check());
    // LogPrinter.showDebug(true);
    Parcel parcel = registerWithNewParcel(contract);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            ItemResult r = node.checkItem(parcel.getPayloadContract().getId());
            System.out.println("Complex contract state: " + r.state);
            if (r.state == ItemState.DECLINED)
                ae.fire();
        }
    }, 0, 500);
    try {
        ae.await(25000);
    } 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(), 18000);
    ItemResult r = node.waitItem(parcel.getPayloadContract().getId(), 3000);
    assertEquals(ItemState.DECLINED, r.state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Parcel(com.icodici.universa.contract.Parcel) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 22 with AsyncEvent

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

the class Node2LocalNetworkTest method checkRegisterContractOnLostPacketsNetwork.

@Test
public void checkRegisterContractOnLostPacketsNetwork() throws Exception {
    for (TestLocalNetwork ln : networks_s) {
        ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
        ln.setUDPAdapterLostPacketsPercentInTestMode(90);
    // ln.setUDPAdapterVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    }
    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;
                }
            }
            if (all_is_approved)
                ae.fire();
        }
    }, 0, 1000);
    boolean time_is_up = false;
    try {
        ae.await(30000);
    } catch (TimeoutException e) {
        time_is_up = true;
        System.out.println("time is up");
    }
    timer.cancel();
    for (TestLocalNetwork ln : networks_s) {
        ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
        ln.setUDPAdapterVerboseLevel(DatagramAdapter.VerboseLevel.NOTHING);
    }
    assertFalse(time_is_up);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 23 with AsyncEvent

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

the class Node2LocalNetworkTest method resyncWithTimeout.

@Test
public void resyncWithTimeout() throws Exception {
    AsyncEvent ae = new AsyncEvent();
    Contract c = new Contract(TestKeys.privateKey(0));
    c.seal();
    addToAllLedgers(c, ItemState.APPROVED);
    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);
    }
    node.getLedger().getRecord(c.getId()).destroy();
    assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state);
    node.resync(c.getId());
    assertEquals(ItemState.PENDING, node.checkItem(c.getId()).state);
    // Start checking nodes
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            if (ItemState.UNDEFINED == node.checkItem(c.getId()).state)
                ae.fire();
        }
    }, 100, 500);
    try {
        ae.await(6000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer.cancel();
    config.setMaxResyncTime(wasDuration);
    assertEquals(ItemState.UNDEFINED, node.waitItem(c.getId(), 3000).state);
    for (int i = 0; i < NODES; i++) {
        networks_s.get(i).setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
    }
}
Also used : Duration(java.time.Duration) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 24 with AsyncEvent

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

the class Node2LocalNetworkTest method resyncRevoked.

@Test
public void resyncRevoked() throws Exception {
    AsyncEvent ae = new AsyncEvent();
    Contract c = new Contract(TestKeys.privateKey(0));
    c.seal();
    addToAllLedgers(c, ItemState.REVOKED);
    // 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()) {
                // System.out.println(n.getLedger().getRecord(c.getId()));
                ItemResult r = n.checkItem(c.getId());
                System.out.println(">>>Node: " + n.toString() + " state: " + r.state);
                if (r.state != ItemState.REVOKED) {
                    all_is_approved = false;
                }
            }
            if (all_is_approved)
                ae.fire();
        }
    }, 0, 1000);
    node.getLedger().getRecord(c.getId()).destroy();
    assertEquals(ItemState.UNDEFINED, node.waitItem(c.getId(), 3000).state);
    node.resync(c.getId());
    try {
        ae.await(3000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer.cancel();
    assertEquals(ItemState.REVOKED, node.waitItem(c.getId(), 8000).state);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 25 with AsyncEvent

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

the class Node2LocalNetworkTest method resyncDeclined.

@Test
public void resyncDeclined() throws Exception {
    AsyncEvent ae = new AsyncEvent();
    Contract c = new Contract(TestKeys.privateKey(0));
    c.seal();
    addToAllLedgers(c, ItemState.DECLINED);
    node.getLedger().getRecord(c.getId()).destroy();
    assertEquals(ItemState.UNDEFINED, node.checkItem(c.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()) {
                // System.out.println(n.getLedger().getRecord(c.getId()));
                ItemResult r = n.checkItem(c.getId());
                System.out.println(">>>Node: " + n.toString() + " state: " + r.state);
                if (r.state != ItemState.DECLINED) {
                    all_is_approved = false;
                }
            }
            if (all_is_approved)
                ae.fire();
        }
    }, 0, 1000);
    // LogPrinter.showDebug(true);
    node.resync(c.getId());
    try {
        ae.await(3000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer.cancel();
    assertEquals(ItemState.DECLINED, node.waitItem(c.getId(), 13000).state);
}
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