Search in sources :

Example 6 with Contract

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

the class Node2EmulatedNetworkTest method resyncDeclined.

@Test(timeout = 15000)
public void resyncDeclined() throws Exception {
    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);
    node.resync(c.getId());
    assertEquals(ItemState.DECLINED, node.waitItem(c.getId(), 12000).state);
}
Also used : Contract(com.icodici.universa.contract.Contract)

Example 7 with Contract

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

the class Node2EmulatedNetworkTest method resyncOther.

@Test(timeout = 15000)
public void resyncOther() throws Exception {
    Contract c = new Contract(TestKeys.privateKey(0));
    c.seal();
    addToAllLedgers(c, ItemState.PENDING_POSITIVE);
    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);
    assertEquals(ItemState.UNDEFINED, node.waitItem(c.getId(), 12000).state);
}
Also used : Contract(com.icodici.universa.contract.Contract)

Example 8 with Contract

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

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

the class Node2LocalNetworkTest method resyncOther.

@Test
public void resyncOther() throws Exception {
    AsyncEvent ae = new AsyncEvent();
    Contract c = new Contract(TestKeys.privateKey(0));
    c.seal();
    addToAllLedgers(c, ItemState.PENDING_POSITIVE);
    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() {
            boolean all_is_approved = true;
            for (Node n : nodesMap_s.values()) {
                ItemResult r = n.checkItem(c.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(c.getId(), 13000).state);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with Contract

use of com.icodici.universa.contract.Contract 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)

Aggregations

Contract (com.icodici.universa.contract.Contract)131 Test (org.junit.Test)67 Decimal (com.icodici.universa.Decimal)31 PrivateKey (com.icodici.crypto.PrivateKey)24 File (java.io.File)16 AsyncEvent (net.sergeych.tools.AsyncEvent)16 TimeoutException (java.util.concurrent.TimeoutException)14 Binder (net.sergeych.tools.Binder)14 KeyRecord (com.icodici.universa.contract.KeyRecord)13 HashSet (java.util.HashSet)9 Parcel (com.icodici.universa.contract.Parcel)8 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)7 Quantiser (com.icodici.universa.node2.Quantiser)7 PublicKey (com.icodici.crypto.PublicKey)6 TransactionPack (com.icodici.universa.contract.TransactionPack)6 IOException (java.io.IOException)6 BackingStoreException (java.util.prefs.BackingStoreException)6 OptionException (joptsimple.OptionException)6 HashId (com.icodici.universa.HashId)5 Arrays.asList (java.util.Arrays.asList)5