Search in sources :

Example 11 with Contract

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

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

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

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

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

the class ResearchTest method registerSimpleContract_recreateNodeClient.

@Test
public void registerSimpleContract_recreateNodeClient() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Contract stepaCoins = Contract.fromDslFile(ROOT_PATH + "stepaCoins.yml");
    stepaCoins.addSignerKey(stepaPrivateKeys.iterator().next());
    stepaCoins.seal();
    System.out.println("nodeClient.register(stepaCoins)...");
    ItemResult itemResult = nodeClient.register(stepaCoins.getLastSealedBinary(), 1000);
    System.out.println("nodeClient.register(stepaCoins)... done! itemResult: " + itemResult.state);
    reCreateNodeClient();
    itemResult = nodeClient.getState(stepaCoins.getId());
    System.out.println("nodeClient.getState(stepaCoins): " + itemResult.state);
    assertEquals(ItemState.APPROVED, itemResult.state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Contract(com.icodici.universa.contract.Contract) HashSet(java.util.HashSet)

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