Search in sources :

Example 66 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method referenceForChangeNumberWithCreateContract.

@Test
public void referenceForChangeNumberWithCreateContract() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PrivateKey> llcPrivateKeys = new HashSet<>();
    Set<PrivateKey> thirdPartyPrivateKeys = new HashSet<>();
    llcPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    thirdPartyPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey")));
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Set<PublicKey> thirdPartyPublicKeys = new HashSet<>();
    for (PrivateKey pk : thirdPartyPrivateKeys) {
        thirdPartyPublicKeys.add(pk.getPublicKey());
    }
    Contract jobCertificate = new Contract(llcPrivateKeys.iterator().next());
    jobCertificate.setOwnerKeys(stepaPublicKeys);
    jobCertificate.getDefinition().getData().set("issuer", "Roga & Kopita");
    jobCertificate.getDefinition().getData().set("type", "chief accountant assignment");
    jobCertificate.seal();
    registerAndCheckApproved(jobCertificate);
    Contract llcProperty = ContractsService.createNotaryContract(llcPrivateKeys, stepaPublicKeys);
    llcProperty.getDefinition().getData().remove("name");
    llcProperty.getDefinition().getData().remove("description");
    Binder binderdata = new Binder();
    binderdata.set("name", "Abonement");
    binderdata.set("description", "Abonement.");
    llcProperty.getDefinition().setData(binderdata);
    List<String> listConditions = new ArrayList<>();
    listConditions.add("ref.definition.issuer == \"26RzRJDLqze3P5Z1AzpnucF75RLi1oa6jqBaDh8MJ3XmTaUoF8R\"");
    listConditions.add("ref.definition.data.issuer == \"Roga & Kopita\"");
    listConditions.add("ref.definition.data.type == \"chief accountant assignment\"");
    Reference reference = new Reference(llcProperty);
    reference.name = "certification_contract";
    reference.type = Reference.TYPE_EXISTING;
    Binder conditions = new Binder();
    conditions.set("all_of", listConditions);
    reference.setConditions(conditions);
    reference.addMatchingItem(jobCertificate);
    // llcProperty.getDefinition().getReferences().add(reference);
    llcProperty.addReference(reference);
    ListRole listRole = new ListRole("list_role");
    SimpleRole ownerRole = new SimpleRole("owner", stepaPrivateKeys);
    listRole.addRole(ownerRole);
    listRole.addRequiredReference("certification_contract", Role.RequiredMode.ALL_OF);
    Binder params = new Binder();
    params.set("min_value", 1);
    params.set("max_step", -1);
    params.set("field_name", "units");
    ChangeNumberPermission ChangeNumberPerm = new ChangeNumberPermission(listRole, params);
    llcProperty.addPermission(ChangeNumberPerm);
    llcProperty.getStateData().set("units", 1000000);
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = llcProperty.createRevision(stepaPrivateKeys);
    llcProperty2.getStateData().set("units", llcProperty.getStateData().getIntOrThrow("units") - 1);
    llcProperty2.seal();
    llcProperty2.check();
    llcProperty2.traceErrors();
    assertFalse(llcProperty2.isOk());
    TransactionPack tp_before = llcProperty2.getTransactionPack();
    // tp_before.addReferencedItem(jobCertificate);
    byte[] data = tp_before.pack();
    TransactionPack tp_after = TransactionPack.unpack(data);
    registerAndCheckApproved(tp_after);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Binder(net.sergeych.tools.Binder) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ListRole(com.icodici.universa.contract.roles.ListRole) Test(org.junit.Test)

Example 67 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method declineItemFromoutWhiteList.

@Test(timeout = 90000)
public void declineItemFromoutWhiteList() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    Contract stepaCoins = Contract.fromDslFile(ROOT_PATH + "stepaCoins.yml");
    stepaCoins.setExpiresAt(ZonedDateTime.now().plusMonths(1));
    stepaCoins.addSignerKey(stepaPrivateKeys.iterator().next());
    stepaCoins.seal();
    stepaCoins.check();
    stepaCoins.traceErrors();
    node.registerItem(stepaCoins);
    ItemResult itemResult = node.waitItem(stepaCoins.getId(), 18000);
    assertEquals(ItemState.UNDEFINED, itemResult.state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Example 68 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method getApprovedTUContract.

protected Contract getApprovedTUContract() throws Exception {
    synchronized (tuContractLock) {
        if (tuContract == null) {
            PrivateKey ownerKey = new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey"));
            Set<PublicKey> keys = new HashSet();
            keys.add(ownerKey.getPublicKey());
            Contract stepaTU = InnerContractsService.createFreshTU(100000000, keys);
            stepaTU.check();
            stepaTU.traceErrors();
            System.out.println("register new TU ");
            node.registerItem(stepaTU);
            tuContract = stepaTU;
        }
        int needRecreateTuContractNum = 0;
        for (Node n : nodes) {
            try {
                ItemResult itemResult = n.waitItem(tuContract.getId(), 15000);
                // assertEquals(ItemState.APPROVED, itemResult.state);
                if (itemResult.state != ItemState.APPROVED) {
                    System.out.println("TU: node " + n + " result: " + itemResult);
                    needRecreateTuContractNum++;
                }
            } catch (TimeoutException e) {
                System.out.println("ping ");
                // System.out.println(n.ping());
                // //                    System.out.println(n.traceTasksPool());
                // System.out.println(n.traceParcelProcessors());
                // System.out.println(n.traceItemProcessors());
                System.out.println("TU: node " + n + " timeout: ");
                needRecreateTuContractNum++;
            }
        }
        int recreateBorder = nodes.size() - config.getPositiveConsensus() - 1;
        if (recreateBorder < 0)
            recreateBorder = 0;
        if (needRecreateTuContractNum > recreateBorder) {
            tuContract = null;
            Thread.sleep(1000);
            return getApprovedTUContract();
        }
        return tuContract;
    }
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) TimeoutException(java.util.concurrent.TimeoutException)

Example 69 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method shouldBreakByQuantizerSplit.

@Test(timeout = 90000)
public void shouldBreakByQuantizerSplit() throws Exception {
    if (node == null) {
        System.out.println("network not inited");
        return;
    }
    PrivateKey key = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    // 100
    Contract c = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    c.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    c.seal();
    registerAndCheckApproved(c);
    Contract.setTestQuantaLimit(60);
    // 30
    Contract c1 = ContractsService.createSplit(c, 30, "amount", new HashSet<PrivateKey>(Arrays.asList(key)));
    Contract c2 = c1.getNew().get(0);
    assertEquals("70", c1.getStateData().get("amount").toString());
    assertEquals("30", c2.getStateData().get("amount").toString());
    node.registerItem(c1);
    ItemResult itemResult = node.waitItem(c1.getId(), 1500);
    System.out.println(itemResult);
    Contract.setTestQuantaLimit(-1);
    assertEquals(ItemState.UNDEFINED, itemResult.state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Example 70 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method joinSnatch.

@Test
@Ignore("it is snatch test")
public void joinSnatch() throws Exception {
    PrivateKey key = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    Set<PrivateKey> keys = new HashSet<>();
    keys.add(key);
    Contract c1 = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    c1.addSignerKey(key);
    assertTrue(c1.check());
    c1.seal();
    registerAndCheckApproved(c1);
    System.out.println("money before split (c1): " + c1.getStateData().getIntOrThrow("amount"));
    Contract c2 = ContractsService.createSplit(c1, 99, "amount", keys);
    Contract c3 = c2.getNew().get(0);
    System.out.println("money after split (c2): " + c2.getStateData().getIntOrThrow("amount"));
    System.out.println("money after split (c3): " + c3.getStateData().getIntOrThrow("amount"));
    registerAndCheckApproved(c3);
    Contract c4 = c3.createRevision(keys);
    c4.addRevokingItems(c1);
    // 150);
    c4.getStateData().set("amount", 199);
    c4.seal();
    System.out.println("money after snatch (c4): " + c4.getStateData().getIntOrThrow("amount"));
    System.out.println("check after snatch (c4): " + c4.check());
    c4.traceErrors();
    registerAndCheckDeclined(c4);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

PrivateKey (com.icodici.crypto.PrivateKey)168 Test (org.junit.Test)122 PublicKey (com.icodici.crypto.PublicKey)76 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)29 Contract (com.icodici.universa.contract.Contract)24 Binder (net.sergeych.tools.Binder)22 IOException (java.io.IOException)16 HashSet (java.util.HashSet)14 ListRole (com.icodici.universa.contract.roles.ListRole)12 SQLException (java.sql.SQLException)11 Ignore (org.junit.Ignore)9 File (java.io.File)8 KeyAddress (com.icodici.crypto.KeyAddress)7 KeyRecord (com.icodici.universa.contract.KeyRecord)7 Role (com.icodici.universa.contract.roles.Role)7 Semaphore (java.util.concurrent.Semaphore)7 RoleLink (com.icodici.universa.contract.roles.RoleLink)5 TimeoutException (java.util.concurrent.TimeoutException)5 Decimal (com.icodici.universa.Decimal)4 HashId (com.icodici.universa.HashId)4