Search in sources :

Example 1 with Contract

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

the class Node2EmulatedNetworkTest method checkRegisterContractOnTemporaryOffedNetwork.

@Test
public void checkRegisterContractOnTemporaryOffedNetwork() throws Exception {
    // switch off half network
    for (int i = 0; i < NODES / 2; i++) {
        ((TestEmulatedNetwork) network).switchOffNodeTestMode(nodes.get(NODES - i - 1));
    }
    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 : nodes) {
                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);
    // wait and now switch on full network
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        timer.cancel();
        System.out.println("switching on network");
        ((TestEmulatedNetwork) network).switchOnAllNodesTestMode();
    }
    Timer timer2 = new Timer();
    timer2.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            System.out.println("-----------nodes state--------------");
            boolean all_is_approved = true;
            for (Node n : nodes) {
                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);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer2.cancel();
    boolean all_is_approved = true;
    for (Node n : nodes) {
        ItemResult r = n.waitItem(contract.getId(), 2000);
        if (r.state != ItemState.APPROVED) {
            all_is_approved = false;
        }
    }
    assertEquals(all_is_approved, true);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with Contract

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

the class Node2EmulatedNetworkTest method unexpectedStrangeCaseWithConcurrent.

// @Test
public void unexpectedStrangeCaseWithConcurrent() throws Exception {
    String FIELD_NAME = "amount";
    PrivateKey ownerKey2 = TestKeys.privateKey(1);
    String PRIVATE_KEY = "_xer0yfe2nn1xthc.private.unikey";
    Contract root = Contract.fromDslFile("./src/test_contracts/coin.yml");
    root.getStateData().set(FIELD_NAME, new Decimal(200));
    root.addSignerKeyFromFile("./src/test_contracts/" + PRIVATE_KEY);
    root.setOwnerKey(ownerKey2);
    root.seal();
    assertTrue(root.check());
    Contract c1 = root.splitValue(FIELD_NAME, new Decimal(100));
    c1.seal();
    assertTrue(root.check());
    assertTrue(c1.isOk());
    // c1 split 50 50
    c1 = c1.createRevision(ownerKey2);
    c1.seal();
    Contract c50_1 = c1.splitValue(FIELD_NAME, new Decimal(50));
    c50_1.seal();
    assertTrue(c50_1.isOk());
    // good join
    Contract finalC = c50_1.createRevision(ownerKey2);
    finalC.addSignerKeyFromFile(Config.tuKeyPath);
    finalC.seal();
    finalC.getStateData().set(FIELD_NAME, new Decimal(100));
    finalC.addRevokingItems(c50_1);
    finalC.addRevokingItems(c1);
    for (int j = 0; j < 500; j++) {
        HashId id;
        StateRecord orCreate;
        int p = 0;
        for (Approvable c : finalC.getRevokingItems()) {
            id = c.getId();
            for (int i = 0; i < nodes.size(); i++) {
                if (i == nodes.size() - 1 && p == 1)
                    break;
                Node nodeS = nodes.get(i);
                orCreate = nodeS.getLedger().findOrCreate(id);
                orCreate.setState(ItemState.APPROVED).save();
            }
            ++p;
        }
        destroyFromAllNodesExistingNew(finalC);
        destroyCurrentFromAllNodesIfExists(finalC);
        node.registerItem(finalC);
        ItemResult itemResult = node.waitItem(finalC.getId(), 1500);
        System.out.println(itemResult.state);
    // if (ItemState.APPROVED != itemResult.state)
    // System.out.println("\r\nWrong state on repetition " + j + ": " + itemResult + ", " + itemResult.errors +
    // " \r\ncontract_errors: " + finalC.getErrors());
    // else
    // System.out.println("\r\nGood. repetition: " + j + " ledger:" + node.getLedger().toString());
    // fail("Wrong state on repetition " + j + ": " + itemResult + ", " + itemResult.errors +
    // " \r\ncontract_errors: " + finalC.getErrors());
    // assertEquals(ItemState.APPROVED, itemResult.state);
    }
}
Also used : HashId(com.icodici.universa.HashId) PrivateKey(com.icodici.crypto.PrivateKey) Decimal(com.icodici.universa.Decimal) Approvable(com.icodici.universa.Approvable) Contract(com.icodici.universa.contract.Contract)

Example 3 with Contract

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

the class Node2EmulatedNetworkTest method resyncApproved.

@Test(timeout = 15000)
public void resyncApproved() throws Exception {
    Contract c = new Contract(TestKeys.privateKey(0));
    c.seal();
    addToAllLedgers(c, ItemState.APPROVED);
    node.getLedger().getRecord(c.getId()).destroy();
    assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state);
    node.resync(c.getId());
    assertEquals(ItemState.APPROVED, node.waitItem(c.getId(), 15000).state);
}
Also used : Contract(com.icodici.universa.contract.Contract)

Example 4 with Contract

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

the class Node2EmulatedNetworkTest method resyncWithTimeout.

@Test(timeout = 15000)
public void resyncWithTimeout() throws Exception {
    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++) {
        ((TestEmulatedNetwork) network).switchOffNodeTestMode(nodes.get(NODES - i - 1));
    }
    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(), 15000).state);
    config.setMaxResyncTime(wasDuration);
    ((TestEmulatedNetwork) network).switchOnAllNodesTestMode();
}
Also used : Duration(java.time.Duration) Contract(com.icodici.universa.contract.Contract)

Example 5 with Contract

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

the class Node2EmulatedNetworkTest method resyncComplex.

@Test(timeout = 15000)
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);
    }
    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);
    assertEquals(ItemState.UNDEFINED, node.waitItem(contract.getId(), 13000).state);
}
Also used : 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