Search in sources :

Example 41 with PrivateKey

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

the class PermissionsTest method shouldModifySeveralStateDataValues.

@Test
public void shouldModifySeveralStateDataValues() throws Exception {
    PrivateKey ownerKey2 = TestKeys.privateKey(1);
    Contract c = basicContractCreation(SUBSCRIPTION_WITH_DATA, PRIVATE_KEY, ownerKey2);
    Binder d = c.getStateData();
    Contract c1 = c.createRevision(ownerKey2);
    Binder d1 = c1.getStateData();
    String oldValue = "An example of smart contract.";
    String newValue = "UniversaSmartContract";
    String field = "description";
    setAndCheckOldNewValues(d, d1, oldValue, newValue, field);
    oldValue = "blockchain-partnership.";
    newValue = "blockchain-universa.";
    field = "partner_name";
    setAndCheckOldNewValues(d, d1, oldValue, newValue, field);
    d1.addToInt("direction", 3);
    sealCheckTrace(c1, true);
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Example 42 with PrivateKey

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

the class PermissionsTest method decrementWithReference.

@Test
public void decrementWithReference() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Set<String> references = new HashSet<>();
    references.add("certification_contract");
    Contract c = Contract.fromDslFile(rootPath + "AbonementWithReferenceDSLTemplate.yml");
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Role r = c.getPermissions().getFirst("decrement_permission").getRole();
    assertThat(r, is(instanceOf(ListRole.class)));
    assertFalse(r.isAllowedFor(stepaPublicKeys, null));
    assertTrue(r.isAllowedFor(stepaPublicKeys, references));
    System.out.println("decrement permission :" + c.getPermissions().get("decrement_permission"));
    c.seal();
    c.check();
    c.traceErrors();
    assertTrue(c.isOk());
    assertEquals(c, (c.getPermissions().getFirst("decrement_permission").getRole()).getContract());
    // Bad contract change: owner has no right to change owner ;)
    Set<PrivateKey> badPrivateKeys = new HashSet<>();
    badPrivateKeys.add(TestKeys.privateKey(0));
    Contract c1 = c.createRevision(TestKeys.privateKey(0));
    c1.getStateData().set("units", c.getStateData().getIntOrThrow("units") - 1);
    c1.seal();
    c1.check();
    c1.traceErrors();
    // assertEquals(1, c1.getErrors().size());
    // ErrorRecord error = c1.getErrors().get(0);
    // assertEquals(Errors.FORBIDDEN, error.getError());
    assertFalse(c1.isOk());
    // bad contract change: good key but no reference
    Contract c2 = c.createRevision(stepaPrivateKeys);
    c2.getStateData().set("units", c.getStateData().getIntOrThrow("units") - 1);
    assertEquals(c2, c2.getPermissions().getFirst("decrement_permission").getRole().getContract());
    System.out.println("-------------");
    c1.seal();
    c1.check();
    c1.traceErrors();
    // assertEquals(1, c1.getErrors().size());
    // ErrorRecord error = c1.getErrors().get(0);
    // assertEquals(Errors.FORBIDDEN, error.getError());
    assertFalse(c1.isOk());
    // good contract change: creator is an owner
    Contract c3 = c.createRevision(stepaPrivateKeys);
    c3.getStateData().set("units", c.getStateData().getIntOrThrow("units") - 1);
    Reference ref = new Reference();
    ref.name = "certification_contract";
    ref.type = Reference.TYPE_EXISTING;
    ref.addMatchingItem(new Contract());
    c3.getReferences().put(ref.name, ref);
    assertEquals(c3, c3.getPermissions().getFirst("decrement_permission").getRole().getContract());
    System.out.println("-------------");
    sealCheckTrace(c3, true);
}
Also used : ListRole(com.icodici.universa.contract.roles.ListRole) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Role(com.icodici.universa.contract.roles.Role) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 43 with PrivateKey

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

the class BaseNetworkTest method registerGoodParcel.

@Test(timeout = 900000)
public void registerGoodParcel() throws Exception {
    if (node == null) {
        System.out.println("network not inited");
        return;
    }
    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());
    }
    Thread.sleep(500);
    // LogPrinter.showDebug(true);
    int N = 100;
    for (int k = 0; k < 1; k++) {
        for (int i = 0; i < N; i++) {
            Contract stepaCoins = Contract.fromDslFile(ROOT_PATH + "stepaCoins.yml");
            stepaCoins.addSignerKey(stepaPrivateKeys.iterator().next());
            stepaCoins.seal();
            stepaCoins.check();
            stepaCoins.traceErrors();
            Parcel parcel = createParcelWithClassTU(stepaCoins, stepaPrivateKeys);
            // System.out.println("-------------- register parcel " + parcel.getId() + " (iteration " + i + ") ------------");
            node.registerParcel(parcel);
            synchronized (tuContractLock) {
                tuContract = parcel.getPaymentContract();
            }
            for (Node n : nodesMap.values()) {
                try {
                    // System.out.println("-------------- wait parcel " + parcel.getId() + " on the node " + n + " (iteration " + i + ") ------------");
                    n.waitParcel(parcel.getId(), 25000);
                    ItemResult r = n.waitItem(parcel.getPayloadContract().getId(), 8000);
                    int numIterations = 0;
                    while (!r.state.isConsensusFound()) {
                        System.out.println("wait for consensus receiving on the node " + n + " state is " + r.state);
                        Thread.sleep(500);
                        n.waitParcel(parcel.getId(), 25000);
                        r = n.waitItem(parcel.getPayloadContract().getId(), 8000);
                        numIterations++;
                        if (numIterations > 20)
                            break;
                    }
                    assertEquals("In node " + n + " parcel " + parcel.getId(), ItemState.APPROVED, r.state);
                } catch (TimeoutException e) {
                    // System.out.println(n.ping());
                    // //                        System.out.println(n.traceTasksPool());
                    // System.out.println(n.traceParcelProcessors());
                    // System.out.println(n.traceItemProcessors());
                    fail("timeout, node " + n + " parcel " + parcel.getId() + " parcel " + parcel.getId() + " (iteration " + i + ")");
                }
            }
            node.waitParcel(parcel.getId(), 25000);
            ItemResult r = node.waitItem(parcel.getPayloadContract().getId(), 8000);
            assertEquals("after: In node " + node + " item " + parcel.getId(), ItemState.APPROVED, r.state);
            System.out.println("-------------- parcel " + parcel.getId() + " registered (iteration " + i + ")------------");
        // Thread.sleep(5000);
        // System.out.println("-------------- parcel " + parcel.getId() + " wait finished (iteration " + i + ")------------");
        }
    }
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 44 with PrivateKey

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

the class BaseNetworkTest method referenceForChangeNumber.

@Test
public void referenceForChangeNumber() throws Exception {
    // You have a notary dsl with llc's property
    // and only owner of trusted manager's contract can chamge the owner of property
    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 = Contract.fromDslFile(ROOT_PATH + "AbonementWithReferenceDSLTemplate.yml");
    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();
    // here we "send" data and "got" it
    TransactionPack tp_after = TransactionPack.unpack(data);
    registerAndCheckApproved(tp_after);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 45 with PrivateKey

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

the class BaseNetworkTest method registerParcelWithTestPayment.

@Test(timeout = 90000)
public void registerParcelWithTestPayment() 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.setExpiresAt(ZonedDateTime.now().plusMonths(1));
    stepaCoins.seal();
    stepaCoins.check();
    stepaCoins.traceErrors();
    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(100, keys, true);
    stepaTU.check();
    // stepaTU.setIsTU(true);
    stepaTU.traceErrors();
    node.registerItem(stepaTU);
    ItemResult itemResult = node.waitItem(stepaTU.getId(), 18000);
    assertEquals(ItemState.APPROVED, itemResult.state);
    Parcel parcel = ContractsService.createParcel(stepaCoins, stepaTU, 1, stepaPrivateKeys, true);
    parcel.getPayment().getContract().paymentCheck(config.getTransactionUnitsIssuerKey());
    parcel.getPayment().getContract().traceErrors();
    parcel.getPayload().getContract().check();
    parcel.getPayload().getContract().traceErrors();
    assertEquals(100, parcel.getPaymentContract().getStateData().getIntOrThrow("transaction_units"));
    assertEquals(10000 - 1, parcel.getPaymentContract().getStateData().getIntOrThrow("test_transaction_units"));
    assertTrue(parcel.getPaymentContract().isOk());
    assertTrue(parcel.getPaymentContract().isLimitedForTestnet());
    assertTrue(parcel.getPayloadContract().isOk());
    assertTrue(parcel.getPayloadContract().isLimitedForTestnet());
    System.out.println("Parcel: " + parcel.getId());
    System.out.println("Payment contract: " + parcel.getPaymentContract().getId() + " is TU: " + parcel.getPaymentContract().isTU(config.getTransactionUnitsIssuerKey(), config.getTUIssuerName()));
    System.out.println("Payload contract: " + parcel.getPayloadContract().getId() + " is TU: " + parcel.getPayloadContract().isTU(config.getTransactionUnitsIssuerKey(), config.getTUIssuerName()));
    // LogPrinter.showDebug(true);
    node.registerParcel(parcel);
    // wait parcel
    node.waitParcel(parcel.getId(), 8000);
    // check payment and payload contracts
    assertEquals(ItemState.APPROVED, node.waitItem(parcel.getPayment().getContract().getId(), 8000).state);
    assertEquals(ItemState.APPROVED, node.waitItem(parcel.getPayload().getContract().getId(), 8000).state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) 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