Search in sources :

Example 1 with Parcel

use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.

the class Node2LocalNetworkTest method checkRegisterContractOnTemporaryOffedAndHalfOnedNetwork.

@Test
public void checkRegisterContractOnTemporaryOffedAndHalfOnedNetwork() throws Exception {
    AsyncEvent ae = new AsyncEvent();
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    contract.seal();
    addDetailsToAllLedgers(contract);
    contract.check();
    contract.traceErrors();
    assertTrue(contract.isOk());
    Parcel parcel = registerWithNewParcel(contract);
    // 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);
    }
    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);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        timer.cancel();
        System.out.println("switching on node 2");
        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(50);
        }
    }
    Timer timer2 = new Timer();
    timer2.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            System.out.println("-----------nodes state--------------");
            for (Node n : nodesMap_s.values()) {
                ItemResult r = n.checkItem(contract.getId());
                System.out.println("Node: " + n.toString() + " state: " + r.state);
            }
        }
    }, 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()) {
        n.waitParcel(parcel.getId(), 13000);
        ItemResult r = n.waitItem(parcel.getPayloadContract().getId(), 3000);
        System.out.println("Node: " + n.toString() + " state: " + r.state);
        if (r.state != ItemState.APPROVED) {
            all_is_approved = false;
        }
    }
    assertEquals(all_is_approved, true);
    for (TestLocalNetwork ln : networks_s) {
        ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
        ln.setUDPAdapterVerboseLevel(DatagramAdapter.VerboseLevel.NOTHING);
    }
}
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 2 with Parcel

use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.

the class Node2LocalNetworkTest method shouldNotResyncWithLessKnownContractsEx.

public void shouldNotResyncWithLessKnownContractsEx(ItemState undefinedState, ItemState definedState) throws Exception {
    // Test should broke condition to resync:
    // should be at least one known (APPROVED, DECLINED, LOCKED, REVOKED) 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;
    int knownSubContractsToResync = config.getKnownSubContractsToResync();
    System.out.println("knownSubContractsToResync: " + knownSubContractsToResync);
    int numDefinedSubContracts = Math.min(wantedSubContracts, knownSubContractsToResync - 1);
    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.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());
    Parcel parcel = registerWithNewParcel(contract);
    node.waitParcel(parcel.getId(), 15000);
    ItemResult r = node.waitItem(parcel.getPayloadContract().getId(), 5000);
    System.out.println("Complex contract state: " + r.state);
    assertEquals(ItemState.DECLINED, r.state);
    for (TestLocalNetwork ln : networks_s) {
        ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
        ln.setUDPAdapterVerboseLevel(DatagramAdapter.VerboseLevel.NOTHING);
    }
}
Also used : Parcel(com.icodici.universa.contract.Parcel) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract)

Example 3 with Parcel

use of com.icodici.universa.contract.Parcel 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 4 with Parcel

use of com.icodici.universa.contract.Parcel 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 5 with Parcel

use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.

the class TestLocalNetwork method getParcel.

@Override
public Parcel getParcel(HashId itemId, NodeInfo nodeInfo, Duration maxTimeout) throws InterruptedException {
    Node node = nodes.get(nodeInfo);
    Parcel parcel = node.getParcel(itemId);
    // return des_parcel;
    return parcel;
}
Also used : Parcel(com.icodici.universa.contract.Parcel)

Aggregations

Parcel (com.icodici.universa.contract.Parcel)12 Contract (com.icodici.universa.contract.Contract)8 AsyncEvent (net.sergeych.tools.AsyncEvent)5 PrivateKey (com.icodici.crypto.PrivateKey)3 TimeoutException (java.util.concurrent.TimeoutException)3 ItemResult (com.icodici.universa.node.ItemResult)2 IOException (java.io.IOException)2 Quantiser (com.icodici.universa.node2.Quantiser)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Instant (java.time.Instant)1