Search in sources :

Example 61 with PrivateKey

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

the class BaseNetworkTest method registerWithNewParcel.

protected synchronized Parcel registerWithNewParcel(Contract c) throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    Parcel parcel = createParcelWithClassTU(c, stepaPrivateKeys);
    System.out.println("register  parcel: " + parcel.getId() + " " + parcel.getPaymentContract().getId() + " " + parcel.getPayloadContract().getId());
    node.registerParcel(parcel);
    synchronized (tuContractLock) {
        tuContract = parcel.getPaymentContract();
    }
    return parcel;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey)

Example 62 with PrivateKey

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

the class BaseNetworkTest method registerWithNewParcel.

protected synchronized Parcel registerWithNewParcel(TransactionPack tp) throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    Contract tu = getApprovedTUContract();
    // stepaPrivateKeys - is also U keys
    Parcel parcel = ContractsService.createParcel(tp, tu, 150, stepaPrivateKeys);
    System.out.println("-------------");
    node.registerParcel(parcel);
    synchronized (tuContractLock) {
        tuContract = parcel.getPaymentContract();
    }
    return parcel;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey)

Example 63 with PrivateKey

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

the class BaseNetworkTest method parallelTest.

@Ignore("parallel test")
@Test
public void parallelTest() throws Exception {
    // assertEquals("http://localhost:8080", main.myInfo.internalUrlString());
    // assertEquals("http://localhost:8080", main.myInfo.publicUrlString());
    PrivateKey myKey = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    // assertEquals(main.cache, main.node.getCache());
    // ItemCache c1 = main.cache;
    // ItemCache c2 = main.node.getCache();
    // Client client = new Client(myKey, main.myInfo, null);
    List<Contract> contractsForThreads = new ArrayList<>();
    int N = 100;
    int M = 2;
    float threshold = 1.2f;
    float ratio = 0;
    boolean createNewContracts = true;
    // assertTrue(singleContract.isOk());
    // ItemResult r = client.getState(singleContract.getId());
    // assertEquals(ItemState.UNDEFINED, r.state);
    // System.out.println(r);
    contractsForThreads = new ArrayList<>();
    for (int j = 0; j < M; j++) {
        Contract contract = new Contract(myKey);
        for (int k = 0; k < 10; k++) {
            Contract nc = new Contract(myKey);
            nc.seal();
            contract.addNewItems(nc);
        }
        contract.seal();
        assertTrue(contract.isOk());
        contractsForThreads.add(contract);
    // ItemResult r = client.getState(contract.getId());
    // assertEquals(ItemState.UNDEFINED, r.state);
    // System.out.println(r);
    }
    Contract singleContract = new Contract(myKey);
    for (int k = 0; k < 10; k++) {
        Contract nc = new Contract(myKey);
        nc.seal();
        singleContract.addNewItems(nc);
    }
    singleContract.seal();
    for (int i = 0; i < N; i++) {
        if (createNewContracts) {
            contractsForThreads = new ArrayList<>();
            for (int j = 0; j < M; j++) {
                Contract contract = new Contract(myKey);
                for (int k = 0; k < 10; k++) {
                    Contract nc = new Contract(myKey);
                    nc.seal();
                    contract.addNewItems(nc);
                }
                contract.seal();
                assertTrue(contract.isOk());
                contractsForThreads.add(contract);
            }
            singleContract = new Contract(myKey);
            for (int k = 0; k < 10; k++) {
                Contract nc = new Contract(myKey);
                nc.seal();
                singleContract.addNewItems(nc);
            }
            singleContract.seal();
        }
        long ts1;
        long ts2;
        Semaphore semaphore = new Semaphore(-(M - 1));
        ts1 = new Date().getTime();
        for (Contract c : contractsForThreads) {
            Thread thread = new Thread(() -> {
                long t = System.nanoTime();
                ItemResult rr = null;
                rr = node.registerItem(c);
                try {
                    rr = node.waitItem(c.getId(), 15000);
                } catch (TimeoutException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("multi thread: " + rr + " time: " + ((System.nanoTime() - t) * 1e-9));
                semaphore.release();
            });
            thread.setName("Multi-thread register: " + c.getId().toString());
            thread.start();
        }
        semaphore.acquire();
        ts2 = new Date().getTime();
        long threadTime = ts2 - ts1;
        // 
        ts1 = new Date().getTime();
        Contract finalSingleContract = singleContract;
        Thread thread = new Thread(() -> {
            long t = System.nanoTime();
            ItemResult rr = null;
            rr = node.registerItem(finalSingleContract);
            try {
                rr = node.waitItem(finalSingleContract.getId(), 15000);
            } catch (TimeoutException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("single thread: " + rr + " time: " + ((System.nanoTime() - t) * 1e-9));
            semaphore.release();
        });
        thread.setName("single-thread register: " + singleContract.getId().toString());
        thread.start();
        semaphore.acquire();
        ts2 = new Date().getTime();
        long singleTime = ts2 - ts1;
        System.out.println(threadTime * 1.0f / singleTime);
        ratio += threadTime * 1.0f / singleTime;
    }
    ratio /= N;
    System.out.println("average " + ratio);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Semaphore(java.util.concurrent.Semaphore) TimeoutException(java.util.concurrent.TimeoutException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 64 with PrivateKey

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

the class BaseNetworkTest method registerParcel.

@Test(timeout = 90000)
public void registerParcel() 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();
    stepaCoins.check();
    stepaCoins.traceErrors();
    Parcel parcel = createParcelWithFreshTU(stepaCoins, stepaPrivateKeys);
    parcel.getPayment().getContract().check();
    parcel.getPayment().getContract().traceErrors();
    parcel.getPayload().getContract().check();
    parcel.getPayload().getContract().traceErrors();
    assertTrue(parcel.getPaymentContract().isOk());
    assertTrue(parcel.getPayloadContract().isOk());
    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)

Example 65 with PrivateKey

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

the class BaseNetworkTest method referenceForSplitJoinWithCreateContract.

@Test
public void referenceForSplitJoinWithCreateContract() 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.createTokenContract(llcPrivateKeys, stepaPublicKeys, "100000000000");
    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.setMode(ListRole.Mode.ALL);
    listRole.addRole(ownerRole);
    listRole.addRequiredReference("certification_contract", Role.RequiredMode.ALL_OF);
    llcProperty.getPermissions().remove("split_join");
    llcProperty.getPermissions().remove("change_owner");
    llcProperty.getPermissions().remove("revoke");
    Binder params = new Binder();
    params.set("min_value", 0.01);
    params.set("min_unit", 0.001);
    params.set("field_name", "amount");
    List<String> listFields = new ArrayList<>();
    listFields.add("state.origin");
    params.set("join_match_fields", listFields);
    SplitJoinPermission splitJoinPerm = new SplitJoinPermission(listRole, params);
    llcProperty.addPermission(splitJoinPerm);
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = ContractsService.createSplit(llcProperty, 100, "amount", stepaPrivateKeys, true);
    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)

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